| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4420 人关注过本帖, 1 人收藏
标题:程序中定义了太多的全局变量,如何修改
只看楼主 加入收藏
龙翔天涯
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-10-25
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:8 
程序中定义了太多的全局变量,如何修改
#include<stdio.h>
#include<string.h>  
#include<stdlib.h>
#include<ctype.h>
#define STACK_INIT_SIZE 10
#define OK 1
#define TRUE 1
#define ERROR 0
struct student {
int carnum;
char lendbook[10];
}student[1000];
struct book
{
int booknum;
char bookname[20];
float bookprice;
char bookcreat[20];
int turefalse;
}book[1000];
struct car
{
int carnum;
char sex[5];
char studentname[20];
int studentclass;
}car[1000];
findbook()
{
FILE *fp;
char bookname[10];
int i;
fp=fopen("book.txt","r");
printf("请输入你要查找的书名:\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
   if(strcmp(bookname,book[i].bookname)==0)
   {
    if(book[i].turefalse==1)
     
    {
    printf(" 书号      书名      作者 \n %d      %s     %s \n",book[i].booknum,book[i].bookname,book[i].bookcreat);
    printf("此书现在无人借阅\n按任意键返回\n"); }
    else {printf("这本书已经有人借出\n");fclose(fp);return 0;}
    fclose(fp);
    return 0;
   }
}
printf("没有你要查询的书籍!\n");
fclose(fp);
return 0;
}
findbook1()
{
FILE *fp;
char bookcreat[10];
int i;
fp=fopen("book.txt","r");
printf("请输入你要查找的作者名:\n");
scanf("%s",bookcreat);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
   if(strcmp(bookcreat,book[i].bookcreat)==0)
   {
    if(book[i].turefalse==1)
    {
     printf(" 书号  书名  作者 \n %d    %s    %s \n",book[i].booknum,book[i].bookname,book[i].bookcreat);
    printf("此书现在无人借阅\n按任意键返回\n");
    }
    else {printf("这本书已经有人借出!\n");
    fclose(fp);
    return 0;
    }
    fclose(fp);
    return 0;
   }
}
printf("没有你要查询的书籍\n");
fclose(fp);
return 0;
}
chabook()
{
char ch5;
do
{
printf(" 1: 按书名查找 \n");
printf(" 2: 按作者查找 \n");
printf(" 0: 返回 \n");
printf("请输入0━2:\n");
ch5=getch();
switch(ch5)
   {
    case '1':findbook();getch();break;
    case '2':findbook1();getch();break;
    case '0':break;
    default:printf("无此操作!\n");getch();break;
   }
}while(ch5!='0');
return 0;
}
lendbook()
{
FILE *fp,*fp2;
int i,n;
int carnum;

printf("请输入借书人的:\n卡号:\n");
scanf("%d",&carnum);
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
   if(car[i].carnum==carnum)
   {
    n=i;
    fclose(fp);
    printf("书名:\n");
    scanf("%s",student[n].lendbook);
    fp=fopen("book.txt","r");
    for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
    {
     if(strcmp(book[i].bookname,student[n].lendbook)==0)
     {
      if(book[i].turefalse==0) {printf("对不起,此书有人借出,请借其他书\n");fclose(fp);getch();return 0;}
      else
      fclose(fp);
      fp=fopen("record.txt","a+");
      student[n].carnum=carnum;
      fwrite(&student[n],sizeof(struct student),1,fp);
      fclose(fp);
      fp=fopen("book.txt","r");
      fp2=fopen("bookl.txt","w");
      for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
      {
       if(strcmp(book[i].bookname,student[n].lendbook)==0)
       {
        book[i].turefalse=0;
        fwrite(&book[i],sizeof(struct book),1,fp2);
        continue;
       }
       fwrite(&book[i],sizeof(struct book),1,fp2);
      }
      fclose(fp);
      fclose(fp2);
      fp=fopen("book.txt","w");
      fp2=fopen("bookl.txt","r");
      for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
      {
       fwrite(&book[i],sizeof(struct book),1,fp);
      }
      fclose(fp);
      fclose(fp2);
      fopen("bookl.txt","w");
      fclose(fp2);
      printf("借书完毕,按任意键返回...\n");
      getch();
      return 0;
     }
    }
    printf("不存在这样的书,任意键返回...\n");
    fclose(fp);
    getch();
    return 0;
   }
}
printf("你的卡号不存在,请申请新卡,按任意键返回...\n");
fclose(fp);
getch();
return 0;}
huanbook()
{
FILE *fp,*fp2;
int i,n;
int carnum;
char lendbook[10];

printf("\t\t(^_^)欢迎进入还书界面(^_^)");
printf("\n请你输入你的\n卡号:\n");
scanf("%d",&carnum);
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)  
{
   if(car[i].carnum==carnum)
   {
    n=i;
    fclose(fp);
    printf("请输入你要还的书的名字:\n");
    scanf("%s",lendbook);
    fp=fopen("record.txt","r");
    for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)  
    {
     if(strcmp(student[i].lendbook,lendbook)==0)  
     {
      fclose(fp);
      fp=fopen("record.txt","r");
      fp2=fopen("bookl.txt","w");
      for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
      {
       if(strcmp(student[i].lendbook,lendbook)==0)
       {
        continue;
       }
       fwrite(&student[i],sizeof(struct student),1,fp2);  
      }
      fclose(fp);
      fclose(fp2);
      fp=fopen("record.txt","w");
      fp2=fopen("bookl.txt","r");
      for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
       {
        fwrite(&student[i],sizeof(struct student),1,fp);
       }
      fclose(fp);
      fclose(fp2);
      fopen("bookl.txt","w");
      fclose(fp2);
      fp=fopen("book.txt","r");
      fp2=fopen("bookl.txt","w");
      for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)  
      {
       if(i==n)
       {
        book[i].turefalse=1;
        fwrite(&book[i],sizeof(struct book),1,fp2);
        continue;
       }
       fwrite(&book[i],sizeof(struct book),1,fp2);
      }
      fclose(fp);
      fclose(fp2);
      fp=fopen("book.txt","w");
      fp2=fopen("bookl.txt","r");
      for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
      {
       fwrite(&book[i],sizeof(struct book),1,fp);
      }
      fclose(fp);
      fclose(fp2);
      fopen("bookl.txt","w");
      fclose(fp2);
      printf("还书完毕,按任意键返回...\n");
      getch();
      return 1;
     }
    }
    printf("你没有借这样的书,任意键返回...\n");
    fclose(fp);
    getch();
    return 0;
   }
}
printf("没这样的卡,和管理员联系,按任意键返回...\n");  
fclose(fp);
getch();
return 0;}
lendcount()
{
FILE *fp;
int i,n=0;
fp=fopen("record.txt","r");
if((fp=fopen("record.txt","r"))==0)
{printf("文件中没有数据!\n");
printf("按任意键返回...\n");
getch();
return(0);
}
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
   printf("卡号      借出的书籍     \n");
   printf("%d       %s \n",student[i].carnum,student[i].lendbook);
   n=n+1;
}
fclose(fp);
printf("目前共有%d本书借出\n",n);
printf("按任意键...\n");
getch();
return n;
}
addcar()
{
FILE *fp;
int i=0;
fp=fopen("car.txt","a+");
printf("卡号:\n");
scanf("%d",&car[i].carnum);
printf("性别:\n");
scanf("%s",car[i].sex);
printf("姓名:\n");
scanf("%s",car[i].studentname);
printf("班级:\n");
scanf("%d",&car[i].studentclass);
fwrite(&car[i],sizeof(struct car),1,fp);
fclose(fp);
printf("输入完毕,任意键返回...\n");
getch();
}
delcar()
{
FILE *fp,*fp2;
int i;
int carnum;
char choice;
fp=fopen("car.txt","r");
fp2=fopen("bookl.txt","w");
printf("要删除的卡号(不存在则不作改动):\n");
scanf("%d",&carnum);
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
   if(car[i].carnum!=carnum)
   {
    fwrite(&car[i],sizeof(struct car),1,fp2);
   }
}
fclose(fp);
fclose(fp2);
 
