}
printf(\个数排序后是:\ for(i=0;i<10;i++) {
printf(\ }
printf(\
for(i=0;i<9;i++) //删除重复数字 {
if(a[i]==a[i+1]) //如果后一个数等于前一个数,就把后一个数删掉(ps:这里已经排过序,所以可以这样做来删除重复数字) {
for(j=i+1;j<9;j++) //这里的删除用的直接覆盖 a[j]=a[j+1]; } }
printf(\删除重复数字后:\ for(i=0;i<10;i++) {
if(i>0&&a[i-1]==a[i]) //只输出前面不重复的数 break;
printf(\ }
printf(\}
三、通过课程选修问题
学生需要修一定数量的课程才能毕业,而这些课程会有一些必须遵循的选修顺序。假设每个学期都开所有课程,并且学生所修课程数量不限制。给出课程和先修课程的列表,求出一个满足最小学期数要求的时间表。以你的专业情况为背景设计。
3.1数据结构设计
typedef struct subjects {
int num; //课程编号 char name[20]; //课程名称 char kind[10]; //课程性质 int stime; //总学时 int ttime; //授课学时
11
int etime; //实验或上机学时 int score; //学分 int term; //开课学期 struct subjects *next; }SUB;
3.2算法设计
1.SUB *create_form() //创建链表 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; if(fp==NULL)exit(0); tail=p;
void savefile() //保存文件
for(p=head;p;p=p->next)
fprintf(fp,\
p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term); fclose(fp);
printf(\创建后的信息已放入'3.txt'文件中\\n\ system(\
void readfile() //阅读文件 while(!feof(fp)) {
newSub=(SUB*)malloc(sizeof(SUB));
fscanf(fp,\nd,
&newSub->stime,&newSub->ttime,&newSub->etime,&newSub->score,&newSub->term);
myInsert(newSub); }
fclose(fp)
12
void *insert()//插入课程信息 {
while(ch!='0') {
subj=(SUB *)malloc(size); ptr=subj;
void search() //课程信息查询for(ptr=head;ptr;ptr=ptr->next) if(ptr->score==num) {
printf(\
ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term); t=0; }
if(t)
printf(\未找到!\\n\ t=1;
system(\ goto L1; case 3:break; }
2主函数main()函数设计地比较简洁,只是提供一个主菜单,switch语句将整个程序分为两个大的功能模块:
进入下一层 选择序号进行选课 进入选课系统
图3.1删除流程图
13
3.3调试分析
1 开始运行
图3.2开始运行界面
2 管理员菜单
图3.3菜单
按1进人录入课程模块
3.4测试结果
图3.4测试结果
14
图3.5运行结果
3.5源程序(带注释)
#include \#include \#include \#include \void prin1(); void choose();
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 *head=NULL;
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(\
15