AC_wy 2023-04-08 18:57 采纳率: 83.3%
浏览 43
已结题

c语言小超市商品管理系统

按销售额和毛利顺序显示时没有办法排序,只能按顺序显示,求帮忙!实在没看出来哪里有问题
要求必须使用结构体数组
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define SIZESG sizeof(struct Goods)
#define SIZEJD sizeof(struct Jd)
struct Goods
{
    char   id[20];/*编号*/
    char   name[20];/*品名*/
    double purchase_price;/*进价*/
    double selling_price;/*售价*/
    int    purchase_quantity;/*进货量*/
    int    sales_volume;/*销售量*/
    double amount;/*销售额*/
    int    remaining_quantity;/*剩余数*/
    double gross_profit;/*毛利*/
};
struct Jd
{
    struct Goods goods;
    struct Jd* next;
};

int main()
{
    struct Jd* head = NULL;
    char FileName[128] = { "data.txt" };
    int i;
    while (1)
    {
        menu();
        scanf("%d", &i);
        switch (i)
        {
        case 0:
            system("cls");
            printf("已退出系统!");
            exit(0);
        case 1:
            system("cls");
            createFile(FileName);
            break;
        case 2:
            system("cls");
            head = createchain(FileName);
            edit(head);
            WriteToFile(head, FileName);
            break;
        case 3:
            system("cls");
            head = createchain(FileName);
            del(&head);                    
            WriteToFile(head, FileName);
            break;
        case 4:
            system("cls");
            head = createchain(FileName);
            purchase(head);
            WriteToFile(head, FileName);
            break;
        case 5:
            system("cls");
            head = createchain(FileName);
            sales(head);
            WriteToFile(head, FileName);
            break;
        case 6:
            system("cls");
            head = createchain(FileName);
            baofei(head);
            WriteToFile(head, FileName);
            break;
        case 7:
            system("cls");
            head = createchain(FileName);
            sale(head);
            WriteToFile(head, FileName);
            break;
        case 8:
            system("cls");
            head = createchain(FileName);
            namechaxun(FileName);
            break;
        case 9:
            system("cls");
            head = createchain(FileName);
            shuchaxun(FileName);
            break;
        case 10:
            system("cls");
            shunxu(FileName);
            break;
        case 11:
            system("cls");
            amount(FileName);
            break;
        case 12:
            system("cls");
            profit(FileName);
            break;
        case 13:
            system("cls");
            head=createchain(FileName);
            add(&head);
            WriteToFile(head,FileName);
            break;
        default:
            break;                  
        }
    }
}
/*菜单*/
void menu()
{
    printf("-------------------菜单-------------------\n");
    printf("|      0. 退出系统                       |\n");
    printf("|      1. 创建商品                       |\n");
    printf("|      2. 修改商品信息                   |\n");
    printf("|      3. 删除原有商品                   |\n");
    printf("|      4. 原有商品的新进货量             |\n");
    printf("|      5. 原有商品的新销售量             |\n");
    printf("|      6. 原有商品的报废量               |\n");
    printf("|      7. 统计销售情况                   |\n");
    printf("|      8. 根据商品名查询                 |\n");
    printf("|      9. 根据剩余数查询                 |\n");
    printf("|      10.按原来商品顺序显示             |\n");
    printf("|      11.按销售额由高到低的顺序显示     |\n");
    printf("|      12.按毛利由高到低的顺序显示       |\n");
    printf("|      13.添加新商品                     |\n");
    printf("------------------------------------------");
    printf("\n");
    printf("\n");
    printf("请选择您要进行的操作选项:   ");
}