printf("确认删除?(Y/N)!\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
   fp=fopen("car.txt","w");
   fp2=fopen("bookl.txt","r");
   for(i=0;fread(&car[i],sizeof(struct car),1,fp2)!=0;i++)
   {
    fwrite(&car[i],sizeof(struct car),1,fp);
   }
   fclose(fp);
   fclose(fp2);
   fp2=fopen("bookl.txt","w");
   fclose(fp2);
   printf("操作超成功!\n按任意键返回...\n");
   getch();
   return 0;
}
else
{
   printf("操作已取消!\n按任意键返回...\n");
   getch();
   return 0;
}
}
carcount()
{
FILE *fp;
int i,n=0;
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
   printf("第%d张卡 \n",i+1);
   printf("   卡号        性别         姓名      班级   \n");
   printf("\t\t\n");
   printf("   %d     %6s    %10s      %3d \n\n",car[i].carnum,car[i].sex,car[i].studentname,car[i].studentclass);
   n=n+1;
}
fclose(fp);
printf("目前共有%d个学生。\n",n);
printf("按任意键返回...\n");
getch();
}
addbook()
{
FILE *fp;
int i=0;
char choice='y';
fp=fopen("book.txt","a+");
while(choice=='y'||choice=='Y')
{  
   printf("书号:\n");
   scanf("%d",&book[i].booknum);
   printf("书名:\n");
   scanf("%s",book[i].bookname);
   printf("作者:\n");
   scanf("%s",book[i].bookcreat);
   printf("单价:\n");
   scanf("%f",&book[i].bookprice);
   printf("请你设定书的状态(1在架/0不在):\n");  
   scanf("%d",&book[i].turefalse);
   fwrite(&book[i],sizeof(struct book),1,fp);
   printf("是否要输入下本书(y/n)\n");
   scanf("%s",&choice);
}
fclose(fp);
}
 
