ss
《面向对象程序设计》课程实验报告
学生成绩管理系统
班级 计算机应用091班 姓名唐俊驰 学号 2009052023 日期 2010-12-26
一、 需求分析
1. 程序的功能:学生数据的录入、添加、修改与删除;成绩统计并排名;学生数据的读取与存储等基本功能。
2. 输入输出的要求:输入学生的成绩及处理要求;输出自己想得到的结果
3. 测试数据。 zhang 123 58 69 74
wang 456 78 96 58
yang 789 89 68 59
二、 详细设计
1. 程序框架设计:
设置了添加、修改、删除、查找、排序和对数据的读取、保存等功能。
2. 程序详细设计
添加:创建一个新的分类,用输入的变量初始化这个类,并用指针链接上一个对象。
修改:先通过链表找到要修改的对象,在输入要修改的值,利用类内函数对值进行修改。
删除:根据输入的条件找到要删除的对象,将此对象的next指针赋值给上一个对象的next值。
查找:按照学号或者姓名按链表查找对象,找到后输出该对象的基本信息。
排序:利用比较法,按照要求的标准进行排序。
数据的读取和保存:文件的打开和关闭,并按照存储的格式读取。
三、 调试分析
1. 调试中遇到的问题及对问题的解决方法
四、 使用说明及测试结果
1. 系统配置:Windows XP SP3;VC 6.0
2. 系统运行效果图
五、 源程序(带注释)
1.程序代码
/*stu.cpp*/
#include<iostream>
#include ".\ClassHead.h"
using namespace std;
void main() {
char c;
cla a;
do {
cout<<"\n score systerm:\n";
cout<<" 1……add \n";
cout<<" 2……delete \n";
cout<<" 3……change \n";
cout<<" 4……look for\n";
cout<<" 5……paixu\n";
cout<<" 6……save \n";
cout<<" 7……read \n";
cout<<" 8……back \n";
cout<<"choose(1-8):";
cin>>c;
switch(c) {
case '1':
a.sadd();
break;
case '2':
a.sremove();
break;
case '3':
a.samend();
break;
case '4':
a.ssearch();
break;
case '5':
a.staxis();
break;
case '6':
a.ssave();
break;
case '7':
a.sload();
break;
}
}while(c!='8');
}
/*类的定义及其成员函数的实现*/
#ifndef _CLASSHEAD_H
#define _CLASSHEAD_H
#include<iomanip>
#include<fstream>
#include<string>
#define max 80;
using namespace std;
class student {
public:
student *next;
public:
string name;
long num;
int x,y,z;
int AA;
void play() {
cout << name << "student's numbers" << num << ",math:" << x
<< ",language:" << y << ",english:" << z << ",in total:" << AA << endl;
};
student(string sname,long snum,int sx,int sy,int sz) {
name = sname;
num = snum;
x = sx;
y = sy;
z = sz;
}
};
class cla {
public:
cla() {
stu = 0;
sload();
}
~cla() {
student *p;
p = stu;
while(p) {
p = p->next;
delete stu;
stu = p;
}
stu = 0;
}
void sadd();
void sremove();
void samend();
void ssearch();
void staxis();
void ssave();
void sload();
void pxh();
void psx();
void pyw();
void pyy();
void pAA();
private:
student *stu;
};
void cla::sadd() {
student *q;
string name1;
long num1;
int x1,y1,z1;
system("cls");
cout << "\n **add student** \n" << endl;
cout << "please input:" << endl;
cout << "name\tnumber\tmath\tlanguage\tenglish:" << endl;
cin >> name1 >> num1 >> x1 >> y1 >> z1;
q = new student(name1,num1,x1,y1,z1);
q->next = 0;
q->AA = x1 + y1 + z1;
if(stu) {
student *t;
t = stu;
if(t->num == num1) {
cout << "number has already been recorded,please input again" << endl;
return;
}
while(t->next) {
if(t->num == num1) {
cout << "number has already been recorded,please input again" << endl;
return;
}
t = t->next;
}
t->next = q;
}
else {
stu = q;
}
cout << "down" << endl;
}
void cla::sremove() {
system("cls");
int num1;
cout << "\n** delete **\n";
cout << "please input the number you want to delete:";
cin >> num1;
student *p1,*p2;
p1 = stu;
while(p1) {
if(p1->num == num1)
break;
else {
p2 = p1;
p1 = p1->next;
}
}
if(p1 != NULL) {
p1->play();
cout << "[Y/N]" << endl;
char c;
cin >> c;
if(toupper(c)!='Y')
return;
if(p1==stu) {
stu = p1->next;
delete p1;
}
else {
p2->next = p1->next;
delete p1;
}
cout << "find the number is:" << num1 << " delete\n";
}
else
cout << "there's no information about that!\n";
}
void cla::samend() {
system("cls");
long num1;
cout << "\n** change the student number**\n";
cout << "input the changing number";
cin >> num1;
student *p1,*p2;
p1 = stu;
while(p1) {
if(p1->num == num1)
break;
else {
p2 = p1;
p1 = p1->next;
}
}
if(p1!=NULL) {
cout << "the number is" << num1 << "'information" << endl;
cout << "name "<<p1->name<<"math"<<p1->x<<"language"<<p1->y<<"english"<<p1->z<<endl;
cout<<"please the changing information:name math language english"<<endl;
cin>>p1->name>>p1->x>>p1->y>>p1->z;
p1->AA=p1->x+p1->y+p1->z;
cout<<"change suceed"<<endl;
}
else
cout<<"no found!\n";
}
void cla::ssearch() {
system("cls");
cout<<"\n** look for **\n"<<endl;
cout<<"please input the way:"<<endl;
cout<<"1.number"<<endl;
cout<<"2.name"<<endl;
cout<<"3.return"<<endl;
char c;
cin>>c;
switch (c) {
case '1':
{
long num1;
cout<<"the number"<<endl;
cin>>num1;
student *p1,*p2;
p1=stu;
while(p1) {
if(p1->num==num1)
break;
else {
p2=p1;
p1=p1->next;
}
}
if(p1!=NULL) {
cout<<"muber is"<<num1<<" information"<<endl;
cout<<"name:"<<p1->name<<" math:"<<p1->x<<" language:"<<p1->y<<" english:"<<p1->z<<endl;
cout<<"down";
}
else
cout<<"no foud!\n";
break;
}
case '2':
{
string name1;
cout<<"the name"<<endl;
cin>>name1;
student *p1,*p2;
p1=stu;
while(p1) {
if(p1->name==name1)
break;
else {
p2=p1;
p1=p1->next;
}
}
if(p1!=NULL) {
cout<<name1<<"information"<<endl;
cout<<"number:"<<p1->num<<" math:"<<p1->x<<" language:"<<p1->y<<" english:"<<p1->z<<endl;
cout<<"down...";
}
else
cout<<"no foud!\n";
break;
}
case '3':
return;
}
}
void cla::pxh() {
student *p1,*p2;
int n;
p1=stu;
n=1;
while(p1->next) {
n++;
p1=p1->next;
}
cout<<"have"<<n<<"message..."<<endl;
int i;
p1=stu;
for(i=1;i<n;i++) {
p1=stu;
if (p1->num>p1->next->num) {
p2=p1->next;
p1->next=p1->next->next;
p2->next=p1;
stu=p2;
}
p1=stu;
while(p1->next->next) {
p2=p1;
p1=p1->next;
if(p1->num>p1->next->num) {
p2->next=p1->next;
p1->next=p1->next->next;
p2->next->next=p1;
p1=p2->next;
}
}
}
p1=stu;
do {
p1->play();
p1=p1->next;
}while(p1);
}
void cla::psx() {
student *p1,*p2;
int n;
p1=stu;
n=1;
while(p1->next) {
n++;
p1=p1->next;
}
cout<<"have"<<n<<"message..."<<endl;
int i;
p1=stu;
for(i=1;i<n;i++) {
p1=stu;
if (p1->x>p1->next->x) {
p2=p1->next;
p1->next=p1->next->next;
p2->next=p1;
stu=p2;
}
p1=stu;
while(p1->next->next) {
p2=p1;
p1=p1->next;
if(p1->x>p1->next->x) {
p2->next=p1->next;
p1->next=p1->next->next;
p2->next->next=p1;
p1=p2->next;
}
}
}
p1=stu;
do {
p1->play();
p1=p1->next;
}while(p1);
}
void cla::pyw() {
student *p1,*p2;
int n;
p1=stu;
n=1;
while(p1->next) {
n++;
p1=p1->next;
}
cout<<"have"<<n<<"message..."<<endl;
int i;
p1=stu;
for(i=1;i<n;i++) {
p1=stu;
if (p1->y>p1->next->y) {
p2=p1->next;
p1->next=p1->next->next;
p2->next=p1;
stu=p2;
}
p1=stu;
while(p1->next->next) {
p2=p1;
p1=p1->next;
if(p1->y>p1->next->y) {
p2->next=p1->next;
p1->next=p1->next->next;
p2->next->next=p1;
p1=p2->next;
}
}
}
p1=stu;
do {
p1->play();
p1=p1->next;
}while(p1);
}
void cla::pyy() {
student *p1,*p2;
int n;
p1=stu;
n=1;
while(p1->next) {
n++;
p1=p1->next;
}
cout<<"have"<<n<<"message..."<<endl;
int i;
p1=stu;
for(i=1;i<n;i++) {
p1=stu;
if (p1->z>p1->next->z) {
p2=p1->next;
p1->next=p1->next->next;
p2->next=p1;
stu=p2;
}
p1=stu;
while(p1->next->next) {
p2=p1;
p1=p1->next;
if(p1->z>p1->next->z) {
p2->next=p1->next;
p1->next=p1->next->next;
p2->next->next=p1;
p1=p2->next;
}
}
}
p1=stu;
do {
p1->play();
p1=p1->next;
}while(p1);
}
void cla::pAA() {
student *p1,*p2;
int n;
p1=stu;
n=1;
while(p1->next) {
n++;
p1=p1->next;
}
cout<<"have"<<n<<"message..."<<endl;
int i;
p1=stu;
for(i=1;i<n;i++) {
p1=stu;
if (p1->AA>p1->next->AA) {
p2=p1->next;
p1->next=p1->next->next;
p2->next=p1; //头结点交换
stu=p2;
}
p1=stu;
while(p1->next->next) { //中间的交换
p2=p1;
p1=p1->next;
if(p1->AA>p1->next->AA) {
p2->next=p1->next;
p1->next=p1->next->next;
p2->next->next=p1;
p1=p2->next; //交换
}
}
}
p1=stu;
do {
p1->play();
p1=p1->next;
}while(p1);
}
void cla::staxis() { //排序
system("cls");
char c;
cout<<"请选择以何种方式排序:"<<endl;
cout<<"1……number"<<endl;
cout<<"2……math"<<endl;
cout<<"3……language"<<endl;
cout<<"4……english"<<endl;
cout<<"5……total"<<endl;
cout<<"6……return"<<endl;
cout<<"choose(1-6)"<<endl;
cin>>c;
switch (c) {
case '1':pxh(); break;
case '2':psx(); break;
case '3':pyw(); break;
case '4':pyy(); break;
case '5':pAA(); break;
case '6':return;
}
}
void cla::ssave() {
system("cls");
char c;
cout<<"\nsafe,[Y/N]:";
cin>>c;
if(toupper(c)!='Y')
return;
ofstream tfile("date.txt",ios_base::binary);
student *p=stu;
while(p) {
tfile<<p->name<<"\t"<<p->num<<"\t"<<p->x<<"\t"<<p->y<<"\t"<<p->z;
tfile<<endl;
p=p->next;
}
tfile.close();
cout<<"down..."<<endl;
}
void cla::sload() {
student *p;
p=stu;
while(p) {
stu=p->next;
delete p;
p=stu;
}
ifstream tfile("date.txt",ios_base::binary);
string name1;
long num1;
int x1,y1,z1;
tfile>>name1>>num1>>x1>>y1>>z1;
while(tfile.good()) {
student *s;
s=stu;
s=new student(name1,num1,x1,y1,z1);
s->next=0;
s->AA=x1+y1+z1;
if(stu) {
student *p2;
p2=stu;
while(p2->next) {
p2=p2->next;
}
p2->next=s;
}
else {
stu=s;
}
tfile>>name1>>num1>>x1>>y1>>z1;
}
tfile.close();
cout<<"\ninformation has down...\n";
}
#endif