#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define size 11
//----------------------课题节点定义----------------------------
typedef struct node
{
int topicId//课题号
char topic[20]//课题名称
char tutor[20]//指导老师
char office[20]//指导老师办公室
char major[size][20]//可选此课题专业
bool choosed//是否已被选
char stuName[20]//选此课题学生姓名
int stuId//选此课题学生学号
node *next//下一个课题
}node,list//node用于定义节点,list用于定义链表
//此链表含有一个空头结点,便于 *** 作。
//----------------------函数声明------------------------------
void input(node *&p)/森锋郑/专门处理数据输入
void insert(list *h)//添加课题
void create(list *&h)//建立链表,采用头插法
node* find(list *h,int Id)//查找课题号为Id的课题
void del(list *h,int Id)//删除Id号课题
void edit(list *h,int Id)//修改Id号课题
void printNodeP(node *p)//根据节点指基尘针输出节点信息,只供其他函数调用
void printNode(list *h,int Id)//输出Id号课题信息
void printList(list *h)//输出所有课题信息
bool emptyPrint(list *h)//判断是否存在课题并输出必要的提示信息
void printChoosed(list *h)//输出已选课题信息
void printUnchoosed(list *h)//输出未选课题信息
void destroy(list *h)//销毁链表
int topicSum(list *h)//课题总数
int topicChoosedSum(list *h)//已选课题总数
int topicUnchoosedSum(list *h)//未选课题总数
//-------------------------具体函数实现----------------------------------------
void input(node *&p)
{//格式化输入
int j,major_number
printf("请依次输入课题号,课题名字,指导老师,办公室编号\n")
scanf("%d%s%s%s",&p->topicId,p->topic,p->tutor,p->office)
printf("请输入可选此课题的专业个数,不超过%d个: "此颂,size - 1)
scanf("%d",&major_number)
printf("请依次输入这%d个专业名称:",major_number)
for(j = 0j <major_numberj++)
scanf("%10s",p->major[j])
strcpy(p->major[major_number],"null")//将null作为major数组的哨兵,便于后续处理
printf("此课题是否已被选[y/n ?] ")
getchar()
char ch = getchar()
if(ch == 'y')
{
p->choosed = true
printf("请输入选此课题学生的名字和学号:")
scanf("%s %d",p->stuName,&p->stuId)
}
else
{
p->choosed = false
p->stuId = -1//-1表示课题未选时学生学号
}
}
void printNode(list *h,int Id)
{
node *p = find(h,Id)
if(p->next != NULL)
printNodeP(p->next)//直接调用printNodeP函数
else
printf("不存在此课题,查询失败!\n")
}
void printNodeP(node *p)
{//格式化输出
printf("课题号:%-10d课题名字:%-10s\n",p->topicId,p->topic)
printf("指导老师:%-10s办公室编号:%-10s\n",p->tutor,p->office)
printf("可选此课题的专业:\n")
int i = 0
while(strcmp(p->major[i],"null") != 0)
printf("%-10s",p->major[i++])
printf("\n")
if(p->choosed)
printf("%-5s已选此课题,其学号为:%-10d\n\n",p->stuName,p->stuId)
else
printf("此课题还未被选!\n\n")
}
void printList(list *h)
{
if(!emptyPrint(h))
{//链表非空
node *p = h->next
while(p != NULL)
{
printNodeP(p)
p = p->next
}
}
}
void insert(list *h)
{
node *p = (node*)malloc(sizeof(node))
input(p)
p->next = h->next
h->next = p
}
void create(list *&h)
{
int N
h = (node*)malloc(sizeof(node))
h->next = NULL
printf("建立课题数目:")
scanf("%d",&N)
int i
for(i = 0i <Ni++)
insert(h)//不断调用insert函数插入节点
}
node* find(list *h,int Id)
{
while(h->next != NULL &&h->next->topicId != Id)
h = h->next
return h//注意,此处返回的是被查找节点上一个节点的指针
}
void del(list *h,int Id)
{
node *p = find(h,Id)
if(p->next == NULL)
printf("不存在此课题,删除失败!\n")
else
{
node *q = p->next
p->next = q->next
free(q)
printf("删除成功!\n")
}
}
void edit(list *h,int Id)
{
node *p = find(h,Id)
if(p->next != NULL)
input(p->next)//调用input函数重新输入该课程信息
else
printf("不存在此课题,修改失败!\n")
return
}
bool emptyPrint(list *h)
{
if(h->next == NULL)
{
printf("没有课题!\n")
return true
}
return false
}
void printChoosed(list *h)
{
if(!emptyPrint(h))
{
h = h->next
do
{
if(h->choosed) printNodeP(h)
h = h->next
}while(h != NULL)
}
printf("\n")
}
void printUnchoosed(list *h)
{
if(!emptyPrint(h))
{
h = h->next
do
{
if(!h->choosed) printNodeP(h)
h = h->next
}while(h != NULL)
}
printf("\n")
}
void destroy(list *h)
{
while(h->next != NULL)
{
node *p = h
h = h->next
free(p)
}
free(h)
}
int topicSum(list *h)
{
int count = 0
if(!emptyPrint(h))
while(h->next != NULL)
{
++count
h = h->next
}
return count
}
int topicChoosedSum(list *h)
{
int count = 0
if(!emptyPrint(h))
{
h = h->next
do
{
if(h->choosed) ++count
h = h->next
}while(h != NULL)
}
return count
}
int topicUnchoosedSum(list *h)
{
return topicSum(h) - topicChoosedSum(h)
}
int main()
{
list *head = NULL
int choice,ID
printf("----------------主菜单----------------\n")
printf("---------------1、建立课题组----------\n")
printf("---------------2、添加课题------------\n")
printf("---------------3、删除课题------------\n")
printf("---------------4、修改课题------------\n")
printf("---------------5、查询课题------------\n")
printf("---------------6、输出所有课题--------\n")
printf("---------------7、输出已选课题--------\n")
printf("---------------8、输出未选课题--------\n")
printf("---------------9、总课题数------------\n")
printf("---------------10、已选课题数---------\n")
printf("---------------11、未选课题数---------\n")
printf("---------------12、销毁课题组---------\n")
printf("---------------0、退出----------------\n")
printf("请输入您的选择:")
scanf("%d",&choice)
do
{
switch(choice)
{
case 1:
create(head)
break
case 2:
insert(head)
break
case 3:
printf("请输入要删除的课题号:")
scanf("%d",&ID)
del(head,ID)
break
case 4:
printf("请输入要修改的课题号:")
scanf("%d",&ID)
edit(head,ID)
break
case 5:
printf("请输入要查询的课题号:")
scanf("%d",&ID)
printNode(head,ID)
break
case 6:
printList(head)
break
case 7:
printChoosed(head)
break
case 8:
printUnchoosed(head)
break
case 9:
printf("课题总数为:%d\n",topicSum(head))
break
case 10:
printf("已选课题总数为:%d\n",topicChoosedSum(head))
break
case 11:
printf("未选课题总数为:%d\n",topicUnchoosedSum(head))
break
case 12:
if(head == NULL)
printf("课题组不存在,销毁失败!\n")
else
destroy(head)
break
case 0:
return 0
default:
printf("选择错误!\n")
}
printf("请输入您的选择:")
scanf("%d",&choice)
}while(choice >= 1 &&choice <= 12)
return 0
}
#include <stdio.h>#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define ID struct id
struct id
{
char name[20]
int num
int a
int b
int c
double ave
ID *next//
}
int pc=0
ID *creat()
{
ID *p1,*p2,*head
int pd
p1=p2=head=NULL
printf("\t\t\t 开始输入记录(学号0结束)!\n")
while(1)
{
printf("请输入学生的码肢橡学号:\n")scanf("%d",&pd)
if(pd==0) break
p1=(ID*)malloc(sizeof(ID))
p1->num=pd
printf("请输入学生的姓名:\n")scanf("%s",p1->name)
printf("请输入学生的语文成绩:\n")scanf("%d",&p1->a)
printf("请输入学生的数学成绩:\n")scanf("%d",&p1->b)
printf("请输入学生的外语成绩:\n")scanf("%d",&p1->c)
p1->ave=(p1->a+p1->b+p1->c)/3.0
if(head==NULL)
{
head=p1
p2=p1
}
else
{
p2->next=p1
p2=p1
}
pc++
}
p2->next=NULL
return(head)
}
ID *sort(ID *head)
{
int temp
char str[100]
double dbl
ID *p1,*p2
for(p1=headp1!=NULLp1=p1->next)
{
for(p2=p1->nextp2!=NULLp2=p2->next)
{
if(p1->ave<p2->ave)
{
temp=p1->num
p1->num=p2->num
p2->num=temp
strcpy(str,p1->name)
strcpy(p1->name,p2->name)
strcpy(p2->name,str)
temp=p1->a
p1->a=p2->a
p2->a=temp
temp=p1->b
p1->b=p2->b
p2->b=temp
temp=p1->c
p1->c=p2->c
p2->c=temp
dbl=p1->ave
p1->ave=p2->ave
p2->ave=dbl
}
}
}
printf("排序成功!!!\n")
return (head)
}
/*输入/添加记录*/
ID *insert(ID *head)
{
ID *temp,*p1,*p2
printf("插入 *** 作饥歼开始!!!\n")
temp=(ID *)malloc(sizeof(ID))
printf("请输入学生的学号:\n")scanf("%d",&temp->num)
printf("请输入学生的姓名:\n")scanf("%s",temp->name)
printf("请输入学生的语文成绩:\n"迟旁)scanf("%d",&temp->a)
printf("请输入学生的数学成绩:\n")scanf("%d",&temp->b)
printf("请输入学生的外语成绩:\n")scanf("%d",&temp->c)
temp->ave=(temp->a+temp->b+temp->c)/3.0
if (head==NULL)
{
head=temp
temp->next=NULL
}
else
{
p1=head
while(p1!=NULL &&p1->ave >temp->ave)
{
p2=p1
p1=p1->next
}
p2->next=temp
temp->next=p1
}
printf("插入成功")
pc++
return (head)
}
/*删除学生记录*/
ID *delet(ID *head)
{
ID *p1,*p2
int num
printf("请输入要删除的学生的学号:")scanf("%d",&num)
p1=head
if (head==NULL)
{
printf("没有记录\n")
goto end
}
while(num!=p1->num &&p1!=NULL)
{
p2=p1p1=p1->next
}
if(num==p1->num)
{
if (p1==head)
head=p1->next
else
p2->next=p1->next
printf("删除成功!!!\n")
pc--
}
end:return head
}
/*查找学生记录*/
ID *search(ID *head)
{
ID *p1,*p2
char str[100]
printf("请输入要查找的学生的姓名:")scanf("%s",str)
p1=head
while(strcmp(str,p1->name) &&p1!=NULL)
{
p2=p1p1=p1->next
}
if(strcmp(str,p1->name)==0)
{
printf("学生的学号:%d\n",p1->num)
printf("学生的姓名:%s\n",p1->name)
printf("学生的语文成绩:%d\n",p1->a)
printf("学生的数学成绩:%d\n",p1->b)
printf("学生的外语成绩:%d\n",p1->c)
printf("学生的平均成绩:%.2lf\n",p1->ave)
}
return head
}
/*显示结果函数*/
void print(ID *head)
{
ID *p
p=head
printf("\t\t\t*****************\n")
printf("显示结果是:\n")
if(head!=NULL)
do
{
printf("%10d%10s%10d%10d%10d%10.2lf\n",p->num,p->name,p->a,p->b,p->c,p->ave)
p=p->next
} while(p!=NULL)
}
void main()
{
ID *head=NULL
int choise
printf("\t\t\t* * * * C语言课设* * * *\n")
while(1)
{
printf("\t\t学生信息管理系统\n")
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
printf("\t\t 1.输入\n")
printf("\t\t 2.显示\n")
printf("\t\t 3.查找\n")
printf("\t\t 4.排序\n")
printf("\t\t 5.插入\n")
printf("\t\t 6.删除\n")
printf("\t\t 0.退出\n")
printf("\n")
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
printf("请选择(0-6):")
scanf("%d",&choise)
switch(choise)
{
case 1: head=creat()
break
case 2: print(head)
break
case 3: head=search(head)
break
case 4: head=sort(head)
break
case 5: head=insert(head)
break
case 6: head=delet(head)
break
case 0:
exit(0)
break
default :printf("输入错误,请重新输入!\n")
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)