最近无聊没事做,就在表白墙投了一个找女朋友帮做课程设计的搞,然后就加了几个学妹,不过都是奔着课程设计来的。哈哈,程序员的无奈!虽然女朋友没找到,但课程设计还是做了一下。
先看下要求:
代码注释了很多,所以下面就是代码了:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Status=1; //定义程序状态 1.运行中 0.退出
struct Employee{
char num[20]; //项目序号
char name[20]; //项目名称
char type[20]; //项目类型
char time[20]; //竞赛时间
int peoples; //参赛人数
struct Employee * next; //连接指针定义
};
struct Employee *head = NULL;//head 头节点
void Add();
void Delete();
void Query();
void Show();
void Quit();
void Edit();
void getData();//读取本地保存的数据
int HdNum(char a[]);//判断项目序号是否存在
void main(){
int n;
while(Status==1){
printf("--------------菜单----------------\n");
printf("1.添加体育竞赛项目\n");
printf("2.删除体育竞赛项目\n");
printf("3.查询体育竞赛项目\n");
printf("4.查看所有竞赛项目信息\n");
printf("5.修改竞赛项目信息\n");
printf("6.读取本地保存的项目信息\n");
printf("7.保存项目信息并退出\n");
printf("----------------------------------\n");
printf("请输入对应功能的序号:");
scanf("%d",&n);
switch(n){
case 1: Add() ; break;
case 2: Delete() ; break;
case 3: Query() ; break;
case 4: Show() ; break;
case 5: Edit() ; break;
case 6: getData() ; break;
case 7: Quit() ; break;
default: system("cls");//清屏
}
}
}
void Add(){
struct Employee *node,*newnode;
char nums[20];
system("cls");
printf("-------------添加体育竞赛项目---------------\n");
while(1){
printf("请输入项目序号(输入内容为空时结束添加):");
fflush(stdin);//过滤回车
gets(nums);
if(strcmp(nums,"")==0){
system("cls");
break;
}
if(HdNum(nums)==0){
printf("输入的项目序号已存在,请重新输入\n");
continue;
}
newnode = (struct Employee *)malloc(sizeof(struct Employee));//开辟内存 创建链表
if (head == NULL)//原表为空
head = newnode;//将新节点做为头节点
else//原表不为空
{
node = head;
while (node->next != NULL)//找到链表尾
node = node->next;
node->next = newnode;//使新节点成为新的尾节点
}
node = newnode;
strcpy(node->num,nums);
printf("请输入项目名称:");
scanf("%s", &node->name);
printf("请输入项目类:");
scanf("%s", &node->type);
printf("请输参赛人数:");
scanf("%d", &node->peoples);
printf("请输入竞赛时间:");
scanf("%s", &node->time);
node->next = NULL;//尾节点
printf("添加成功\n");
}
system("cls");
}
void Delete(){
struct Employee *node,*node2;
char nums[20];
system("cls");
if (head == NULL){
system("cls");
printf("没有项目信息,请先添加信息\n\n");
return;
}
printf("-------------删除体育竞赛项目---------------\n");
while(1){
if(head==NULL){
system("cls");
printf("所有项目信息已删除完毕\n\n");
break;
}
printf("输入要删除项目的序号(输入内容为空时返回主菜单):");
fflush(stdin);//过滤回车
gets(nums);
if(strcmp(nums,"")==0){
system("cls");
break;
}
node = head;//pre
node2= head->next;//p
if(strcmp(node->num,nums)==0){
head=node->next;
free(node);
printf("删除成功\n");
}//删除的是第一个节点
else{
while (node2 != NULL){
if (strcmp(node2->num,nums)==0) //找到了
{
node->next = node2->next; //前面结点和后面结点连接
free(node2);//释放内存
printf("删除成功\n");
break;
}
else //没找到
{
node = node->next;
node2 =node2->next;
}
}
if (node2 == NULL) printf("没有该项目信息!!\n");
}//删除的是第一个节点以后
}
}
void Query(){
struct Employee *node;
char nums[20];
system("cls");
node = head;
while(1){
if (head == NULL){
printf("没有体育竞赛项目信息,请先添加信息\n\n");
return;
}
printf("\n请输入要查找项目的序号(输入内容为空时返回主菜单):");
fflush(stdin);//过滤回车
gets(nums);
if(strcmp(nums,"")==0){
system("cls");
break;
}
node = head ;
while(node!= NULL){
if(strcmp(node->num,nums)==0){
printf("\n 项目序号 项目名称 项目类 参赛人数 竞赛时间\n");
printf("%10s %10s %10s %10d %12s\n", node->num, node->name, node->type, node->peoples, node->time);
break;
}
else node=node->next ;
}
if(node==NULL) printf("没有%s对应项目的信息!\n",nums);
}
system("cls");
}
void Edit(){
struct Employee *node;
char nums[20];
system("cls");
node = head;
while(1){
if (head == NULL){
printf("没有体育竞赛项目信息,请先添加信息\n\n");
return;
}
printf("\n请输入要修改项目的序号(输入内容为空时返回主菜单):");
fflush(stdin);//过滤回车
gets(nums);
if(strcmp(nums,"")==0){
system("cls");
break;
}
node = head ;
while(node!= NULL){
if(strcmp(node->num,nums)==0){
printf("\n 项目序号 项目名称 项目类 参赛人数 竞赛时间\n");
printf("%10s %10s %10s %10d %12s\n", node->num, node->name, node->type, node->peoples, node->time);
printf("请输入要修改为的项目名称:");
scanf("%s", &node->name);
printf("请输入要修改为的项目类:");
scanf("%s", &node->type);
printf("请输入要修改为的参赛人数:");
scanf("%d", &node->peoples);
printf("请输入要修改为的竞赛时间:");
scanf("%s", &node->time);
printf("修改成功\n");
break;
}
else node=node->next ;
}
if(node==NULL) printf("没有%s对应项目的信息,无法对其进行修改!\n",nums);
}
system("cls");
}
void Show(){
struct Employee *node;
node = head;
if (head == NULL){
printf("没有项目信息,请先添加信息\n\n");
return;
}
printf("\n 项目序号 项目名称 项目类 参赛人数 竞赛时间\n");
do{
printf("%10s %10s %10s %10d %12s\n", node->num, node->name, node->type, node->peoples, node->time);
node=node->next;
}while (node != NULL);
}
void getData(){
struct Employee *node,*newnode;
int res = 1;
FILE * input = fopen("data.dat","rb");//生成的stud.dat文件
if(input == NULL){
printf("本地没有已保存的项目信息文件\n\n");
}else{
do{
newnode = (struct Employee *)malloc(sizeof(struct Employee));//开辟内存 创建链表
res = fread(newnode,sizeof(struct Employee),1,input);
if( res>0 && HdNum( newnode->num )==1 ){
if (head == NULL)//原表为空
head = newnode;//将新节点做为头节点
else//原表不为空
{
node = head;
while (node->next != NULL)//找到链表尾
node = node->next;
node->next = newnode;//使新节点成为新的尾节点
}
node = newnode;
node->next = NULL;//尾节点
}
}while( res>0 );//将保存的数据全部添加到链表尾
fclose(input);
printf("数据读取成功!\n");
}
}
void Quit(){
struct Employee *node,*newnode;
FILE * output = fopen("data.dat","wb");//生成的stud.dat文件
if(output == NULL){
printf("无法打开文件");
}else{
while(head != NULL)//保存并释放链表
{
node = head ;
head = head->next;
fwrite(node,sizeof(struct Employee),1,output);
free(node);
}
fclose(output);
}
Status = 0;//结束程序的运行
}
int HdNum( char num[] ){
struct Employee *node;
while(1){
if (head == NULL) return 1;
node = head ;
while(node!= NULL){
if( strcmp(node->num,num)==0 ) return 0;
else node=node->next ;
}
if(node==NULL) return 1;
}
}
上面的代码是还可以优化一下的,不过就留给各位大佬进行修改了。