delbook()
{
FILE *fp,*fp2;
int i;
char bookname[10],choice;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("书名:\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
   if(strcmp(bookname,book[i].bookname)!=0)
   {
    fwrite(&book[i],sizeof(struct book),1,fp2);
   }
}
fclose(fp);
fclose(fp2);
printf("确认删除(y/n)?\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
   fp=fopen("book.txt","w");
   fp2=fopen("bookl.txt","r");
   for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
   {
    fwrite(&book[i],sizeof(struct book),1,fp);
   }
   fclose(fp);
   fclose(fp2);
   fp2=fopen("bookl.txt","w");
   fclose(fp2);
   printf("按任意键返回...\n");
   getch();
   return 0;
}
else
{
   printf("按任意键返回...\n");
   getch();
   return 0;
}
}
changebook()
{
FILE *fp,*fp2;
char bookname[10],choice;
int i;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("书名:\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
   if(strcmp(book[i].bookname,bookname)==0)
   {
    printf("你所要修改的书的资料如下,请选择你要修改的内容:\n");
    printf("序号       书名      作者 \n");
    printf("%d   %s  %s\n",book[i].booknum,book[i].bookname,book[i].bookcreat);
    printf("1: 修改书号:\n");
    printf("2: 修改书名:\n");
    printf("3: 修改作者:\n");
    printf("4: 修改单价:");
    printf("请输入1━3:");
     choice=getch();
    switch(choice)
    {
     case '1':
     {
      printf("新的书号:\n");
      scanf("%d",&book[i].booknum);
      fwrite(&book[i],sizeof(struct book),1,fp2);
     }break;
     case '2':
     {
      printf("新的书名:\n");
      scanf("%s",book[i].bookname);
      fwrite(&book[i],sizeof(struct book),1,fp2);
     }break;
     case '3':
     {
      printf("新的作者:\n");
      scanf("%s",book[i].bookcreat);
      fwrite(&book[i],sizeof(struct book),1,fp2);
     }break;
     case '4':
    {
        printf("新的单价:");
        scanf("%f",book[i].bookprice);
        fwrite(&book[i],sizeof(struct book),1,fp2);
    }
     default:printf("没有这样的操作!");break;
    }
    continue;
   }
   fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
   fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回...\n");
getchar();
return 0;
}
bookcount()
{
FILE *fp;
int i,n=0;
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
   if(book[i].booknum!=0&&strlen(book[i].bookname)!=0&&strlen(book[i].bookcreat)!=0)
   {
    printf("第%d本书\n序号            书名               作者       单价    状态 \n",i+1);
    printf("\t\t\n");
    printf(" %d     %10s    ",book[i].booknum,book[i].bookname);
    printf("%15s      %4.2f    %d(1在/0不在)\n",book[i].bookcreat,book[i].bookprice,book[i].turefalse);
    n=n+1;
   }
}
fclose(fp);
printf("目前共有%d本书.\n",n);
printf("按任意键返回...\n");
}
end()
{
    printf("\t\t\n");
    printf("\t\t\n");
    printf("\t\t┃┃ 谢谢使用本系统 , 再见! ┃┃\n");
      printf("\t\t\n");
      printf("\t\t\n");   
}
main()
{
char ch1,ch2,ch4;
do
{  
   printf("\t\n");
   printf("\t\t    『制作:郭献安 学号:20101001871 班号: 054101-13』\n\n");
   printf("\t\t\n");
   printf("\t\t\n");
   printf(" 请选择需要的服务:\n");
   printf(" 1:查阅图书 \n");
   printf(" 2:借阅图书 \n");
   printf(" 3:管理系统 \n");
   printf(" 4:还书 \n");
   printf(" 0:退出 \n");
   printf("请输入数字0━4:\n");
   ch1=getch();
   switch(ch1)
   {
    case '1':chabook();break;
    case '2':lendbook();;break;
    case '3':{
       do
       {
        printf(" 1:增加图书 \n");
        printf(" 2:删除图书 \n");
        printf(" 3:修改图书 \n");
        printf(" 4:图书统计 \n");
        printf(" 5:学生管理系统 \n");
        printf(" 0:返回 \n");
        printf("请输入0━5:\n");
         ch2=getch();
        switch(ch2)
        {
         case '1':addbook();
         break;
         case '2':delbook();break;
         case '3':changebook();break;
         case '4':bookcount();getch();break;
         case '5':{
            do
            {
            printf(" 1: 申请新卡 \n");
            printf(" 2: 删除卡号 \n");
            printf(" 3: 借书统计 \n");
            printf(" 4: 卡号统计 \n");
            printf(" 0: 返回 \n");
            printf("请输入0━4:\n");
             ch4=getch();
            switch(ch4)
             {
              case '1':addcar();break;
              case '2':delcar();break;
              case '3':lendcount();break;
              case '4':carcount();break;
              case '0':break;
              default:printf("无此操作!\n");getch();break;
             }
            }while(ch4!='0');}break;
            case '0':break;
            default:printf("无此操作!\n");getch();break;
           }
        }
             while(ch2!='0');}break;
        case '4':huanbook();break;
        case '0':break;
        default:printf("无此操作!\n");getch();break;
   }
}while(ch1!='0');
}                    
搜索更多相关主题的帖子: include 如何 
2013-10-26 14:42
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:3 
晕,那么长啊。
边写边检查,别一次性把代码写好了再来找错

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-10-26 16:11
小小程序猿
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:1
帖 子:755
专家分:2785
注 册:2013-7-18
收藏
得分:3 
楼上说的对

