本人是菜鸟,所以编的比较简单,这里是我一次小尝试,希望大家可以批评指正,本菜鸟将不胜感激。
图1 输入数据和操作命令界面
图2 计算均值列成绩清单命令
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
using std::string;
string s,name,code,name1,code1;
char comd;
float chinese,english,math,tchinese = 0,tenglish = 0,tmath = 0;
int i = 0;
class studentrecord
{
private:
string name;
string code;
float chinese;
float english;
float math;
float total;
float average;
public:
studentrecord(){}
studentrecord(string n,string co,float c,float e,float m):name(n),code(co),chinese(c),english(e),math(m)
{
total = c + e + m;
average = total/3;
}
string getname()
{return name;}
string getcode()
{return code;}
float getchinese()
{return chinese;}
float getenglish()
{return english;}
float getmath()
{return math;}
float gettotal()
{return total;}
void print()
{
cout << "---------------------------------------------" << endl;
cout << "Name : " << name << endl;
cout << "Code : " << code << endl;
cout << "Marks : " << endl;
cout << " Chinese : " << chinese << endl;
cout << " English : " << english << endl;
cout << " Math : " << math << endl;
cout << "Total : " << total << endl;
cout << "Average : " << average << endl;
cout << "---------------------------------------------" << endl;
}
};
void list(studentrecord *pobj,int i); //声明列学生清单的函数
void fname(studentrecord *pobj,int i); //声明按名字查找学生的函数
void fcode(studentrecord *pobj,int i); //声明按学号查找学生的函数
void getaverage(studentrecord *pobj,int i); //声明计算平均成绩的函数
void sortlist(int i); //声明按总分排序后输出清单的函数
void getfirst(int k,int i); //排序函数
studentrecord obj[20]; //定义20个studentrecord的对象
int _tmain(int argc, _TCHAR* argv[])
{
cout << "***********************************" << endl;
cout << " 学生成绩管理程 " << endl;
cout << " 制作人:Dragonfly " << endl;
cout << " 谢谢使用!!! " << endl;
cout << "***********************************" << endl;
do //输入学生信息阶段
{
if(s == "end") break;
cout << "Please enter the Name : ";
cin >> name;
cout << "Please enter the Code : ";
cin >> code;
cout << "Please enter the Chinese mark : ";
cin >> chinese;
cout << "Please enter the English mark : ";
cin >> english;
cout << "Please enter the Math mark : ";
cin >> math;
obj[i] = studentrecord(name,code,chinese,english,math);
i++;
cout << "Pass any key to continue......enter end to exit : ";
}while(cin >> s);
cout << "----------------------------------------------------------" << endl;
cout << "Now you can input a command to manage the record." << endl;
cout << "l : list all the record." << endl;
cout << "n : search record by student's name." << endl;
cout << "c : search record by student's code." << endl;
cout << "m : mean of the marks." << endl;
cout << "s : sort and list the record by the total." << endl;
while(cin >> comd) //输入完学生信息开始学生信息处理
{
switch(comd)
{
case 'l':
list(obj,i);
break;
case 'n':
fname(obj,i);
break;
case 'c':
fcode(obj,i);
break;
case 'm':
getaverage(obj,i);
break;
case 's':
sortlist(i);
}
}
return 0;
}
void list(studentrecord *pobj,int i)
{
for(int j = 0;j<i;j++)
{
pobj -> print();
pobj ++;
}
cout << "Please enter the command : ";
}
void fname(studentrecord *pobj,int i)
{
cout << "Please enter the name : ";
cin >> name1;
for(int j = 0;j<i;j++)
{
if (pobj -> getname() == name1)
{
pobj -> print();
break;
}
pobj ++;
}
cout << "Please enter the command : ";
}
void fcode(studentrecord *pobj,int i)
{
cout << "Please enter the code : ";
cin >> code1;
for(int j = 0;j<i;j++)
{
if (pobj -> getcode() == code1)
{
pobj -> print();
break;
}
pobj ++;
}
cout << "Please enter the command : ";
}
void getaverage(studentrecord *pobj,int i)
{
tchinese = 0;
tenglish = 0;
tmath = 0;
for (int j = 0;j<i;j++)
{
tchinese += pobj -> getchinese();
tenglish += pobj -> getenglish();
tmath += pobj -> getmath();
pobj ++;
}
cout << "---------------------------------------------" << endl;
cout << "Chinese 's average is : " << (tchinese/i) << endl;
cout << "English 's average is : " << (tenglish/i) << endl;
cout << "Math 's average is : " << (tmath/i) << endl;
cout << "---------------------------------------------" << endl;
cout << "Please enter the command : ";
}
void sortlist(int i)
{
for(int k = 0;k < i;k++)
getfirst(k,i);
cout << "Please enter the command : ";
}
void getfirst(int k,int i)
{
for(int j = k + 1;j < i;j++)
{
if(obj[k].gettotal() < obj[j].gettotal())
{
studentrecord temp;
temp = obj[k];
obj[k] = obj[j];
obj[j] = temp;
}
}
cout << "第" << (k+1) << "名 : " << obj[k].getname() << endl;
obj[k].print();
}