//姓名 学号 成绩
#include <iostream>
#include <string.h>
using namespace std;
struct student
{
char name[10];
int num;
float score;
};
class studentlist
{
public:
studentlist()
{
len = 0;
}
void Insert(char *inputstr)
{
if (len >= 40)
{
cout << "学生已经满了!" << endl;
return;
}
char *p = strtok(inputstr, " ");
if (strlen(p) >= 10)
{
cout << "姓名输入错误!" << endl;
return;
}
else
{
strcpy(v[len].name, p);
}
p = strtok(NULL, " ");
if (strlen(p) > 7)
{
cout << "学号输入有误!" << endl;
return;
}
else
{
v[len].num = atoi(p);
}
p = strtok(NULL, "\n");
if (strlen(p) > 3)
{
cout << "成绩输入有误!" << endl;
return;
}
else
{
v[len].score = atof(p);
}
len++;
cout << "插入新的同学成功!" << endl;
}
void Delete(char *_name)
{
for (int i = 0; i < len; ++i)
{
if (strcmp(_name, v[i].name) == 0)
{
for (int j = i; j < len - 1; ++j)
{
strcpy(v[j].name, v[j + 1].name);
v[j].num = v[j + 1].num;
v[j].score = v[j + 1].score;
}
cout << "删除同学成功!" << endl;
len--;
return;
}
}
cout << "没有找到这个同学哦!"<<endl;
}
void Sort()
{
student stu;
//用的冒泡排序。
for (int i = 0; i < len-1; ++i)
{
for (int j = i+1; j < len; ++j)
{
if (v[i].score < v[j].score)
{
strcpy(stu.name, v[i].name);
stu.num = v[i].num;
stu.score = v[i].score;
strcpy(v[i].name,v[j].name);
v[i].num = v[j].num;
v[i].score = v[j].score;
strcpy(v[j].name, stu.name);
v[j].num = stu.num;
v[j].score = stu.score;
}
}
}
}
void Printf()
{
cout << "姓名\t" << "学号\t" << "成绩"<<endl;
for (int i = 0; i < len; ++i)
{
cout << v[i].name << "\t" << v[i].num << "\t" << v[i].score << endl;
}
}
private:
struct student v[40];
int len;
};
int main()
{
int flags = 1;
studentlist list;
while (flags)
{
cout << "输入数字1插入学生信息" << endl;
cout << "输入数字2打印学生信息" << endl;
cout << "输入数字3删除学生信息" << endl;
cout << "输入数字4排序学生成绩" << endl;
cout << "输入数字0退出学生信息" << endl;
cout << "请输入你的选择:>" << endl;
cin >> flags;
switch (flags)
{
case 1:
cout << "请输入学生信息:>" << endl;
char input[30];
cin.get();
cin.get(input,30);//不要最后的"\n"。
cin.get();
list.Insert(input);
break;
case 2:
list.Printf();
break;
case 3:
char name[10];
cin >> name;
list.Delete(name);
break;
case 4:
list.Sort();
break;
default:
break;
}
}
return 0;
}
c++笔试题:简易学生信息系统
最新推荐文章于 2024-02-18 21:42:40 发布