主要思想,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型的變量。第一個循環是判斷接收的數據是否是一個整型,如果不是繼續接收;第二個循環是接收錯誤的數據,知道下一個換行符是停止接收!