草率了 之前那个把long 和long long的输出输入整错了 %lld 和%ld改错了 ,谢谢这位小兄弟
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
const int maxn = 100; // 最多存储maxn条学籍记录
struct Student {
long long id; // 学号
char name[200]; // 姓名
char sex[200]; // 性别,
int score; // 分数
}stu[100];
int n;
void show(int i)
{
// 显示一条记录
printf("%lld\t%s\t%s\t%d", stu[i].id, stu[i].name, stu[i].sex, stu[i].score);
}
bool add()
{
// 新增记录
if (n >= maxn - 5)
return false;
printf("请输入学号: ");
scanf("%lld", &stu[n].id);//输入学号的代码
printf("请输入姓名: ");
scanf("%s", stu[n].name);// 输入姓名的代码
printf("请输入性别: ");
scanf("%s", stu[n].sex); // 请勿改动此句
printf("请输入分数: ");
scanf("%d", &stu[n].score);
n++;
return true;
}
void show_all()
{
// 显示所有记录
for (int i = 0; i < n; i++)
{
show(i);
printf("\n");
}
}
int search1(long long id)
{
// 按学号id查找记录
for (int i = 0; i < n; i++)
if (id == stu[i].id)
return i;
return -1;
}
int search(char name[])
{
// 按姓名name查找记录
for (int i = 0; i < n; i++)
if (strcmp(name, stu[i].name) == 0)
return i;
return -1;
}
bool modify_by_id()
{
// 按学号修改记录
long long id;
printf("请输入学号:");
scanf("%lld", &id);
int position = search1(id);
if (position < 0)
{
return false;
}
else {
printf("请输入学号: ");
scanf("%lld", &stu[position].id);//输入学号的代码
printf("请输入姓名: ");
scanf("%s", stu[position].name);// 输入姓名的代码
printf("请输入性别: ");
scanf("%s", stu[position].sex); // 请勿改动此句
printf("请输入分数: ");
scanf("%d", &stu[position].score);
}
return true;
}
bool del_by_id()
{
// 删除指定学号的记录
long long id;
printf("请输入学号:");
scanf("%lld", &id);
int position = search1(id);
if (position < 0)
{
return false; // 记录不存在,返回false
}
else {
for (int i = position + 1; i < n; i++) {
stu[i - 1] = stu[i];
}
n--;
return true;
}// 删除position位置的元素
// 删除成功,返回true
}
void sort()
{
// 按成绩从高到低排序
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (stu[i].score < stu[j].score)
{
struct Student tmp;
tmp.id = stu[i].id;
tmp.score = stu[i].score;
strcpy(tmp.name, stu[i].name);
strcpy(tmp.sex, stu[i].sex);
stu[i].id = stu[j].id;
stu[i].score = stu[j].score;
strcpy(stu[i].name, stu[j].name);
strcpy(stu[i].sex, stu[j].sex);
stu[j].id= tmp.id;
stu[j].score= tmp.score;
strcpy( stu[j].name, tmp.name);
strcpy(stu[j].sex, tmp.sex);
}
}
void menu()
{
printf("1. 新增一条学籍记录\n");
printf("2. 显示所有学籍记录\n");
printf("3. 按学号查找学籍记录\n");
printf("4. 按姓名查找学籍记录\n");
printf("5. 按学号修改学籍记录\n");
printf("6. 按学号删除学籍记录\n");
printf("7. 按分数排序排序\n");
printf("8. 退出\n");
}
int main()
{
int choice;
do
{
system("cls");
menu();
scanf("%d", &choice);
if (choice == 1)
{
if (add())
{
printf("新增成功!\n");
}
else
{
printf("数组已满,新增失败\n");
}
}
else if (choice == 2)
{
show_all();// ______补充代码,调用show_all函数显示所有记录
}
else if (choice == 3)
{
long long id;
printf("请输入学号:");
scanf("%lld", &id);
int position = search1(id);// int position = ______ 补充代码,调用searh按学号查找记录在数组中的位置
if (position < 0)
{
printf("记录不存在!\n");
}
else
{
show(position);//______ 补充代码,调用show显示记录
}
}
else if (choice == 4)
{
char name[20];
printf("请输入姓名:");
scanf("%s", name);// ____ 补充代码输入姓名name
int position = search(name);// int position = ______ 补充代码,调用searh按姓名查找记录在数组中的位置
if (position < 0)
{
printf("记录不存在!\n");
}
else
{
show(position);//______ 补充代码,调用show显示记录
}
}
else if (choice == 5)
{
if (modify_by_id())
{
printf("修改成功!\n");
}
else
{
printf("记录不存在\n");
}
}
else if (choice == 6)
{
if (del_by_id())
{
printf("删除成功\n");
}
else
{
printf("记录不存在\n");
}
}
else if (choice == 7)
{
sort(); // 调用sort函数排序
show_all();// 调用show_all显示排序后的结果
}
else
{
break; // 退出系统
}
system("pause");
} while (true);
printf("bye!\n");
return 0;
}