转眼间一学期要过去了,C语言课程我们也迎来了尾声,我很喜欢这么学科,在学期最后我所在的小组完成了课程设计。
以下是全部代码(详细讲解在下一期):
#include <stdio.h>
#include <string.h>
#define NAME_LEN 64 /* 姓名字符数 */
double time;
int index;
char name[NAME_LEN];
long studne_id;
typedef struct students {
long studne_id; /* 学号 */
char name[NAME_LEN]; /* 姓名 */
char sex[3]; /* 性别 */
int age; /* 年龄 */
double time; /* 入团时间 */
char telephone[13]; /* 联系电话 */
} Students;
struct students student[] = {
{1240701, "陈二", "男", 17, 2016, "17100005963"},
{1240702, "张三", "男", 18, 2016, "17200005912"},
{1240703, "李四", "女", 19, 2018, "15100005934"},
{1240704, "王五", "男", 20, 2020, "12300005463"},
{1240705, "赵六", "男", 21, 2020, "11100002983"},
};
void select_studnet_all(Students std[], int count); // 显示函数
int add_studnet_info(Students std[], int count); // 添加函数
int find_Students_id(Students std[], long studne_id, int count); //调用学号函数
void update_studnet_info(Students std[], int count); // 更新函数
int delete_studnet_info(Students std[], int count); // 删除函数
void sort_student_info(Students std[], int count);//排序函数
void find_studnet_info(Students std[], int count); // 统计函数
void search_studnet_info(Students std[], int count);//按姓名查找函数
void seek_studnet_info(Students std[], int count);//按学号查找函数
int main(void) {
int num;
int count = 5;
while (1) {
puts("----------欢迎进入团员管理系统----------");
puts("| 1.显示团员信息 |");
puts("| 2.添加团员信息 |");
puts("| 3.更新团员信息 |");
puts("| 4.删除团员信息 |");
puts("| 5.排序团员信息 |");
puts("| 6.统计团员信息 |");
puts("| 7.按姓名搜索团员信息 |");
puts("| 8.按学号搜索团员信息 |");
puts("| 9.退出系统 |");
puts("----------------------------------------");
printf("请输入1至9选项:");
scanf("%d", &num);
switch (num) {
case 1:
select_studnet_all(student, count);
break;
case 2:
count = add_studnet_info(student, count);
break;
case 3:
update_studnet_info(student, count);
break;
case 4:
count = delete_studnet_info(student, count);
break;
case 5:
sort_student_info(student, count);
break;
case 6:
find_studnet_info(student, count); // 统计函数
break;
case 7:
search_studnet_info(student, count); //查找姓名函数
break;
case 8:
seek_studnet_info(student, count); //查找姓名函数
break;
case 9:
puts("系统结束运行!!");
return 0;
default:
puts("输入错误,请重新输入!");
break;
}
}
return 0;
}
void select_studnet_all(Students std[], int count)
{
int i;
puts(" 学号 姓名 性别 年龄 入团时间 联系电话");
for (i = 0; i < count; i++)
{
printf("%ld %-5s %s %d %6.2f %s\n", std[i].studne_id, std[i].name, std[i].sex, std[i].age, std[i].time, std[i].telephone);
}
puts("\n");
}
int add_studnet_info(Students std[], int count)
{
long studne_id; /* 学号 */
char name[NAME_LEN]; /* 姓名 */
char sex[3]; /* 性别 */
int age; /* 年龄 */
double time; /* 入团时间 */
char telephone[13]; /* 联系电话 */
int index;
printf("请输入学号:");
scanf("%ld", &studne_id);
index = find_Students_id(std, studne_id, count);
printf("请输入姓名:");
scanf("%s", name);
printf("请输入性别:");
scanf("%s", sex);
printf("请输入年龄:");
scanf("%d", &age);
printf("请输入入团时间:");
scanf("%lf", &time);
printf("请输入联系电话:");
scanf("%s", &telephone);
if (index == -1)
{
std[count].studne_id = studne_id;
strcpy(std[count].name, name);
strcpy(std[count].sex, sex);
std[count].age = age;
std[count].time = time;
strcpy(std[count].telephone, telephone);
puts("已成功添加新的团员信息:");
printf("%ld %-5s %s %d %6.2f %s\n", studne_id, name, sex, age, time, telephone);
count++;
}else
{
puts("学号重复添加失败!");
}
return count;
}
int find_Students_id(Students std[], long studne_id, int count)
{
int i;
int index;
for (i = 0; i < count; i++)
{
if (std[i].studne_id == studne_id)
{
index = i;
break;
}
else
{
index = -1;
}
}
return index;
}
void update_studnet_info(Students std[], int count)
{
long studne_id; /* 学号 */
char name[NAME_LEN]; /* 姓名 */
char sex[3]; /* 性别 */
int age; /* 年龄 */
double time; /* 身高 */
char telephone[50]; /* 联系电话 */
printf("请输入要修改的团员的学号:");
scanf("%ld", &studne_id);
index = find_Students_id(std, studne_id, count);
if (index != -1)
{
puts("请进行修改操作!");
printf("请输入学号:");
scanf("%ld", &studne_id);
printf("请输入姓名:");
scanf("%s", name);
printf("请输入性别:");
scanf("%s", sex);
printf("请输入年龄:");
scanf("%d", &age);
printf("请输入入团时间:");
scanf("%lf", &time);
printf("请输入联系电话:");
scanf("%s", &telephone);
puts("已更新的团员信息:");
printf("%ld %-5s %s %d %6.2f %s\n", studne_id, name, sex, age, time, telephone);
std[index].studne_id = studne_id;
strcpy(std[index].name, name);
strcpy(std[index].sex, sex);
std[index].age = age;
std[index].time = time;
strcpy(std[index].telephone ,telephone);
puts("修改成功!");
}
else
{
puts("没有查找到该团员!");
}
}
int delete_studnet_info(Students std[], int count)
{
long studne_id; /* 学号 */
int i;
printf("请输入要删除的团员的学号:");
scanf("%ld", &studne_id);
for (i = 0; i < count; i++)
{
if (std[i].studne_id == studne_id)
{
while (1)
{
if (i < count)
{
std[i] = std[i + 1];
i++;
}
else
{
break;
}
}
puts("删除成功!");
count--;
break;
}
}
if (count == i)
{
puts("没有查找到该团员!");
}
return count;
}
void sort_student_info(Students std[], int count)
{
int i,j;
Students t;
for(i=1;i<count;i++) //用"冒泡排序法"进行排序
{
for(j=0;j<count-i;j++)
{
if(strcmp(student[j].name,student[j+1].name)>0)
{
t=student[j];
student[j]=student[j+1];
student[j+1]=t;
}
}
}
printf("排序后的结果为:\n");
for(i=0;i<count;i++) //输出排序后的团员信息
{
printf("%ld %-5s %s %d %6.2f %s\n",student[i].studne_id,student[i].name,student[i].sex,student[i].age,student[i].time,student[i].telephone);
}
}
void find_studnet_info(Students std[], int count)
{
double time_find; /*入团时间*/
int i,j=0;//该年份入团的学生人数
printf("请输入入团时间:");
scanf("%lf", &time_find);
for(i=0;i<count;i++)
{
if(std[i].time==time_find)
{
printf("%ld %-5s %s %d %6.2f %s\n",student[i].studne_id,student[i].name,student[i].sex,student[i].age,student[i].time,student[i].telephone);
j++;
}
}
printf("该年份入团的学生人数为%d\n",j);
}
void search_studnet_info(Students std[], int count)
{
char a[8];
int i,y=0;
printf("输入查要找的姓名:\n");
scanf("%s",a);
for(i=0;i<count;i++)
{
if(strcmp(std[i].name,a)==0)
{
y=1;
printf("%ld %-5s %s %d %6.2f %s\n",student[i].studne_id,student[i].name,student[i].sex,student[i].age,student[i].time,student[i].telephone);
}
}
if(y==0)
printf("查不到该学生\n");
}
void seek_studnet_info(Students std[], int count)
{
long b;
int i,x=0;
printf("输入查要找的学号:\n");
scanf("%ld",&b);
for(i=0;i<count;i++)
{
if(std[i].studne_id==b)
{
x=1;
printf("%ld %-5s %s %d %6.2f %s\n",student[i].studne_id,student[i].name,student[i].sex,student[i].age,student[i].time,student[i].telephone);
}
}
if(x==0)
printf("查不到该学生\n");
}