孤独与寂寞是催化一个人迅速成长的良药,没有之一
2013-10-26 17:00
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:3 
不知道楼主问题出在哪
2013-10-26 18:38
a592963144
Rank: 2
等 级:论坛游民
帖 子:41
专家分:95
注 册:2013-9-27
收藏
得分:3 
有点长 我还是看 大神更正过的吧
2013-10-26 21:13
在这里爬起
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:43
专家分:113
注 册:2013-8-9
收藏
得分:3 
如果没加函数类型就默认为int型,所以你好多是需要返回值的。
2013-10-28 09:58
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:3 
這是學語言不學程序設計的後果

授人以渔,不授人以鱼。
2013-10-28 11:59
heroinearth
Rank: 10Rank: 10Rank: 10
来 自:云南曲靖
等 级:青峰侠
帖 子:430
专家分:1506
注 册:2011-10-24
收藏
得分:3 
我的想法是:
struct student {
    int carnum;
    char lendbook[10];
}student[1000];

struct car
{
    int carnum;
    char sex[5];
    char studentname[20];
    int studentclass;
}car[1000];
合并为一个,不定义数组,通过链表来完成,
2013-10-28 12:03
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
回复 8楼 heroinearth
數據結構不是根據業務邏輯來,而是依照自己寫代碼方便來?

授人以渔,不授人以鱼。
2013-10-28 12:28
快速回复:程序中定义了太多的全局变量,如何修改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.122709 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved