l课程设计
2016-01-04 22:05
274 查看
#include <iomanip>
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <stdlib.h>
#include <string>
#include <process.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
struct student
{
char name[12]; /// 姓名
char id[10]; /// 学号
float Cppnum; /// c++分数
float Snum; /// 数学分数
float Enum; /// 英语分数
float number; /// 总分
float average; ///平均分
student *next;
};
///////////-------定义类-----------------------////////////
class stud{
student *p1,*p2,*head;
public:
~stud() /////----析构函数------------------------
{
while(head){
p1=head->next;
delete head;
head=p1;
}
}
///------------成员函数------------
void output(student *head); // 输出学生成绩
student * input(student *head); // 增加学生记录
student* del(student *head, char*p); // 删除记录
student* find(student *head,char *p,int &n); // 查找学生记录(可查找多个同名数据)
student* stat(student *head); //排序统计学生总分
friend void total(student*head);//统计学生总分
friend void pingjun(student*head);//统计平均分
student* insert(student *head); //按学生总分插入记录
student* clear(student *head); // 删除当前表
void Inputs(student *p); //用于添加数据的子函数
};//----------------------------------------------------
////---------用于添加数据的子函数-------///////
void stud::Inputs(student*p)
{
cout<<setw(6)<<"姓名"<<setw(8)<<" 学号"<<setw(8)<<"C++"<<setw(8)<<"数学"<<setw(8)<<"英语"<<endl;
cin>>p->name >>p->id;
cin >>p->Cppnum;
while(cin.fail()){
cerr<<"您的输入有误,请重新输入"<<endl;
cin.clear ();
cin.sync ();
cin>>p->Cppnum;
}
cin>>p->Snum;
while(cin.fail()){
cerr<<"您的输入有误,请重新输入"<<endl;
cin.sync ();
cin.clear ();
cin>>p->Snum;
}
cin>>p->Enum;
while(cin.fail()){
cerr<<"您的输入有误,请重新输入"<<endl;
cin.clear ();
cin.sync ();
cin>>p->Enum;
}
total(p); //计算出总分
pingjun(p);//计算平均分
}
////////-----输出学生成绩-----------------/////////////////////
void stud::output (student *head)
{
p1=head;
while(p1!=NULL){
cout<<setw(6)<<p1->name<<setw(8)<<p1->id<<setw(8)<<p1->Cppnum<<setw(8)<<p1->Snum <<setw(8)<<p1->Enum <<setw(7)<<p1->number<<setw(7)<<p1->average<<endl;
p1=p1->next ;
}
}
/////////------------插入学生成绩记录--------////////////////
student* stud::insert(student *head)
{
p1=new student;
Inputs(p1); //调用子函数 增加数据
p2=head;
student* p3=NULL;
while((p2->number < p1->number ) && p2->next !=NULL){
p3=p2;
p2=p2->next;
}
if(p2->number > p1->number){
p1->next=p2;
if(p3==NULL) // 若当前值是最小的
return p1;
p3->next =p1;
return head;
}
else {
p2->next=p1;
p1->next=NULL;
return head;
}
}
//////----------清空数据------------/////////////
student* stud::clear(student*head)
{
while(head){
p1=head->next ;
delete head;
head=p1;
}
return head;
}
//////////-----------排序统计函数-----------/////////////////
student *stud::stat(student *head)
{
p2=head;
p1=p2->next;
while(p2->next){ //冒泡泡法, 呵呵`~~~
if(p2->number > p1->number){ // 把头指针指向当前比较小的节点
p2->next=p1->next;
p1->next=head;
head=p1; // 把用于比较的两个指针复位
//p2=head;
p1=p2->next ;
}
else{ // 指向下一个节点
p2=p2->next ;
p1=p2->next ;
}//-------------------------------------------
}
cout<<"当前表以按学生总分排序成功"<<endl;
return head;
}
/////-----------删除记录-----------//////////////////////
student* stud::del (student *head,char *p)
{
p1=head;
p2=NULL;
while(strcmp(p1->name ,p)&& p1->next !=NULL){
p2=p1;
p1=p1->next ;
}
if(!strcmp(p1->name ,p)){
if(p1==head)
head=p1->next;
else
p2->next=p1->next ;
cout<<"删除成功,OK"<<endl;
delete p1;
}
else
cout<<" 没找到姓名"<<p<<"的学生.\n"; //结点没找到
return head ;
}
///////----------统计总分---------------///////////////
void total(student *p)
{
p->number = p->Cppnum + p->Snum + p->Enum;
}
//////---------计算平均分-------------////////////////
void pingjun(student *p)
{
p->average=(p->Cppnum+p->Snum+p->Enum)/3;
}
///////-------------查找函数----------///////////////////
student* stud::find (student *head,char *p,int& n)
{
p2=head;
while(strcmp(p2->name ,p) !=0 && p2->next !=NULL)
p2=p2->next ;
if(0==strcmp(p2->name,p)){
cout<<setw(6)<<p2->name<<setw(8)<<p2->id<<setw(8)<<p2->Cppnum<<setw(8)<<p2->Snum <<setw(8)<<p2->Enum <<setw(7)<<p2->number <<setw(10)<<p2->average<<endl;
n++;
return p2;
}
else if(n==0){
system("cls");
cout<<"对不起,没有您要查找的学生数据"<<endl;
}
return NULL;
}
///////----------------增加学生记录-----------////////////////////////////
student *stud::input (student *head)
{
p1=new student;
p2=head;
Inputs(p1); //调用子函数 增加数据
if(head ==NULL){
head=p1;
p1->next =NULL;
return head;
}
while(p2->next !=NULL)
p2=p2->next;
p2->next=p1;
p1->next=NULL;
return head;
}
//----------- 输出错误 -----------//////////
void error()
{
cout<<"错误,这还是一张空表,请输入数据"<<endl;
getch();
}
///////////////////------------main函数--------//////////////////-----------
int main()
{
stud stus;
student *head=NULL;
student *pd; //临时指针, 用于查找函数
char choice; //用于存放用户的选择项
char name[10]; //查找,删除记录的 key
while(1){
system("cls");
cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓";
cout<<"┃**************** ☆ 学 生 学 籍 成 绩 管 理 系 统 ☆ **************┃";
cout<<"┃********** ★★★★★ ★★★★★★★ ★★★★★ *********** ┃";
cout<<"┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫";
cout<<"┃****************★ ☆ 1.增加学生成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 2.显示学生成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 3.排序统计成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 4.查找学生成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 5.删除学生成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 6.插入学生成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 7.清空所有数据 ☆ ★****************┃";
cout<<"┃****************★ ☆ 8.安全退出系统 ☆ ★****************┃";
cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛";
cout<<" 请输入您的选择(0--8):";
cout<<endl;
int n=0; //计数器,用于在查找时计算有没有同名学生
cin>>choice;
fflush(stdin); //清空缓冲区
if(choice=='8'){ //安全退出
cout<<"谢谢使用,再见"<<endl;
exit(0);
}//------------------------------------------------
switch(choice){
case '1':
head=stus.input (head);
break;//------------------------------------------------
case '2':
if(head==NULL){
error();
break;
}
cout<<setw(6)<<"姓名"<<setw(8)<<" 学号"<<setw(8)<<"C++"<<setw(8)<<"数学"<<setw(8)<<"英语"<<setw(8)<<"总分"<<setw(10)<<"平均分"<<endl;
stus.output (head);
getch();
break;//------------------------------------------------
case '3':
if(head==NULL){
error();
break;
}
head=stus.stat(head);
getch();
break;//------------------------------------------------
case '4':
if(head ==NULL){
error(); //调用函数输出错误信息
break;
}
cout<<"请输入想要查找的学生姓名"<<" ,"<<"本系统可以查找重复姓名学生"<<endl;
cin>>name;
pd=head;
cout<<setw(6)<<"姓名"<<setw(8)<<" 学号"<<setw(8)<<"C++"<<setw(8)<<"数学"<<setw(8)<<"英语"<<setw(8)<<"总分"<<setw(10)<<"平均分"<<endl;
while(pd){ // 循环调用函数, 用于输出多个的同名学生成绩
pd=stus.find (pd,name,n);
if(pd==NULL)
break;
pd=pd->next ; //指针指向当前以找到的下一个节点,用于查找多个同名学生
}
getch();
break;//------------------------------------------------
case '5':
if(head==NULL){
error();
break;
}
cout<<"请输入想要删除学生姓名"<<endl;
cin>>name;
head=stus.del(head,name);
getch();
break;//------------------------------------------------
case '6':
if(head==NULL){
error();
break;
}
head=stus.stat (head);
head=stus.insert(head);
break;//-----------------------------------------------
case '7':
if(head==NULL){
error();
break;
}
head=stus.clear(head);
cout<<"删除表成功~"<<endl;
getch();
break;//-----------------------------------------------
default :
cout<<" 对不起,您的输入有误,请重新输入。\n";
getch();
break;
}//------------------------------------------------------
}
getch();
return 0;
}
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <stdlib.h>
#include <string>
#include <process.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
struct student
{
char name[12]; /// 姓名
char id[10]; /// 学号
float Cppnum; /// c++分数
float Snum; /// 数学分数
float Enum; /// 英语分数
float number; /// 总分
float average; ///平均分
student *next;
};
///////////-------定义类-----------------------////////////
class stud{
student *p1,*p2,*head;
public:
~stud() /////----析构函数------------------------
{
while(head){
p1=head->next;
delete head;
head=p1;
}
}
///------------成员函数------------
void output(student *head); // 输出学生成绩
student * input(student *head); // 增加学生记录
student* del(student *head, char*p); // 删除记录
student* find(student *head,char *p,int &n); // 查找学生记录(可查找多个同名数据)
student* stat(student *head); //排序统计学生总分
friend void total(student*head);//统计学生总分
friend void pingjun(student*head);//统计平均分
student* insert(student *head); //按学生总分插入记录
student* clear(student *head); // 删除当前表
void Inputs(student *p); //用于添加数据的子函数
};//----------------------------------------------------
////---------用于添加数据的子函数-------///////
void stud::Inputs(student*p)
{
cout<<setw(6)<<"姓名"<<setw(8)<<" 学号"<<setw(8)<<"C++"<<setw(8)<<"数学"<<setw(8)<<"英语"<<endl;
cin>>p->name >>p->id;
cin >>p->Cppnum;
while(cin.fail()){
cerr<<"您的输入有误,请重新输入"<<endl;
cin.clear ();
cin.sync ();
cin>>p->Cppnum;
}
cin>>p->Snum;
while(cin.fail()){
cerr<<"您的输入有误,请重新输入"<<endl;
cin.sync ();
cin.clear ();
cin>>p->Snum;
}
cin>>p->Enum;
while(cin.fail()){
cerr<<"您的输入有误,请重新输入"<<endl;
cin.clear ();
cin.sync ();
cin>>p->Enum;
}
total(p); //计算出总分
pingjun(p);//计算平均分
}
////////-----输出学生成绩-----------------/////////////////////
void stud::output (student *head)
{
p1=head;
while(p1!=NULL){
cout<<setw(6)<<p1->name<<setw(8)<<p1->id<<setw(8)<<p1->Cppnum<<setw(8)<<p1->Snum <<setw(8)<<p1->Enum <<setw(7)<<p1->number<<setw(7)<<p1->average<<endl;
p1=p1->next ;
}
}
/////////------------插入学生成绩记录--------////////////////
student* stud::insert(student *head)
{
p1=new student;
Inputs(p1); //调用子函数 增加数据
p2=head;
student* p3=NULL;
while((p2->number < p1->number ) && p2->next !=NULL){
p3=p2;
p2=p2->next;
}
if(p2->number > p1->number){
p1->next=p2;
if(p3==NULL) // 若当前值是最小的
return p1;
p3->next =p1;
return head;
}
else {
p2->next=p1;
p1->next=NULL;
return head;
}
}
//////----------清空数据------------/////////////
student* stud::clear(student*head)
{
while(head){
p1=head->next ;
delete head;
head=p1;
}
return head;
}
//////////-----------排序统计函数-----------/////////////////
student *stud::stat(student *head)
{
p2=head;
p1=p2->next;
while(p2->next){ //冒泡泡法, 呵呵`~~~
if(p2->number > p1->number){ // 把头指针指向当前比较小的节点
p2->next=p1->next;
p1->next=head;
head=p1; // 把用于比较的两个指针复位
//p2=head;
p1=p2->next ;
}
else{ // 指向下一个节点
p2=p2->next ;
p1=p2->next ;
}//-------------------------------------------
}
cout<<"当前表以按学生总分排序成功"<<endl;
return head;
}
/////-----------删除记录-----------//////////////////////
student* stud::del (student *head,char *p)
{
p1=head;
p2=NULL;
while(strcmp(p1->name ,p)&& p1->next !=NULL){
p2=p1;
p1=p1->next ;
}
if(!strcmp(p1->name ,p)){
if(p1==head)
head=p1->next;
else
p2->next=p1->next ;
cout<<"删除成功,OK"<<endl;
delete p1;
}
else
cout<<" 没找到姓名"<<p<<"的学生.\n"; //结点没找到
return head ;
}
///////----------统计总分---------------///////////////
void total(student *p)
{
p->number = p->Cppnum + p->Snum + p->Enum;
}
//////---------计算平均分-------------////////////////
void pingjun(student *p)
{
p->average=(p->Cppnum+p->Snum+p->Enum)/3;
}
///////-------------查找函数----------///////////////////
student* stud::find (student *head,char *p,int& n)
{
p2=head;
while(strcmp(p2->name ,p) !=0 && p2->next !=NULL)
p2=p2->next ;
if(0==strcmp(p2->name,p)){
cout<<setw(6)<<p2->name<<setw(8)<<p2->id<<setw(8)<<p2->Cppnum<<setw(8)<<p2->Snum <<setw(8)<<p2->Enum <<setw(7)<<p2->number <<setw(10)<<p2->average<<endl;
n++;
return p2;
}
else if(n==0){
system("cls");
cout<<"对不起,没有您要查找的学生数据"<<endl;
}
return NULL;
}
///////----------------增加学生记录-----------////////////////////////////
student *stud::input (student *head)
{
p1=new student;
p2=head;
Inputs(p1); //调用子函数 增加数据
if(head ==NULL){
head=p1;
p1->next =NULL;
return head;
}
while(p2->next !=NULL)
p2=p2->next;
p2->next=p1;
p1->next=NULL;
return head;
}
//----------- 输出错误 -----------//////////
void error()
{
cout<<"错误,这还是一张空表,请输入数据"<<endl;
getch();
}
///////////////////------------main函数--------//////////////////-----------
int main()
{
stud stus;
student *head=NULL;
student *pd; //临时指针, 用于查找函数
char choice; //用于存放用户的选择项
char name[10]; //查找,删除记录的 key
while(1){
system("cls");
cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓";
cout<<"┃**************** ☆ 学 生 学 籍 成 绩 管 理 系 统 ☆ **************┃";
cout<<"┃********** ★★★★★ ★★★★★★★ ★★★★★ *********** ┃";
cout<<"┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫";
cout<<"┃****************★ ☆ 1.增加学生成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 2.显示学生成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 3.排序统计成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 4.查找学生成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 5.删除学生成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 6.插入学生成绩 ☆ ★****************┃";
cout<<"┃****************★ ☆ 7.清空所有数据 ☆ ★****************┃";
cout<<"┃****************★ ☆ 8.安全退出系统 ☆ ★****************┃";
cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛";
cout<<" 请输入您的选择(0--8):";
cout<<endl;
int n=0; //计数器,用于在查找时计算有没有同名学生
cin>>choice;
fflush(stdin); //清空缓冲区
if(choice=='8'){ //安全退出
cout<<"谢谢使用,再见"<<endl;
exit(0);
}//------------------------------------------------
switch(choice){
case '1':
head=stus.input (head);
break;//------------------------------------------------
case '2':
if(head==NULL){
error();
break;
}
cout<<setw(6)<<"姓名"<<setw(8)<<" 学号"<<setw(8)<<"C++"<<setw(8)<<"数学"<<setw(8)<<"英语"<<setw(8)<<"总分"<<setw(10)<<"平均分"<<endl;
stus.output (head);
getch();
break;//------------------------------------------------
case '3':
if(head==NULL){
error();
break;
}
head=stus.stat(head);
getch();
break;//------------------------------------------------
case '4':
if(head ==NULL){
error(); //调用函数输出错误信息
break;
}
cout<<"请输入想要查找的学生姓名"<<" ,"<<"本系统可以查找重复姓名学生"<<endl;
cin>>name;
pd=head;
cout<<setw(6)<<"姓名"<<setw(8)<<" 学号"<<setw(8)<<"C++"<<setw(8)<<"数学"<<setw(8)<<"英语"<<setw(8)<<"总分"<<setw(10)<<"平均分"<<endl;
while(pd){ // 循环调用函数, 用于输出多个的同名学生成绩
pd=stus.find (pd,name,n);
if(pd==NULL)
break;
pd=pd->next ; //指针指向当前以找到的下一个节点,用于查找多个同名学生
}
getch();
break;//------------------------------------------------
case '5':
if(head==NULL){
error();
break;
}
cout<<"请输入想要删除学生姓名"<<endl;
cin>>name;
head=stus.del(head,name);
getch();
break;//------------------------------------------------
case '6':
if(head==NULL){
error();
break;
}
head=stus.stat (head);
head=stus.insert(head);
break;//-----------------------------------------------
case '7':
if(head==NULL){
error();
break;
}
head=stus.clear(head);
cout<<"删除表成功~"<<endl;
getch();
break;//-----------------------------------------------
default :
cout<<" 对不起,您的输入有误,请重新输入。\n";
getch();
break;
}//------------------------------------------------------
}
getch();
return 0;
}
相关文章推荐
- 扩展系统功能——装饰模式
- 68.scala编程思想笔记——使用Option
- C++模版-重载,嵌套
- 303. Range Sum Query - Immutable
- 反射机制复习总结
- 从var func=function 和 function func()区别谈Javascript的预解析机制
- 67.scala编程思想笔记——使用Either
- 66.scala编程思想笔记——构造器和异常
- 创业团队吸贤大法,如何设计完美的员工股权激励计划?
- HDOJ 2011 多项式求和
- C/C++之回调函数
- 软件项目管理四个核心价值观
- phpcms V9 数据模型基类
- 65.scala编程思想笔记——用过异常进行错误处理
- 安卓捕获应用的运行时异常并保存代码
- 64.scala编程思想笔记——使用元组的模式匹配
- Week7-4HMM2
- Android——在活动中使用Menu
- 二叉树的创建,遍历,求高度,算出节点数,算出叶子节点数,左右子树的交换,二叉树的销毁。
- mvn compile错误之SunCertPathBuildException解决思路