(2022级)成都工业学院面向对象程序设计(C++)实验七:C++的输入和输出

写在前面

1、基于2022级计算机类实验指导书

2、代码仅提供参考

3、如果代码不满足你的要求,请寻求其他的途径

运行环境

window11家庭版

CLion 2023.2.2

实验要求、源代码和运行结果

1、编写程序,输出如下所示的九九乘法表。

*   1   2   3   4   5   6   7   8   9

1   1 

2   2   4

3   3   6   9

4   4   8   12  16

5   5   10  15  20  25

6   6   12  18  24  30  36

7   1   14  21  28  35  42  49

8   8   16  24  32  40  38  56  64

9   9   18  27  36  45  54  63  72  81

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    // 输出表头
    cout << "*   ";
    for (int i = 1; i <= 9; ++i) {
        cout << setw(4) << i << " ";
    }
    cout << endl;

    // 输出表格内容
    for (int i = 1; i <= 9; ++i) {
        cout << i << "   ";
        for (int j = 1; j <= i; ++j) {
            cout << setw(4) << i * j << " ";
        }
        cout << endl;
    }

    return 0;
}

2、有两2*3规模的矩阵a和b,编写程序,求两个矩阵之和。重做插入运算符和提取运算符,使之能用于该矩阵的输入和输出。重载运算符“+”,使之能用于矩阵相加,如c=a+b。

#include <iostream>
using namespace std;

class Matrix {
private:
    int data[2][3]; // 矩阵数据

public:
    friend ostream& operator<<(ostream& os, const Matrix& matrix) {
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 3; ++j) {
                os << matrix.data[i][j] << " ";
            }
            os << endl;
        }
        return os;
    }

    friend istream& operator>>(istream& is, Matrix& matrix) {
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 3; ++j) {
                is >> matrix.data[i][j];
            }
        }
        return is;
    }

    Matrix operator+(const Matrix& other) const {
        Matrix result;
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 3; ++j) {
                result.data[i][j] = this->data[i][j] + other.data[i][j];
            }
        }
        return result;
    }
};

int main() {
    Matrix a, b;
    cout << "请输入矩阵a的元素:" << endl;
    cin >> a;

    cout << "请输入矩阵b的元素:" << endl;
    cin >> b;

    cout << "矩阵a为:" << endl;
    cout << a;

    cout << "矩阵b为:" << endl;
    cout << b;

    Matrix c = a + b;
    cout << "矩阵的和c为:" << endl;
    cout << c;

    return 0;
}

3、编写一个程序,将下面信息表写入文件stock.txt中:

Zhang ming li 100001

Wang li li    100002

Li mu zhe    100003

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream file("stock.txt");
    if (file.is_open()) {
        file << "Zhang ming li 100001" << endl;
        file << "Wang li li 100002" << endl;
        file << "Li mu zhe 100003" << endl;
        file.close();
        cout << "文件写入成功!" << endl;
    } else {
        cout << "无法打开文件。" << endl;
    }

    return 0;
}

4、编写一个程序,与输入文件file1.txt建立联系,文件file1.txt的内容如下:

Hello

C++

定义out为fstream的对象,与输出文件file2.txt建立关联。当文件打开成功后将file1.txt文件的内容转换为大写字母,输出到file2.txt文件中。

#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;

int main() {
    ifstream fileIn("file1.txt");
    ofstream fileOut("file2.txt");
    if (fileIn.is_open() && fileOut.is_open()) {
        char c;
        while (fileIn.get(c)) {
            fileOut.put(toupper(c));
        }
        fileIn.close();
        fileOut.close();
        cout << "文件输出成功!" << endl;
    } else {
        cout << "无法打开文件。" << endl;
    }

    return 0;
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值