#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
struct Computer
{
char Name[50];
char Brand[50];
char Model[50];
int Sell_quantity;
float Purchase_price;
int Stock_quantity;
float Sell_price;
}Computer;
typedef struct ComputerLink{
char Name[50];
char Brand[50];
char Model[50];
int Sell_quantity;
float Purchase_price;
int Stock_quantity;
float Sell_price;
}ComputerLink;
void MainMenu();
void InputComputer();
void DeleteComputer();
void AlterComputer();
void FindStockComputer();
void FindSellgoodComputer();
void FindTypeStockComputer();
void ComputerSell();
ComputerLink *CreatComputerLink();
void SaveComputer(ComputerLink *head);
int main(void){
MainMenu();
return 0;
}
void MainMenu(){
int num;
printf("\n--------------------欢迎进入笔记本电脑销售系统----------------------------------");
printf("\n --------------------------------------------------------------------------");
printf("\n服务编号;");
printf("\n1.添加笔记本电脑信息");
printf("\n2.删除笔记本电脑信息");
printf("\n3.修改笔记本电脑信息");
printf("\n4.查询笔记本库存现状");
printf("\n5.查询目前销售最高的笔记本");
printf("\n6.查询某品牌笔记本库存");
printf("\n7.笔记本销售");
printf("\n8.退出系统");
printf("\n9.格式化系统");
printf("\n Input;");
scanf("%d",&num);
switch(num){
case 1: InputComputer();
case 2: DeleteComputer();
case 3: AlterComputer();
case 4: FindStockComputer();
case 5: FindSellgoodComputer();
case 6: FindTypeStockComputer();
case 7: ComputerSell();
break;
}
}
void InputComputer(){
ComputerLink *p,*q,*head;
char Name[50];
int x;
printf("请按如下格式输入来添加笔记本信息:");
printf("产品名称\t产品品牌\t产品型号\t销售数量\t进价\t库存数量\t售价\t");
printf("当输入产品名称为0时则为停止录入!");
printf("Input:");
head=(ComputerLink *)malloc(sizeof(ComputerLink));
p=head;
scanf("%s",Name);
while(1){
if(Name==0) break;
else{
q=(ComputerLink *)malloc(sizeof(ComputerLink));
strcpy(q->Name,Name);
scanf("%s%s%s%d%d%d%d",q->Name,q->Brand,q->Model,q->Sell_quantity,q->Purchase_price,q->Stock_quantity,q->Sell_price);
p->next=q;
p=q;
}
scanf("%s",Name);
}
q->next=NULL;
printf("保存成功!请按回车键继续!");
}
void DeleteComputer(){
ComputerLink *p,*head,*q;
char Name[50];
int x;
head=CreatComputerLink();
scanf("%d",Name);
q=head;
p=q->next;
while(p!=NULL&&(p->Name,Name)!=0){
q=p;
p=p->next;
if(p==NULL){
printf("改电脑不存在");
}else{
q->next=p->next;
free(p);
SaveComputer(head);
printf("该电脑信息已被删除!\n");
}
}
}
void AlterComputer(){
ComputerLink *head,*p;
char Name[50];
int x;
head=CreatComputerLink();
scanf("%s",Name);
p=head->next;
for(p!=NULL&&strcmp(p->Name,Name)!=0;p=p->next)
if(p==NULL){
printf("没有找到该电脑信息!\n");
}else{
printf("请按如下格式输入来修改一条笔记本信息:");
printf("产品名称\t产品品牌\t产品型号\t销售数量(修改后)\t进价(修改后)\t库存数量(修改后)\t售价(修改后)\t");
scanf("%s%s%s%d%d%d%d",q->Name,q->Brand,q->Model,q->Sell_quantity,q->Purchase_price,q->Stock_quantity,q->Sell_price);
SaveRaed(head);
}
}
void FindStockComputer(){
Computer C;
char Name[50];
FILE *fp;
printf("Input:");
scanf("%s",Name);
if((fp=fopen("reader2.txt","r"))==NULL) {
exit(0);
}
while(!feof(fp)){
fscanf(fp,"%s%s%s%d%d%d%d",C.Name,C.Brand,C.Model,C.Sell_quantity,C.Purchase_price,C.Stock_quantity,C.sell_price);
if(strcmp(C.Name,Name)==0){
printf("\n%s\t%s\t%s\t%d\t%d\t%d\t%d",C.Name,C.Brand,C.Model,C.Sell_quantity,C.Purchase_price,C.Stock_quantity,C.sell_price);
break;
}
}
fclose(fp);
}
void FindSellgoodComputer(){
Computer C;
char Name[50];
FILE *fp;
printf("Input:");
scanf("%s",Name);
if((fp=fopen("reader2.txt","r"))==NULL) {
exit(0);
}
while(!feof(fp)){
fscanf(fp,"%s%s%s%d%d%d%d",C.Name,C.Brand,C.Model,C.Sell_quantity,C.Purchase_price,C.Stock_quantity,C.sell_price);
if(strcmp(C.Name,Name)==0){
printf("\n%s\t%s\t%s\t%d\t%d\t%d\t%d",C.Name,C.Brand,C.Model,C.Sell_quantity,C.Purchase_price,C.Stock_quantity,C.sell_price);
break;
}
}
fclose(fp);
}
void FindTypeStockComputer();
{
Computer C;
char Name[50];
FILE *fp;
printf("Input:");
scanf("%s",Name);
if((fp=fopen("reader2.txt","r"))==NULL) {
exit(0);
}
while(!feof(fp)){
fscanf(fp,"%s%s%s%d%d%d%d",C.Name,C.Brand,C.Model,C.Sell_quantity,C.Purchase_price,C.Stock_quantity,C.sell_price);
if(strcmp(C.Name,Name)==0){
printf("\n%s\t%s\t%s\t%d\t%d\t%d\t%d",C.Name,C.Brand,C.Model,C.Sell_quantity,C.Purchase_price,C.Stock_quantity,C.sell_price);
break;
}
}
fclose(fp);
}
代码修改如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Computer
{
char Name[50];
char Brand[50];
char Model[50];
int Sell_quantity;
float Purchase_price;
int Stock_quantity;
float Sell_price;
};
typedef struct _computerLink{
struct Computer data;
struct _computerLink* next;
}ComputerLink;
//因为下面的函数中都没有参数,所以这里定义全局变量来记录链表头
ComputerLink *head=0; // 链表头
char* filename="text.txt"; //文件名
void MainMenu();
void ReadFile(const char* file); //读取文件
void InputComputer();
void DeleteComputer();
void AlterComputer();
void FindStockComputer();
void FindSellgoodComputer();
void FindTypeStockComputer();
void ComputerSell();
ComputerLink *CreatComputerLink();
void SaveComputer(ComputerLink *head);
int main(void)
{
MainMenu();
return 0;
}
void MainMenu()
{
int num;
ReadFile(filename);
if(head==0)
{
printf("您是第一次使用该系统,请先录入笔记本销售信息!\n");
printf("服务编号:\n");
printf("1.新建笔记本电脑信息\n");
printf("2.退出系统\n");
scanf("%d",&num);
switch(num)
{
case 1: InputComputer();break;
case 2: return;
}
}
while(1)
{
system("cls");
printf("\n--------------------欢迎进入笔记本电脑销售系统----------------------------------");
printf("\n --------------------------------------------------------------------------");
printf("\n服务编号;");
printf("\n1.添加笔记本电脑信息");
printf("\n2.删除笔记本电脑信息");
printf("\n3.修改笔记本电脑信息");
printf("\n4.查询笔记本库存现状");
printf("\n5.查询目前销售最高的笔记本");
printf("\n6.查询某品牌笔记本库存");
printf("\n7.笔记本销售");
printf("\n8.退出系统");
printf("\n9.格式化系统");
printf("\n Input:");
scanf("%d",&num);
switch(num)
{
case 1: InputComputer(); break;
case 2: DeleteComputer(); break;
case 3: AlterComputer(); break;
case 4: FindStockComputer();break;
case 5: FindSellgoodComputer();break;
case 6: FindTypeStockComputer();break;
case 7: ComputerSell(); break;
case 8: return ;
case 9:break;
}
}
}
//从文件中读取数据,并返回链表头 。这个可能用不到,参考
void ReadFile(const char* file)
{
ComputerLink *p,*t;
FILE* fp = 0;
fp = fopen(file,"r");
if (fp == 0)
{
printf("file open error\n");
return ;
}
//
head = (ComputerLink*)malloc(sizeof(ComputerLink));
head->next = 0;
p = head;
while(!feof(fp))
{
t = (ComputerLink*)malloc(sizeof(ComputerLink));
t->next = 0;
if( fscanf(fp,"%s %s %s %d %f %d %f",t->data.Name,t->data.Brand,t->data.Model,&t->data.Sell_quantity,&t->data.Purchase_price,&t->data.Stock_quantity,&t->data.Sell_price))
{
p->next = t;
p = t;
}else
{
free(t);
}
}
}
//写文件
void SaveComputer(ComputerLink *head)
{
FILE *fp = 0;
ComputerLink* p ;
if (head==0 || head->next == 0) return;
p = head->next;
fp = fopen(filename,"w");
while(p)
{
fprintf(fp,"%s %s %s %d %f %d %f",p->data.Name,p->data.Brand,p->data.Model,p->data.Sell_quantity,p->data.Purchase_price,p->data.Stock_quantity,p->data.Sell_price);
p = p->next;
}
fclose(fp);
}
//新增电脑,要返回链表头
void InputComputer()
{
ComputerLink *p,*q;
char Name[50];
system("cls");
printf("请按如下格式输入来添加笔记本信息:");
printf("产品名称\t产品品牌\t产品型号\t销售数量\t进价\t库存数量\t售价\t");
printf("当输入产品名称为0时则为停止录入!");
printf("Input:");
if (head == 0)
{
head=(ComputerLink *)malloc(sizeof(ComputerLink));
head->next = 0;
p=head;
}else
{
//将指针移动到链表末尾
p = head;
while(p->next)
p = p->next;
}
while(1)
{
scanf("%s",Name);
if(strcmp(Name,"0")==0) break;
else{
q=(ComputerLink *)malloc(sizeof(ComputerLink));
q->next = 0; //把下一个节点置0
strcpy(q->data.Name,Name);
//注意,因为前面已经读取了Name,所以这里就不需要再读取Name了,否则读取的数据不正确
scanf("%s%s%d%f%d%f",q->data.Brand,q->data.Model,&q->data.Sell_quantity,&q->data.Purchase_price,&q->data.Stock_quantity,&q->data.Sell_price);
p->next=q;
p=q;
}
}
printf("保存成功!请按回车键继续!");
system("pause");
}
//删除信息
void DeleteComputer()
{
ComputerLink *p,*q;
char Name[50];
//int x;
//head=CreatComputerLink();
system("cls");
printf("请输入名称:");
scanf("%d",Name);
q=head;
p=q->next;
while(p!=NULL&&strcmp(p->data.Name,Name)!=0)
{
q=p;
p=p->next;
}
if(p==NULL){
printf("改电脑不存在");
}else{
q->next=p->next;
free(p);
SaveComputer(head);
printf("该电脑信息已被删除!\n");
}
system("pause");
}
//更改电脑信息
void AlterComputer()
{
ComputerLink *p;
char Name[50];
//int x;
//head=CreatComputerLink();
system("cls");
printf("请输入名称:");
scanf("%s",Name);
p=head->next;
for(;p!=NULL&&strcmp(p->data.Name,Name)!=0;p=p->next);
if(p==NULL)
{
printf("没有找到该电脑信息!\n");
}else{
printf("请按如下格式输入来修改一条笔记本信息:");
printf("产品名称\t产品品牌\t产品型号\t销售数量(修改后)\t进价(修改后)\t库存数量(修改后)\t售价(修改后)\t");
scanf("%s%s%s%d%f%d%f",p->data.Name,p->data.Brand,p->data.Model,&p->data.Sell_quantity,&p->data.Purchase_price,&p->data.Stock_quantity,&p->data.Sell_price);
SaveComputer(head);
}
system("pause");
}
//查看库存
void FindStockComputer()
{
ComputerLink* p;
char Name[50];
system("cls");
printf("Input:");
scanf("%s",Name);
if(head==0) return;
p = head->next;
while(p && strcmp(p->data.Name,Name)!= 0)
p = p->next;
if(p==0)
printf("未找到该电脑信息\n");
else
printf("%s\t%s\t%s\t%d\t%f\t%d\t%f\n",p->data.Name,p->data.Brand,p->data.Model,p->data.Sell_quantity,p->data.Purchase_price,p->data.Stock_quantity,p->data.Sell_price);
system("pause");
}
//查看销售数量最多的
void FindSellgoodComputer()
{
ComputerLink* p,*pmax=0;
system("cls");
if(head==0) return;
p = head->next;
pmax = head->next;
p = p->next;
while(p)
{
if(p->data.Sell_quantity > pmax->data.Sell_quantity)
pmax = p;
p = p->next;
}
printf("%s\t%s\t%s\t%d\t%f\t%d\t%f\n",pmax->data.Name,pmax->data.Brand,pmax->data.Model,pmax->data.Sell_quantity,pmax->data.Purchase_price,pmax->data.Stock_quantity,pmax->data.Sell_price);
system("pause");
}
//查看某品牌库存
void FindTypeStockComputer()
{
ComputerLink* p;
char Name[50];
system("cls");
if(head==0) return;
printf("Input:");
scanf("%s",Name);
p = head->next;
while(p && strcmp(p->data.Brand,Name)!=0) //品牌应该是Brand字段,不是Name字段
p = p->next;
if(p==0)
printf("未找到该品牌\n");
else
printf("%s\t%s\t%s\t%d\t%f\t%d\t%f\n",p->data.Name,p->data.Brand,p->data.Model,p->data.Sell_quantity,p->data.Purchase_price,p->data.Stock_quantity,p->data.Sell_price);
system("pause");
}
//电脑销售(这个函数要实现什么功能?)
void ComputerSell()
{
//
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!