|
谢谢大家的参与,我的程序已经更新完毕,希望大家提出宝贵意见
//main函数
#include<iostream.h>
#include <cstdlib>
#include"need.h"
void main()
{
int n;
cout<<endl<<endl;
cout<<"欢迎登陆学员成绩管理系统!!"<<endl<<endl;
cout<<"*****************************************"<<endl<<endl;
cout<<"首先从文件中读取学员信息(学号/姓名/成绩)..."<<endl<<endl;
enregister<stu>();
cout<<"读取完毕"<<endl;
cout<<"-----------------------------------------"<<endl;
cout<<"您能进行的操作如下:"<<endl;
cout<<endl<<endl;
lable:
cout<<"1.打印成绩单"<<endl;
cout<<"2.学员查找"<<endl;
cout<<"3.插入学员成绩"<<endl;
cout<<"4.删除学员"<<endl;
cout<<"5.学员排序"<<endl;
cout<<"0.退出系统"<<endl<<endl;
cout<<"请选择您要进行的操作(数字1-5):";
{
cin>>n;
switch(n)
{
case 1:
cout<<"\n学生成绩单如下"<<endl;
cout<<"-----------------------------------------"<<endl;
print<stu>();
cout<<"-----------------------------------------"<<endl;
cout<<"\n打印完毕,请选择其他操作:"<<endl;
goto lable;
case 2:
findstudent<stu>();
cout<<"查找完毕,请选择其他操作:"<<endl;
goto lable;
case 3:
insert<stu>();
cout<<"-----------------------------------------"<<endl<<endl;
cout<<"修改完毕,请选择其他操作:"<<endl;
goto lable;
case 4:
cout<<"删除学员信息..."<<endl<<endl;
deletestudent<stu>();
cout<<"清理完毕,请选择其他操作:"<<endl;
goto lable;
case 5:
inorder<stu>();
cout<<"排序完毕,请选择其他操作:"<<endl<<endl;
goto lable;
case 0:
cout<<"\n退出本系统,欢迎下次使用,再见!!!"<<endl;
cout<<"*****************************************"<<endl<<endl;
break;
default:
cout<<"输入字母错误,请重新选择:"<<endl;
goto lable;
}
}
}
\\need.h函数
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#include<fstream.h>
#include"cnode.h"
struct stu
{
char num[30];
char name[30];
double score;
};
CNode<stu>*head=new CNode<stu>();
template<class T>
void enregister(void)
{
ifstream in;
in.open("dengji.txt");
if(in.fail())
{
cerr<<"Error is happen,founction closed!";
exit(-1);
}
CNode<T>*p,*ptr;
ptr=head;
struct stu std;
while(in>>std.num)
{
in>>std.name;
in>>std.score;
p=new CNode<T>(std);
ptr->InsertAfter(p);
ptr=p;
}
in.close();
}
template<class T>
void print(void)
{
CNode<T>*ptr;
struct stu stp;
ptr=head->NextNode();
cout<<"学号"<<" "<<"姓名"<<" "<<"成绩"<<endl;
while(ptr!=head)
{
stp=ptr->data;
cout<<setw(7)<<setiosflags(ios::left)<<stp.num<<" ";
cout<<setw(10)<<setiosflags(ios::left)<<stp.name<<" "<<setw(7)<<setiosflags(ios::left)<<stp.score;
cout<<resetiosflags(ios::left);
ptr=ptr->NextNode();
cout<<endl;
}
}
template<class T>
void findstudent()
{
struct stu stp;
char n[30];
char m='y';
CNode<T>*ptr=head->NextNode();
while(m!='n')
{
cout<<endl;
cout<<"输入需要查找学员的学号:";
cin>>n;
stp=ptr->data;
while(ptr!=head && strcmp(n,stp.num))
{
ptr=ptr->NextNode();
stp=ptr->data;
}
if(ptr==head)
{
cout<<"-----------------------------------------"<<endl;
cout<<"这个学员不存在。"<<endl;
cout<<"-----------------------------------------"<<endl;
}
else
{
cout<<"-----------------------------------------"<<endl;
cout<<stp.num<<" ";
cout<<stp.name<<" "<<stp.score<<endl;
cout<<"-----------------------------------------"<<endl;
}
ptr=head->NextNode();
cout<<"是否继续进行查找(y/n):";
cin>>m;
}
cout<<endl;
}
template<class T>
void insert(void)
{
ofstream out;
out.open("shuchu.txt");
struct stu std;
CNode<T>*ptr,*p,*q;
double n;
char m='y';
ptr=head->NextNode();
q=head;
if(out.fail())
{
cerr<<"Error is happen,founction closed!";
exit(-1);
}
while(m!='n')
{
cout<<"输入需要插入学员的信息..."<<endl;
cout<<"学号:";
cin>>std.num;
cout<<"姓名:";
cin>>std.name;
cout<<"成绩:";
cin>>std.score;
p=new CNode<T>(std);
n=std.score;
while(ptr!=head&&n>ptr->data.score)
{
q=ptr;
ptr=ptr->NextNode();
}
q->InsertAfter(p);
ptr=head->NextNode();
for(p=head->NextNode();p!=head;p=p->NextNode())
{
out<<setw(7)<<setiosflags(ios::left)<<p->data.num;
out<<setw(10)<<setiosflags(ios::left)<<p->data.name;
out<<setw(7)<<setiosflags(ios::left)<<p->data.score<<endl;
out<<resetiosflags(ios::left);
}
out.close();
cout<<"是否继续进行插入(y/n):";
cin>>m;
}
cout<<endl;
}
template<class T>
void deletestudent(void)
{
struct stu ;
CNode<T>*ptr=head->NextNode(),*p;
char n[30]="1";
char m='y';
while(m!='n')
{
p=head;
cout<<"输入要删除学员的学号:";
cin>>n;
while(ptr!=head && strcmp(n,ptr->data.num))
{
p=ptr;
ptr=ptr->NextNode();
}
cout<<"\n-----------------------------------------"<<endl;
if(ptr==head)
cout<<"这个学员不存在。";
else
{
p->DeleteAfter();
cout<<"学员"<<n<<"被删除";
}
cout<<"\n-----------------------------------------"<<endl;
ptr=head->NextNode();
cout<<endl;
cout<<"是否继续进行删除(y/n):";
cin>>m;
}
cout<<endl;
}
template<class T>
void inorder()
{
CNode<T>*ptr,*p;
double m,n;
struct stu st;
for(ptr=head->NextNode();ptr->NextNode()!=head;ptr=ptr->NextNode())
for(p=ptr->NextNode();p!=head;p=p->NextNode())
{
m=ptr->data.score;n=p->data.score;
if(m>n)
{
st=ptr->data;ptr->data=p->data;p->data=st;
}
}
cout<<endl;
}
\\cnode.h函数
#ifndef CIRCULAR_NODE_CLASS
#define CIRCULAR_NODE_CLASS
template <class T>
class CNode
{
private:
// circular link to the next node
CNode<T> *next;
public:
// data is public
T data;
// constructors
CNode(void);
CNode (const T& item);
// list modification methods
void InsertAfter(CNode<T> *p);
CNode<T> *DeleteAfter(void);
// obtain the address of the next node
CNode<T> *NextNode(void) const;
};
// constructor that creates an empty list and
// leaves the data uninitialized. use for header
template <class T>
CNode<T>::CNode(void)
{
// initialize the node so it points to itself
next = this;
}
// constructor that creates an empty list and initializes data
template <class T>
CNode<T>::CNode(const T& item)
{
// set node to point to itself and initialize data
next = this;
data = item;
}
// return pointer to the next node
template <class T>
CNode<T> *CNode<T>::NextNode(void) const
{
return next;
}
// insert a node p after the current one
template <class T>
void CNode<T>::InsertAfter(CNode<T> *p)
{
// p points to successor of the current node, and current node
// points to p.
p->next = next;
next = p;
}
// delete the node following current and return its address
template <class T>
CNode<T> *CNode<T>::DeleteAfter(void)
{
// save address of node to be deleted
CNode<T> *tempPtr = next;
// if next is the address of current object (this), we are
// pointing to ourself. We don't delete ourself! return NULL
if (next == this)
return NULL;
// current node points to successor of tempPtr.
next = tempPtr->next;
// return the pointer to the unlinked node
return tempPtr;
}
#endif // CIRCULAR_NODE_CLASS
\\TXT文件
\\"dengji.txt"
07703 刘志海 67
06404 王帅 57
06005 尹浩 76
70006 刘松杨 88
44007 徐国旺 77
44009 孙晶晶 97
06010 何燕辉 33
04012 车少君 68
44015 刘兆洋 66
更新的内容
1。输入采用重记事本输入
2。优化了结构,程序更加人性化
希望大家能多提出宝贵意见谢谢!! |
|