读取和显示数据文件,更正结果,但代码不可行 [英] Reading and Displaying Data Files, Correct results, but code is unfeasible

查看:109
本文介绍了读取和显示数据文件,更正结果,但代码不可行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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.

推荐答案

我认为他们希望你使用 function

你也可以使用Structures。

I think they expect you to make use of function.
You could also use Structures.

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

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

and create an array of structures (in main()), like this

/* 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);
}

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

Also you could write a generic function to calculate:

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

and add the following to function 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"); 

这篇关于读取和显示数据文件,更正结果,但代码不可行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