/*创建文件,存放结构体数据*/
void createFile(char* FName)
{
    int i = 1;
    struct Goods go;
    FILE* fp;
    if ((fp = fopen(FName, "a+")) == NULL) 
    {
        printf("文件打开失败!\n");
        exit(0);
    }
    while (i != 0)/*给结构体各成员变量赋值*/
    {
        printf("创建商品档案:\n");
        output(&go);
        fprintf(fp, "%-6s %-6s %-lf %-lf %-6d %-6d %-lf %-6d %-lf\n", go.id, go.name, go.purchase_price, go.selling_price, go.purchase_quantity, go.sales_volume, go.amount, go.remaining_quantity, go.gross_profit);
        printf("是否需要输入新的数据(1=继续,0=退出)?");
        scanf("%d", &i);
    }
    fclose(fp);
}


/*利用文件创建链表*/
struct Jd* createchain(char* FName)
{
    FILE* fp;
    struct Jd* pt, * pEnd, * head = NULL;
    struct Goods go;
    if ((fp = fopen(FName, "r")) == NULL)
    {
        printf("文件打开失败!\n");
        return head;  
    }
    while (fscanf(fp, "%s %s %lf %lf %d %d %lf %d %lf\n", go.id, go.name, &go.purchase_price, &go.selling_price,&go.purchase_quantity, &go.sales_volume, &go.amount, &go.remaining_quantity, &go.gross_profit) == 9)  /*从文件里读出一个结构体数据*/
    {
        pt = (struct Jd*)malloc(SIZEJD);  /*创建链表结点空间*/
        pt->goods = go;/*把前面读出的结构体数据赋值给结点的goods变量*/
        pt->next = NULL;                                              
        if (head == NULL)/*如果链表以前是空,则生成头结点*/
        {
            head = pt;
            pEnd = pt;
        }
        else  /*在尾部插入结点*/
        {
            pEnd->next = pt;/*将新结点链入链表*/
            pEnd = pt;/*让pEnd指向尾结点,为下一次插入做准备*/
        }
    }
    fclose(fp);
    return head;
};

/*根据剩余数查询*/
void shuchaxun(char *FName)
{
    FILE *fp;
    struct Goods good;
    int s, b;
    int flag = 0;
    printf("请输入查询区间: \n");
    scanf("%d%d", &s, &b);
    if((fp=fopen(FName,"r"))==NULL)
    {
        printf("文件打开失败!\n");
        exit(0);
    }
    if (s > b)
        printf("输入错误,请重新输入!\n");
    else
    {
        while (fscanf(fp, "%s %s %lf %lf %d %d %lf %d %lf\n", good.id, good.name, &good.purchase_price, &good.selling_price,&good.purchase_quantity, &good.sales_volume, &good.amount, &good.remaining_quantity, &good.gross_profit) !=EOF)
        {
            if(good.remaining_quantity>=s&&good.remaining_quantity<=b)
            {
                flag=1;
                printf("商品编号: %-6s\n,商品品名: %-6s\n,商品进价: %-lf\n,商品售价: %-lf\n,商品进货量: %-6d\n,商品销售量: %-6d\n,商品销售额: %-lf\n,商品剩余数: %-6d\n,商品毛利: %-lf\n",good.id,good.name,good.purchase_price,good.selling_price,good.purchase_quantity,good.sales_volume,good.amount,good.remaining_quantity,good.gross_profit);
            }
        }
        if(flag==0)
            printf("该商品不存在!\n");
        printf("\n\n");
    }
    fclose(fp);
}


/*按原来商品顺序显示*/
void shunxu(char *FName)
{
    FILE *fp;
    struct Goods good;
    if((fp=fopen(FName,"r"))==NULL)
    {
        printf("文件打开失败!\n");
        exit(0);
    }
    while (fscanf(fp, "%s %s %lf %lf %d %d %lf %d %lf\n", good.id, good.name, &good.purchase_price, &good.selling_price,&good.purchase_quantity, &good.sales_volume, &good.amount, &good.remaining_quantity, &good.gross_profit) !=EOF)
        printf("商品编号: %-6s\n,商品品名: %-6s\n,商品进价: %-lf\n,商品售价: %-lf\n,商品进货量: %-6d\n,商品销售量: %-6d\n,商品销售额: %-lf\n,商品剩余数: %-6d\n,商品毛利: %-lf\n",good.id,good.name,good.purchase_price,good.selling_price,good.purchase_quantity,good.sales_volume,good.amount,good.remaining_quantity,good.gross_profit);
    fclose(fp);
}


