#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ERROR -1
#define OK 1
typedef struct STUDENT
{
char name[30];
int id;
int other_data;
struct STUDENT *next;
}student;
FILE *f;
char filename[30];
student *head;
int count=0;
void init()
{
printf("----Enter filename:\n");
scanf("%s",filename);
head=(student *)malloc(sizeof(student));
head->next=NULL;
}
void write()
{
student *p;p=head;
if( (f=fopen(filename,"w+"))==NULL)
{
exit(ERROR);
}
fprintf(f," 姓名 学号 其他数据\n");
for(int i=0;i<count;i++)
{
p=p->next;
fprintf(f,"%16s %16d %16d\n",p->name,p->id,p->other_data);
}
fprintf(f,"共计%d个学生\n",count);
fclose(f);
}
void get()
{
char way[30];
void getfromfile();void getfromkeyboard();
printf("----Get information from file or keyboard?\n");
scanf("%s",way);
if(strcmp(way,"file")==0)
{
getfromfile();
}
else
{
if(strcmp(way,"keyboard")==0)
{
getfromkeyboard();
}
else
{
printf("----Wrong way!\n");
exit(ERROR);
}
}
printf("----Any other operation?\n");
}
void getfromfile()
{
FILE *origin;
student *p,*q;q=head;
char originfile[30];
printf("----Enter name of the origin file:\n");
scanf("%s",originfile);
if( (origin=fopen(originfile,"r"))!=NULL)
{
while( !feof(origin) )
{
p=(student*)malloc(sizeof(student));
while(q->next!=NULL)
{
q=q->next;
}
fscanf(origin,"%s %d %d",&p->name,&p->id,&p->other_data) ;
p->next=q->next;
q->next=p;
}
}
else
{
printf("----Can not find the file!\n");
exit(ERROR);
}
}
void getfromkeyboard()
{
student *p,*q;
int i,n;
printf("----Enter number of student:\n");
scanf("%d",&n);
printf("----Enter data:\n");
for(i=0;i<n;i++)
{
p=(student*)malloc(sizeof(student));
q=head;
while(q->next!=NULL)q=q->next;
p->next=q->next;
q->next=p;
count++;
scanf("%s %d %d",&p->name,&p->id,&p->other_data);
}
}
void seek()
{
student *p;
p=head->next;
char a[30];
int b=0;
printf("----Enter related information:\n");
scanf("%s",a);
if('0'<=a[0]&&a[0]<='9')
{
b=atoi(a);
}
if(b==0)
{
while(strcmp(p->name,a)!=0)
{
if(p->next==NULL)
{
printf("----Do not exist!\n");
exit(ERROR);
}
p=p->next;
}
}
else
{
while(p->id!=b)
{
if(p->next==NULL)
{
printf("----Do not exist!\n");
exit(ERROR);
}
p=p->next;
}
}
printf("----Target data:\n %s %d %d\n",p->name,p->id,p->other_data);
printf("----Any other operation?\n");
}
void alter()
{
int i,n;
char item[30];
student *p;p=head;
printf("----Which one to alter?\n");
scanf("%d",&n);
if(n>count)exit(ERROR);
for(i=n;i>0;i--)
{
p=p->next;
}
printf("----Which item to alter?\n");
scanf("%s",item);
printf("----Enter correct data:\n");
if(strcmp(item,"name")==0)scanf("%s",&p->name);
if(strcmp(item,"id")==0)scanf("%d",&p->id);
if(strcmp(item,"other_data")==0)scanf("%d",&p->other_data);
printf("----Any other operation?\n");
}
void add()
{
int i,n;
student *p,*q;q=head;
p=(student*)malloc(sizeof(student));
printf("----Where to add?\n");
scanf("%d",&n);
if(n>count)exit(ERROR);
for(i=0;i<n-1;i++)
{
q=q->next;
}
printf("----Enter data:\n");
scanf("%s %d %d",&p->name,&p->id,&p->other_data);
p->next=q->next;
q->next=p;
count++;
printf("----Any other operation?\n");
}
void deletee()
{
int i,n;
student *p,*q;q=head;
printf("----Which one to delete?\n");
scanf("%d",&n);
if(n>count)exit(ERROR);
for(i=0;i<n-1;i++)
{
q=q->next;
}
p=q->next;
q->next=q->next->next;
free(p);
count--;
printf("----Any other operation?\n");
}
char transform(char *operation)
{
k:
if(strcmp(operation,"get")==0)return 'A';
if(strcmp(operation,"seek")==0)return 'B';
if(strcmp(operation,"alter")==0)return 'C';
if(strcmp(operation,"add")==0)return 'D';
if(strcmp(operation,"delete")==0)return 'E';
if(strcmp(operation,"end")==0)return 'Z';
printf("----Wrong operation!Please enter operation again!\n");
scanf("%s",operation);
goto k;
}
int main()
{
init();
char operation[30],flag;
printf("----Enter operation:\n");
do
{
scanf("%s",operation);
flag=transform(operation);
if(flag=='Z')break;
switch(flag)
{
case 'A':get(); write();break;
case 'B':seek(); break;
case 'C':alter(); write();break;
case 'D':add(); write();break;
case 'E':deletee(); write();break;
}
}while(1);
return 0;
}
3PFT}G7$U{HAAQP.png")
在这个测试中,程序好像找到了文件(因为没有显示Can not find the file!),但又没有成功从getfromfile函数跳回get函数,这是为什么?