學生檔案

段某不吃飯發表於2020-10-11

檔案讀到連結串列中,對連結串列做相關操作,最後重寫進檔案中

typedef struct student{
 int num;
 char name[20];
 int score;
 struct student *next;
}stu;
stu *cre()
{
 stu*head = (stu*)malloc(sizeof(stu));
 head->next = NULL;
 return head;
}
stu* m = cre();//全域性連結串列
stu *sort(stu*head){       //氣泡排序

  stu*p = head;
  stu*q = head->next;
  stu*last = NULL;
  int temp;
  while (p != last)
  {
   while (q != last)
   {
    	if (p->num > q->num)
    	{
     		temp = p->num;
     		p->num = q->num;
     		q->num = temp;
    	}
   	 p = p->next;
   	 q = q->next;
   }
   last = p;
   p = head;  //重置p,q
   q = head->next;
  }
  return head;
}
void headinsert(stu *head)  //頭插法
{
 stu *p = cre();
 printf("輸入學號:\n");
 scanf("%d", &p->num);
 printf("輸入姓名:\n");
 scanf("%s", p->name);
 printf("輸入分數:\n");
 scanf("%d", &p->score);
 p->next = head->next;
 head->next = p;
}
stu *del(stu*head){        //根據學號刪除
 stu  *p, *q;// 帶頭節點的
 int n;
 printf("請輸入要刪除學生的學號:\n");
 scanf("%d", &n);
 q = head;
 p = q->next;
 while (p != NULL&&p->num != n){
  	q = p;
  	p = p->next;
 }
 if (p != NULL){
  	q->next = p->next;
  	free(p);
        printf("刪除成功!\n");
 }
 else{
  printf("沒有此人!\n");
 }
return head;
}
stu *revise(stu*head){     //根據學號修改
 stu *p, *q;
 int n;
 q = head;
 p = q->next;
 printf("請輸入要修改的學生的學號:\n");
 scanf("%d", &n);
 while (p != NULL&&p->num != n){
  	q = p;
 	p = p->next;
 }
 if (p != NULL){
  p->num = n;
  printf("輸入姓名:\n");
  scanf("%s", p->name);
  printf("輸入分數:\n");
  scanf("%d", &p->score);
  printf("修改成功!\n");
 }
 else
 {
  	printf("沒有此人!\n");
 }
 return head;
}
stu *find(stu*head){  //yes
 stu*p;
 int n;
 p = head;
 p = p->next;
 printf("請輸入要查詢的學生學號:\n");
 scanf("%d", &n);
 while (p&&p->num!=n){
  	p = p->next;
 }
 if (p)
 {
     printf("學號:%d\n", p->num);
     printf("姓名:%s\n", p->name);
     printf("分數:%d\n", p->score);
 }
 else 
     printf("沒有此人!\n");
  return head;
}
void shuchu(stu*haed){  //傳指標 輸出連結串列
 printf("全部資料:\n");
 stu *p;
 p = haed->next;
 printf("學號\t姓名\t分數\n");
 while (p != NULL)
 {
  printf("%d\t%s\t%d\n", p->num,p->name,p->score);
  p = p->next;
 }
}
void sa(stu*head){     //檔案
 FILE *fp;
 if((fp=fopen("abc.txt","w"))==NULL)  //只寫方式
  {   
        printf("cuowu打不開檔案");
    	system("pause"); 
  } 
 stu *p;
 p = head->next;
 while (p != NULL)
 {
  	fprintf(fp,"%d %s %d\n",p->num,p->name,p->score);
  	p = p->next;
 }
 fclose(fp);
 }
void readfile(stu*in)//將檔案的內容讀出並且儲存到連結串列中去
{
 FILE*fp;
 stu* infor;
 infor = (struct student*)malloc(sizeof(student)); 
 infor->name[20] = {0};
 infor->num = 0;
 infor->score = 0;
 infor->next = NULL;

if ((fp = fopen("abc.txt", "r")) == NULL)
 {
  	printf("cuowu打不開檔案");
  	system("pause");
 }
 while (fscanf(fp,"%d %s %d\n",&infor->num,infor->name,&infor->score)!=EOF)
 {  
  	insert(in,infor);
 }
 fclose(fp);
 printf("檔案讀取成功!(連結串列)\n");
}
int main(int argc, char** argv) {
int a = 0;
 readfile(m);
 while (1)
 {
  meau();
  printf("請輸入命令:\n");
  scanf("%d",&a);
  switch (a)
  {
  case 1:sort(m);//排序
   	break;
  case 2:headinsert(m); //頭插法
   	break;
  case 3:del(m); //刪除
   	break;
  case 4:revise(m); //修改
  	break;
  case 5:find(m);  //查詢
   	break;
  case 6:shuchu(m); //輸出
   	break;
  case 7:sa(m);    //儲存到檔案,w形式開啟
   	exit(0);
   	break;
  }
  	system("pause");
  	system("cls");
 }
 return 0;
 }
void meau()
{
 printf("1排序\n");
 printf("2新增\n");
 printf("3刪除\n");
 printf("4修改\n");
 printf("5查詢\n");
 printf("6輸出\n");
 printf("7儲存退出\n");//只寫方式,檔案存在會重寫
}

相關文章