《C++程序设计实践》实验十二

《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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值