布依族服饰图片手绘:编程高手们帮帮忙啊

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/27 21:32:17
#include<stdio.h>
#include<stdlib.h>
struct Customer
{
int id;
char name[20];
float price;
};
int writeCustomer();
int readCustomer();
void main()
{
char choice;

printf("请选择操作\n \t 输入客户信息:r或R\n \t 2:读取客户信息:w或W\n \t 3:退出\n");
scanf(" %c",&choice);

if (choice=='r' || choice=='R')
{
//printf("%c",choice);
readCustomer();

}
else if(choice=='w' || choice=='W')
{
//printf("%c",choice);
writeCustomer();
}
else
{
exit(0);
}
}

int writeCustomer()
{
FILE *wfp;
struct Customer custdata;
char choice;

if((wfp=fopen("customer.dat","a+"))==NULL)
{
printf("文件不存在!");
exit(1);
}

do
{
printf("\n请输入客户编号:");
scanf("%d",&custdata.id );
printf("请输入客户姓名:");
scanf("%s",custdata.name );
printf("请输入客户消费的金额:");
scanf("%f",&custdata.price);

fwrite(&custdata,sizeof(Customer),1,wfp);

printf("还要继续存储客户的信息吗?(Y/N):");
scanf(" %c",&choice);
}while(choice=='y' || choice=='Y');

fclose(wfp);
return (0);
}
int readCustomer()
{
FILE *rfp;
struct Customer custdata;

if((rfp=fopen("customer.dat","r"))==NULL)
{
printf("文件不存在!");
exit(1);
}

while(!feof(rfp))
{
fread(&custdata,sizeof(Customer),1,rfp);
printf("客户编号:%d \n",custdata.id);
printf("客户姓名:%s \n",custdata.name);
printf("客户消费金额:%.2f \n",custdata.price);

printf("\n");
}
fclose(rfp);
return 0;
}

谢谢你们能一步一步帮我解释一下 每一行都代表着什么意思吗?

#include<stdio.h> //标准输入输出库头文件
#include<stdlib.h> //标准库头文件
struct Customer //定义结构,包含id,姓名,价格
{
int id;
char name[20];
float price;
};
int writeCustomer(); //函数定义
int readCustomer(); //函数定义
void main()
{
char choice; //定义字符型变量choice

printf("请选择操作\n \t 输入客户信息:r或R\n \t 2:读取客户信息:w或W\n \t 3:退出\n"); //屏幕上输出
scanf(" %c",&choice); // 输入一个字符

if (choice=='r' || choice=='R') //如果输入字符为r或者R,调用readCustomer函数
{
//printf("%c",choice);
readCustomer();

}
else if(choice=='w' || choice=='W') //如果输入字符为w或者W,调用WriteCustomer函数

{
//printf("%c",choice);
writeCustomer();
}
else //如果不是上述字符,那么退出程序。
{
exit(0);
}
}

int writeCustomer()
{
FILE *wfp; //定义文件指针
struct Customer custdata; //定义结构变量cistdata
char choice; //定义字符变量choice

if((wfp=fopen("customer.dat","a+"))==NULL)//如果以添加的方式打开文件,但指针返回为空,那么说明文件不存在
{
printf("文件不存在!"); //输出提示信息
exit(1); //退出函数
}

do //循环结构,DO……WHILE
{
printf("\n请输入客户编号:"); //屏幕输出提示信息
scanf("%d",&custdata.id ); //输入
printf("请输入客户姓名:");
scanf("%s",custdata.name );
printf("请输入客户消费的金额:");
scanf("%f",&custdata.price);

fwrite(&custdata,sizeof(Customer),1,wfp);//写入文件

printf("还要继续存储客户的信息吗?(Y/N):"); //输出提示信息
scanf(" %c",&choice); //输入
}while(choice=='y' || choice=='Y');//只有输入字符y或者Y时,循环继续,否则,循环结束

fclose(wfp); //关闭文件
return (0); //返回调用函数
}
int readCustomer()
{
FILE *rfp;
struct Customer custdata;

if((rfp=fopen("customer.dat","r"))==NULL)
{
printf("文件不存在!");
exit(1);
}

while(!feof(rfp)) //只要不是文件末尾就循环
{
fread(&custdata,sizeof(Customer),1,rfp);
printf("客户编号:%d \n",custdata.id);
printf("客户姓名:%s \n",custdata.name);
printf("客户消费金额:%.2f \n",custdata.price);

printf("\n");
}
fclose(rfp);
return 0;
}