用C语言文件形式编写一个计算机教学系统,要求对信息进行增加,删除,修改,查询,浏览操作
1个回答
2013-08-20
展开全部
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
FILE *p;
struct student
{
int num;
float score;
char name[20];
struct student *next;
};
void main()
{struct student * FIND(struct student *head);<br/>void ADD(struct student *head);<br/>void CHANGE(struct student *head);<br/><br/>struct student *p1,*p2,*head,*p3; <br/> <br/>int h,n,j;<br/>if((p=fopen("课程设计.txt","r+"))!=NULL) //打开文件<br/>{<br/> <br/> n=0;<br/>p2=(struct student * )malloc(LEN);<br/>p1=p2;<br/>fscanf(p,"%d" "%f",&p1->num,&p1->score);<br/>fscanf(p,"%s",&p1->name);<br/>printf("%d %5.2f ",p1->num,p1->score);<br/>printf("%s",p1->name);<br/>printf("\n");<br/>head=NULL;<br/>while(p1->num!=0)<br/>{n=n+1;<br/>if(n==1)<br/>head=p1;<br/>else<br/>p2->next=p1;<br/>p2=p1;<br/>p1=(struct student * )malloc(LEN);<br/>fscanf(p,"%d %f",&p1->num,&p1->score);<br/>fscanf(p,"%s",&p1->name);<br/>printf("%d %5.2f ",p1->num,p1->score);<br/>printf("%s",p1->name);<br/>printf("\n");<br/>}
p2->next=NULL; //创建结束
printf("do you want do sth? such as find,add...\n");
printf("1-yes 2-no\n");
scanf("%d",&j);
while(j==1)
{
printf("what you want to do?\n");
printf("please chose what you want to do:\n");
printf("1-find 2-add 3-change 4-nothing\n");
printf("please chose a number\n");
scanf("%d",&h);
switch(h)
{
case 1:FIND(head);break;
case 2:ADD(head);break;
case 3:CHANGE(head);break;
case 4:break;
}
printf("do you want do anything more?\n");
printf("1-yes 2-no\n");
scanf("%d",&j);
}
}
else
printf("can't open file\n");
p3=head;
printf("现在成绩记录表是:\n");
fprintf(p,"\n");
if(head!=NULL)
do
{printf("%d %5.2f ",p3->num,p3->score);<br/>printf("%s\n",p3->name);<br/>fprintf(p,"%d %5.2f ",p3->num,p3->score);<br/>fprintf(p,"%s\n",p3->name);<br/>p3=p3->next;}
while(p3!=NULL);
fclose(p);
}
struct student * FIND(struct student *head)
{int t,num,h;<br/>h=0;<br/>char name[20];<br/>printf("which you get to find?\n");<br/>printf("1-name 2-num\n");<br/>printf("please chose\n");<br/>scanf("%d",&t);<br/>struct student *p1;<br/>p1=head;<br/>if(t==1)<br/>{<br/>printf("please input the name\n");<br/>scanf("%s",&name);<br/>while(h==0)<br/>{if(strcmp(p1->name, name)==0)<br/><br/>{printf("%d ",p1->num);<br/>printf("%s ",p1->name);<br/>printf("%5.2f \n",p1->score);<br/>h=1;}
else
p1=p1->next;
}
}
if(t==2)
{
printf("please input the num\n");
scanf("%d",&num);
while(h==0)
{if(p1->num==num)<br/>{printf("%d ",p1->num);<br/>printf("%s ",p1->name);<br/>printf("%5.2f \n",p1->score);<br/>h=1;}
else
p1=p1->next;
}
}
if(h==0)
{return(0);<br/>printf("can not find!");}
else return(p1);
}
void ADD(struct student *head)
{printf("you add student must next to the student you find!!!\n");<br/>struct student *p1,*p2; <br/>printf("before find\n");<br/>p1=FIND(head);<br/>printf("after find\n");<br/>if(p1!=0)<br/>{p2=(struct student * )malloc(LEN);<br/>printf("please input num score you want add:\n");<br/>scanf("%d %f",&p2->num,&p2->score);<br/>printf("please input name\n");<br/>scanf("%s,",&p2->name);<br/>p2->next=p1->next;<br/>p1->next=p2;<br/>}
else
printf("can not add!\n");
}
void CHANGE(struct student *head)
{struct student *p1;<br/>int *n;<br/>float *s;<br/>char (*m)[20];<br/>p1=FIND(head);<br/>if(p1!=0)<br/>{<br/><br/>int t;<br/>printf("which do you want to change?\n");<br/>t=1;<br/>while(t==1)<br/>{printf("1-name 2-num 3-score\n");<br/>scanf("%d",&t);<br/>m=&p1->name;<br/>n=&p1->num;<br/>s=&p1->score;<br/>printf("plaese input the right:\n");<br/>switch(t)<br/>{<br/>case 1:scanf("%s",m);break;<br/>case 2:scanf("%d",n);break;<br/>case 3:scanf("%f",s);break;<br/>}
printf("do you want to change the others of this student?\n");
printf("1-yes 2-no\n");
scanf("%d",&t);
}
}
else
printf("can not change\n");
}
这个是个成绩记录薄
功能跟你的差不多
支持文件操作
VC++6.0下编译成功
#include<string.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
FILE *p;
struct student
{
int num;
float score;
char name[20];
struct student *next;
};
void main()
{struct student * FIND(struct student *head);<br/>void ADD(struct student *head);<br/>void CHANGE(struct student *head);<br/><br/>struct student *p1,*p2,*head,*p3; <br/> <br/>int h,n,j;<br/>if((p=fopen("课程设计.txt","r+"))!=NULL) //打开文件<br/>{<br/> <br/> n=0;<br/>p2=(struct student * )malloc(LEN);<br/>p1=p2;<br/>fscanf(p,"%d" "%f",&p1->num,&p1->score);<br/>fscanf(p,"%s",&p1->name);<br/>printf("%d %5.2f ",p1->num,p1->score);<br/>printf("%s",p1->name);<br/>printf("\n");<br/>head=NULL;<br/>while(p1->num!=0)<br/>{n=n+1;<br/>if(n==1)<br/>head=p1;<br/>else<br/>p2->next=p1;<br/>p2=p1;<br/>p1=(struct student * )malloc(LEN);<br/>fscanf(p,"%d %f",&p1->num,&p1->score);<br/>fscanf(p,"%s",&p1->name);<br/>printf("%d %5.2f ",p1->num,p1->score);<br/>printf("%s",p1->name);<br/>printf("\n");<br/>}
p2->next=NULL; //创建结束
printf("do you want do sth? such as find,add...\n");
printf("1-yes 2-no\n");
scanf("%d",&j);
while(j==1)
{
printf("what you want to do?\n");
printf("please chose what you want to do:\n");
printf("1-find 2-add 3-change 4-nothing\n");
printf("please chose a number\n");
scanf("%d",&h);
switch(h)
{
case 1:FIND(head);break;
case 2:ADD(head);break;
case 3:CHANGE(head);break;
case 4:break;
}
printf("do you want do anything more?\n");
printf("1-yes 2-no\n");
scanf("%d",&j);
}
}
else
printf("can't open file\n");
p3=head;
printf("现在成绩记录表是:\n");
fprintf(p,"\n");
if(head!=NULL)
do
{printf("%d %5.2f ",p3->num,p3->score);<br/>printf("%s\n",p3->name);<br/>fprintf(p,"%d %5.2f ",p3->num,p3->score);<br/>fprintf(p,"%s\n",p3->name);<br/>p3=p3->next;}
while(p3!=NULL);
fclose(p);
}
struct student * FIND(struct student *head)
{int t,num,h;<br/>h=0;<br/>char name[20];<br/>printf("which you get to find?\n");<br/>printf("1-name 2-num\n");<br/>printf("please chose\n");<br/>scanf("%d",&t);<br/>struct student *p1;<br/>p1=head;<br/>if(t==1)<br/>{<br/>printf("please input the name\n");<br/>scanf("%s",&name);<br/>while(h==0)<br/>{if(strcmp(p1->name, name)==0)<br/><br/>{printf("%d ",p1->num);<br/>printf("%s ",p1->name);<br/>printf("%5.2f \n",p1->score);<br/>h=1;}
else
p1=p1->next;
}
}
if(t==2)
{
printf("please input the num\n");
scanf("%d",&num);
while(h==0)
{if(p1->num==num)<br/>{printf("%d ",p1->num);<br/>printf("%s ",p1->name);<br/>printf("%5.2f \n",p1->score);<br/>h=1;}
else
p1=p1->next;
}
}
if(h==0)
{return(0);<br/>printf("can not find!");}
else return(p1);
}
void ADD(struct student *head)
{printf("you add student must next to the student you find!!!\n");<br/>struct student *p1,*p2; <br/>printf("before find\n");<br/>p1=FIND(head);<br/>printf("after find\n");<br/>if(p1!=0)<br/>{p2=(struct student * )malloc(LEN);<br/>printf("please input num score you want add:\n");<br/>scanf("%d %f",&p2->num,&p2->score);<br/>printf("please input name\n");<br/>scanf("%s,",&p2->name);<br/>p2->next=p1->next;<br/>p1->next=p2;<br/>}
else
printf("can not add!\n");
}
void CHANGE(struct student *head)
{struct student *p1;<br/>int *n;<br/>float *s;<br/>char (*m)[20];<br/>p1=FIND(head);<br/>if(p1!=0)<br/>{<br/><br/>int t;<br/>printf("which do you want to change?\n");<br/>t=1;<br/>while(t==1)<br/>{printf("1-name 2-num 3-score\n");<br/>scanf("%d",&t);<br/>m=&p1->name;<br/>n=&p1->num;<br/>s=&p1->score;<br/>printf("plaese input the right:\n");<br/>switch(t)<br/>{<br/>case 1:scanf("%s",m);break;<br/>case 2:scanf("%d",n);break;<br/>case 3:scanf("%f",s);break;<br/>}
printf("do you want to change the others of this student?\n");
printf("1-yes 2-no\n");
scanf("%d",&t);
}
}
else
printf("can not change\n");
}
这个是个成绩记录薄
功能跟你的差不多
支持文件操作
VC++6.0下编译成功
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询