其实早就写完了不过没发现在发下。
主要思路:先确定一个主要的控制面板,随后才开始编写每一个小功能的函数。
要理清楚结构体的关系。
代码
#define _CRT_SECURE_NO_WARNINGS
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<malloc.h>
#include<assert.h>
//学生信息结构(学号、姓名、性别、分数、电话号码)
struct Stu_Info
{
char stu_number[20];
char stu_name[20];
char stu_sex[5];
int stu_grade;
char stu_phone_number[12];
};
//人数结构
struct Stu_group
{
struct Stu_Info* p_Info;
int count;
int capcity;
};
//菜单
void menu()
{
printf("******************************************\n");
printf("****** 1.增加 2.修改 *************\n");
printf("****** 3.删除 4.浏览 *************\n");
printf("****** 5.查找 6.排序 *************\n");
printf("****** 7.统计 0.退出 *************\n");
printf("******************************************\n");
}
//创造空间函数
void cre(struct Stu_group* pc)
{
assert(pc);
pc->capcity = 3;
pc->count = 0;
pc->p_Info = calloc(3, sizeof(struct Stu_Info));
if (pc->p_Info == NULL)
{
printf("创建空间失败\n");
}
}
//初始化空间
void Int_cre(struct Stu_Info* p)
{
assert(p);
memset(p, 0, 3 * sizeof(struct Stu_Info));
if (p == NULL)
{
printf("初始化失败\n");
}
}
//增加功能
void Add(struct Stu_group* a)
{
system("cls");
if (a->count == a->capcity)
{
a->p_Info = realloc(a->p_Info, sizeof(struct Stu_Info) * (a->capcity + 2));
a->capcity += 2;
if (a->p_Info == NULL)
{
printf("增容失败\n");
return;
}
}
printf("输入学生学号");
scanf("%s", a->p_Info[a->count].stu_number);
printf("输入学生姓名");
scanf("%s", a->p_Info[a->count].stu_name);
printf("输入学生性别");
scanf("%s", a->p_Info[a->count].stu_sex);
printf("输入学生分数");
scanf("%d", &a->p_Info[a->count].stu_grade);
printf("输入学生电话号码");
scanf("%s", a->p_Info[a->count].stu_phone_number);
a->count += 1;
printf("增加成功!");
Sleep(1000);
}
//修改功能
void Rev(struct Stu_group* a)
{
system("cls");
assert(a);
if (a->count == 0)
{
printf("无数据\n");
return;
}
printf("请输入要修改学生的姓名\n");
char arr[20] = { 0 };
int b = 0;
int c = 0;
scanf("%s", &arr);
for (c = 0; c < a->count; c++)
{
b = strcmp(arr, a->p_Info[c].stu_name);
if (b == 0)
{
break;
}
}
if (b == 0)
{
printf("输入学生学号");
scanf("%s", a->p_Info[c].stu_number);
printf("输入学生姓名");
scanf("%s", a->p_Info[c].stu_name);
printf("输入学生性别");
scanf("%s", a->p_Info[c].stu_sex);
printf("输入学生分数");
scanf("%d", &a->p_Info[c].stu_grade);
printf("输入学生电话号码");
scanf("%s", a->p_Info[c].stu_phone_number);
}
if (b != 0)
{
printf("查无此人\n");
}
printf("修改成功!");
Sleep(1000);
return;
}
//删除功能
void Del(struct Stu_group* a)
{
system("cls");
assert(a);
if (a->count == 0)
{
printf("无数据\n");
Sleep(1000);
return;
}
printf("请输入要删除学生的姓名\n");
char arr[20] = { 0 };
int b = 0;
int c = 0;
scanf("%s", &arr);
for (c = 0; c < a->count; c++)
{
b = strcmp(arr, a->p_Info[c].stu_name);
if (b == 0)
{
break;
}
}
if (b == 0)
{
int d = 0;
for (d = c;d<a->count-1;d++)
{
a->p_Info[d] = a->p_Info[d + 1];
}
a->count -= 1;
printf("删除成功!");
Sleep(1000);
}
if (b != 0)
{
printf("查无此人\n");
Sleep(1000);
}
return;
}
//浏览功能
void Print(struct Stu_group* a)
{
system("cls");
printf("学号 姓名 性别 分数 电话号码\n");
for(int b = 0; b < a->count; b++)
{
printf("%-20s%-20s%-10s%-12d%s\n",a->p_Info[b].stu_number,
a->p_Info[b].stu_name,
a->p_Info[b].stu_sex,
a->p_Info[b].stu_grade,
a->p_Info[b].stu_phone_number);
}
int c = 1;
while (c)
{
printf("0 退出\n");
scanf("%d", &c);
}
}
//查找功能
void Search(struct Stu_group* a)
{
system("cls");
assert(a);
if (a->count == 0)
{
printf("无数据\n");
Sleep(1000);
return;
}
printf("请输入要查找学生的姓名\n");
char arr[20] = { 0 };
int b = 0;
scanf("%s", &arr);
for (int c = 0; c < a->count; c++)
{
b = strcmp(arr, a->p_Info[c].stu_name);
if (b == 0)
{
printf("学号 姓名 性别 分数 电话号码\n");
printf("%-20s%-20s%-10s%-12d%s\n", a->p_Info[c].stu_number,
a->p_Info[c].stu_name,
a->p_Info[c].stu_sex,
a->p_Info[c].stu_grade,
a->p_Info[c].stu_phone_number);
break;
}
}
if (b != 0)
{
printf("查无此人\n");
Sleep(1000);
}
int c = 1;
while (c)
{
printf("0 退出\n");
scanf("%d", &c);
}
return;
}
//排序功能
int Cmp1(void* e1, void* e2)
{
return strcmp(((struct Stu_Info*)e1)->stu_number, ((struct Stu_Info*)e2)->stu_number);
}
int Cmp2(void* e1, void* e2)
{
return (((struct Stu_Info*)e1)->stu_grade - ((struct Stu_Info*)e2)->stu_grade);
}
void Sort(struct Stu_group* a)
{
system("cls");
if (a->count == 0)
{
printf("无数据\n");
Sleep(1000);
return;
}
assert(a);
printf("请选择你的排序方式 1 按学号排 2 按分数排 0 不排了,走了");
int b = 0;
AAAAA:
scanf("%d", &b);
if (b == 1)
{
qsort(a->p_Info,a->count,sizeof(struct Stu_Info),Cmp1);
printf("排序成功\n");
Sleep(1000);
return;
}
if (b == 2)
{
qsort(a->p_Info,a->count, sizeof(struct Stu_Info),Cmp2);
printf("排序成功\n");
Sleep(0);
return;
}
if (b == 0)
{
return;
}
if (b != 1 && b != 2 && b != 0)
{
printf("选择错误,重新选择\n");
goto AAAAA;
}
}
//统计功能
void Sta(struct Stu_group* a)
{
system("cls");
printf("人数:%d\n", a->count);
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int sum = 0;
float average = 0;
for (int f = 0; f < a->count; f++)
{
int l = a->p_Info[f].stu_grade;
sum +=l;
if (l >= 90)
{
b++;
}
else if (l >= 80)
{
c++;
}
else if (l >= 70)
{
d++;
}
else
{
e++;
}
}
average = (float)sum / a->count;
printf("平均分是%g,其中A(90-100)的人数为%d,其中B(80-90)的人数为%d,其中C(70-80)的人数为%d,其中D(小70)的人数为%d\n", average, b, c, d, e);
int j = 1;
while (j)
{
printf("0 退出\n");
scanf("%d", &j);
}
}
//文件读取功能
void Fread(struct Stu_group* a)
{
assert(a);
FILE* pfile = fopen("contact.txt", "r");
if (pfile == NULL)
{
printf("打开文件失败。\n");
}
struct Stu_Info w = {0};
while(fread(&w, sizeof(struct Stu_Info), 1, pfile) == 1)
{
if (a->count == a->capcity)
{
a->p_Info = realloc(a->p_Info, sizeof(struct Stu_Info) * (a->capcity + 2));
a->capcity += 2;
if (a->p_Info == NULL)
{
printf("增容失败\n");
return;
}
}
a->p_Info[a->count] = w;
a->count++;
}
fclose(pfile);
pfile = NULL;
}
//文件保存功能
void Freverse(struct Stu_group* a)
{
FILE* pfile = fopen("contact.txt","w");
if (pfile == NULL)
{
printf("打开文件失败。\n");
}
fwrite(a->p_Info, sizeof(struct Stu_Info), a->count, pfile);
fclose(pfile);
pfile = NULL;
}
int main()
{
struct Stu_group group;
cre(&group);
Int_cre(group.p_Info);
Fread(&group);
while (1)
{
system("cls");
int n = 0;
menu();
scanf("%d", &n);
switch (n)
{
case 1:
{
Add(&group);
break;
}
case 2:
{
Rev(&group);
break;
}
case 3:
{
Del(&group);
break;
}
case 4:
{
Print(&group);
break;
}
case 5:
{
Search(&group);
break;
}
case 6:
{
Sort(&group);
break;
}
case 7:
{
Sta(&group);
break;
}
case 0:
{
Freverse(&group);
return 0;
}
}
}
return 0;
}
只是没事写博客,顺便给一些可能会写C语言作业的人一个小思路。
问题:1.需要自己新建一个"contact.txt"文件来保存你输入的数据
2.没分头文件写,感觉有点捞。\
3.由读者来发现 ; - ; 。