我的这个代码怎么把它从VC++里的C改成CodeBlocks里的C 啊!?!谢谢!代码如下
#include<stdio.h>#include<string.h>
#include<conio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct
{
int month;
int day;
}TIME;
struct goods
{
char name[20];//货物名
char kind[50];//货物种类
int count;//货物数量
TIME time1;//入店日期
TIME time2;//保质期截止日期
}goods[MAXSIZE];
int SIZE;//定义SIZE为全局变量
void save()//将数据保存到文件中
{
FILE *fp;
int i;
if((fp=fopen("goods_list2.txt","w"))==NULL)
{
printf("cannot open file\n");
}
for(i=0;i<SIZE;i++)
{
fprintf(fp,"货物名:%s\n",goods[i].name);
fprintf(fp,"货物种类:%s\n",goods[i].kind);
fprintf(fp,"货物数量:%d\n",goods[i].count);
fprintf(fp,"入店日期:%d %d\n",goods[i].time1.month,goods[i].time1.day);
fprintf(fp,"保质期截止日期:%d %d\n",goods[i].time2.month,goods[i].time2.day);
}
fclose(fp);
}
void output()//从文件中读出数据
{
FILE *fp1;
int i;
if((fp1=fopen("goods_list2.txt","r"))==NULL)
{
printf("cannot open file\n");
}
for(i=0;i<SIZE;i++)
{
fscanf(fp1,"货物名:%s\n",goods[i].name);
printf(" 货物名:%s\n",goods[i].name);
fscanf(fp1,"货物种类:%s\n",goods[i].kind);
printf(" 货物种类:%s\n",goods[i].kind);
fscanf(fp1,"货物数量:%d\n",&goods[i].count);
printf(" 货物数量:%d\n",goods[i].count);
fscanf(fp1,"入店日期:%d %d\n",&goods[i].time1.month,&goods[i].time1.day);
printf(" 入店日期:%d %d\n",goods[i].time1.month,goods[i].time1.day);
fscanf(fp1,"保质期截止日期:%d %d\n",&goods[i].time2.month,&goods[i].time2.day);
printf(" 保质期截止日期:%d %d\n",goods[i].time2.month,goods[i].time2.day);
printf("\n\n");
}
fclose(fp1);
}
void get_goods()//将事先保存在文件中的数据保存在结构体数组中
{
FILE *fp1;
int i;
if((fp1=fopen("goods_list2.txt","r"))==NULL)
{
printf("cannot open file\n");
}
for(i=0;i<SIZE;i++)
{
fscanf(fp1,"货物名:%s\n",goods[i].name);
fscanf(fp1,"货物种类:%s\n",goods[i].kind);
fscanf(fp1,"货物数量:%d\n",&goods[i].count);
fscanf(fp1,"入店日期:%d %d\n",&goods[i].time1.month,&goods[i].time1.day);
fscanf(fp1,"保质期截止日期:%d %d\n",&goods[i].time2.month,&goods[i].time2.day);
}
fclose(fp1);
}
void delete_goods()//删除无存货的商品
{
int i,j;
int p=0;
for(i=0;i<SIZE;i++)
{
if(goods[i].count==0)
{
printf(" 物 品 %s 无 存 货,已 被 删 除!\n\n",goods[i].name);
for(j=i;j<SIZE-1;j++)
goods[j]=goods[j+1];
p++;
SIZE--;
}
}
if(p==0)
printf(" 没 有 找 到 无 存 货 的 商 品!\n\n");
save();
}
void edit_goods()//修改商品信息
{
int i,choice,loop=1;
int p=0;
char name1[20];
printf("请输入需要修改的货物名称: ");
scanf("%s",name1);
for(i=0;i<SIZE;i++)
{
if(strcmp(goods[i].name,name1)==0)
{
while(loop)
{
printf("\n 1.货物名称 2.货物种类 3.货物数量 0.返回上级菜单\n");
printf("请选择需要修改的项目: ");
scanf("%d",&choice);
getchar();
switch(choice)
{
case 1:
printf("\n请输入新的货物名称: ");
scanf("%s",goods[i].name);
break;
case 2:
printf("\n请输入新的货物种类: ");
scanf("%s",goods[i].kind);
break;
case 3:
printf("\n请输入新的货物数量: ");
scanf("%d",&goods[i].count);
break;
case 0:
loop=0;
break;
default:
printf(" e r r o r!\n");
}
}
p++;
}
}
if(p==0)
{
printf(" 没 有 找 到 需 要 修 改 的 货 物\n\n");
loop=0;
}
save();
}
void list_goods()//查看货物清单
{
output();
}
void exchange_data(int i,int j)//数据互换
{
struct goods p;
p=goods[i];
goods[i]=goods[j];
goods[j]=p;
}
void sort_goods()//对商品按照入店顺序先后排序
{
int i,j;
for(i=0;i<SIZE-1;i++)
{
for(j=i+1;j<SIZE;j++)
{
if(goods[j].time1.month<goods[i].time1.month)
exchange_data(i,j);
else
if(goods[j].time1.month==goods[i].time1.month)
{
if(goods[j].time1.day<goods[i].time1.day)
exchange_data(i,j);
}
}
}
save();
}
int judge_date()//判断日期输入是否正确
{
int i;
for(i=0;i<SIZE;i++)
{
if(goods[i].time1.month<1||goods[i].time1.month>12||goods[i].time1.day<1||goods[i].time1.day>31)
return 0;
if(goods[i].time2.month<1||goods[i].time2.month>12||goods[i].time2.day<1||goods[i].time2.day>31)
return 0;
}
return 1;
}
void add()//添加货物
{
int i,n;
printf("请输入您要添加的商品数:");
scanf("%d",&n);
for(i=SIZE,SIZE=SIZE+n;i<SIZE;i++)
{
printf("请输入货物名: ");
scanf("%s",goods[i].name);
printf("请输入货物种类: ");
scanf("%s",goods[i].kind);
printf("请输入货物数量: ");
scanf("%d",&goods[i].count);
printf("请输入入店日期: ");
scanf("%d%d",&goods[i].time1.month,&goods[i].time1.day);
printf("请输入保质期截止日期: ");
scanf("%d%d",&goods[i].time2.month,&goods[i].time2.day);
}
if(judge_date()==1)
sort_goods();
else
{
SIZE=SIZE-n;
printf("日期输入错误,请重新添加\n");
}
}
void manage_goods()//商品管理
{
int choice, loop=1;
while(loop)
{
printf(" 【 菜 单 选 择 】\n\n");
printf("1.【删除无存货商品】2.【 修改商品信息 】3.【 添加商品 】4.【 查看商品清单 】0.返回主菜单\n");
scanf("%d",&choice);
getchar();
switch (choice)
{
case 1:
delete_goods();
break;
case 2:
edit_goods();
break;
case 3:
add();
break;
case 4:
list_goods();
break;
case 0:
loop=0;
break;
default:
printf(" e r r o r!\n");
}
}
}
void sell_goods()//商品销售
{
char name1[20];
int i,j,count1=0;
j=0;
get_goods();
sort_goods();
printf("请输入你需要购买的商品名称: ");
scanf("%s",name1);
for(i=0;i<SIZE;i++)
{
if(strcmp(goods[i].name,name1)==0)
{
if(count1==0)
{
printf("\n请输入您所需购买的商品数量: ");
scanf("%d",&count1);
}
if(goods[i].count<count1)
{
count1=count1-goods[i].count;
goods[i].count=0;
}
else
if(count1>0&&goods[i].count>=count1)
{
goods[i].count=goods[i].count-count1;
count1=0;
break;
}
j++;
}
}
if(i<SIZE&&count1==0)
{
printf("\n 销 售 成 功!\n");
save();
}
else
if(i>=SIZE&&count1==0&&j==0)
printf("\n 没 有 找 到 您 所 需 要 的 商 品\n");
else
printf(" \n 库 存 不 足,销 售 失 败!\n");
}
void search_goods()//商品查询
{
char name2[20];
int i,choice,p,loop=1;
p=0;
get_goods();
while(loop)
{
printf(" 功能菜单:1.查询某商品信息 2.查看全部货物清单 0.返回主菜单\n");
scanf("%d",&choice);
getchar();
if(choice==1)
{
printf("请输入您要查询的商品名称: ");
scanf("%s",name2);
for(i=0;i<SIZE;i++)
{
if(strcmp(goods[i].name,name2)==0)
{
printf("\n 货物名:%s\n",goods[i].name);
printf(" 货物种类:%s\n",goods[i].kind);
printf(" 货物数量:%d\n",goods[i].count);
printf(" 入店日期:%d %d\n",goods[i].time1.month,goods[i].time1.day);
printf(" 保质期截止日期:%d %d\n",goods[i].time2.month,goods[i].time2.day);
printf("\n\n");
p++;
}
}
if(p==0)
{
printf(" \n 没有找到您所需要的商品\n\n");
}
}
else
if(choice==2)
list_goods();
else
if(choice==0)
loop=0;
else
{
printf(" e r r o r!\n");
}
}
}
void Initiall_SIZE()//对库存数量进行初始化
{
FILE *fp1;
int i=0,p;
if((fp1=fopen("goods_list2.txt","r"))==NULL)
{
printf("cannot open file\n");
}
while(!feof(fp1))
{
p=fscanf(fp1,"货物名:%s\n",goods[i].name);
if(p==EOF)
break;
fscanf(fp1,"货物种类:%s\n",goods[i].kind);
fscanf(fp1,"货物数量:%d\n",&goods[i].count);
fscanf(fp1,"入店日期:%d %d\n",&goods[i].time1.month,&goods[i].time1.day);
fscanf(fp1,"保质期截止日期:%d %d\n",&goods[i].time2.month,&goods[i].time2.day);
i++;
}
SIZE=i;
fclose(fp1);
}
void main()
{
int loop=1,choice;
Initiall_SIZE();
get_goods();
while(loop)
{
if ( SIZE == 0 ) /* 判断有没有库存商品,如果没有的话执行 */
{
printf("\n******************目前无任何库存,请输入要添加的商品数******************\n");
add();
}
else
if(SIZE>0)
{
printf("\n\n");
printf( " ≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌\n");
printf( " Θ Θ\n");
printf( " Θ 欢迎使用商店存货管理系统 Θ\n");
printf( " Θ Θ\n");
printf( " Θ 【商品管理】…(1) Θ\n");
printf( " Θ Θ\n");
printf( " Θ 【商品销售】…(2) Θ\n");
printf( " Θ Θ\n");
printf( " Θ 【商品查询】…(3) Θ\n");
printf( " Θ Θ\n");
printf( " Θ 退出系统…(0) Θ\n");
printf( " Θ Θ\n");
printf( " ≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌≌\n");
scanf("%d",&choice);
getchar();
printf("\n");
switch (choice)
{
case 1:
manage_goods();
break;
case 2:
sell_goods();
break;
case 3:
search_goods();
break;
case 0:
loop=0;
break;
default:
printf(" e r r o r!\n");
}
}
}
}