第一范文网 - 专业文章范例文档资料分享平台

C 面向对象程序设计教程[第3版]--陈维兴,林小茶课后习题答案解析

来源:用户分享 时间:2025/10/2 10:50:21 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

WORD格式.分享

} 7.10

#include #include 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 #include 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 &);

精品.资料

WORD格式.分享

};

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;

精品.资料

WORD格式.分享

cout << total; return 0; } 7.12

#include #include using namespace std;

int main() {

fstream fout(\,ios::out); if(!fout) {

cout << \文件打开失败!\ << endl; return 1; }

fout << \;

fout << \; fout << \; fout.close(); return 0; } 7.13

#include #include using namespace std;

int main() {

char str[30];

fstream in(\, ios::in); if(!in) {

精品.资料

WORD格式.分享

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(\, ios::out); if(!out) {

cout << \打开文件file2.txt失败!\\n\; abort(); }

out >> str; in.close(); out.close(); return 0; } 7.14

#include #include using namespace std;

int main() {

char str[30];

fstream in(\, ios::in); if(!in) {

cout << \打开文件file1.txt错误!\\n\; abort(); }

in >> str >> str;

for(int i = 0; i < 30; i++) {

精品.资料

C 面向对象程序设计教程[第3版]--陈维兴,林小茶课后习题答案解析.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c6ur5w7g6lf0fluh9boav3qhtz4wh2h00txe_11.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top