|
发表于 2019-9-18 22:09:00
|
显示全部楼层
我知道晚了,不过既然都写完了,那就贴出来吧
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <iomanip>
- class c_t
- {
- public:
- c_t(const std::string &filename)
- {
- std::ifstream file(filename);
- size_t cols, rows;
- file >> rows >> cols;
- for(size_t y = 0; y < rows; ++y)
- {
- this->data.push_back(std::vector<int>());
- for(size_t x = 0; x < cols; ++x)
- {
- int temp;
- file >> temp;
- this->data[y].push_back(temp);
- }
- }
- }
- friend std::ostream &operator <<(std::ostream &os, const c_t &rhs)
- {
- for(size_t y = 0; y < rhs.data.size(); ++y)
- {
- int rowsum = 0;
- for(size_t x = 0; x < rhs.data[y].size(); ++x)
- {
- rowsum += rhs.data[y][x];
- os << std::setw(4) << rhs.data[y][x];
- }
- os << "\trowsum = " << rowsum << std::endl;;
- }
- os << "-----------------------------------------" << std::endl;
- int totalsum = 0;
- for(size_t x = 0; x < rhs.data[0].size(); ++x)
- {
- int colsum = 0;
- for(size_t y = 0; y < rhs.data.size(); ++y)
- colsum += rhs.data[y][x];
- os << std::setw(4) << colsum;
- totalsum += colsum;
- }
- os << "\ttotalsum = " << totalsum << std::endl;
- return os;
- }
- private:
- std::vector<std::vector<int>> data;
- };
- int main()
- {
- c_t c("3C.dat");
- std::cout << c << std::endl;
- return 0;
- }
复制代码
- 5 9 2 3 rowsum = 19
- 2 12 -2 0 rowsum = 12
- -5 14 7 111 rowsum = 127
- -----------------------------------------
- 2 35 7 114 totalsum = 158
复制代码 |
|