#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct subjects{?int num;?char name[20];?char kind[10];?int stime;?int ttime;?int etime;?int score;?int term;?struct subjects *next;}SUB;?SUB *create_form(){?SUB *head,*tail,*p;?int num,stime,ttime;?int etime,score,term;?char name[20],kind[10];?int size=sizeof(SUB);?head=tail=NULL;?printf("輸入選修課程信息:\n");?scanf("%d %s %s %d %d %d %d %d",&num,&name,&kind,&stime,&ttime,&etime,&score,&term);?while(num!=0){? p=(SUB *)malloc(size);? p->num=num;? strcpy(p->name,name);? strcpy(p->kind,kind);? p->stime=stime;? p->ttime=ttime;? p->etime=etime;? p->score=score;? p->term=term;?if(head==NULL)? head=p;?else? tail->next=p;?tail=p;?scanf("%d %s %s %d %d %d %d %d",&num,&name,&kind,&stime,&ttime,&etime,&score,&term);?}?tail->next=NULL;?return head;}?void savefile(SUB *head){?SUB *p;?FILE *fp;?fp=fopen("subjects.txt","w");?fprintf(fp,"課程編號(hào) ?課程名稱(chēng) ?課程性質(zhì) ?總學(xué)時(shí) ?授課學(xué)時(shí) ?實(shí)驗(yàn)或上機(jī)學(xué)時(shí) ?學(xué)分 ?開(kāi)課學(xué)期\n");?for(p=head;p;p=p->next)? fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);?fclose(fp);}?void savefileadd(SUB *head){?SUB *p;?FILE *fp;?fp=fopen("subjectsadd.txt","w");?fprintf(fp,"課程編號(hào) ?課程名稱(chēng) ?課程性質(zhì) ?總學(xué)時(shí) ?授課學(xué)時(shí) ?實(shí)驗(yàn)或上機(jī)學(xué)時(shí) ?學(xué)分 ?開(kāi)課學(xué)期\n");?for(p=head;p;p=p->next)? fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);?fclose(fp);}?void savefiledel(SUB *head){?SUB *p;?FILE *fp;?fp=fopen("subjectsdel.txt","w");?fprintf(fp,"課程編號(hào) ?課程名稱(chēng) ?課程性質(zhì) ?總學(xué)時(shí) ?授課學(xué)時(shí) ?實(shí)驗(yàn)或上機(jī)學(xué)時(shí) ?學(xué)分 ?開(kāi)課學(xué)期\n");?for(p=head;p;p=p->next)? fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);?fclose(fp);}?void prin(SUB *head){?SUB *ptr;?if(head==NULL){? printf("沒(méi)有此門(mén)課程記錄!\n");? return;?}?printf("課程編號(hào) ?課程名稱(chēng) ?課程性質(zhì) ?總學(xué)時(shí) ?授課學(xué)時(shí) ?實(shí)踐或上機(jī)學(xué)時(shí) ?學(xué)分 ?開(kāi)課學(xué)期\n");?for(ptr=head;ptr;ptr=ptr->next)? printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);}void search(SUB *head){?int a,num;?int t=1;?char type[10];?char ch='a',ch1;?SUB *ptr;??while(ch!=' '){? printf("若要按課程性質(zhì)查找請(qǐng)輸入1,若要按學(xué)分查找請(qǐng)輸入2:\n");? scanf("%d",&a);? switch(a){? case 1:printf("請(qǐng)輸入要查找的課程的性質(zhì):\n");? ?scanf("%s",type);? ?printf("課程編號(hào) ?課程名稱(chēng) ?課程性質(zhì) ?總學(xué)時(shí) ?授課學(xué)時(shí) ?實(shí)踐或上機(jī)學(xué)時(shí) ?學(xué)分 ?開(kāi)課學(xué)期\n");? ?for(ptr=head;ptr;ptr=ptr->next)? ? if(strcmp(type,ptr->kind)==0){? ? ?printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);? ? ? ? ? ?t=0;? ? }? ? if(t) printf("未找到!\n");? ? t=1;? ?break;? case 2:printf("輸入要查找的課程的學(xué)分\n");? ?scanf("%d",&num);? ?printf("課程編號(hào) ?課程名稱(chēng) ?課程性質(zhì) ?總學(xué)時(shí) ?授課學(xué)時(shí) ?實(shí)踐或上機(jī)學(xué)時(shí) ?學(xué)分 ?開(kāi)課學(xué)期\n");? ?for(ptr=head;ptr;ptr=ptr->next)? ? if(ptr->score==num){? ? ?printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);? ? ? ? ? ? ? ? ? ? t=0;? ? }? ? if(t) printf("未找到!\n");? ? t=1;???}?printf("繼續(xù)查找請(qǐng)按回車(chē)鍵,結(jié)束請(qǐng)按空格鍵:\n");?ch1=getchar(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//將回車(chē)鍵賦給CH1,否則CASE里面最后輸入的回車(chē)鍵會(huì)賦給CH,因此用CH1填補(bǔ)。?ch=getchar();?}}SUB *insert(SUB *head){?SUB *ptr,*ptr2,*subj;?int size=sizeof(SUB);?char ch='a',ch1;?while(ch!=' '){? ? ? subj=(SUB *)malloc(size);? ? ? ptr=subj;? ? ? printf("輸入要插入的課程信息:\n");? ? ? scanf("%d%s%s%d%d%d%d%d",&subj->num,subj->name,subj->kind,&subj->stime,&subj->ttime,&subj->etime,&subj->score,&subj->term);? ? ? ?if(head==NULL){? ? ? ? ?head=ptr;? ? ? ? ?head->next=NULL;? ? }? ? ? ?else{? ? ? ? ? for(ptr2=head;ptr2;ptr2=ptr2->next)? ? ? ? ? ? ?if(ptr2->next==NULL){? ? ? ? ? ? ?ptr2->next=subj;? ? ? ? ? ? ?subj->next=NULL;? ? ? ? ? ? ?break;? ? ? ?}? ? }? ? printf("繼續(xù)插入請(qǐng)按回車(chē),結(jié)束請(qǐng)按空格:\n");? ? ch1=getchar(); ? ? ? ? ? ? ? ? ? ? ?//將回車(chē)鍵賦給CH1,否則subj->term輸完后輸入的回車(chē)鍵會(huì)賦給CH,因此用CH1填補(bǔ)。? ? ch=getchar();? ? }?return head;}SUB *del(SUB *head){?SUB *p1,*p2;?char ch='a',ch1;?int num;?while(ch!=' '){? printf("輸入想要?jiǎng)h除的課程編號(hào):\n");? ? ?scanf("%d",&num);? ? ?if(head->num==num){? ? ? p2=head;? ? ? head=head->next;? ? ? free(p2);? }? ? ?if(head==NULL)? ? ? return NULL;? ? ?p1=head;? ? ?p2=head->next;? ? ?while(p2){? ? if(p2->num==num){? ? ? ?p1->next=p2->next;? ? ? ?free(p2);? ? }? ? ?else p1=p2;? ? ?p2=p1->next;? }? printf("繼續(xù)刪除請(qǐng)按回車(chē),結(jié)束請(qǐng)按空格:\n");? ch1=getchar(); ? ? ? ? ? ? ? ? ? ? ? //將回車(chē)鍵賦給CH1,否則num輸完后再輸入的回車(chē)鍵會(huì)賦給CH,因此用CH1填補(bǔ)。? ch=getchar();?}?return head;}?void choose(SUB *head){?SUB *p,*q;?int a[5];?int num,total=0,i=0,j;?printf("輸入要選修的課程的編號(hào),編號(hào)之間以空格分開(kāi),輸完后以0結(jié)束\n");?scanf("%d",&num);?while(num!=0){? for(p=head;p;p=p->next)? ?if(p->num==num){? ? total=total+p->score;? ? a[i]=num;? ? i++;? ?}? ?scanf("%d",&num);?}?if(total<60) printf("選修總學(xué)分未達(dá)到60,選修失敗!\n");?else {? printf("選修成功!\n");? printf("您選修的課程為:\n");?for(j=0;j<i;j++) ?? ? for(q=head;q;q=q->next)? ? if(q->num==a[j])? ? ?printf("%s ?",q->name);?printf("\n");printf("***********************************\n");printf(" ? ? ?作品 ? ? ? ? ?\n");printf(" ? 作品 ? \n");printf("***********************************\n");? ?? ? }}void main(){?SUB *head=create_form();?savefile(head);?prin(head);?search(head);?head=insert(head); ? ? ? ? ? ? ? ? ? ? ? ?//注意此處的必要性,函數(shù)insert的的返回值重新賦給head!?savefileadd(head);?printf("修改后的信息為:\n");?prin(head);?head=del(head); ? ? ? ? ? ? ? ? ? ? ? ? ? //注意此處的必要性,函數(shù)del的的返回值重新賦給head!?savefiledel(head);?printf("修改后的課程信息為:\n");?prin(head);?choose(head);}===================================分割線(xiàn)===============================================請(qǐng)問(wèn)是哪里出錯(cuò)了,為什么運(yùn)行之后只有一步??????????求大觸來(lái)幫幫忙解答一下?。。。。。。。。。。。。。。。?
- 1 回答
- 0 關(guān)注
- 1128 瀏覽
添加回答
舉報(bào)
0/150
提交
取消