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

2026/4/29 21:11:09

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文档下载到电脑
搜索更多关于: C 面向对象程序设计教程[第3版]--陈维兴,林小茶课后习 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219