使用C语言编程,对市场十种常见商品进行信息录入,并可实现排序输出功能 谢谢

2025-03-10 13:22:24
推荐回答(1个)
回答1:

很久以前大一刚入学时候做的商品管理系统,多文件结构,编译的时候编译Mainfile.c就行,当时还不懂事~用了goto语句,不过可以替换成循环【Datafile.c】#include
#include /*文件*/
#include /*链表*/
#include /*字体*/
#include "Headfile.h"
#define LEN sizeof(struct thing)
int n=0,m=0;/*主程序变量*/
struct thing *ha=NULL;/*主程序指针*/
FILE *fp=NULL;
struct thing/*商品数据*/
{
long d;
char name[10];
int num;
int mon;
long tol;
struct thing *next;
};
struct thing *creat(void)/*建立*/
{
struct thing *headc,*p1,*p2;
char q;
n=1;
p1=p2=(struct thing *)malloc(LEN);
C: printf("Creat A Ware->\n");
printf("Number:");
scanf("%ld",&p1->d);
printf("Name:");
scanf("%s",&p1->name);
printf("Quantity:");
scanf("%d",&p1->num);
printf("Price:");
scanf("%d",&p1->mon);
//printf("Sales Volume:");
//scanf("%ld",&p1->tol);
p1->tol=p1->num*p1->mon;
headc=NULL;/*头地址空*/
B: printf("--------------------\n");
printf("Mission Complete!\n");
printf("Creat Another Ware?\n");
printf("1.Yes\n");
printf("2.No\n");
printf("3.Quit System\n");
printf("Please Input Order:");
scanf("%s",&q);
printf("--------------------\n");
switch(q)
{
case 49:
if(n==1)
{
headc=p1;/*定义地址头*/
}
else
{
p2->next=p1;/*连接上一个地址*/
}
n=n+1;
p2=p1;
p1=(struct thing *)malloc(LEN);
printf("Creat A Ware->\n");
printf("Number:");
scanf("%ld",&p1->d);
printf("Name:");
scanf("%s",&p1->name);
printf("Quantity:");
scanf("%d",&p1->num);
printf("Price:");
scanf("%d",&p1->mon);
//printf("Sales Volume:");
//scanf("%ld",&p1->tol);
p1->tol=p1->num*p1->mon;
goto B;
case 50:
if(n==1)
{
headc=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct thing *)malloc(LEN);
p2->next=NULL;/*末地址空*/
printf("Please Wait...\n");
ha=headc;
return(headc);
case 51:
ha=headc;printf("Thank You!");ep();exit(0);break;
default:
printf("--------------------\nError!\n");
goto C;
}
}
void print(struct thing *headp)/*输出*/
{
struct thing *p;
printf("%d record:\n",n);
p=headp;/*指针转换*/
if(headp!=NULL)/*非空指针*/
{
do
{
printf("Number:%5ld Name:%10s Quantity:%5d Price:%5d Sales volume:%5ld\n",p->d,p->name,p->num,p->mon,p->tol);
p=p->next;
}while(p!=NULL);/*输出地址数据*/
}
printf("--------------------\nPlease Wait...\n");
}
struct thing *revise(struct thing *headr,long d)/*修改*/
{
struct thing *p1,*p2;
if(headr==NULL)/*头地址空*/
{
printf("No record!\n");
goto F;
}
p1=headr;/*地址转换*/
while(d!=p1->d&&p1->next!=NULL)/*找出符合条件的地址*/
{
p2=p1;
p1=p1->next;
}
if(d==p1->d)
{
printf("Revise The Ware->\n");
printf("Number:");
scanf("%ld",&p1->d);
printf("Name:");
scanf("%s",&p1->name);
printf("Quantity:");
scanf("%d",&p1->num);
printf("Price:");
scanf("%d",&p1->mon);
//printf("Sales Volume:");
//scanf("%ld",&p1->tol);
p1->tol=p1->num*p1->mon;
printf("Revise Complete!\n",d);
}
else
{
printf("Revise ware failure,ware not been found!\n");
}
F: return(headr);
}
struct thing *del(struct thing *headd,long d)/*删除*/
{
struct thing *p1,*p2;
if(headd==NULL)/*头地址空*/
{
printf("No record!\n");
goto end;
}
p1=headd;/*地址转换*/
while(d!=p1->d&&p1->next!=NULL)/*找出符合条件的地址*/
{
p2=p1;
p1=p1->next;
}
if(d==p1->d)
{
if(p1==headd)/*符合条件的地址为头地址*/
{
headd=p1->next;/*设定下个地址为头地址*/
}
else
{
p2->next=p1->next;/*连接间断地址*/
}
printf("Delete The %ld Number.\n",d);
n=n-1;
}
else
{
printf("Removed node failure,node not been found!\n");
}
end:return(headd);
}
struct thing *insert(struct thing *headi,struct thing *th)/*插入*/
{
struct thing *p0,*p1,*p2;
p1=headi;/*老数据地址*/
p0=th;/*新数据地址*/
if(headi==NULL)/*老数据地址为空*/
{
headi=p0;
p0->next=NULL;
}
else
{
while((p0->d>p1->d)&&(p1->next!=NULL))/*找出符合条件的地址*/
{
p2=p1;
p1=p1->next;
}
if(p0->d<=p1->d)
{
if(headi==p1)/*空数据*/
{
headi=p0;
}
else
{
p2->next=p0;
}
p0->next=p1;
}
else
{
p1->next=p0;/*连接老地址*/
p0->next=NULL;
}
}
n=n+1;
return(headi);
}
void run()/*加载文件*/
{
struct thing *h1;
FILE *fp;
h1=(struct thing *)malloc(LEN);
//fp=fopen("set_n","rb");
if((fp=fopen("set_n","rb"))==NULL)/*读取文件*/
{
//fp=fopen("set_n","wb+");
if((fp=fopen("set_n","wb+"))==NULL)/*如果没有则创建文件*/
{
printf("Error!\n");
exit(0);
}
printf("No Data.Please Creat A New!\n");
h1=creat();/*创建函数*/
save(h1,fp);
fclose(fp);
goto D;
}
load(fp);/*指针转换*/
D: fclose(fp);
}
void query()/*查询选项的函数*/
{
print(ha);
}
void add()/*增加选项的函数*/
{
struct thing *h3,*th;
printf("Input The Add Data:\n");
th=(struct thing *)malloc(LEN);
printf("Number:");
scanf("%ld",&th->d);
printf("Name:");
scanf("%s",&th->name);
printf("Quantity:");
scanf("%d",&th->num);
printf("Price:");
scanf("%d",&th->mon);
//printf("Sales Volume:");
//scanf("%ld",&th->tol);
th->tol=th->num*th->mon;
h3=insert(ha,th);/*增加函数*/
print(h3);
ha=h3;/*指针转换*/
}
void removed()/*删除选项的函数*/
{
struct thing *h4;
long dn;
printf("Input The Removed Number:");
scanf("%ld",&dn);
h4=del(ha,dn);/*删除函数*/
print(h4);
ha=h4;/*指针转换*/
}
void change()/*修改选项的函数*/
{
struct thing *h5;
long dr;
printf("Input The Wares Number You Want To Revise:");
scanf("%ld",&dr);
h5=revise(ha,dr);/*修改函数*/
print(h5);
ha=h5;/*指针转换*/
}
void ep()/*退出保存*/
{
FILE *fp;
fp=fopen("set_n","wb");
if((fp=fopen("set_n","wb"))==NULL)
{
printf("Error!");
exit(0);
}
save(ha,fp);
fclose(fp);
}
void save(struct thing *p,FILE*fp)/*保存函数*/
{
if(p!=NULL)
{
do
{
fwrite(p,LEN,1,fp);/*保存地址信息*/
p=p->next;
}while(p!=NULL);
}
printf("Save Data!\n");
}
void load(FILE *f)/*读取函数*/
{
struct thing *p1,*p2,*headl=NULL;
/*ha=(struct thing *)malloc(LEN);
if(fread(ha,LEN,1,f)!=1)
{
free(ha);
ha=NULL;
goto E;
}*/
p1=p2=(struct thing *)malloc(LEN);
while(fread(p1,LEN,1,f)!=0)/*读取文件*/
{
if(n==0)
{
headl=p1;
}
else
{
p2->next=p1;
}
p2=p1;
n++;
p1=(struct thing *)malloc(LEN);
}
p2->next=NULL;
ha=headl;
} 【Headfile.h】void run();
void query();
void add();
void removed();
void menu();
void save();
void ep();
void load();
void change();【Mainfile.c】#include
#include /*文件*/
#include /*链表*/
#include /*字体*/
#include "Headfile.h"
#include "Datafile.c"
void main()/*主函数*/
{
char i;
printf(" *************************************************************\n");
printf("\n");
printf(" ** Welcome! < Wares Management System > - XuHaiPing WarSki **\n");
printf("\n");
printf(" *************************************************************\n");
printf("\n\n\n");
run();/*初始化*/
A: printf("*********************\n");
printf("** 1.Query Wares **\n");
printf("** 2.Add Wares **\n");
printf("** 3.Removed Wares **\n");
printf("** 4.Revise Wares **\n");
printf("** 5.Save Wares **\n");
printf("** 6.Screen Clean **\n");
printf("** 7.Quit System **\n");
printf("*********************\n");
printf("Please Input Your Order:");
scanf("%s",&i);
switch(i)
{
case 49:printf("--------------------\nPlease Wait...\n");query();break;/*查询*/
case 50:printf("--------------------\nPlease Wait...\n");add();break;/*增加*/
case 51:printf("--------------------\nPlease Wait...\n");removed();break;/*删除*/
case 52:printf("--------------------\nPlease Wait...\n");change();break;/*修改*/
case 53:printf("--------------------\nPlease Wait...\n");ep();break;/*保存*/
case 54:system("cls");break;/*清屏*/
case 55:ep();printf("--------------------\nThank You!\n");exit(0);break;/*退出*/
default:printf("--------------------\nError!\n");goto A;/*返回*/
}
goto A;
}