网上找的程序是c++的 可是我们只学了c语言 想求高手帮忙转换一下
如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define B 1000
typedef struct Book /*定义教材结构体*/
{ char num[20];
char name[20];
char author[20];
char press[20];
int count;
float shoujia;
}book[B];
struct Book books[B];
void luru() /*录入教材信息*/
{
static int i = 0;
printf(“\n请输入教材名称:”);
scanf(“%s”, books[i].name);
printf(“请输入作者:”);
scanf(“%s”, books[i].author);
printf(“请输入出版社:”);
scanf(“%s”, books[i].press);
printf(“请输入教材册数:”);
scanf(“%d”, &books[i].count);
printf(“请输入编号:”);
scanf(“%s”, books[i].num);
printf(“请输入售价:”);
scanf(“%f”, &books[i].shoujia);
printf(“教材录入结束.\n”);
i++;
}
void tianjia() /*教材添加*/
{
int i,count;
char name[10];
printf(“\n请输入教材名称:”);
scanf(“%s”,name);
for(i=0;i<1000;i++)
{ if(strcmp(books[i].name,name)==0)
{ printf(“\n输入添加册数:”);
scanf(“%d”, &count);
books[i].count += count;
printf(“\n教材已添加\n”);
break;
}
else
{
printf(“\n无此教材\n”);
break;
}
}
}
void save()
{
int i = 0;
FILE *fp;
fp = fopen(“e:\book.txt”, “wr”);
while(books[i].count > 0)
{
fprintf(fp, “教材名称:%s 教材作者:%s 出版社:%s 册数:%d 编号:%s 售价:%f\n”, books[i].name, books[i].author,books[i].press,books[i].count,books[i].num,books[i].shoujia);
i++;
}
fclose(fp);
}
void chaxun() /*教材查询*/
{ FILE *fp;
int i,flag=0;
int a;
char x[32];
char y[32];
if((fp=fopen(“e:\book.txt”,”r”))==NULL)
{
printf(“此文件无法打开!\n”);
return;
}
printf(“请选择查询方式:1.教材名 2.编号\n”);
scanf(“%d”,&a);
switch(a)
{
case 1: printf(“输入教材名:\n”);scanf(“%s”,x);
for(i=0;i<1000;i++)
if (strcmp(books[i].name,x)==0) /*比较输入名与存储名的字符串大小*/
{printf(“教材名:%s 教材作者:%s 出版社:%s 册数:%d 编号:%s 售价:%f\n”,books[i].name,books[i].author,books[i].press,books[i].count,books[i].num,books[i].shoujia);
flag=1;
}
if(flag==0)
printf(“此教材不存在\n”);
break;
case 2: printf(“输入编号:\n”);scanf(“%s”,y);
for(i=0;i<1000;i++)
if (strcmp(books[i].num,y)==0)
{printf(“教材名称:%s 教材作者:%s 出版社:%s 册数:%d 编号:%s 售价:%f\n”,books[i].name,books[i].author,books[i].press,books[i].count,books[i].num,books[i].shoujia);
flag=1;
}
if(flag==0)
printf(“此教材不存在\n”);
break;
default:
break;
}
fclose(fp);
}
void show() /*显示操作界面*/
{printf(“\n ******************沈阳航空航天大学教材管理系统********************* “);
printf(“\n * 1 录入教材信息 *”);
printf(“\n * 2 查询教材信息 *”);
printf(“\n * 3 更改教材信息 *”);
printf(“\n * 4 保存教材信息 *”);
printf(“\n * 5 退出系统 *\n”);
printf(“\n 请输入您要选择的操作序号,按回车键确认:”);
}
void main()
{int n;
show();
scanf(“%d”,&n);
while (1)
{switch(n)
{ case 1: luru(); break;
case 2: chaxun(); break;
case 3: tianjia(); break;
case 4: save(); break;
case 5: exit(0); break;
default: break;
}
show();
scanf(“%d”,&n);
}
FILE *fp;
fp = fopen(“e:\book.txt”, “r+”);
if(fp==0)
printf(“无法打开文件\n”);
fclose(fp);
}
如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define B 1000
typedef struct Book /*定义教材结构体*/
{ char num[20];
char name[20];
char author[20];
char press[20];
int count;
float shoujia;
}book[B];
struct Book books[B];
void luru() /*录入教材信息*/
{
static int i = 0;
printf(“\n请输入教材名称:”);
scanf(“%s”, books[i].name);
printf(“请输入作者:”);
scanf(“%s”, books[i].author);
printf(“请输入出版社:”);
scanf(“%s”, books[i].press);
printf(“请输入教材册数:”);
scanf(“%d”, &books[i].count);
printf(“请输入编号:”);
scanf(“%s”, books[i].num);
printf(“请输入售价:”);
scanf(“%f”, &books[i].shoujia);
printf(“教材录入结束.\n”);
i++;
}
void tianjia() /*教材添加*/
{
int i,count;
char name[10];
printf(“\n请输入教材名称:”);
scanf(“%s”,name);
for(i=0;i<1000;i++)
{ if(strcmp(books[i].name,name)==0)
{ printf(“\n输入添加册数:”);
scanf(“%d”, &count);
books[i].count += count;
printf(“\n教材已添加\n”);
break;
}
else
{
printf(“\n无此教材\n”);
break;
}
}
}
void save()
{
int i = 0;
FILE *fp;
fp = fopen(“e:\book.txt”, “wr”);
while(books[i].count > 0)
{
fprintf(fp, “教材名称:%s 教材作者:%s 出版社:%s 册数:%d 编号:%s 售价:%f\n”, books[i].name, books[i].author,books[i].press,books[i].count,books[i].num,books[i].shoujia);
i++;
}
fclose(fp);
}
void chaxun() /*教材查询*/
{ FILE *fp;
int i,flag=0;
int a;
char x[32];
char y[32];
if((fp=fopen(“e:\book.txt”,”r”))==NULL)
{
printf(“此文件无法打开!\n”);
return;
}
printf(“请选择查询方式:1.教材名 2.编号\n”);
scanf(“%d”,&a);
switch(a)
{
case 1: printf(“输入教材名:\n”);scanf(“%s”,x);
for(i=0;i<1000;i++)
if (strcmp(books[i].name,x)==0) /*比较输入名与存储名的字符串大小*/
{printf(“教材名:%s 教材作者:%s 出版社:%s 册数:%d 编号:%s 售价:%f\n”,books[i].name,books[i].author,books[i].press,books[i].count,books[i].num,books[i].shoujia);
flag=1;
}
if(flag==0)
printf(“此教材不存在\n”);
break;
case 2: printf(“输入编号:\n”);scanf(“%s”,y);
for(i=0;i<1000;i++)
if (strcmp(books[i].num,y)==0)
{printf(“教材名称:%s 教材作者:%s 出版社:%s 册数:%d 编号:%s 售价:%f\n”,books[i].name,books[i].author,books[i].press,books[i].count,books[i].num,books[i].shoujia);
flag=1;
}
if(flag==0)
printf(“此教材不存在\n”);
break;
default:
break;
}
fclose(fp);
}
void show() /*显示操作界面*/
{printf(“\n ******************沈阳航空航天大学教材管理系统********************* “);
printf(“\n * 1 录入教材信息 *”);
printf(“\n * 2 查询教材信息 *”);
printf(“\n * 3 更改教材信息 *”);
printf(“\n * 4 保存教材信息 *”);
printf(“\n * 5 退出系统 *\n”);
printf(“\n 请输入您要选择的操作序号,按回车键确认:”);
}
void main()
{int n;
show();
scanf(“%d”,&n);
while (1)
{switch(n)
{ case 1: luru(); break;
case 2: chaxun(); break;
case 3: tianjia(); break;
case 4: save(); break;
case 5: exit(0); break;
default: break;
}
show();
scanf(“%d”,&n);
}
FILE *fp;
fp = fopen(“e:\book.txt”, “r+”);
if(fp==0)
printf(“无法打开文件\n”);
fclose(fp);
}
解决方案
200
这就是C语言的代码。
把变量定义放在函数的最前面就行了。
把变量定义放在函数的最前面就行了。
#include <stdio.h> #include <stdlib.h> #include <string.h> #define B 1000 typedef struct Book /*定义教材结构体*/ { char num[20]; char name[20]; char author[20]; char press[20]; int count; float shoujia; }book[B]; struct Book books[B]; void luru() /*录入教材信息*/ { static int i = 0; printf("\n请输入教材名称:"); scanf("%s", books[i].name); printf("请输入作者:"); scanf("%s", books[i].author); printf("请输入出版社:"); scanf("%s", books[i].press); printf("请输入教材册数:"); scanf("%d", &books[i].count); printf("请输入编号:"); scanf("%s", books[i].num); printf("请输入售价:"); scanf("%f", &books[i].shoujia); printf("教材录入结束.\n"); i++; } void tianjia() /*教材添加*/ { int i,count; char name[10]; printf("\n请输入教材名称:"); scanf("%s",name); for(i=0;i<1000;i++) { if(strcmp(books[i].name,name)==0) { printf("\n输入添加册数:"); scanf("%d", &count); books[i].count += count; printf("\n教材已添加\n"); break; } else { printf("\n无此教材\n"); break; } } } void save() { int i = 0; FILE *fp; fp = fopen("e:\book.txt", "wr"); while(books[i].count > 0) { fprintf(fp, "教材名称:%s 教材作者:%s 出版社:%s 册数:%d 编号:%s 售价:%f\n", books[i].name, books[i].author,books[i].press,books[i].count,books[i].num,books[i].shoujia); i++; } fclose(fp); } void chaxun() /*教材查询*/ { FILE *fp; int i,flag=0; int a; char x[32]; char y[32]; if((fp=fopen("e:\book.txt","r"))==NULL) { printf("此文件无法打开!\n"); return; } printf("请选择查询方式:1.教材名 2.编号\n"); scanf("%d",&a); switch(a) { case 1: printf("输入教材名:\n");scanf("%s",x); for(i=0;i<1000;i++) if (strcmp(books[i].name,x)==0) /*比较输入名与存储名的字符串大小*/ {printf("教材名:%s 教材作者:%s 出版社:%s 册数:%d 编号:%s 售价:%f\n",books[i].name,books[i].author,books[i].press,books[i].count,books[i].num,books[i].shoujia); flag=1; } if(flag==0) printf("此教材不存在\n"); break; case 2: printf("输入编号:\n");scanf("%s",y); for(i=0;i<1000;i++) if (strcmp(books[i].num,y)==0) {printf("教材名称:%s 教材作者:%s 出版社:%s 册数:%d 编号:%s 售价:%f\n",books[i].name,books[i].author,books[i].press,books[i].count,books[i].num,books[i].shoujia); flag=1; } if(flag==0) printf("此教材不存在\n"); break; default: break; } fclose(fp); } void show() /*显示操作界面*/ { printf("\n ******************沈阳航空航天大学教材管理系统********************* "); printf("\n * 1 录入教材信息 *"); printf("\n * 2 查询教材信息 *"); printf("\n * 3 更改教材信息 *"); printf("\n * 4 保存教材信息 *"); printf("\n * 5 退出系统 *\n"); printf("\n 请输入您要选择的操作序号,按回车键确认:"); } void main() { int n; FILE *fp; show(); scanf("%d",&n); while (1) { switch(n) { case 1: luru(); break; case 2: chaxun(); break; case 3: tianjia(); break; case 4: save(); break; case 5: exit(0); break; default: break; } show(); scanf("%d",&n); } fp = fopen("e:\book.txt", "r+"); if(fp==0) printf("无法打开文件\n"); fclose(fp); }