| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 723 人关注过本帖
标题:一个课程设计的代码,想优化下。关于文件的
只看楼主 加入收藏
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
结帖率:60%
收藏
已结贴  问题点数:10 回复次数:8 
一个课程设计的代码,想优化下。关于文件的
#include <stdio.h>
#include<stdlib.h>
struct course
{
int num;
char name[15];
char xingzhi[10];
int time;
int ttime;
int etime;
int mark;
int term;
};
struct student
{
int snum;
int cnum;
};

/*insert*/
void insert()
{
void menu();
 FILE *fp;
struct course c;
if((fp=fopen("course.txt","a+"))==NULL)
{
printf("cannot open file\n");
  }
printf("\n\tInput course's information :\n");
printf("\tthe format is num name xingzhi time ttime etime mark term:\n");
scanf("%d %s %s %d %d %d %d %d",&c.num,&c.name,&c.xingzhi,&c.time,
&c.ttime,&c. etime,&c.mark,&c.term); //想在这里加一个判断语句,判断输入是否错误,确保course.txt中的内容是正确的。
fprintf(fp,"%d %s %s %d %d %d %d %d",c.num,c.name,c.xingzhi,c.time,
c.ttime,c.etime,c.mark,c.term);
fclose(fp);
menu();
}

/*browse liu_lan*/

void browser()
{
    void menu();
    FILE *fp;
    struct course c;
    if((fp=fopen("course.txt","r"))==NULL)
    {
    printf("\nCannot open course!\n");
    }
    for(;!feof(fp);)
    {
    fscanf(fp,"%d%s%s%d%d%d%d%d",&c.num,&c.name,&c.xingzhi,&c.time,
&c.ttime,&c.etime,&c.mark,&c.term);
printf("%d %s %s %d %d %d %d %d",c.num,c.name,c.xingzhi,c.time,
c.ttime,c.etime,c.mark,c.term);
    }
    fclose(fp);
    menu();
}

/*search*/
void search()
{
    void menu();
    FILE *fp;
    struct course c;
    int score;
    printf("\ninput mark your wantto search:");
    scanf("%d",&score);
   
    if((fp=fopen("course.txt","r"))==NULL)
    {
        printf("\nCannot open course!\n");
    }
    for(;!feof(fp);)
    {
        fscanf(fp,"%d %s %s %d %d %d %d %d",&c.num,&c.name,&c.xingzhi,&c.time,&c.ttime,&c.etime,&c.mark,&c.term);
    if(c.mark==score)
    printf("%d %s %s %d %d %d %d %d",c.num,c.name,c.xingzhi,c.time,
c.ttime,c.etime,c.mark,c.term);
   

}
    fclose(fp);
    menu();
}
/*tong ji xuan ke ren  shu*/
void xuanke_information()
{
    void menu();
    int total=0;
    FILE *fp;
    struct student c;
    int num;
    printf("\ninput course -number your wantto search:");
 scanf("%d",&num);
    if((fp=fopen("student.txt","r"))==NULL)
    {
     printf("\nCannot open student!\n");
    }
    for(;!feof(fp);)
    {
    fscanf(fp,"%d%d",&c.snum,&);
    if()
    {
   total++;
    printf("student_num=%d,course_num=%d",c.snum,);
    }
    }
    fclose(fp);
    printf("\n The number choose course_num %d is %d!",num,total);
    menu();
}

/*xuanke*/
void xuan_ke()
{
    void menu();
    int snum,cnum;
    FILE *fp;
    printf("\nPlease input your student_number and course number you want to choose:");
    scanf("%d%d",&snum,&cnum);
    if((fp=fopen("student.txt","a+"))==NULL)
    {
    printf("\nCannot open student!\n");
    }
    fprintf(fp,"%d %d",snum,cnum);
    fclose(fp);
    menu();
}
void menu()
{
int n,w=0;
printf("\n\t\t PLEASE ENTER A NUMBER FROM 0 TO 5!");
printf("\n\n\n\t*******************MENU**************************");
printf("\n\t\t 1 - insert informations");
printf("\n\t\t 2 - browers informations");
printf("\n\t\t 3 - search informations");
printf("\n\t\t 4 - xuan ke  informations");
printf("\n\t\t 5- xuan ke ");
printf("\n\t\t 0 - Exit system");
printf("\n\n\n\t*************************************************");
printf("\n\n\n\n\n\n\t\tChoose the number,please:");
scanf("%d",&n);
do
{
if(n>5||n<0)
{
printf("\nWrong! input again!");
scanf("%d",&n);
}
else w=1;
}while(w==0);
switch(n)
{
case 1:insert();break;
case 2:browser();break;
case 3:search();break;
case 4:xuan_ke();break;   
case 5:xuanke_information();break;
case 0:exit(0);
}
}
main()
{
menu();
}
搜索更多相关主题的帖子: 课程 文件 代码 设计 
2010-08-10 15:29
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
收藏
得分:0 
void insert()
{
void menu();
 FILE *fp;
struct course c;
if((fp=fopen("course.txt","a+"))==NULL)
{
printf("cannot open file\n");
  }
printf("\n\tInput course's information :\n");
printf("\tthe format is num name xingzhi time ttime etime mark term:\n");
scanf("%d %s %s %d %d %d %d %d",&c.num,&c.name,&c.xingzhi,&c.time,
&c.ttime,&c. etime,&c.mark,&c.term); //想在这里加一个判断语句,判断输入是否错误
fprintf(fp,"%d %s %s %d %d %d %d %d",c.num,c.name,c.xingzhi,c.time,
c.ttime,c.etime,c.mark,c.term);
fclose(fp);
menu();
}
具体的问题在这个函数中,大家帮忙看看,
2010-08-10 15:31
erikyo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:270
专家分:1154
注 册:2010-6-10
收藏
得分:3 
不知道你的出错处理是不是这个意思:
if(scanf("%d %s %s %d %d %d %d %d",&c.num,&c.name,&c.xingzhi,&c.time,&c.ttime,&c. etime,&c.mark,&c.term)!=8)
{
    printf("Error:while scanf!\n");
    return ;
}
2010-08-10 15:41
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
收藏
得分:0 
具体想要解决的问题就是在insert的过程中如果没有检验的话,会使得错误的信息保存在course.txt中然后在browser中会由于course.txt中的错误导致持续出错。
2010-08-10 15:47
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
收藏
得分:0 
大家帮忙看看啊~~
2010-08-10 17:28
曼珠沙华
Rank: 2
来 自:广东
等 级:论坛游民
帖 子:121
专家分:98
注 册:2009-5-21
收藏
得分:3 
回复 2楼 xjy080
你可以声明一个变量,用于控制判断
若是为错误,则变量的值为0,正确为1

走过了,却留下了痕迹。伤过了,却是一种无奈。人生只不过是一种擦肩而过的过程。
2010-08-10 17:50
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
收藏
得分:0 
回复 6楼 曼珠沙华
可是对于这个错误的界定,该如何进行呢?我想知道对于文件中的这样的情况,一般是怎么处理的?
2010-08-10 18:14
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
收藏
得分:0 
回复 3楼 erikyo
这样处理的话,对于输入数据大于8的话好像是没有效果~~
2010-08-10 20:56
xjy080
Rank: 2
等 级:论坛游民
帖 子:55
专家分:78
注 册:2008-9-15
收藏
得分:0 
回复 3楼 erikyo
同时输入数据小于8的话,则还要继续等待。
2010-08-10 21:00
快速回复:一个课程设计的代码,想优化下。关于文件的
数据加载中...
 
   



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

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