如何在此代码中使用自定义函数?

有问题的

无论如何,我可以在此代码中使用自定义函数吗?我的作业要求我使用自定义功能,但是我对此不太擅长,我该怎么做?这是一个收银员程序,要求用户提供他们想要购买的产品的商品代码。

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

int main(void)
{
    int i,j = 0, code, amt, key, lines = 0;
    int id[100], stock[100], k = 0;
    char name[100][20], product[100], newstock[100];
    float price[100], sum;
    float total = 0;  
    char ipname[100][20];
    int quantity[100], ch;
    float ipprice[100];
    float ipsub[100],pay,balance;

    FILE *fp1;

    fp1 = fopen("Fruit.txt", "r");

    if(fp1 == NULL)
    {
        printf("ERROR in opening file\n");
        return 1;
    }
    else
    {
        while((ch = getc(fp1))!= EOF)
        {
            if(ch == '\n')
                lines++;
        }
    }

    fclose(fp1);

    fp1 = fopen("Fruit.txt","r");

    if(fp1 == NULL)
    {
        printf("ERROR in opening file\n");
        return 1;
    }
    else
    {
        for(i = 0; i < lines; i++)
        {
            fgets(product, sizeof(product), fp1);
            id[i] = atoi(strtok(product, ","));
            strcpy(name[i], strtok(NULL, ","));
            price[i] = atof(strtok(NULL, ","));
            stock[i]=atoi(strtok(NULL,"\n"));
        }
    }

    fclose(fp1);

    FILE *fp3;

    fp3 = fopen("Stock.txt", "r");

    if(fp3!=NULL)
    {
        for(i = 0; i < lines; i++)
        {
            fgets(newstock, sizeof(newstock), fp3);

            id[i] = atoi(strtok(newstock, ","));
            strcpy(name[i], strtok(NULL, ","));
            price[i] = atof(strtok(NULL, ","));
            stock[i] = atoi(strtok(NULL, "\n"));
        }
    }

    fclose(fp3);

    printf("=============================================================\n");

    for(i = 0; i < lines; i++)
    { 
        printf("%d:%-10s\t", i +1, name[i]);
    }

    printf("\n\n%-5s%-20s%-15s%-10s%s\n", "No.", "Product", "Price", "Quantity", "Subtotal");
    printf("%-5s%-20s%-15s%-10s%s\n", "===", "=======", "=====", "========", "========");

    do
    {  
        k = 0;
        system("cls");
        printf("=============================================================\n");

        for(i = 0; i < lines; i++)
        {
            printf("%d:%-10s\t", i +1, name[i]);
        }

        printf("\n\n%-5s%-20s%-15s%-10s%s\n", "No.", "Product", "Price", "Quantity", "Subtotal");
        printf("%-5s%-20s%-15s%-10s%s\n", "===", "=======", "=====", "========", "========");

        if(j > 0)
        {
            for(k = 0; k < j; k++)
            {
                printf("%-5d%-20sRM%-13.2f%-10dRM%.2f\n", k +1, ipname[k], ipprice[k], quantity[k], ipsub[k]);
            }
        }

        sum = 0;
        amt = 0;

        printf("\nProduct code:");
        scanf("%d", &code);

        i = code -1;

        if(i < lines && i >= 0)
        {
            printf("Quantity:");
            scanf("%d", &amt);
            stock[i] = stock[i] -amt;

            system("cls");

            sum = price[i] *amt;
            total = total + sum;
            ipprice[j] = price[i];

            strcpy(ipname[j], name[i]);

            quantity[j] = amt;
            ipsub[j] = sum;

            printf("=============================================================\n");

            for(i = 0; i < lines; i++)
            {
                printf("%d:%-10s\t", i +1, name[i]);
            }

            printf("\n\n%-5s%-20s%-15s%-10s%s\n", "No.", "Product", "Price", "Quantity", "Subtotal");
            printf("%-5s%-20s%-15s%-10s%s\n", "===", "=======", "=====", "========", "========");

            for(k = 0; k <= j; k++)
            {
                printf("%-5d%-20sRM%-13.2f%-10dRM%.2f\n", k +1, ipname[k], ipprice[k], quantity[k], ipsub[k]);
            }

            printf("\nPress ESC on keyboard to finalize the bill or Press any key continue...\n");

            j++;

            key = _getch();
        }
        else
        {
            system("cls");

            printf("=============================================================\n");

            for(i = 0; i < lines; i++)
            { 
                printf("%d:%-10s\t", i +1, name[i]);
            }

            printf("\n\n%-5s%-20s%-15s%-10s%s\n", "No.", "Product", "Price", "Quantity", "Subtotal");
            printf("%-5s%-20s%-15s%-10s%s\n", "===", "=======", "=====", "========", "========");

            if(j > 0)
            {
                for(k = 0; k < j; k++)
                {
                    printf("%-5d%-20sRM%-13.2f%-10dRM%.2f\n", k +1, ipname[k], ipprice[k], quantity[k], ipsub[k]);
                }
            }

            printf("\nInvalid product code entered, press any key to try again or press esc to finalize   bill...\n\n");
        }
    } while(key != 27);

    do
    {
        system("cls");

        printf("=============================================================\n");

        for(i = 0; i < lines; i++)
        {
            printf("%d:%-10s\t",i+1,name[i]);
        }

        printf("\n\n%-5s%-20s%-15s%-10s%s\n", "No.", "Product", "Price", "Quantity", "Subtotal");
        printf("%-5s%-20s%-15s%-10s%s\n", "===", "=======", "=====", "========", "========");

        for(k = 0; k < j; k++)
        {
            printf("%-5d%-20sRM%-13.2f%-10dRM%.2f\n", k +1, ipname[k], ipprice[k], quantity[k], ipsub[k]);
        }

        printf("\nGrand Total = RM%.2f\n",total);
        printf("\nPayment = RM");
        scanf("%f", &pay);

        if(pay >= total)
        {
            balance = pay - total;
            printf("\nBalance = RM%.2f\n", balance);
        }
        else
        {
            printf("\nInsufficient payment, Please enter larger amount!\nPress any key...");

            getch();
        }

    }while(pay < total);

    FILE *fp2;
    fp2 = fopen("receipt.txt", "w");

    if(fp1 == NULL)
    {
        printf("ERROR in opening file\n");
        return 1;
    }

    time_t now;
    time(&now);

    fprintf(fp2,"\n\n%-5s%-20s%-15s%-10s%s\n", "No.", "Product", "Price", "Quantity", "Subtotal");
    fprintf(fp2,"%-5s%-20s%-15s%-10s%s\n", "===", "=======", "=====", "========", "========");

    for(k = 0; k < j; k++)
    {
        fprintf(fp2,"%-5d%-20sRM%-13.2f%-10dRM%.2f\n", k +1, ipname[k], ipprice[k], quantity[k], ipsub[k]);
    }

    fprintf(fp2,"\n\nGrand Total = RM%.2f\n", total);
    fprintf(fp2,"Payment = RM%.2f\n", pay);
    fprintf(fp2,"Balance = RM%.2f\n", balance);

    fclose(fp2);

    fp3 = fopen("Stock.txt", "w");

    if(fp3 == NULL)
    {
        printf("ERROR in updating stock file\n");
        return 1;
    }
    else
    {
        for(i = 0; i < lines; i++)
        {
            fprintf(fp3,"%d,%s,%.2f,%d\n", id[i], name[i], price[i], stock[i])
        }
    }

    fclose(fp1);

    return 0;
}

txt文件的内容

18156,Apple,1.49,73
45776,Aprikot,1.59,23
73191,Avocado,2.49,63
72829,Banana,2.99,27
74084,Blueberry,5.49,36
79800,Coconut,3.49,80
16611,Grape,8.99,42
62690,Grapefruit,2.19,34
47089,Guava,4.99,42
70412,Jackfruit,19.99,29
44640,Kiwifruit,2.15,38
94768,Lemon,0.99,42
86240,Mango,3.99,62
78697,Orange,1.69,23
74470,Papaya,4.49,23
23959,Pear,2.49,36
78862,Pineapple,2.99,71
82943,Pomegranate,4.35,19
56180,Pomelo,12.99,72
67712,Starfruit,2.79,59
33974,Strawberry,13.99,31
45584,Watermelon,6.88,21
Arpit

首先也是最重要的一点是,扔掉笔记,并为自己准备一本不错的C编程书。《权威C书籍指南和清单》中,您可以找到一个不错的清单,并且可以有个好主意。

只是有关功能的基本概念。尝试一遍又一遍地识别您需要的代码,然后将该代码放入函数中,这样程序将很小,易于理解,而不必再次编写。就像,在您的代码中,我看到了此代码被使用了很多次:

        printf("=============================================================\n");

        for(i = 0; i < lines; i++)
        { 
            printf("%d:%-10s\t", i +1, name[i]);
        }

        printf("\n\n%-5s%-20s%-15s%-10s%s\n", "No.", "Product", "Price", "Quantity", "Subtotal");
        printf("%-5s%-20s%-15s%-10s%s\n", "===", "=======", "=====", "========", "========");

因此,将其放在这样的函数中:

void PrintNewBillHeader ()
{
            printf("=============================================================\n");

            for(i = 0; i < lines; i++)
            { 
                printf("%d:%-10s\t", i +1, name[i]);
            }

            printf("\n\n%-5s%-20s%-15s%-10s%s\n", "No.", "Product", "Price", "Quantity", "Subtotal");
            printf("%-5s%-20s%-15s%-10s%s\n", "===", "=======", "=====", "========", "========");
}

然后,无论您在何处执行这些步骤,只需调用此函数PrntNewBillHeader()即可简单的。

首先,您正在从文件中读取数据,并且该文件已被调用两次。因此,创建一个新函数ReadFileData()并将读取代码放入该函数中。但是在这里,您必须传递文件名和要在其中存储数据的数组。因此,将执行以下操作:

