《C++程序设计实践》实验十二
一、课内实验题(共5小题,50分)
题型得分 50
【描述】
声明并实现一个Student类,表示学生信息。Student类包括:
int类型的私有数据成员num,表示学号。
string类型的私有数据成员name,表示姓名。
int类型的私有数据成员score,表示成绩
char类型的私有数据成员grade,表示等级。
无参(默认)构造函数。
有参构造函数,将学号、姓名、成绩和等级设置为给定的参数。
访问器函数getNum、getName、getScore、getGrade,分别用于访问学号、姓名、成绩和等级。
重载流提取运算符>>和流插入运算符<<。输入输出一个Student对象
输入若干个学生的信息(学号、姓名和成绩),学号为0时,输入结束,根据成绩计算出对应等级。假设90分以上的成绩属于A级;80~89分、70~79分、60~69分的成绩分别属于B、C、D级;60分以下属于E级。创建Student对象,将它们写入文本文件student.txt中。
【输入】
输入若干个学生的信息。
每行一个学生信息,学号、姓名和成绩之间以空格间隔。
学号为0时,输入结束。
【输出】
文件student.txt。
不需要在屏幕上显示信息。
【输入示例】
100101 ZhangSan 78
100102 LiSi 67
100103 WangWu 83
100104 LiuLiu 45
100105 QianQi 93
0
【输出示例】
生成文件student.txt,其中内容:
100101 ZhangSan 78 C
100102 LiSi 67 D
100103 WangWu 83 B
100104 LiuLiu 45 E
100105 QianQi 93 A
【来源】
《程序设计基础——以C++为例》第8章实验3。(10分)
我的答案:
#include
#include
#include
#include
using namespace std;

class Student{
public:
Student(){
}
Student(int num,string name,int score,char grade){
this->num=num;
this->name=name;
this->score=score;
this->grade=grade;
}

 int getNum() const{return num;}string getName() const {return name;}int getScore() const{return score;}char getGrade() const{return grade;}friend ostream &operator<<(ostream &out,const Student &student);friend istream &operator>>(istream &in, Student &student);
private:int num;      //学号string name;  //姓名 int score;   //成绩 char grade;   //等级

};
ostream &operator<<(ostream &out,const Student &student){
out << student.num<<" “<<student.name<<” “<<student.score<<” "<<student.grade<<endl;
return out;
}
istream &operator>>(istream &in,Student &student){
in >>student.num>>student.name>>student.score>>student.grade;
return in;
}

int main(){
ofstream outFile;
outFile.open(“student.txt”);
if(!outFile.is_open()){
cerr<<“不能新建文件!”<<endl;
exit(EXIT_FAILURE);
}
int num,score;
string name;
char grade;
cin>>num;
while(num!=0){
cin>>name>>score;
switch(score/10){
case 10:
case 9:
grade=‘A’;
break;
case 8:
grade=‘B’;
break;
case 7:
grade=‘C’;
break;
case 6:
grade=‘D’;
break;
default:
grade=‘E’;
break;
}
Student student(num,name,score,grade);
outFile<<student;
cin>>num;
}
outFile.close();
return 0;
}
题目得分 10
【描述】
声明并实现了一个Rectangle类,表示矩形。Rectangle类包括:
double类型的私有数据成员width和height,表示矩形的宽和高。
带默认参数的构造函数,将矩形的宽和高设置为给定的参数。宽和高的默认参数值为1。
更改器函数setWidth和setHeight,分别用于修改矩形的宽和高。
访问器函数getWidth和getHeight,分别用于访问矩形的宽和高。
成员函数computeArea,返回矩形的面积。
成员函数computePerimeter,返回矩形的周长。
创建5个Rectangle对象(每个Rectangle对象的宽和高分别为1、2、3、4、5)并将它们写入二进制文件object.dat中。修改第3个对象的宽为10、高为3.5,再把修改后的该对象写回文件原位置。
【输入】
没有输入。
【输出】
生成文件object.dat
不需要在屏幕上显示信息。
【来源】
《程序设计基础——以C++为例》第8章实验4。(10分)
我的答案:
#include
#include
#include
#include
using namespace std;

