主要思想,main函数开始的时候从文件中读取数据,然后把数据做成一个链表存放到内存。然后开始通过菜单选项进行想要的操作(增加信息,修改信息,删除信息·····)。最后要推出程序的时候删除存放数据的文件,然后在创建一个文件把操作后的数据依次写入文件中。(练手程序,没什么技术含量!)
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 struct stu 5 { 6 int num; 7 char name[20]; 8 }; 9 typedef struct stul 10 { 11 struct stu stuinfo; 12 struct stul *next; 13 }stul; 14 15 int menu(); //菜单 16 stul *create(); //创建链表 17 void openfile(stul *h); //初始化文件中的学生信息 18 void append(stul *h); //添加学生信息 19 void out(stul *h); //输出学生信息 20 void savetofile(stul *h); //把链表中的信息放入文件中 21 void search(stul *h); //根据学号查询信息 22 void modify(stul *h); //修改学生信息 23 void delete(stul *h); //删除学生信息 24 25 int main(void) 26 { 27 stul *h; 28 h=create(); 29 openfile(h); 30 while(1) 31 { 32 switch(menu()) 33 { 34 case 1:append(h); break; 35 case 2:search(h); break; 36 case 3:modify(h); break; 37 case 4:delete(h); break; 38 case 5:out(h); break; 39 case 6:savetofile(h); exit(0); break; 40 } 41 } 42 } 43 44 void delete(stul *h) //删除学生信息 45 { 46 stul *p,*l; 47 int num; 48 char ch; 49 p=h; 50 l=p->next; 51 printf("请输入需要删除的学生学号:"); 52 while(scanf("%d",&num)!=1) 53 { 54 while(ch=getchar()!='\n'); 55 printf("请输入需要删除的学生学号(001 002 003):"); 56 } 57 while(l!=NULL) 58 { 59 if(l->stuinfo.num==num) 60 { 61 p->next=p->next->next; 62 free(l); 63 break; 64 } 65 l=l->next; 66 p=p->next; 67 } 68 } 69 70 void modify(stul *h) //修改学生信息 71 { 72 stul *p; 73 int num; 74 char ch; 75 p=h->next; 76 while(p!=NULL) 77 { 78 printf("请输入需要修改学生信息的学号:"); 79 while(scanf("%d",&num)!=1) 80 { 81 while(ch=getchar()!='\n'); 82 printf("请输入需要修改学生信息的学号(001 002 003):"); 83 } 84 if(p->stuinfo.num==num) 85 { 86 printf("请输入需要修改的姓名:"); 87 scanf("%s",&p->stuinfo.name); 88 break; 89 } 90 else 91 p=p->next; 92 } 93 } 94 95 96 void search(stul *h) //根据学号查询信息 97 { 98 stul *p; 99 int num; 100 char ch; 101 p=h->next; 102 printf("请输入需要查询的学号:"); 103 while(scanf("%d",&num)!=1) 104 { 105 while(ch=getchar()!='\n'); 106 fprintf(stdout,"请输入需要查询的学号(001 002 003)"); 107 } 108 while(p!=NULL) 109 { 110 if(p->stuinfo.num==num) 111 { 112 printf("学号:%d; 姓名:%s\n",p->stuinfo.num,p->stuinfo.name); 113 break; 114 } 115 else 116 p=p->next; 117 } 118 } 119 120 void savetofile(stul *h) //把链表中的信息放入文件中 121 { 122 FILE *fp; 123 stul *p; 124 struct stu stuinfo; 125 remove("stuinfo.txt"); 126 fp=fopen("stuinfo.txt","wb"); 127 p=h->next; 128 while(p!=NULL) 129 { 130 fwrite(&p->stuinfo,sizeof(stuinfo),1,fp); 131 p=p->next; 132 } 133 } 134 135 void out(stul *h) //输出学生信息 136 { 137 stul *p; 138 p=h; 139 p=p->next; 140 while(p!=NULL) 141 { 142 printf("学号:%d;姓名:%s;\n",p->stuinfo.num,p->stuinfo.name); 143 p=p->next; 144 } 145 } 146 147 stul *create() //创建链表 148 { 149 stul *p; 150 p=(stul *)malloc(sizeof(stul)); 151 p->next=NULL; 152 return p; 153 } 154 155 void openfile(stul *h) //把文件中的内容做成一个链表 156 { 157 FILE *fp; 158 struct stu stuinfo; 159 stul *p,*l; 160 p=h; 161 if((fp=fopen("stuinfo.txt","rb"))==NULL) 162 fprintf(stdout,"sorry cound not open stuinfo.txt file"); 163 else 164 { 165 while(fread(&stuinfo,sizeof(stuinfo),1,fp)==1) 166 { 167 l=create(); 168 l->stuinfo=stuinfo; 169 p->next=l; 170 p=l; 171 } 172 } 173 } 174 175 void append(stul *h) //添加学生信息 176 { 177 stul *p,*l; 178 struct stu stuinfo; 179 int num; 180 char name[20]; 181 char ch,y; 182 p=h; 183 while(p->next!=NULL) 184 p=p->next; 185 do 186 { 187 puts("请输入学号(001 002 003):"); 188 while(scanf("%d",&num)!=1) 189 { 190 while(ch=getchar()!='\n'); 191 fprintf(stdout,"请输入学号(001 002 003):"); 192 } 193 printf("\n请输入姓名:"); 194 scanf("%s",stuinfo.name); 195 l=create(); 196 stuinfo.num=num; 197 //stuinfo.name=name; 198 l->stuinfo=stuinfo; 199 p->next=l; 200 p=l; 201 getchar(); 202 fprintf(stdout,"是否继续添加?(y/n)"); 203 y=getchar(); 204 } 205 while(y=='y'||y=='Y'); 206 } 207 208 int menu() //菜单 209 { 210 int num; 211 char ch; 212 puts("please choose the following chooses!"); 213 puts("\t1.append student`s info."); 214 puts("\t2.search student's info from name."); 215 puts("\t3.modify student's info."); 216 puts("\t4.delete student's info."); 217 puts("\t5.out student's info."); 218 puts("\t6.exit."); 219 while(scanf("%d",&num) != 1 || num>6 || num<1) //获取整数 220 { 221 while(ch=getchar()!='\n'); 222 printf("please input your choose as 1 2 ...5 6\n"); 223 224 } 225 return num; 226 }
比较有用的检测输入算法:
1 while(scanf("%d",&num)!=1) 2 { 3 while(ch=getchar()!='\n'); 4 fprintf(stdout,"请输入需要查询的学号(001 002 003)"); 5 }
有两个循环来接收输入的一个int型的变量。第一个循环是判断接收的数据是否是一个整型,如果不是继续接收;第二个循环是接收错误的数据,知道下一个换行符是停止接收!