1.最近有些同学的课设是做一个学生管理系统,有使用c++的,有用c的,也有可视化的。来找我的时候代码逻辑很乱这里我来写一个简单的demo,其他的我也都有,有需要的可以联系QQ:1356931781.(c语言的,QT基于文件实现的,QT基于SQ实现的)。废话不多说,我直接上代码。
#include"stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include<iomanip>
using namespace std;
class Student//学生类
{
public:
Student() {};//默认构造函数
~Student();//析构函数
void Input();//输入学生信息
void Output();//输出学生信息
void Delete();//删除学生信息
void Search();//查找学生信息
void Change();//修改学生信息
void Scort_by_chinese();//对学生的语文成绩排序
void Scort_by_math(); //对学生的数学成绩排序
void Scort_by_English();//对学生的英语成绩排序
void Scort_by_Total();//对学生的总分排序
int display_all();
void load();//加载数据
void save();//保存数据
private:
Student * St; //学生类指针
int Size; //学生的人数
string Name; //姓名
int Age; //年龄
int No; //学号
float Score[3];//三科的成绩。分别为语文数学英语
float Total; //总分
float Ave; //平均分
};
//析构函数
Student::~Student()
{
delete(St);
}
//输入学生的信息.
void Student::Input()
{
int len;//学生的人数
cout << "请输入学生的人数:";
cin >> len;
system("cls");
Size = len;
St = new Student[Size];
for (int i = 0; i<len; i++)
{
cout << "请输入第" << i + 1 << "个学生的姓名:";
cin >> St[i].Name;
cout << "请输入第" << i + 1 << "个学生的年龄:";
cin >> St[i].Age;
cout << "请输入第" << i + 1 << "个学生的学号:";
cin >> St[i].No;
cout << "请输入第" << i + 1 << "个学生的语文成绩:";
cin >> St[i].Score[0];
cout << "请输入第" << i + 1 << "个学生的数学成绩:";
cin >> St[i].Score[1];
cout << "请输入第" << i + 1 << "个学生的英语成绩:";
cin >> St[i].Score[2];
St[i].Total = St[i].Score[0] + St[i].Score[1] + St[i].Score[2];
St[i].Ave = St[i].Total / 3.0f;
system("cls");
}
}
//输出学生的信息
void Student::Output()
{
cout << "姓名 年龄 学号 语文 数学 英语 总分 平均分" << endl;
for (int i = 0; i<Size; i++)
{
cout << St[i].Name << " " << St[i].Age << " " << St[i].No << " " << St[i].Score[0] << " " << St[i].Score[1] << " " << St[i].Score[2] << " "
<< St[i].Score[2] << " " << St[i].Total << " " << St[i].Ave << endl;
}
}
void Student::Delete()//删除学生信息
{
string str;
cout << "请输入你需要删除的学生的姓名:";
cin >> str;
int num;//标记姓名相等时的下标
//寻找姓名相等时的下标
for (int i = 0; i<Size; i++)
{
//当姓名相等时
if (str == St[i].Name)
{
num = i;
}
}
//将后面的学生向前移
for (int j = num + 1; j<Size; j++)
{
St[j - 1] = St[j];
}
Size -= 1;//学生人数减少一个
}
void Student::Search()//查找学生信息
{
string name;
cout << "请输入你需要查找的学生的姓名:";
cin >> name;
int i;
for (i = 0; i<Size; i++)
{
if (name == St[i].Name)
{
break;
}
}
cout << St[i].Name << " " << St[i].Age << " " << St[i].No << " " << St[i].Score[0] << " " << St[i].Score[1] << " "
<< St[i].Score[2] << " " << St[i].Total << " " << St[i].Ave << endl;
}
void Student::Change()//修改学生信息
{
string name;
cout << "请输入你需要修改的学生的姓名:";
cin >> name;
int i;
for (i = 0; i<Size; i++)
{
if (name == St[i].Name)
{
break;
}
}
cout << "修改前学生的信息:" << endl;
cout << St[i].Name << " " << St[i].Age << " " << St[i].No << " " << St[i].Score[0] << " " << St[i].Score[1] << " "
<< St[i].Score[2] << " " << St[i].Total << " " << St[i].Ave << endl << endl;
cout << "请输入学生的姓名:";
cin >> St[i].Name;
cout << "请输入学生的年龄:";
cin >> St[i].Age;
cout << "请输入第个学生的学号:";
cin >> St[i].No;
cout << "请输入学生的语文成绩:";
cin >> St[i].Score[0];
cout << "请输入学生的数学成绩:";
cin >> St[i].Score[1];
cout << "请输入第学生的英语成绩:";
cin >> St[i].Score[2];
St[i].Total = St[i].Score[0] + St[i].Score[1] + St[i].Score[2];
St[i].Ave = St[i].Total / 3.0f;
}
void Student::Scort_by_chinese(void)//对学生的语文成绩排序
{
//提供插入数组中的数据
for (int i = 1; i<Size; i++)
{
int j = i - 1;
//插入数组中的数据
Student temp = St[i];
while (temp.Score[0] > St[j].Score[0] && j >= 0)
{
St[j + 1] = St[j];
j--;
}
St[++j] = temp;
}
}
void Student::Scort_by_math(void)//对学生的数学成绩排序
{
//提供插入数组中的数据
for (int i = 1; i<Size; i++)
{
int j = i - 1;
//插入数组中的数据
Student temp = St[i];
while (temp.Score[1] > St[j].Score[1] && j >= 0)
{
St[j + 1] = St[j];
j--;
}
St[++j] = temp;
}
}
void Student::Scort_by_English(void)//对学生的英语成绩排序
{
//提供插入数组中的数据
for (int i = 1; i<Size; i++)
{
int j = i - 1;
//插入数组中的数据
Student temp = St[i];
while (temp.Score[2] > St[j].Score[2] && j >= 0)
{
St[j + 1] = St[j];
j--;
}
St[++j] = temp;
}
}
void Student::Scort_by_Total(void)//对学生的总分排序
{
//提供插入数组中的数据
for (int i = 1; i<Size; i++)
{
int j = i - 1;
//插入数组中的数据
Student temp = St[i];
while (temp.Total > St[j].Total && j >= 0)
{
St[j + 1] = St[j];
j--;
}
St[++j] = temp;
}
}
int Student::display_all(){
if (Size == 0) {
cout << "当前学生数量为空!" << endl;
return 0;
}
cout << "姓名"<<setw(8)<<" 年龄 "<<setw(8)<<"学号"<<setw(8)<<"语文"<<setw(8)<<"数学"<<setw(8)<<"英语"<<setw(8)<<"总分"<<setw(8)<<"平均分" << endl;
for (int i = 0; i < Size; i++) {
cout << St[i].Name << setw(8) << St[i].Age << setw(8) << St[i].No << setw(8) << St[i].Score[0] << setw(8) <<
St[i].Score[1] << setw(8) << St[i].Score[2] << setw(8) << St[i].Total << setw(8) << St[i].Ave << endl;
}
return 0;
}
/*导航操作*/
void banner() {
cout << "================================================================================\n\n";
cout << "------------------------ 请选择要操作的命令:-----------------------------------\n\n";
cout << "-------------------------- 1 输入学生信息--------------------------------------\n\n";
cout << "-------------------------- 2 输出学生信息--------------------------------------\n\n";
cout << "-------------------------- 3 删除学生信息--------------------------------------\n\n";
cout << "-------------------------- 4 查找学生信息--------------------------------------\n\n";
cout << "-------------------------- 5 修改学生信息--------------------------------------\n\n";
cout << "-------------------------- 6 将学生的语文成绩按从大到小排----------------------\n\n";
cout << "-------------------------- 7 将学生的数学成绩按从大到小排----------------------\n\n";
cout << "-------------------------- 8 将学生的英语成绩按从大到小排----------------------\n\n";
cout << "-------------------------- 9 将学生的总成绩按从大到小排------------------------\n\n";
cout << "-------------------------- 10 显示当前所有学生----------------------------------\n\n";
cout << "================================================================================\n\n";
int Item;//操作命令
}
int main()
{
cout << "================================================================================\n" << endl;
cout << "================================================================================\n" << endl;
cout << "*************************欢迎使用学生成绩管理系统*******************************\n" << endl;
cout << "---------------------------------------------------------------------------------\n" << endl;
cout << "********************************************************************************\n" << endl;
cout << "================================================================================\n" << endl;
cout << "请按任意将进入学生管理系统:" << endl;
getchar();
system("cls");//清屏指令
banner();
int Item;//操作命令
Student st;//学生对象
while (1)
{
cout << "请选择操作命令:";
cin >> Item;
system("cls");//清屏
switch (Item)
{
case 1://输入学生信息
{
st.Input();
banner();
}
break;
case 2://输出学生信息
{
st.Output();
banner();
}
break;
case 3://删除学生信息
{
st.Delete();
banner();
}
break;
case 4://查找学生信息
{
st.Search();
banner();
}
break;
case 5://修改学生信息
{
st.Change();
banner();
}
break;
case 6://对学生的语文成绩排序
{
st.Scort_by_chinese();
st.Output();
banner();
}
break;
case 7://对学生的数学成绩排序
{
st.Scort_by_math();
st.Output();
banner();
}
break;
case 8://对学生的英语成绩排序
{
st.Scort_by_English();
st.Output();
banner();
}
break;
case 9://对学生的总分排序
{
st.Scort_by_Total();
st.Output();
banner();
}
case 10: {
st.display_all();
banner();
}
break;
default:
break;
}
}
system("pause");
}