1

I have a program in class that reads a data file for a store and then prints them out on a prompt.

1 Suits    300 100  92        
1 Coats    200  60  65
1 Shirts  1000  12  13
2 Dresses  400  60  65
2 Coats    185 184 200
2 Shoes    600  40  30
3 Jeans    200  40  35  
3 Shoes    200  30  34
4 Jeans    300  42  43

The numbers being department, item name, quantity, cost to buy, and cost to sale.

#include <stdio.h>
#include <stdlib.h>
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif

int main()
{
    FILE *in;
    char item[8];
    int department, quantity, prev = 1,k=0;
    float cost, market, cost2, mark2, total, totcost, totmark = 0, lowest;


    if((in = fopen("blinn.dat", "r")) == NULL)
    {
        printf ("Can't open file blinn.dat\n");
        system("pause");
        exit(1);
    }
    else
    {
        printf ("\n\n\t\t\t\t  Blinn Apparel Store");
        printf ("\n\n\n\t\t        Unit Cost                 Extended");
        printf ("\n             Quantity     Cost  Market         Cost      Market   Lower Cost\n");
        printf ("\nMens Dept");
        int m=0;
        while(fscanf(in, "%d %s %d %f %f", &department, item, &quantity, &cost, &market) != EOF)
        {
            if(department != prev)
            {
                lowest = min(totcost, totmark);
                printf ("\n  Total\t\t\t\t            $%8.2f  $%8.2f  $%8.2f", totcost, totmark, lowest);
                totcost = 0;
                totmark = 0;
                prev = department;
                total += lowest;
                switch (m)
                {
                    case 0:
                        printf("\nLadies Dept");
                        break;
                    case 1:
                        printf("\nGirls Dept");
                        break;
                    case 2:
                        printf("\nBoys Dept");
                        break;
                }
                m++;
            }

            if (department == 1)
            {
                cost2 = cost * quantity;
                mark2 = market * quantity;

                printf ("\n%8s       %4d   %8.2f   %4.2f       %8.2f   %8.2f   ", 
                        item, quantity, cost, market, cost2, mark2);
                totcost = totcost + cost2;
                totmark = totmark + mark2;
            }

            if (department == 2)
            {
                cost2 = cost * quantity;
                mark2 = market * quantity;

                printf ("\n%8s       %4d   %8.2f%8.2f       %8.2f   %8.2f   ", 
                        item, quantity, cost, market, cost2, mark2);
                totcost = totcost + cost2;
                totmark = totmark + mark2;
            }

            if (department == 3)
            {
                cost2 = cost * quantity;
                mark2 = market * quantity;

                printf ("\n%8s       %4d    %8.2f  %5.2f       %8.2f   %8.2f   ",
                        item, quantity, cost, market, cost2, mark2);
                totcost = totcost + cost2;
                totmark = totmark + mark2;
            }

            if (department == 4)
            {
                cost2 = cost * quantity;
                mark2 = market * quantity;

                printf ("\n%8s       %4d    %8.2f  %5.2f       %8.2f   %8.2f   ", 
                        item, quantity, cost, market, cost2, mark2);
                totcost = totcost + cost2;
                totmark = totmark + mark2;
                lowest = min(totcost, totmark);
                printf ("\nTotal\t\t\t\t\t    $%8.2f  $%8.2f  $%8.2f\n", totcost, totmark, lowest);
                total += lowest;
            }
        }
    }
    printf("Inventory at lower cost\t\t\t\t\t         $%8.2f\n", total);
    system ("pause");
}a

Disregard the nonuniform spacing for each line; I was troubleshooting a large number I was inexplicably getting. I have the code done all nice and dandy and it all checks out, but my professor has given me half credit because of the if statements, saying if there were a large number of departments my code wouldn't be feasible. He said I could replace it with one statement and that threw me off because I've tried working it into the switch statement, but that obviously doesn't work because some of the clothing items don't appear.

I just can't seem to change things without the math going crazy. My first thought was to make a function for the multiple additions and multiplications to totmark, totcost, cost2, and mark2, but anytime I disrupt it, everything falls apart and I can't seem to put it back together.

This should be an easy fix, but I appreciate any help.

4

3 回答 3

0

我认为他们希望您使用function
您也可以使用结构。

/* Structure Definition */
typedef struct goods                                                            
{                                                                               
    int dept;                                                                   
    char item[64];                                                             
    int quantity;                                                               
    int sell_price;                                                             
    int cost_price;                                                             
} goods_t; 

并创建一个结构数组(in main()),像这样

/* Array of Structure */
#define NO_ENTRIES 10   /* No of entries in your file */
goods_t arr[NO_ENTRIES];
for(i = 0; i< TOTAL_DEPT; i++)
{
    fscanf(fp, "%d %s %d %d %d", &arr[i].dept, arr[i].item, &arr[i].quantity, &arr[i].sell_price, &arr[i].cost_price);
    cal(arr[i].dept, arr[i].item, arr[i].quantity, arr[i].sell_price ,arr[i].cost_price);
}

您也可以编写一个通用函数来计算:

void calculate (int dept, char *item, int quantity, int sp, int cp)
{                                                                               
    int total_cost = 0, total_market = 0;
    total_cost += quantity * cp;
    total_sell += quantity * sp;
    ...
    printf("%s\t%d\t%d\t%d\n", item, quantity, sp, cp);                         
    printf("Total : %d\t %d\n", total_cost, total_sell);
    ...
    return;
}

旁注:
您可以使用enum

enum type{                                                                      
    mens = 0,                                                                   
    ladies,                                                                     
    boys,                                                                       
    girls,                                                                      
}; 

并将以下内容添加到函数中calculate

if(dept == mens)                                                            
    printf("Mens Dept");                                                    
else if(dept == ladies)                                                     
    printf("Ladies Dept");                                                  
else if(dept == boys)                                                       
    printf("Boys Dept");                                                    
else                                                                        
    printf("Girls Dept"); 
于 2014-12-10T10:14:38.807 回答
0
the following compiles with no warnings/errors
it implements the logic needed 
however, certain printf statements and certain calcuations
are left for the OP

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


static const char * departmentNames[] =
{
    "Mens Department",
    "Womens Department",
    "Girls Department",
    "Boys Department"
};

int main()
{
    int  firstRecord = 1;
    FILE *in;

    char item[20];
    int departmentNum = 0;
    int quantity = 0;
    int prevDepartmentNum = 0;

    float cost    = 0.0f;  // store cost of one item
    float market  = 0.0f;  // customer cost of one item
    float cost2   = 0.0f;  // cost * quantity
    float mark2   = 0.0f;  // narket * quantity
    float totcost = 0.0f;  // sum of product cost for one department
    float totmark = 0.0f;  // sum of product sales for one department



    if((in = fopen("blinn.dat", "r")) == NULL)
    {
        perror( "fopen for reading blinn.data failed" );
        printf ("Can't open file blinn.dat\n");
        system("pause");
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    // display the report header
    printf ("\n\n\t\t\t\t  Blinn Apparel Store");
    printf ("\n\n\n\t\t        Unit Cost                 Extended");
    printf ("\n             Quantity     Cost  Market         Cost      Market\n");


    while(1)
    {
        memset(item, 0x00, sizeof(item) );
        if( 5 != fscanf(in, " %d %19s %d %f %f", &departmentNum, item, &quantity, &cost, &market) )
        { // fscanf failed
            perror( "fscanf failed" );
            exit( EXIT_FAILURE );
        }

        // implied else, fscanf successful

        if( firstRecord )
        {
            firstRecord = 0;
            prevDepartmentNum = departmentNum;
            totcost = 0.0f;
            totmark = 0.0f;
        } // end if

        if(departmentNum != prevDepartmentNum)
        { // then output previous department totals

            printf( "\n%s", departmentNames[prevDepartmentNum]);
            printf ("\n  Total\t\t\t\t            $%8.2f  $%8.2f", totcost, totmark );
            // re-initialize for new department
            totcost = 0.0f;
            totmark = 0.0f;
            prevDepartmentNum = departmentNum; // prep for next itteration
        } // end if

        cost2 = cost * quantity;
        mark2 = market * quantity;

        printf ("\n%8s       %4d   %8.2f   %4.2f       %8.2f   %8.2f   ",
                item, quantity, cost, market, cost2, mark2);
        totcost = totcost + cost2;
        totmark = totmark + mark2;
    } // end while

    system ("pause");
    return(0);
} // end function: main
于 2014-12-10T11:19:52.980 回答
0

也许你可以尝试使用像这样的数组;

    int department[ k]
    string itemname[ ]
    Float quantity[ ] , cost_to_buy[ ],
    cost_to_sale[ ]

如有必要,请向用户询问 k 个部门的可行性。在此之后,您可以应用 switch 或 if 语句为每个数组分配其等效项,然后使用 for 循环显示每个数组。使用数组,您可以与更多部门合作祝您好运

于 2014-12-10T08:14:58.190 回答