实验一 学生成绩管理系统
1. 问题描述
学生成绩管理系统中的数据元素具有如下形式:
学生的成绩记录包括学号、姓名、语文、数学、计算机等数据项。
2.功能要求
对成绩登记表要求完成如下功能:
⑴ 插入:将某学生成绩记录插入到登记表中;
⑵ 删除:将满足条件的记录删除;
⑶ 修改:对成绩记录的数据项进行修改;
⑷ 查询:查找满足条件的学生;
⑸ 输出:将成绩记录登记表中的全部(或满足条件)成绩记录输出。
3.实现要点
对成绩记录登记表采用顺序的存储结构,在建立表时,由成绩记录表的书写形式转化顺序表存储结构,还要把学生的书面形式转化为具体的类。
4.类定义
为顺序表建立SeqList,其类定义如下:
在SeqList类中,提供了如下成员函数
(1) 函数声明 SeqList(T a[ ], int n);
完成的功能 构造长度为n,元素为数组a[ ]中元素的顺序表
(2) 函数声明 int Length( ) {return length;}
完成的功能 求顺序表长度
(3) 函数声明 T Get(int i)
完成的功能 依次查找,读取表中第i个元素
(4) 函数声明 void Insert(int i, T x);
完成的功能 在顺序表中第i个位置插入值为x的元素
(5) 函数声明 T Delete(int i);
完成的功能 查找并删除顺序表的第i个元素
(6) 函数声明 void PrintList( )
完成的功能 遍历顺序表,按序号依次输出各元素
(7) 函数声明 void PrintStudent(T x)
完成的功能 输出元素x
5.类定义
#pragma once
struct Student
{
string name;//姓名
long stuNum;//学号
int chinese;//语文
int matheMatics;//数学
int computer;//计算机
int tScore;//总分
};
6.代码实现
#include "SeqList.h"
#include<iostream>
using namespace std;
template<class T>
SeqList<T>::SeqList() {
length = 0;
}
template<class T>
SeqList<T>::SeqList(T a[], int n) {
if (n > MaxSize) {
throw "参数非法";
}
for (int i = 0; i < n; i++) {
data[i] = a[i];
length = n;
}
}
//插入算法
template<class T>
void SeqList<T>::Insert(int i, T x) {
if (length == MaxSize) throw"上溢";
if (i < 1 || i>length + 1)throw"位置错误";
for (int j = length; j >= i; j--) {
data[j] = data[j - 1];
}
data[i - 1] = x;
length++;
}
//删除操作算法
template <class T>
T SeqList<T>::Delete(int i) {
if (length == 0)throw"UnderFlow";
if (i < 1 || i>length)throw"Wrong Position";
T x = data[i - 1];
for (int j = i; j <= length - 1; j++) {
data[j - 1] = data[j];
}
length--;
return x;
}
//查找算法操作
//1)按位查找
template<class T>
T SeqList<T>::Get(int i) {
if (i<1 || i>length)throw"wrong position";
else {
return data[i - 1];
}
}
//2)按值查找
template<class T>
int SeqList<T>::Locate(T x) {
for (int i = 0; i < length; i++) {
if (data[i] == x)return i + 1;
else {
return 0;
}
}
}
//输出操作算法
template<class T>
void SeqList<T>::PrintList() {
for (int i = 0; i < length; i++) {
cout << data[i].name << '\t' << data[i].stuNum << '\t' << data[i].chinese << '\t' << data[i].matheMatics << '\t' << data[i].computer << '\t' << Total(data[i]);
}
}
//主函数
#include<iostream>
#include "Seqlist.h"
#include"LinkLeast.h"
#include"student.h";
#include<string>
using namespace std;
void menu() {
cout << "------------【学生信息管理系统】--------------\n";
cout << "\t\t0.退出系统\n";
cout << "\t\t1.录入信息\n";
cout << "\t\t2.浏览信息\n";
cout << "\t\t3.删除信息\n";
cout << "\t\t4.查找信息\n";
cout << "\t\t5.修改信息\n";
cout << "--------------------------------------------------\n";
}
float Total(Student stu) {
return (stu.chinese + stu.computer + stu.matheMatics);
}
void Print(Student stu) {
cout << "姓名" << '\t' << "学号" << '\t' << "语文" << '\t' << "数学" << '\t' << "计算机" << '\t' << "总分" << endl;;
cout << stu.name << '\t' << stu.stuNum << '\t' << stu.chinese << '\t' << stu.matheMatics << '\t' << stu.computer << '\t' << Total(stu);
}
int main() {
SeqList<Student> a;
Student stu;
Student s1;
stu.name = 'k';
stu.stuNum = 1;
stu.chinese = 90;
stu.computer = 80;
stu.matheMatics = 70;
menu();
while (1) {
int chioce;
cin >> chioce;
switch (chioce) {
case 0:
cout << "正常退出!";
exit(0);
break;
case 1:
cout << "-------------------【录入信息】--------\n";
cin >> s1.name;
cin >> s1.stuNum;
cin >> s1.chinese;
cin >> s1.computer;
cin >> s1.matheMatics;
a.Insert(1, stu);
cout << endl;
a.Insert(2, s1);
break;
case 2:
cout << "-------------------【浏览信息】--------\n";
cout << "姓名" << '\t' << "学号" << '\t' << "语文" << '\t' << "数学" << '\t' << "计算机" << '\t' << "总分" << endl;
a.PrintList();
break;
case 3:
cout << "-------------------【删除信息】--------\n";
int k;
cin >> k;
a.Delete(k);
break;
case 4:
cout << "-------------------【查找信息】--------\n";
int m;
cin >> m;
a.Get(m);
break;
case 5:
cout << "-------------------【修改信息】--------\n";
int newchinese,newmatheMatics,newcomputer,i;
cin >> i;
cin >> newchinese;
cin >> newcomputer;
cin >> newmatheMatics;
s1.chinese = newchinese;
s1.computer = newcomputer;
s1.matheMatics = newmatheMatics;
a.PrintList();
break;
default:
break;
}
//system("cls");
}
}