petdog 发表于 2008-9-8 00:39

救命啊

假定有n门课程,每门课程有课程编号,课程名称,课程性质,总学时,授课学时,实验或上机学时,学分,开课学期等信息,学生可按要求(如总学分不得少于60)自由选课。试设计一选修课程系统,使之能提供以下功能:

系统以菜单方式工作

课程信息录入功能(课程信息用文件保存)--输入

课程信息浏览功能--输出

查询功能:(至少一种查询方式)--算法

按学分查询

按课程性质查询

学生选修课程(可选项)
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "process.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;
        if((fp=fopen("d:\\subjects.txt","w"))==NULL){
                printf("open fail!\n");
                exit(0);
        }
        fprintf(fp,"课程编号  课程名称  课程性质  总学时  授课学时  实验或上机学时  学分  开课学期\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);
        if(fclose(fp)){
                printf("close fail!\n");
                        exit(0);
        }
        printf("创建后的信息已放入D盘根目录的“subjects.txt”文件中\n");
}



void savefileadd(SUB *head)             //将修改后的信息存入新文件
{
        SUB *p;
        FILE *fp;
        if((fp=fopen("d:\\subjectsadd.txt","w"))==NULL){
                printf("open fail!\n");
                exit(0);
        }
        fprintf(fp,"课程编号  课程名称  课程性质  总学时  授课学时  实验或上机学时  学分  开课学期\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);
        if(fclose(fp)){
                printf("close fail!\n");
                exit(0);
        }
        printf("修改后的信息已放入D盘根目录的“subjectsadd.txt”文件中\n");
}



void savefiledel(SUB *head)                // 将再次修改的信息存入新文件
{
        SUB *p;
        FILE *fp;
        if((fp=fopen("d:\\subjectsdel.txt","w"))==NULL){
                printf("open fail\n");
                exit(0);
        }
        fprintf(fp,"课程编号  课程名称  课程性质  总学时  授课学时  实验或上机学时  学分  开课学期\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);
        if(fclose(fp)){
                printf("close fail!\n");
                exit(0);
        }
        printf("修改后的信息已放入D盘根目录的“subjectsdel.txt”文件中\n");
}



void prin(SUB *head)                            //输出信息
{
        SUB *ptr;
        if(head==NULL){
                printf("NO RECORDS!\n");
                return;
        }
        printf("课程编号  课程名称  课程性质  总学时  授课学时  实践或上机学时  学分  开课学期\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("若要按课程性质查找请输入1,若要按学分查找请输入2:\n");
                scanf("%d",&a);
                switch(a){
                case 1:printf("请输入要查找的课程的性质:\n");
                        scanf("%s",type);
                        printf("课程编号  课程名称  课程性质  总学时  授课学时  实践或上机学时  学分  开课学期\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("输入要查找的课程的学分\n");
                        scanf("%d",&num);
                        printf("课程编号  课程名称  课程性质  总学时  授课学时  实践或上机学时  学分  开课学期\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("继续查找请按回车键,结束请按空格键:\n");
        ch1=getchar();                              //将回车键赋给CH1,否则CASE里面最后输入的回车键会赋给CH,因此用CH1填补。
        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("继续插入请按回车,结束请按空格:\n");
                  ch1=getchar();                      //将回车键赋给CH1,否则subj->term输完后输入的回车键会赋给CH,因此用CH1填补。
                  ch=getchar();
                  }
        return head;
}

               








SUB *del(SUB *head)                       //删除信息
{
        SUB *p1,*p2;
        char ch='a',ch1;
        int num;
        while(ch!=' '){
                printf("输入想要删除的课程编号:\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("继续删除请按回车,结束请按空格:\n");
                ch1=getchar();                       //将回车键赋给CH1,否则num输完后再输入的回车键会赋给CH,因此用CH1填补。
                ch=getchar();
        }

        return head;
}



void choose(SUB *head)                      //学生选课
{
        SUB *p,*q;
        int a[5];
        int num,total=0,i=0,j;
        printf("输入要选修的课程的编号,编号之间以空格分开,输完后以0结束\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("选修总学分未达到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");
                                }
}



void readfile()                    //将第一次建立的文件重新读出
{
    int num,stime,ttime,etime;
    int score,term;
    char c,name[20],kind[10];
    FILE *fp=fopen("d:\\subjects.txt","r");
    while(!feof(fp))
    {
        if((c=fgetc(fp))=='\n')
        break;
    }
    printf("课程编号  课程名称  课程性质  总学时  授课学时  实验或上机学时  学分  开课学期\n");
    while(!feof(fp))
    {
        fscanf(fp,"%d%s%s%d%d%d%d%d\n",&num,name,kind,&stime,&ttime,&etime,&score,&term);
        printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",num,name,kind,stime,ttime,etime,score,term);
    }
    fclose(fp);
}

void main()
{
        SUB *head;
        int i;
        char c;
       
        printf("\n\n");
        for(i=0;i<34;i++)
                printf("*");
        printf("课程信息录入");
        for(i=0;i<34;i++)
                printf("*");
        printf("\n\n");
       
        head=create_form();
        savefile(head);
       
        printf("\n\n");
    for(i=0;i<34;i++)
                printf("*");
        printf("课程信息输出");
        for(i=0;i<34;i++)
                printf("*");
        printf("\n\n");
       
        prin(head);
       
        printf("\n\n");
        for(i=0;i<34;i++)
                printf("*");
        printf("课程信息查找");
        for(i=0;i<34;i++)
                printf("*");
        printf("\n\n");
       
        search(head);
       
        printf("\n\n");
        for(i=0;i<34;i++)
                printf("*");
        printf("课程信息插入");
        for(i=0;i<34;i++)
                printf("*");
        printf("\n\n");
       
        head=insert(head);                        //注意此处的必要性,函数insert的的返回值重新赋给head!
        savefileadd(head);
        printf("修改后的信息为:\n");
        prin(head);
       
        printf("\n\n");
        for(i=0;i<34;i++)
                printf("*");
        printf("课程信息删除");
        for(i=0;i<34;i++)
                printf("*");
        printf("\n\n");
       
        head=del(head);                           //注意此处的必要性,函数del的的返回值重新赋给head!
        savefiledel(head);
        printf("修改后的课程信息为:\n");
        prin(head);
       
        printf("\n\n");
        for(i=0;i<34;i++)
                printf("*");
        printf("课程选修");
        for(i=0;i<34;i++)
                printf("*");
        printf("\n\n");
       
        choose(head);
        printf("是否再次显示文件“subjects.txt”的内容(Y/N)?\n");
        getchar();                      //接收choose函数中最后输入的回车字符
        c=getchar();
        if(c=='y'||c=='Y'){
                printf("\n");
                printf("“subjects.txt”文件的信息为:\n");
                readfile();
        }
        else printf("程序结束!\n");
}
如何才能让他以菜单方式运作啊,就是1。输入课程信息2。修改课程信息。。。。。请选择(1——8)这样的操作界面,好痛苦,请高手帮忙修改一下


页: [1]

编程论坛