C++第七章习题
7.1 C++为什么要有自己的输入输出系统?
C++的编译系统对数据类型进行严格的检查,凡是类型不正确的数据都不可能通过编译。
C++中需要定义众多的用户自定义类型且能够输入输出。
7.2 C++有哪4个预定义的流对象?他们分别与什么具体设备相关联?
标准输入流对象cin、标准输出流对象cout、非缓冲型的标准出错流对象cerr和缓冲型的标准出错流对象clog。
cin对应标准输入设备
cout对应标准输出设备
cerr对应标准错误输出设备
clog对应标准错误输出设备
7.3. cerr和clog间的区别是?
cerr不经过缓冲区直接显示错误信息。而clog存放在缓冲区,缓冲区满或遇上endl时再输出。
7.4 C++提供哪两种控制输入输出格式的方法?
一种是使用ios类中的有关格式控制的流成员函数进行格式控制,另一种是使用称为操纵符的特殊类型的函数控制。
7.5 C++进行文件输入输出的基本过程是什么?
首先创建一个流对象,然后将这个流对象与文件相关联,即打开文件,此时才能进行读写操作,读写操作完成后再关闭这个文件。
7.6-7.8 BCA
7.9
#include <iostream> #include <iomanip> using namespace std; int factorial(int n) { if(n == 0 || n == 1) return n; return factorial(n-1)*n; } int main() { for(int i = 1; i <= 9; i++) { cout << setw(5) << factorial(i); if(i % 3 == 0) { cout << endl; } } return 0; }
7.10
#include <iostream> #include <iomanip> using namespace std; int main() { for(int i = 1; i <= 7; i++) { cout << setw(16-i); for(int j = 1; j <= (2*i - 1); j++) { cout << 'A'; } cout << endl; } return 0; }
7.11
#include <iostream> #include <iomanip> using namespace std; class matrix { private: int data[2][3]; public: matrix(){} friend ostream &operator<<(ostream &, matrix &); friend istream &operator>>(istream &, matrix &); friend matrix operator+(matrix &, matrix &); }; ostream &operator<<(ostream &os, matrix &a) { for(int i = 0; i < 2; i++) { for(int j = 0; j < 3; j++) { os << a.data[i][j] << " "; } os << endl; } return os; } istream &operator>>(istream &in, matrix &a) { cout << "请输入一个2行3列矩阵:" << endl; for(int i = 0; i < 2; i++) { for(int j = 0; j < 3; j++) { in >> a.data[i][j]; } } return in; } matrix operator+(matrix &a, matrix &b) { matrix temp; for(int i = 0; i < 2; i++) { for(int j = 0; j < 3; j++) { temp.data[i][j] = a.data[i][j] + b.data[i][j]; } } return temp; } int main() { matrix m1, m2, total; cin >> m1 >> m2; total = m1 + m2; cout << total; return 0; }
7.12
#include <iostream> #include <fstream> using namespace std; int main() { fstream fout("stock.txt",ios::out); if(!fout) { cout << "文件打开失败!" << endl; return 1; } fout << "Shen fa zhan 000001\n"; fout << "shang hai qi che 600104\n"; fout << "Guang ju neng yuan 000096"; fout.close(); return 0; }
7.13
#include <iostream> #include <fstream> using namespace std; int main() { char str[30]; fstream in("file1.txt", ios::in); if(!in) { cout << "打开文件file1.txt错误!\n"; abort(); } in >> str >> str; for(int i = 0; i < 30; i++) { if(str[i] != 0 && (str[i] < 'A')) { str[i] += ('A' - 'a'); } } fstream out("file2.txt", ios::out); if(!out) { cout << "打开文件file2.txt失败!\n"; abort(); } out >> str; in.close(); out.close(); return 0; }
7.14
#include <iostream> #include <fstream> using namespace std; int main() { char str[30]; fstream in("file1.txt", ios::in); if(!in) { cout << "打开文件file1.txt错误!\n"; abort(); } in >> str >> str; for(int i = 0; i < 30; i++) { if(str[i] != 0 && (str[i] < 'A')) { str[i] += ('A' - 'a'); } } fstream out("file2.txt", ios::app); if(!out) { cout << "打开文件file2.txt失败!\n"; abort(); } out >> str; in.close(); out.close(); return 0; }
7.15 不会。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?