本帖最后由 YFSafe 于 2023-1-20 20:33 编辑
翻到了之前写的一个项目,于是想再实现一个。
用二十分钟写了一个大概的模版,结果改bug用了很久
[C++] 纯文本查看 复制代码 #include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Student
{
int id;//学号
string name;
int score;
}Stu[1005];
int cnt = 0;//总数
void printstu();//ok
void insertstu();//ok
void deletestu();//ok
void changestu();
int searchstu(int myid);//ok
int searchstu(int myid)//搜索到则返回数值在数组中的位置,未搜到返回-1
{
for(int i = 1;i <= cnt;i++)
{
if(Stu[i].id == myid)
{
cout << i << endl;
return i;
}
}
return -1;
}
void printstu()
{
cout << "以下是现有的学生" << endl;
for(int i = 1;i <= cnt;i++)
{
if(Stu[i].id == -1) continue;
cout << "学号:" << Stu[i].id << " 姓名:" << Stu[i].name << " 分数:" << Stu[i].score << endl;
}
}
void insertstu()
{
int insert = 0;
if(cnt + 1 >= 1005)
{
cout << "空间不足!请删除一些学生再添加" << endl;
return;
}
for(int i = 1;i <= cnt;i++)//这里先不count++的原因是stu[count++]这个内存未被使用,如果访问会错误
{
if(Stu[i].id == -1)
{
insert = i;
break;
}
}
if(insert == 0) insert = ++cnt;
string stuname;
int stuid,stuscore;
cout << "名称:";
cin >> stuname;
Stu[insert].name = stuname;
cout << "学号:";
cin >> stuid;
if(searchstu(stuid) != -1)
{
cout << "学号重复!!!" << endl;
return;
}
Stu[insert].id = stuid;
cout << "成绩:";
cin >> stuscore;
Stu[insert].score = stuscore;
}
void deletestu()
{
cout << "请输入删除学生的学号" << endl;
int myid;
cin >> myid;
int res = searchstu(myid);
if(res) Stu[res].id = -1;
else cout << "学号错误!" << endl;
}
void changestu()
{
cout << "请输入学生的学号" << endl;
int myid;
cin >> myid;
int res = searchstu(myid);
if(res != -1)
{
cout << "请输入成绩:";
int myscore;
cin >> myscore;
Stu[res].score = myscore;
cout << "修改成功!" << endl;
}
else cout << "学号错误!" << endl;
}
int main()
{
char select;
while(true)
{
cout<<"============\n";
cout<<"1.插入学生数据\n";
cout<<"2.删除学生信息\n";
cout<<"3.打印学生信息\n";
cout<<"4.修改学生数据\n";
cout<<"q.退出\n";
cin >> select;
if(select == '1') insertstu();
else if(select == '2') deletestu();
else if(select == '3') printstu();
else if(select == '4') changestu();
else if(select == 'q') return 0;
else cout << "不支持的操作!" << endl;
}
return 0;
} |