📄 student.cpp
字号:
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<fstream.h>
/***********************************************************************************************
M=10.0为班委奖励分;
N为测试总人数,测试时可根据测试方便选取
bN为班委人数,也可根据实际设定
***********************************************************************************************/
const double M=10.0;
const int N=7;
const int bN=2;
/***********************************************************************************************
Student类的说明及有关函数的实现,将List类定义为它的友元类,以便下面实现对Student类无阻隔的访问
其中类的各个成员说明如下:
chose_id用于存放对象心目中的优秀班委学号
vote_num用于存放每个班委所得的票数
escore-考试分数,zscore-综合素质分
t_c_num用于存放总票数
*ptr定义一个静态指针
next指针指向下一个结点
Student(){vote_num=0;next=NULL;} //令每个同学的初始票数都为0
friend istream & operator >> (istream & stream,Student &s); //定义>>运算符重载
void change_v_num() {vote_num++;} //被选班委票数加1
void print(); //数据输出
void print1();
void count_s(); //计算总分
***********************************************************************************************/
class Student
{
friend class List;
protected:
char pos[2],name[20],iden[10],chose_id[10];
int vote_num;
double escore,zscore,totals;
static int t_v_num;
// static Student *ptr;
Student *next;
public:
Student(){vote_num=0;next=NULL;}
friend istream & operator >> (istream & stream,Student &s);
void change_v_num() {vote_num++;}
void print();
void print1();
void count_s();
};
/***********************************************************************************************
Student类静态成员初始化及其他函数的实现
***********************************************************************************************/
//Student* Student::ptr=NULL;
int Student::t_v_num=0;
istream & operator >> (istream &stream,Student &s)
{
char p[2],n[20],id[6],ch[6];
double es,zs;
stream>>id>>n>>es>>zs>>p>>ch;
strcpy(s.pos,p);
strcpy(s.name,n);
strcpy(s.iden,id);
cout<<"hihi"<<endl;
strcpy(s.chose_id,ch);
s.escore=es;
s.zscore=zs;
if(strcmp(s.chose_id,"01000")>=0&&strcmp(s.chose_id,"01009")<0)
Student::t_v_num++;
return stream;
}
void Student::print1()
{
cout<<iden<<"\t"<<name<<"\t"<<" "<<escore<<"\t"<<"\t"<<zscore<<"\t"<<"\t"<<pos<<"\t"<<"\t"<<chose_id<<endl;
}
void Student::print ()
{
cout<<iden<<" "<<name<<" "<<pos<<" "<<totals<<endl;
}
void Student::count_s()
{
if(t_v_num>(N-bN)/2)
{
totals=0.8*escore+0.2*zscore+vote_num*M/(t_v_num);
if(totals>100)
totals=100;
}
else
totals=0.8*escore+0.2*zscore;
}
/***********************************************************************************************
友元类,可对Student类任何成员进行操作
***********************************************************************************************/
class List
{
Student s[N],*root;
public:
List()
{
root=new Student;
}
void input();
void c_vote_num();
void count_list();
void insert_student(Student *node);
void print_list();
void print_list1();
Student &reS(int);
};
Student &List::reS(int a)
{
Student ptr1[1];
if(a>=0&&a<N)
return s[a];
else
{
cout<<"no list";
return ptr1[0];
}
}
void List::input()
{
for(int i=0;i<N;i++)
{
cout<<"请输入第"<<i+1<<"个学生的信息:";
cin>>s[i];
cout<<endl;
}
}
void List::c_vote_num()
{
if(Student::t_v_num>=(N-bN)/2)
for(int j=0;j<N;j++)
{
if(strcmp(s[j].pos,"n")==0)
if(strcmp(s[j].chose_id,"01000")>=0&&strcmp(s[j].chose_id,"01009")<0)
{
for(int i=0;i<N;i++)
if(strcmp(s[i].iden,s[j].chose_id)==0)
s[i].change_v_num();
}
}
}
void List::count_list()
{
for(int k=0;k<N;k++)
s[k].count_s();
}
/***********************************************************************************************
成绩的排名是利用建立一个以总分为关键字的各个结点组成的链表
然后再进行输出实现的,利用了数据结构所学的知识
***********************************************************************************************/
void List::insert_student(Student *node)
{
if(node!=NULL)
{
double key;
key=node->totals;
Student *curr_node=new Student;
curr_node=root;
Student *previous=new Student;
previous->next=NULL;
while (curr_node->next!=NULL&&curr_node->next->totals>key)
{
previous->next=curr_node->next;
curr_node=curr_node->next;
}
if(previous->next==NULL)
{ if(root->next==NULL)root->next=node;
else
{node->next=root->next;
root->next=node;
}
}
else
{ node->next=previous->next->next;
previous->next->next=node;
}
}
}
void List::print_list()
{
Student *cur=root->next;
for(int i=0;i<N;i++)
{
if(cur!=NULL)
{
cur->print();
cur=cur->next;
}
}
}
void List::print_list1()
{
Student *cur=root->next;
for(int i=0;i<N;i++)
{
if(cur!=NULL)
{
cur->print1();
cur=cur->next;
}
}
}
void read(List & stu1)
{
fstream f1("studentfile.txt",ios::in|ios::out);//写文件
f1.seekg(0);
for(int j=0;j<N;j++)
{
f1.read((char*)&stu1.reS(j),sizeof(Student));//读文件
}
f1.close();
}
/***********************************************************************************************
主函数。其中利用了文件流的读写知识。可实现信息的输入,处理,也可以实现对已有信息的查询
***********************************************************************************************/
void main()
{
fstream f2("studentfile.txt",ios::in|ios::out);
List stu;
cout<<endl;
cout<<" "<<"学生综合排名系统"<<endl;
cout<<endl;
cout<<"请按下列信息输入有关信息";
cout<<"(每一项之间请用空格隔开):"<<endl;
cout<<" 1.输入N个学生信息(其中班委有bN个);"<<endl;
cout<<" 2.学号01000-01009;"<<endl;
cout<<" 3.是否班委用(y或n);"<<endl;
cout<<" 4.不投票则在选举学号上填上0,投票则写上所选班委的学号。"<<endl;
cout<<"要输入学生信息,请键入1;要查询已有的信息,请键入2;退出,请键入3"<<endl;
cout<<"你的选择:";
int a=0;
cin>>a;
if(a==1||a==2)
{
if(a==1)
{
cout<<" "<<"学号"<<" "<<"姓名"<<" "<<"学习成绩"
<<" "<<"综合素质"<<" "<<"是否班委"<<" "<<"优秀班委学号"<<endl;
stu.input();
}
if(a==2)
read(stu);
cout<<"你输入的学生信息如下:"<<endl;
cout<<"学号"<<"\t"<<"姓名"<<"\t"<<"学习成绩"<<"\t"<<"综合素质"<<"\t"<<"是否班委"<<"\t"
<<"优秀班委学号"<<endl;
stu.c_vote_num();
stu.count_list();
for(int i=0;i<N;i++)
{
Student *no=&stu.reS(i);
stu.insert_student(no);
}
stu.print_list1();
cout<<"你输入学生的排名信息如下:"<<endl;
cout<<"学号"<<" "<<"姓名"<<" "<<"是否班委"<<" "<<"总分"<<endl;
stu.print_list();
int b=0;
cout<<"是否保存当前学生信息!"<<endl;
cout<<"若保存,请键入88;若不保存,请键入99"<<endl;
cin>>b;
if(b==88)
{
f2.seekg(0);
for(int t=0;t<N;t++)
f2.write((char*)&stu.reS(t),sizeof(Student));
cout<<"学生数据信息已保存!谢谢使用!"<<endl;
}
if(b==99)
cout<<"你选择了不保存!谢谢使用!"<<endl;
}
else
cout<<"你没有选择输入!谢谢使用!"<<endl;
f2.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -