按销售额和毛利顺序显示时没有办法排序,只能按顺序显示,求帮忙!实在没看出来哪里有问题
要求必须使用结构体数组
```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);
}
c语言小超市商品管理系统
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注 - 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7445202
- 这篇博客你也可以参考下:c语言猜数字小游戏
- 你还可以看下c语言参考手册中的 c语言-求值顺序与定序
- 除此之外, 这篇博客: c语言小知识整理中的 移动 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
按照0-8排列
第0位要到第几位上去,就直接向右移动几位就行(移动几位就是加几)- 您还可以看一下 朱有鹏老师的C语言位操作视频精讲-C语言专题第二部分课程中的 4.2.3.如何用位运算构建特定二进制数小节, 巩固相关知识点
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥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关系验证