声明:仅供留档查阅,仅用作起到提示引导性作用,仅用作学习交流,切勿直接照搬
代码主体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <iostream> using namespace std;
struct Student { long num; char name[10]; float score; };
class ScoreList { private: Student data[100]; int length;
public: ScoreList() { length = 5; data[0] = { 1001, "张三", 85 }; data[1] = { 1002, "李四", 90 }; data[2] = { 1003, "王五", 80 }; data[3] = { 1004, "赵六", 95 }; data[4] = { 1005, "孙七", 88 }; }
void del(long num) { for (int i = 0; i < length; i++) { if (data[i].num == num) { for (int j = i; j < length - 1; j++) { data[j] = data[j + 1]; } length--; break; } } } void insert(Student stu) { int pos = 0; while (pos < length && data[pos].score > stu.score) pos++; for (int i = length; i > pos; i--) { data[i] = data[i - 1]; } data[pos] = stu; length++; } void output() { cout << "学号\t姓名\t分数" << endl; for (int i = 0; i < length; i++) { cout << data[i].num << "\t" << data[i].name << "\t" << data[i].score << endl; } }
};
int main() { long a; char b[10]; float c; ScoreList list; list.output(); cout << "删除一个学生,请输入学生学号" << endl; cin >> a; list.del(a); list.output(); cout << "插入一个新学生(学号 姓名 分数 )用空格隔开" << endl; cin >> a >> b >> c; Student newStu = { a, "", c }; strcpy_s(newStu.name, b); // 使用 strcpy 函数复制字符串 list.insert(newStu); list.output(); return 0; }
|