/*按销售额排序显示*/
void amount(char *FName)
{
    FILE *fp;
    struct Goods good[1000],temp;
    int i=0,j,k;
    fp=fopen(FName,"r");
    if(fp==NULL)
    {
        printf("文件打开失败!\n");
        exit(0);
    }
    while (fscanf(fp, "%s %s %lf %lf %d %d %lf %d %lf\n", good[i].id, good[i].name, &good[i].purchase_price, &good[i].selling_price,&good[i].purchase_quantity, &good[i].sales_volume, &good[i].amount, &good[i].remaining_quantity, &good[i].gross_profit) !=EOF)
         i++;
    for(j=0;j<i-1;j++)
    {
        for(k=0;k<i-j-1;k++)
        {
            if(good[k].amount<good[k].amount)
            {
                temp=good[k];
                good[k]=good[k+1];
                good[k+1]=temp;
            }
        }
    }
    for(j=0;j<i;j++)
        printf("商品编号: %s\n,商品品名: %s\n,商品进价: %lf\n,商品售价: %lf\n,商品进货量: %d\n,商品销售量: %d\n,商品销售额: %lf\n,商品剩余数: %d\n,商品毛利: %lf\n",good[j].id,good[j].name,good[j].purchase_price,good[j].selling_price,good[j].purchase_quantity,good[j].sales_volume,good[j].amount,good[j].remaining_quantity,good[j].gross_profit);
    fclose(fp);
}


/*按毛利显示*/
void profit(char *FName)
{
    FILE *fp;
    struct Goods good[1000],temp;
    int i=0,j,k;
    fp=fopen(FName,"r");
    if(fp==NULL)
    {
        printf("文件打开失败!\n");
        exit(0);
    }
    while (fscanf(fp, "%s %s %lf %lf %d %d %lf %d %lf\n", good[i].id, good[i].name, &good[i].purchase_price, &good[i].selling_price,&good[i].purchase_quantity, &good[i].sales_volume, &good[i].amount, &good[i].remaining_quantity, &good[i].gross_profit) !=EOF)
        i++;
    for(j=0;j<i-1;j++)
    {
        for(k=0;k<i-j-1;k++)
        {
            if(good[k].gross_profit<good[k].gross_profit)
            {
                temp=good[k];
                good[k]=good[k+1];
                good[k+1]=temp;
            }
        }
    }
    for(j=0;j<i;j++)
        printf("商品编号: %s\n,商品品名: %s\n,商品进价: %lf\n,商品售价: %lf\n,商品进货量: %d\n,商品销售量: %d\n,商品销售额: %lf\n,商品剩余数: %d\n,商品毛利: %lf\n",good[j].id,good[j].name,good[j].purchase_price,good[j].selling_price,good[j].purchase_quantity,good[j].sales_volume,good[j].amount,good[j].remaining_quantity,good[j].gross_profit);
    fclose(fp);
}


/*把链表结点数据写往文件*/
void WriteToFile(struct Jd* head, char* FName)
{
    FILE* fp;
    struct Jd* pt;
    if ((fp = fopen(FName, "w")) == NULL)
    {
        printf("文件打开失败!");
        exit(0);
    }
    pt = head;
    while (pt != NULL)
    {
        fprintf(fp, "%-6s %-6s %-lf %-lf %-6d %-6d %-lf %-6d %-lf\n", pt->goods.id, pt->goods.name, pt->goods.purchase_price, pt->goods.selling_price,pt->goods.purchase_quantity, pt->goods.sales_volume, pt->goods.amount, pt->goods.remaining_quantity, pt->goods.gross_profit);
        pt = pt->next;
    }
    fclose(fp);
}

  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月8日
  • 已采纳回答 4月8日
  • 创建了问题 4月8日

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证