class Rectangle{
public:
Rectangle(double width=1,double height=1){
this->width=width;
this->height=height;
}

void setWidth(double width){this->width=width;
}
void setHeight(double height){this->height=height;
}
double getWidth() const{return width;
}
double getHeight() const{return height;
}
double computeArea() const{return width*height;
}
double computePerimeter() const{return 2*(width+height);
}

private:
double width;
double height;
};

int main(){
fstream ioFile;
ioFile.open(“object.dat”, ios::in | ios::out | ios::trunc | ios::binary);
if(!ioFile.is_open()) {
cerr<<“不能新建文件!”<<endl;
exit(EXIT_FAILURE);
}
Rectangle r1;
Rectangle r2(2, 2);
Rectangle r3(3, 3);
Rectangle r4(4, 4);
Rectangle r5(5, 5);
ioFile.write(reinterpret_cast<char *>(&r1), sizeof(Rectangle));
ioFile.write(reinterpret_cast<char *>(&r2), sizeof(Rectangle));
ioFile.write(reinterpret_cast<char *>(&r3), sizeof(Rectangle));
ioFile.write(reinterpret_cast<char *>(&r4), sizeof(Rectangle));
ioFile.write(reinterpret_cast<char *>(&r5), sizeof(Rectangle));
Rectangle ra;
ra.setWidth(10);
ra.setHeight(3.5);
ioFile.seekp(2 * sizeof(Rectangle), ios::beg);
ioFile.write(reinterpret_cast<char *>(&ra), sizeof(Rectangle));
ioFile.close();
return 0;
}
题目得分 10
【描述】
给定文件hours.txt,其中包含每个员工工作时间的记录。每一行表示一周的工作时间。每周有7天,所以每行最多有7个数字。规定每周从周一开始,文件中的每一行都是从周一的工作小时数开始,后面是周二,等等,周日的数字放在这一行的最后。每行中的数字可以少于7个,因为员工并不是每天都要工作。下面是文件hours.txt的内容:
8 8 8 8 8
8 4 8 4 8 4 4
8 4 8 4 8
3 0 0 8 6 4 4
8 8
0 0 8 8
8 8 4 8 4
编写一个程序从输入文件中读取数据,计算并报告每行和每列的总和。每行的总和表示该员工每周工作的小时数。每列的总和表示员工周一、周二等每天工作的累计小时数。最后输出总的小时数。针对上述文件hours.txt的输出结果见【输出示例】。
【输入】
文件hours.txt。(该文件已经存在,无需自己创建)
注意:本地调试程序时,则要求自己预先建立好hours.txt文件。在Windows下,可以使用记事本。
【输出】
员工每周工作的小时数。
员工周一、周二等每天工作的累计小时数。
最后输出总的小时数。
【输入示例】
文件hours.txt。
【输出示例】
Total hours = 40
Total hours = 40
Total hours = 32
Total hours = 25
Total hours = 16
Total hours = 16
Total hours = 32

Mon hours = 43
Tue hours = 32
Wed hours = 36
Thu hours = 40
Fri hours = 34
Sat hours = 8
Sun hours = 8
Total hours = 201
【提示】
文件hours.txt中数据组数是不定的。

(10分)
我的答案:
#include <bits/stdc++.h>
using namespace std;

const int DAYS = 7;
void process(ifstream &inFile);
int transferFrom(int numbers[], const string &line);
int sum(int numbers[], int size);
void addTo(int total[], int numbers[]);
void print(int total[]);

int main() {
ifstream inFile;
inFile.open(“hours.txt”);
if (!inFile.is_open()) {
exit(EXIT_FAILURE);
}
process(inFile);
return 0;
}

void process(ifstream &inFile) {
int total[DAYS] = {0};
int numbers[DAYS] = {0};
int size;
while (!inFile.eof()) {
string line;
getline(inFile, line);
size = transferFrom(numbers, line);
cout << "Total hours = " << sum(numbers, size) << endl;
addTo(total, numbers);
}
cout << endl;
print(total);
}

int transferFrom(int numbers[], const string &line) {
int i = 0, j, value;
stringstream myStream(line);
while (myStream >> value) {
numbers[i] = value;
++i;
}
for (j = i; j < DAYS; ++j)
numbers[j] = 0;
return i;
}

