高级语言程序设计II 实验报告四学生学籍系统,使用c++

简介:      高级语言程序设计II实验报告四       姓名:许恺学号:2014011329日期:6月26日       1. 实验目的制作学生学籍系统,使用c++知识,又补充和查找的功能2. 设计思路建立三个类,student储存读出来的文件内容,change对文件进行补充和读取,seek进行查找和打印。

 

 

 

 

 

 

高级语言程序设计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,太傻太天真~)

 

 

相关文章
|
1月前
|
安全 算法 编译器
【C++ 泛型编程 进阶篇】深入探究C++模板参数推导:从基础到高级
【C++ 泛型编程 进阶篇】深入探究C++模板参数推导:从基础到高级
248 3
|
1月前
|
缓存 编译器 API
【C/ C++链接】深入C/C++链接:从基础到高级应用(二)
【C/ C++链接】深入C/C++链接:从基础到高级应用
47 1
|
1月前
|
编译器 C++ 开发者
【C/ C++链接】深入C/C++链接:从基础到高级应用(一)
【C/ C++链接】深入C/C++链接:从基础到高级应用
42 0
|
1月前
|
算法 编译器 数据库
【C++ 泛型编程 高级篇】使用SFINAE和if constexpr灵活处理类型进行条件编译
【C++ 泛型编程 高级篇】使用SFINAE和if constexpr灵活处理类型进行条件编译
246 0
|
1月前
|
JSON JavaScript 前端开发
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
269 0
|
1月前
|
算法 编译器 C++
【C++ 格式化输出 】C++ 高级输出格式化:掌握 iostream 与 iomanip
【C++ 格式化输出 】C++ 高级输出格式化:掌握 iostream 与 iomanip
49 1
|
1月前
|
存储 安全 数据库
【C++ 17 包裹器类 std::optional】“深入理解C++:std::optional的高级应用与原理
【C++ 17 包裹器类 std::optional】“深入理解C++:std::optional的高级应用与原理
52 0
|
15天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
39 6
|
1月前
|
编译器 C++
深入理解 C++ 语法:从基础知识到高级应用
了解C++基础语法,包括`#include &lt;iostream&gt;`引入输入输出库,`using namespace std`简化命名。`int main()`是程序入口,`cout &lt;&lt; &quot;Hello World!&quot;`用于输出文本。换行可使用`\n`或`endl`。注释使用`//`进行单行注释,`/* */`进行多行注释。
27 0
|
9天前
|
缓存 编译器 API
NumPy与其他语言(如C/C++)的接口实践
【4月更文挑战第17天】本文介绍了NumPy与C/C++的接口实践,包括Python与C/C++交互基础、NumPy的C API和Cython的使用。通过案例展示了如何将C++函数与NumPy数组结合,强调了内存管理、类型匹配、错误处理和性能优化的最佳实践。掌握这些技能对于跨语言交互和集成至关重要。