int ReadFileData (char * pFileName, char * name, float *pPrice, float *pStock)
{
    /**Do declare the variable to be use in this function**/

    fp1 = fopen("Fruit.txt","r");

    if(fp1 == NULL)
    {
        return -1;
    }
    else
    {
        for(i = 0; i < lines; i++)
        {
            fgets(product, sizeof(product), fp1);
            id[i] = atoi(strtok(product, ","));
            strcpy(name[i], strtok(NULL, ","));
            price[i] = atof(strtok(NULL, ","));
            stock[i]=atoi(strtok(NULL,"\n"));
        }
    }

    fclose(fp1);

    return 1;
}

我希望您一定有很多想法。

只需尝试通过一次删除一个冗余代码来拆分主要功能。

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在自定义jQuery函数中使用函数

来自分类Dev

Android-如何在此列表视图中使用自定义列表视图

来自分类Dev

如何在此代码中使用 group by 函数

来自分类Dev

如何在自定义jquery函数中使用this的值以使函数输出数据

来自分类Dev

Google Analytics Api:如何在代码中使用自定义变量?

来自分类Dev

如何在Facebook Pixel JavaScript代码中使用Wordpress自定义插件选项的值

来自分类Dev

如何在WAF构建系统中使用自定义代码生成器?

来自分类Dev

如何在代码中创建自定义UIButton并在xib中使用它?

来自分类Dev

如何在Visual Studio代码中使用自定义字体

来自分类Dev

如何在文档注释中使用自定义链接文本创建代码引用?

来自分类Dev

Xamarin.Forms:如何在 C# 代码中使用自定义字体

来自分类Dev

如何在C ++中使用带有自定义排序成员函数的sort()?

来自分类Dev

如何在BTreeSet中使用自定义比较器函数?

来自分类Dev

如何在Tensorflow中使用自定义python函数预取数据

来自分类Dev

如何在HashSet或HashMap中使用自定义哈希函数?

来自分类Dev

如何在Spark TextFile函数中使用自定义换行符?

来自分类Dev

如何在flask的模板文件中使用python3自定义函数?

来自分类Dev

如何在自定义验证程序中使用PropType内置函数?

来自分类Dev

如何在PostgreSQL表中使用自定义函数作为列的默认值

来自分类Dev

如何在janusgraph中使用自定义函数计算边缘的权重?

来自分类Dev

如何在R中的data.table中使用自定义函数

来自分类Dev

如何在Keras自定义损失函数中使用张量?

来自分类Dev

如何在连接子句中使用自定义函数?

来自分类Dev

如何在HashSet或HashMap中使用自定义哈希函数?

来自分类Dev

如何在Tableau中的Script_Real()中使用自定义的R函数?

来自分类Dev

如何在自定义 R 函数中使用文本字符串作为变量

来自分类Dev

如何使用C ++代码调用Matlab自定义函数

来自分类Dev

如何在自定义jQuery函数内使用函数

来自分类Dev

如何在此代码中使用map()?

Related 相关文章

  1. 1

    如何在自定义jQuery函数中使用函数

  2. 2

    Android-如何在此列表视图中使用自定义列表视图

  3. 3

    如何在此代码中使用 group by 函数

  4. 4

    如何在自定义jquery函数中使用this的值以使函数输出数据

  5. 5

    Google Analytics Api:如何在代码中使用自定义变量?

  6. 6

    如何在Facebook Pixel JavaScript代码中使用Wordpress自定义插件选项的值

  7. 7

    如何在WAF构建系统中使用自定义代码生成器?

  8. 8

    如何在代码中创建自定义UIButton并在xib中使用它?

  9. 9

    如何在Visual Studio代码中使用自定义字体

  10. 10

    如何在文档注释中使用自定义链接文本创建代码引用?

  11. 11

    Xamarin.Forms:如何在 C# 代码中使用自定义字体

  12. 12

    如何在C ++中使用带有自定义排序成员函数的sort()?

  13. 13

    如何在BTreeSet中使用自定义比较器函数?

  14. 14

    如何在Tensorflow中使用自定义python函数预取数据

  15. 15

    如何在HashSet或HashMap中使用自定义哈希函数?

  16. 16

    如何在Spark TextFile函数中使用自定义换行符?

  17. 17

    如何在flask的模板文件中使用python3自定义函数?

  18. 18

    如何在自定义验证程序中使用PropType内置函数?

  19. 19

    如何在PostgreSQL表中使用自定义函数作为列的默认值

  20. 20

    如何在janusgraph中使用自定义函数计算边缘的权重?

  21. 21

    如何在R中的data.table中使用自定义函数

  22. 22

    如何在Keras自定义损失函数中使用张量?

  23. 23

    如何在连接子句中使用自定义函数?

  24. 24

    如何在HashSet或HashMap中使用自定义哈希函数?

  25. 25

    如何在Tableau中的Script_Real()中使用自定义的R函数?

  26. 26

    如何在自定义 R 函数中使用文本字符串作为变量

  27. 27

    如何使用C ++代码调用Matlab自定义函数

  28. 28

    如何在自定义jQuery函数内使用函数

  29. 29

    如何在此代码中使用map()?

热门标签

归档