int sum(int numbers[], int size) {
int s = 0;
for (int i = 0; i < size; ++i)
s += numbers[i];
return s;
}

void addTo(int total[], int numbers[]) {
for (int i = 0; i < DAYS; ++i)
total[i] += numbers[i];
}

void print(int total[]) {
string dayNames[] = {“Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”};
int s = 0;
for (int i = 0; i < DAYS; ++i) {
cout << dayNames[i] << " hours = " << total[i] << endl;
s += total[i];
}
cout << "Total hours = " << s << endl;
}
题目得分 10
【描述】
处理日志文件,日志文件的储存格式为“年/月/日 时:分:秒 用户名 操作”。
日志文件有多条记录:
2015/4/218:00:33 37c3b6b58c6ac3 LOGIN
2015/4/218:15:35 11734e186f24fe4c LOGIN
2015/4/218:34:57 9f3cf331d19a9f LOGIN
2015/4/219:00:29 389bcca2159f5de7 LOGIN
2015/4/219:08:29 c3bde693fdb3c3d LOGIN
……
可以下载日志文件:
鼠标右键另存为
【输入】
日志文件log.txt。(该文件已经存在,无需自己创建)
【输出】
日志文件中活跃用户的数量。
【输入示例】

【输出示例】
123
【提示】
活跃用户指的是在日志文件中有过操作的用户,记得把重复出现的用户去掉。
输出示例只是格式说明,并非正确答案。

(10分)
我的答案:
#include
#include
#include
#include
using namespace std;

int main() {
ifstream fin(“log.txt”);
int user_count = 0;
string ids[600];
while (!fin.eof()) {
int year, month, day, hour, minute, second;
char tmp;
string id, operation;
fin >> year >> tmp >> month >> tmp >> day;
fin >> hour >> tmp >> minute >> tmp >> second;
fin >> id;
fin >> operation;
int found = -1;
for (int i = 0; i < user_count; i++) {
if (id == ids[i]) {
found = i;
break;
}
}
if (found == -1)
ids[user_count++] = id;
}
fin.close();
cout << user_count << endl;
return 0;
}
题目得分 10
【描述】
将一个明文文件plaintext.txt中的内容,按照一定的方法,对每个字符加密后存放到另一个密文文件ciphertext.txt中。
【输入】
文件plaintext.txt。(该文件已经存在,无需自己创建)
注意:本地调试程序时,则要求自己预先建立好plaintext.txt文件。在Windows下,可以使用记事本。
【输出】
生成文件ciphertext.txt,里面存放加密后的信息。
不需要在屏幕上显示信息。
【输入示例】
文件plaintext.txt,假定其中内容如下:
Welcome to C++!
【输出示例】
生成文件ciphertext.txt,加密后的内容如下:
Ygneqog"vq"E–#
【提示】
这里采用一种简单的加密方法,将每个字符的编码加2。
【来源】
《程序设计基础——以C++为例》第8章实验2。(10分)
我的答案:
#include
#include
#include
#include
using namespace std;

int main(){
ifstream inFile;
inFile.open(“plaintext.txt”);
if(!inFile.is_open()){
cerr<<“不能打开文件!”<<endl;
exit(EXIT_FAILURE);
}
ofstream outFile;
outFile.open(“ciphertext.txt”);
if(!outFile.is_open()){
cerr<<“不能新建文件!”<<endl;
exit(EXIT_FAILURE);
}
char ch;
inFile.get(ch);
while(!inFile.eof()){
ch+=2;
outFile.put(ch);
inFile.get(ch);
}
inFile.close();
outFile.close();
return 0;
}
题目得分 10

