#include "sCalculator.h"//main.cpp
#include <fstream>
char GetChar()
{
char ch;
while((ch=cin.get())==' '||ch=='\t'||ch=='\n');
return ch;
}
void main()//显示全部成绩,必修成绩,全部绩点,部分绩点,平均分,Student修改密码,CManager录入学生信息
{
/*CStudent a;
string name;
string password;
char sex[3];
cout<<"input the name of student a:";
cin>>name;
cout<<"input the password of student a:";
cin>>password;
cout<<"input the sex of student:";
cin>>sex;
a.SetStudentPrivacy(password,name,sex);
cout<<"output the password of student a:"<<a.GetStudentPassword()<<endl;
cout<<"output the name of student a:"<<a.GetStudentName()<<endl;
cout<<"output the num of student a:"<<a.GetStudentNum()<<endl;*/
string managerAccount="woshimanager";//进入管理者的界面
string managerAccountPassword="woshimanager";
string account;
cout<<"input the account of the manager:";
cin>>account;
string accountPassword;
cout<<"input the accoutPassword of the manager:";
cin>>accountPassword;
CManager manager;
if(account==managerAccount&&accountPassword==managerAccountPassword)
{
cout<<"scucceed to enter the window of the manager"<<endl;
cout<<"if build a new student,please input 'y':";
char ch;
while((ch=GetChar())=='y')
{
string name;
char sex[3];
cout<<"input the name of the student:";
cin>>name;
cout<<"input the sex of the student:";
cin>>sex;
manager.BuildStudent(name,sex);
cout<<"if build a new student,please input 'y':";
}
/*cout<<"if show the student's information,please input 's':";
while((ch=GetChar())=='s')
{
cout<<"the num of student:";
int studentnum;
cin>>studentnum;
manager.ShowStudent(studentnum);
cout<<"if show the student's information,please input 's':";
}*/
cout<<"if input the grade of student,please input 'i'";
while((ch=GetChar())=='i')
{
string subject;
float grade;
int number;
cout<<"the number of student:";
cin>>number;
do
{
cout<<"input the subject of student:";
cin>>subject;
cout<<"input the grade of student:";
cin>>grade;
cout<<"if it is complusory,please input '0':";
int h=GetChar()-'0';
//cout<<"h="<<h<<endl;
manager.InputStudentGrade(grade,subject,number,h);
cout<<"if wanted input another subject of this student,please input 'd':";
}while((ch=GetChar())=='d');
cout<<"if input the grade of student,please input 'i'";
}
/* cout<<"if show the grade and subject of student,please input 'c'";
while((ch=GetChar())=='c')
{
int number;
cout<<"input the number of student:";
cin>>number;
manager.ShowStudentGrade(number);
cout<<"if show the grade and subject of student,please input 'c'";
}*/
}
else
cout<<"password is wrong"<<endl;
//进入学生登录界面
int numberOfStudent;
string passwordOfStudent;
char c;
cout<<"if enter the student's windows,please input 'w':";
while((c=GetChar())=='w')
{
cout<<"please input your num:";
cin>>numberOfStudent;
cout<<"input the password:";
cin>>passwordOfStudent;
//判断密码是否正确
CStudent* p=new CStudent(manager.GetStudent(numberOfStudent));
if(p->GetStudentPassword()==passwordOfStudent)
{
cout<<"succeed to enter the student's window."<<endl;
/*cout<<"the name of the student is:"<<p->GetStudentName()<<endl;
cout<<"if output all the subject, grade and GPA of the student,please input 'a':";
char cChar;
//显示全部成绩,绩点,平均分
if((cChar=GetChar())=='a')
{
p->ShowAllSubject_Grade();
p->CalculateAllGPA();
p->CalculateAllAverage();
cout<<"All the average grade is:"<<p->GetAllAverage()<<endl;
cout<<"All the GPA is:"<<p->GetAllGPA()<<endl;
}
//显示必修成绩,绩点,平均分
cout<<"if output the complusory subject,grade and GPA of the student,please input 'c':";
if((cChar=GetChar())=='c')
{
p->ShowComplusorySubject_Grade();
p->CalculateCompulsoryAverage();
p->CalculateCompulsoryGPA();
cout<<"average grade of the complusory grade is:"<<p->GetComplusoryAverage()<<endl;
cout<<"average grade of the complusory GPA is:"<<p->GetComplusoryGPA()<<endl;
}
//修改密码
cout<<"if want to revise the password,please input 'r':";
if((cChar=GetChar())=='r')
{
cout<<"input the new password:";
string newPassword;
cin>>newPassword;
manager.GetStudentIP(numberOfStudent)->RevisePassword(newPassword);
}*/
//从文件中显示成绩,学科名
fstream f1;
f1.open(p->all_all,ios::in|ios::binary);
if(f1.fail())
{
cout<<"打开文件失败!"<<endl;
}else
{
char bufread[52];
f1.read((char *)&bufread,13*sizeof(int));//先要读一遍
//cout<<"bufread="<<bufread<<endl;
strcpy(p->buf_all,bufread);
while((f1.read((char *)&bufread,13*sizeof(int)))&&(!f1.eof()))
{
//f1.read((char *)&bufread,13*sizeof(int));
//cout<<"bufread"<<bufread<<endl;
strcat(p->buf_all,bufread);
}
cout<<"显示全部成绩,学科名:"<<p->buf_all<<endl;
// p->CalculateAllGPA();
// p->CalculateAllAverage();
f1.close();
}
//显示必修课的成绩,学科名
/*fstream f2;
f2.open("calculation of compulsory GPA.dat",ios::out|ios::binary);
if(f2.fail())
{
cout<<"打开文件out!失败"<<endl;
exit(3);
}else
{
f2.close();
}*/
fstream f2;
f2.open(p->all_compulsory,ios::in|ios::binary);
if(f2.fail())
{
cout<<"打开文件失败!"<<endl;
exit(5);
}else
{
char bufread2[52];
f2.read((char *)&bufread2,13*sizeof(int));
//cout<<"bufread2:"<<bufread2<<endl;//先要读一遍
strcpy(p->buf_compulsory,bufread2);
while(f2.read((char *)&bufread2,13*sizeof(int))&&!f2.eof())
{
//cout<<"bufread:"<<bufread<<endl;
strcat(p->buf_compulsory,bufread2);
}
cout<<"显示选修的成绩,学科名:"<<p->buf_compulsory<<endl;
f2.close();
// p->CalculateAllGPA();
// p->CalculateAllAverage();
}
}else
cout<<"password is wrong!"<<endl;
delete p;
cout<<"if enter the student's windows,please input 'w':";
}
}
#include <fstream>//sCalcutor.cpp
#include <cstdlib>
#include "sCalculator.h"
int CStudent::num=0;
void CManager::BuildStudent(string name,string sex)
{
p_student[m_studentNumber]=new CStudent;
p_student[m_studentNumber]->m_name=name;
p_student[m_studentNumber]->m_sex=sex;
m_studentNumber++;
}
string CStudent::GetStudentPassword()
{
return m_password;
}
string CStudent::GetStudentName()
{
return m_name;
}
/*int CStudent::GetStudentNum()
{
return m_num;
}*/
CManager::CManager()
{
m_studentNumber=0;
for(int i=0;i<MaxStudent+1;i++)
p_student[i]=NULL;
}
void CManager::InputStudentGrade(float grade,string subject,int number,int j)
{
const char *char_subject=subject.c_str();
p_student[number]->m_grade[p_student[number]->m_numberOfGrade]=grade;
p_student[number]->m_AllGrade+=grade;
float sum=0.0;
if(grade<60){;}
else if(grade>59&&grade<65)
{
sum=(float)1.0;
}else if(grade>64&&grade<70)
{
sum=(float)1.7;
}else if(grade>69&&grade<75)
{
sum=(float)2.2;
}else if(grade>74&&grade<80)
{
sum=(float)2.7;
}else if(grade>79&&grade<85)
{
sum=(float)3.2;
}else if(grade>84&&grade<90)
{
sum=(float)3.6;
}
else if(grade>89&&grade<95)
{
sum=(float)3.8;
}else
sum=(float)4;
fstream f1;
/*f.out("calculation of GPA.dat",ios::out|ios::binary);
if(f1.fail())
{
MessageBox("打开文件失败!");
exit(0);
}else
f1.close();*/
/*if(p_student[number]->i==0)
{
f1.open("calculation of GPA.dat",ios::out|ios::binary);
if(f1.fail())
{
//MessageBox("打开文件失败!");
exit(0);
}else
f1.close();
}
}*/
f1.open(p_student[number]->all_all,ios::app|ios::out|ios::binary);//需要更换名字注意out如果文件存在打开后会清零,c++中打开文件有顺序
if(f1.fail())
{
//MessageBox("打开文件失败!");
cout<<"打开文件失败app!"<<endl;
exit(1);
}else
{
// cout<<p_student[number]->GetBuf_allIP()<<endl;
sprintf(p_student[number]->buf_all,"%10s%14s%8s%.2f\0","subject:",char_subject,"grade:",grade);
//cout<<p_student[number]->buf_all<<endl;
//cout<<"i="<<p_student[number]->i<<endl;
int num=(p_student[number]->i)*((sizeof(int))*13);
//cout<<"num="<<num<<endl;
f1.seekp(num,ios::beg);
(p_student[number]->i)++;
f1.write((char *)&(p_student[number]->buf_all),13*sizeof(int));
/*char buf[52];
f1.seekp(0);
//num=13*(sizeof(int))*(p_student[number]->i);
//cout<<"write后的num:"<<num<<endl;
//f1.read((char *)&buf,13*sizeof(int));
for(int bian=0;bian<p_student[number]->i;bian++)
{
f1.read(buf,13*sizeof(int));//f.read()在读取char时遇到‘\0’就结束
// f1.read((char *)&buf,num);
cout<<"buf="<<buf<<endl;
}*/
//char buf[90];
//f1.read(buf,13*sizeof(int));
//cout<<buf<<endl;
f1.close();
}
//cout<<"5"<<endl;
p_student[number]->m_allSum+=sum;
//记录学生的必修分数的文件
if(j==0)
{
//cout<<"6"<<endl;
p_student[number]->m_complusory[p_student[number]->m_numberOfGrade]=1;
p_student[number]->m_ComplusoryGrade+=grade;
p_student[number]->m_ComplusorySum+=sum;
(p_student[number]->m_numberOfComplusory)++;
//cout<<"必修"<<endl;
fstream f2;
/*if((p_student[number]->k)==0)
{
// cout<<"0"<<endl;
f2.open("calculation of compulsory GPA.txt",ios::out|ios::binary);
if(f2.fail())
{
cout<<"打开文件out失败!"<<endl;
exit(3);
}else
f2.close();
}*/
//cout<<"1"<<endl;
f2.open(p_student[number]->all_compulsory,ios::in|ios::app|ios::out|ios::binary);
if(f2.fail())
{
cout<<"文件打开失败!app";
exit(4);
}else
{
sprintf(p_student[number]->buf_compulsory,"%10s%14s%8s%5.2f\0","compulsory_subject:",char_subject,"compulsory_grade:",grade);
//strcat(p_student[number]->buf_compulsory," ");
//cout<<"buf_compulsory:"<<p_student[number]->buf_compulsory<<endl;
int num=(p_student[number]->k)*(sizeof(int)*13);
//cout<<"num="<<num<<endl;
f2.seekp(num,ios::beg);
(p_student[number]->k)++;
// cout<<"p_student[number]->k="<<p_student[number]->k<<endl;
f2.write((char *)&(p_student[number]->buf_compulsory),(13*sizeof(int)));
//cout<<"buf_compulsory:"<<p_student[number]->buf_compulsory<<endl;
/* char buf[52];
f2.seekp(0);
for(int y=0;y<p_student[number]->k;y++)
{
f2.read((char *)&buf,13*(sizeof(int))+3);
cout<<"buf:"<<buf<<endl;
}
// delete []buf2;
//f2.read((char *)&buf2,13*(sizeof(int)));
// cout<<"buf2"<<buf2<<endl;
//num=13*(sizeof(int))*(p_student[number]->i);
//cout<<"write后的num:"<<num<<endl;
//f1.read((char *)&buf,13*sizeof(int));*/
/*for(int bian=0;bian<p_student[number]->k;bian++)
{
f2.read((char *)&buf,13*sizeof(int));//f.read()在读取char时遇到‘\0’就结束
// f1.read((char *)&buf,num);
cout<<"buf="<<buf<<endl;
}*/
f2.close();
}
}
else
p_student[number]->m_complusory[p_student[number]->m_numberOfGrade]=0;
p_student[number]->m_subject[p_student[number]->m_numberOfGrade]=subject;
(p_student[number]->m_numberOfGrade)++;
}
/*void CManager::ShowStudent(int number)
{
if(p_student[number]==NULL)
{
cout<<"不存在改学生,输入有误!"<<endl;
}
else
{
cout<<"show the information of student whose num is "<<number<<endl;
cout<<"the name of student:"<<p_student[number]->m_name<<endl;
cout<<"the sex of the student:"<<p_student[number]->m_sex<<endl;
}
}(/
/*int CStudent::GetStudentNumberOfGrade()
{
return m_numberOfGrade;
}*/
/*void CStudent::ADDStudentNumberOfGrade()
{
m_numberOfGrade++;
}*/
/*void CManager::ShowStudentGrade(int number)
{
cout<<"the student's number is:"<<number<<endl;
for(int i=0;i<p_student[number]->m_numberOfGrade;i++)
{
cout<<"the student's subject is:"<<p_student[number]->m_subject[i]<<endl;
cout<<"this subject's grade is"<<p_student[number]->m_grade[i]<<endl;
}
}*/
void CStudent::ShowGrade()
{
for(int i=0;i<m_numberOfGrade;i++)
{
cout<<"the student's subject is:"<<m_subject[i]<<endl;
cout<<"this subject's grade is"<<m_grade[i]<<endl;
}
}
float CStudent::GetAllGPA()
{
return m_GPA;
}
// 95-100 90-94 85-89 80-84 75-79 70-74 65-69 60-64 <60
//绩点 4.0 3.8 3.6 3.2 2.7 2.2 1.7 1.0 0
void CStudent::CalculateAllGPA()
{
//m_sumGPA=0.0;
m_GPA=m_allSum/m_numberOfGrade;
}
//重设密码
void CStudent::RevisePassword(string newPassword)
{
m_password=newPassword;
// cout<<"succeed to revise the password!"<<endl;
}
CStudent CManager::GetStudent(int number)
{
return *p_student[number];
}
/*void* CStudent ::operator new(size_t obj)
{
// cout<<"new一个CStudent对象"<<endl;
return malloc(obj);
}
//CStudent CStudent::operator =(CStudent &obj)
void CStudent::operator delete(void *p)
{
//cout<<"消除对象p"<<endl;
free(p);
}*/
void CStudent::ShowAllSubject_Grade()
{
for(int i=0;i<m_numberOfGrade;i++)
{
cout<<"the name of the subject "<<i<<":"<<m_subject[i]<<endl;
cout<<"the grade of the subject "<<i<<":"<<m_grade[i]<<endl;
if(m_complusory[i]==1)
cout<<"the subject "<<i<<" is complusory"<<endl;
else
cout<<"the subject "<<i<<" isn't complusory"<<endl;
}
}
char *CStudent::GetBuf_allIP()
{
char *p=&buf_all[0];
return p;
//return buf_all;
}
char *CStudent::GetBuf_compulsoryIP()
{
return buf_compulsory;
}
char *GetBuf_compulsoryIP();
CStudent* CManager::GetStudentIP(int number)
{
return p_student[number];
}
void CStudent::CalculateAllAverage()
{
m_average=m_AllGrade/m_numberOfGrade;
}
void CStudent::CalculateCompulsoryGPA()
{
m_ComplusoryGPA=m_ComplusorySum/m_numberOfComplusory;
}
void CStudent::CalculateCompulsoryAverage()
{
m_ComplusoryAverage=m_ComplusoryGrade/m_numberOfComplusory;
}
float CStudent::GetAllAverage()
{
return m_average;
}
float CStudent::GetComplusoryAverage()
{
return m_ComplusoryAverage;
}
float CStudent::GetComplusoryGPA()
{
return m_ComplusoryGPA;
}
void CStudent::ShowComplusorySubject_Grade()
{
int j=0;
for(int i=0;i<m_numberOfGrade;i++)
{
if(m_complusory[i]==1)
{
cout<<"the name of complusory subject "<<j<<":"<<m_subject[i]<<endl;
cout<<"the grade of complusory subject "<<j<<":"<<m_grade[i]<<endl;
j++;
}
}
}
#ifndef _SCALCULATOR_H_
#define _SCALCULATOR_H_
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int SubjectLen=10;
const int MaxStudent=20;
class CManager;
class CStudent
{
friend CManager;
public:
static int num;
public:
// void* operator new(size_t obj);
// void operator delete(void *p);
// CStudent operator =(CStudent &obj);
CStudent()
{
m_AllGrade=0;
m_ComplusoryGrade=0;
m_num=num;
num++;
m_numberOfGrade=0;
m_password="woshistudent";
m_numberOfComplusory=0;
m_allSum=0;
m_ComplusorySum=0;
i=0;
k=0;
fstream f;
all_compulsory[0]='0'+m_num;
all_compulsory[1]='\0';
all_all[0]='0'+m_num;
all_all[1]='\0';
char name_compulsory[]="compulsory.dat";
char name_all[]="all.dat";
strcat(all_compulsory,name_compulsory);
strcat(all_all,name_all);
f.open(all_all,ios::out|ios::binary);
if(f.fail())
{
cout<<"创建文件时失败!"<<endl;
exit(6);
}else
{
cout<<"创建文件名为:"<<all_all<<endl;
f.close();
}
f.open(all_compulsory,ios::out|ios::binary);
if(f.fail())
{
cout<<"创建文件时失败!"<<endl;
exit(6);
}else
{
cout<<"创建文件名为:"<<all_compulsory<<endl;
f.close();
}
}
void ShowComplusorySubject_Grade();
void ShowGrade();
float GetAllGPA();
void CalculateAllGPA();
float GetAllAverage();
float GetComplusoryAverage();
float GetComplusoryGPA();
int GetStudentNumber()
{
return m_num;
}
//重设密码
void RevisePassword(string newPassword);
~CStudent(){}
/*int GetNumberOfGrade()
{
return m_numberOfGrade;
}*/
void ShowAllSubject_Grade();
// void SetStudentPrivacy(string password,string name,char sex[]);
string GetStudentPassword();
string GetStudentName();
// int GetStudentNum();
//protected: int GetStudentNumberOfGrade();
// void ADDStudentNumberOfGrade();
void CalculateAllAverage();
void CalculateCompulsoryGPA();
void CalculateCompulsoryAverage();
char *GetBuf_allIP();
char *GetBuf_compulsoryIP();
private:
int m_num;//学生的学号
string m_password;
string m_name;
string m_sex;
int m_grade[SubjectLen];
string m_subject[SubjectLen];
int m_complusory[SubjectLen];
int m_numberOfGrade;
float m_GPA;
//float m_allGrade;
float m_ComplusoryGPA;
float m_average;
float m_ComplusoryAverage;
float m_AllGrade;
float m_ComplusoryGrade;
float m_allSum;
float m_ComplusorySum;
int m_numberOfComplusory;
public:
char buf_all[280];
char buf_compulsory[280];
int i;
int k;
char all_compulsory[20];
char all_all[20];
};
class CManager
{
public:
CManager();//初始化CManager
// SetStudentGrade();
/*SetAccoutPassword()
{
m_accout="woshimanager";
m_accoutPassword="woshimanager";
}*/
//创建学生的各种信息
void BuildStudent(string name,string sex);
//void ShowStudent(int number);
void InputStudentGrade(float grade,string subject,int number,int j);
//void ShowStudentGrade(int number);
~CManager(){}
CStudent GetStudent(int number);
CStudent *GetStudentIP(int number);
private:
int m_studentNumber;
// string m_account;
// string m_accountPassword;
CStudent *p_student[MaxStudent];
};
#endif