高级语言程序设计II
实验报告四
姓名:许恺
学号:2014011329
日期:6月26日
1. 实验目的
制作学生学籍系统,使用c++知识,又补充和查找的功能
2. 设计思路
建立三个类,student储存读出来的文件内容,change对文件进行补充和读取,seek进行查找和打印。
3. 代码实现
主函数:
// c++第四次报告.cpp :定义控制台应用程序的入口点
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "student.h"
#include "change.h"
#include "seek.h"
const int N=1000;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int t,ch;
char f[50];
student* stu=new student[N]; 学生信息对象
change change1; 使用类的对象
seek seek1;
cout<<"Hello! Welcome to Student Information System!"<<endl;
cout<<"Please input student information file:";
cin>>f; 输入文件名
fstream fp; 文件
fp.open(f,ios::_Nocreate|ios::out|ios::in);
t=change1.read(fp,stu);
cout<<"The file contians total "<<t<<" students' information\n"<<endl;
do
{
cout<<"Please select the following one function by input its number"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"[1] input student information"<<endl;
cout<<"[2] query a student's information by his or her name"<<endl;
cout<<"[3] query students' list according to scope of scores"<<endl;
cout<<"[4] quit the program"<<endl;
cout<<"----------------------------------------------------------------"<<endl;
cin>>ch;
if(ch==1)
t=change1.reinput(fp,stu,t);
if(ch==2)
seek1.two(stu,t);
if(ch==3)
seek1.three(stu,t);
if(ch==4)
return 0;
}while(1);
return 0;
}
student.h://储存文件的内容作为桥梁
#pragma once
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
class student
{
public:
int getnum(){return num;};
void getnum(int a){this->num=a;};
char* getname(){return name;};
char* getsex(){return sex;};
int getage(){return age;};
void getage(int b){this->age=b;};
char* getadd(){return address;};
int getadd(char *str)
{
int len;
strcpy_s(address,str);
len=strlen(str);
return len;
};
private:
int num;
char name[20];
char sex[5];
int age;
char address[1000];
};
change.h: //读文件和写入信息
#pragma once
//#pragma comment( lib, "opencv_highgui244d.lib")
//#pragma comment( lib, "opencv_core244d.lib")
#include "student.cpp"
#include <fstream>
class change
{
public:
int read(std::fstream &f,student *stu);
int reinput(std::fstream &f,student *stu,int a);
};
change.cpp:
#include "StdAfx.h"
#include "change.h"
#include "student.h"
#include <iomanip>
#include <iostream>
#include <string>
int read(fstream &f,student *stu)
{
int a,old;
for(a=0;;a++)
{
char str[1000];
int num=0;
f>>setw(10)>>num;
if(num==0)
break;
stu[a].getnum(num);
f>>setw(20)>>stu[a].getname();
f>>setw(5)>>stu[a].getsex();
f>>setw(5)>>old;
stu[a].getage(old);
f.getline(str,1000,'\n');
stu[a].getadd(str);
}
return a;
}
int reinput(fstream &f,student *stu,int a)
{
char c;
do
{
a++;
int b,o,len;
char str[1000];
f.seekg(0,ios::end);
f<<"\n";
cout<<"Please input student's information:"<<endl;
cout<<"num:";
cin>>b;
stu[a].getnum(b);
cout<<"name:";
cin>>stu[a].getname();
cout<<"sex:";
cin>>stu[a].getsex();
cout<<"old:";
cin>>o;
stu[a].getage(o);
cout<<"address:";
cin>>str;
cout<<"_____________________________________________"<<endl;
cout<<"Are you sure to add the information?(Y/N):";
cin>>c;
if(c=='Y')
{
len=stu[a].getadd(str);
f<<setw(10)<<setiosflags(ios::left)<<b;
f<<setw(20)<<setiosflags(ios::left)<<stu[a].getname();
f<<setw(5)<<setiosflags(ios::left)<<stu[a].getsex();
f<<setw(5)<<setiosflags(ios::left)<<o;
f<<setw(len)<<str<<endl;
cout<<"The file has "<<a+1<<" students."<<endl;
}
else
{
cout<<"The file has "<<a<<" students."<<endl;
stu[a].getnum(0);
memset(stu[a].getname(),0,20);
memset(stu[a].getsex(),0,5);
stu[a].getage(0);
memset(str,0,1000);
stu[a].getadd(str);
}
cout<<"Do you continute to input another information?(Y/N):";
cin>>c;
}while(c=='Y');
return a;
}
seek.h://按照姓名查找和年龄范围
#pragma once
#include "student.h"
class seek
{
public:
void two(student *stu,int t);
void three(student *stu,int t);
};
seek.cpp:
#include "StdAfx.h"
#include "seek.h"
#include "student.h"
void seek::two(student *stu,int t)
{
char c;
do
{
int a,b=0,s[100];
char name[20];
cout<<"Please input the student's name:";
cin>>name;
for(a=0;a<20;a++)
if(name[a]=='\0')
break;
for(;a<20;a++)
name[a]=' ';
name[19]='\0';
for(a=0;a<t;a++)
{
if(strcmp(stu[a].getname(),name)==0)
{
s[b]=a;
b++;
}
}
cout<<"Search "<<b<<" students."<<endl;
cout<<"------------------------------------"<<endl;
for(a=0;a<b;a++)
{
cout<<"num:";
cout<<stu[a].getnum()<<endl;
cout<<"name:";
cout<<stu[a].getname()<<endl;
cout<<"sex:";
cout<<stu[a].getsex()<<endl;
cout<<"age:";
cout<<stu[a].getage()<<endl;
cout<<"address:";
cout<<stu[a].getadd()<<endl;
}
cout<<"-------------------------------------------------------------"<<endl;
cout<<"Do you continue to query another student's information?(Y/N):";
cin>>c;
}while(c=='Y');
}
void seek::three(student *stu,int t)
{
char c;
do
{
int b=0,a,min,max,s[1000];
cout<<"Please input the scope of age:"<<endl;
cout<<"Min-age:";
cin>>min;
cout<<"Max-age:";
cin>>max;
for(a=0;a<t;a++)
{
if(stu[a].getage()<=max&&stu[a].getage()>=min)
{
s[b]=a;
b++;
}
}
cout<<"Search "<<b<<" students."<<endl;
cout<<"-------------------------------------------"<<endl;
for(a=0;a<b;a++)
{
cout<<setw(10)<<setiosflags(ios::left)<<stu[s[a]].getnum();
cout<<setw(20)<<setiosflags(ios::left)<<stu[s[a]].getname();
cout<<setw(5)<<setiosflags(ios::left)<<stu[s[a]].getsex();
cout<<setw(5)<<setiosflags(ios::left)<<stu[s[a]].getage();
cout<<setw(strlen(stu[s[a]].getadd()))<<stu[s[a]].getadd()<<endl;
}
cout<<"-----------------------------------------------"<<endl;
cout<<"Do you continue to query another scope?(Y/N):";
cin>>c;
}while(c=='Y');
}
4. 实验结果及分析
程序还有很多不足,需要改进,有些问题实在难以解决,还需继续努力,望老师谅解。
(我c,这报告大一写的,这tm一看就是抄的啊,自己当时估计都羞愧的不好意思写结果分析了23333333,太傻太天真~)