读取和显示数据文件,更正结果,但代码不可行

我在类中有一个程序,它读取商店的数据文件,然后在提示符下打印出来。

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 

数字是部门,项目名称,数量,购买成本和销售成本。

 #include  #include  #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 

忽略每条线的不均匀间距; 我正在排除大量故障,我莫名其妙地得到了。 我的代码做得很好,花花公子并且全部检查出来,但是我的教授因为if语句给了我一半的功劳,说如果有大量的部门我的代码是不可行的。 他说我可以用一个声明替换掉它,因为我已经尝试将它放到switch语句中了,但是这显然不起作用,因为有些服装项目没有出现。

如果没有数学变得疯狂,我似乎无法改变一切。 我的第一个想法是为totmark,totcost,cost2和mark2创建多个加法和乘法的函数,但是无论何时我破坏它,一切都崩溃了,我似乎无法将它重新组合在一起。

这应该是一个简单的解决方案,但我感谢任何帮助。

我认为他们希望你能利用function
您也可以使用Structures。

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

并创建一个结构数组(在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"); 

也许你可以尝试使用数组;

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

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

 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  #include  #include  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