《C++程序设计实践》实验十二相关推荐

  1. c语言程序设计实践课选题,c语言程序设计实践实验题目

    c语言程序设计实践实验题目 绥化学院程序设计实践实验报告范例 参考1实验题目:循环结构程序设计实验目的:1.熟悉 VC++6.0 的运行环境,掌握 C 程序的执行方法:2.掌握三种基本数据类型.部分运 ...

  2. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十二:串口模块① — 发送

    实验十二:串口模块① - 发送 串口固然是典型的实验,想必许多同学已经作烂,不过笔者还要循例介绍一下.我们知道串口有发送与接收之分,实验十二的实验目的就是实现串口发送,然而不同的是 ... 笔者会用另 ...

  3. 虚拟机dhcp服务器怎么检验,实验十二虚拟机上DHCP服务器的配置和验证.doc

    实验十二 虚拟机上DHCP服务器的配置与验证 一.实验目的 了解DHCP的基本概念和服务器的新特性 掌握DHCP服务器的安装与配置 掌握DHCP的运行方式 掌握DHCP客户机的设置 掌握ipconfi ...

  4. 实验十二 HTTP 协议分析实验

    实验十二 HTTP 协议分析实验 1.HTTP 协议简介 HTTP是超文本传输协议 (Hyper Text Transfer Protocol)的缩写,用于WWW 服务. (1)HTTP的工作原理 H ...

  5. 实验十二、十三 配置PPP协议、配置Frame-relay协议

    实验十二     配置PPP协议 要求: 配置PPP协议,实现广域网连接.然后,分别采用PAP和CHAP进行验证,最后启用OSPF协议,实现局域网之间相互通信. 基础配置 为路由器R1.R2设置相应的 ...

  6. 20189200余超 2018-2019-2 移动平台应用开发实践第十二周作业

    20189200余超 2018-2019-2 移动平台应用开发实践第十二周作业 服务 Service的声明 Service是Android中的四大组件,使用它一定要在AndroidManifest.x ...

  7. C语言程序设计实践题,2020年C语言程序设计实践实验题目.doc

    C语言程序设计实践实验题目 绥化学院程序设计实践实验报告范例 参考 实验题目循环结构程序设计 实验目的 熟悉VC++的运行环境,掌握C程序的执行方法: 掌握三种基本数据类型.部分运算符号和常用函数: ...

  8. 计算机组成原理Rsel什么意思,el-jy-ⅱ 计算机组成原理第二套 实验十二

    el-jy-ⅱ 计算机组成原理第二套 实验十二 (9页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 19.90 积分 梁旷啦倔洲湿悔胚啼铆止羊香蔫夏髓解师 ...

  9. 同步检波 matlab,实验十二 包络检波及同步检波实验

    实验十二 包络检波及同步检波实验 一.实验目的 1.进一步了解调幅波的原理,掌握调幅波的解调方法. 2.掌握二极管峰值包络检波的原理. 3.掌握包络检波器的主要质量指标,检波效率及各种波形失真的现象, ...

最新文章

  1. 基于mpi的奇偶排序_并行程序设计(第2版)pdf
  2. Java获取Mybatis动态生成的sql
  3. Android 动画之View动画效果和Activity切换动画效果
  4. 直播报名 | 超低延时直播架构解析
  5. 关于烂代码的那些事——什么是好代码
  6. Linux下 Nginx 启动 重启 关闭
  7. 移动应用ios和网页应用_如何在iOS上一次移动多个应用
  8. 为什么机油使用后变红_水泥固化剂的使用原理是什么,涂洒后时间为什么要足够长?...
  9. struts2 在线用户记录
  10. 电子科技大学计算机课程表,电子科技大学课表.doc
  11. Atitit 多继承实现解决方案 java c#
  12. libtorrent源码分析(一)整体框架
  13. Spring Boot系列 —(一)手把手搭建 SpringBoot 项目
  14. python模拟勒索病毒
  15. 女人需要调教人需要调教
  16. Android系统升级
  17. CSS-基础选择器、字体文本属性、引入方式
  18. viewpager禁止左右滑动
  19. 怎么搭建网校系统,培训机构搭建专属网校平台源码
  20. Unity 工作经历+近期面试经历

热门文章

  1. javascript系列-Class1.JavaScript基础
  2. 一些制作app的软件
  3. CSDN虚拟化蓝色深海世界旅者札记【定基篇】
  4. 关于装饰模式的一个小例子
  5. 发现一个很好的图片占位工具网。placehold.it
  6. eclipse官网 无法下载 解决
  7. Vue 视频截取第一帧图片-组件
  8. 转: 写给想成为前端工程师的同学们 (from 360前端团队)
  9. YTU 2530: 小勇玩lol
  10. [LeetCode]动态规划求解博弈问题