编程作业: 编程作业—运算符重载
4w3:第四周程序填空题1
描述
下面程序的输出是:
3+4i
5+6i
请补足Complex类的成员函数。不能加成员变量。
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; class Complex { private: double r,i; public: void Print() { cout << r << "+" << i << "i" << endl; }
// 在此处补充你的代码
}; int main() { Complex a; a = "3+4i"; a.Print(); a = "5+6i"; a.Print(); return 0; }
输入无输出3+4i
5+6i样例输入
None
样例输出
3+4i 5+6i
Approach:
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; class Complex { private: double r,i; public: void Print() { cout << r << "+" << i << "i" << endl; } Complex(): r(0), i(0){}; Complex& operator=(string s) { int pos = s.find('+', 0); string sTmp = s.substr(0, pos); r = stoi(sTmp); sTmp = s.substr(pos+1); i = stoi(sTmp); } }; int main() { Complex a; a = "3+4i"; a.Print(); a = "5+6i"; a.Print(); return 0; }
4w4:第四周程序填空题2
描述
下面的MyInt类只有一个成员变量。MyInt类内部的部分代码被隐藏了。假设下面的程序能编译通过,且输出结果是:
4,1
请写出被隐藏的部分。(您写的内容必须是能全部放进 MyInt类内部的,MyInt的成员函数里不允许使用静态变量)。
#include <iostream> using namespace std; class MyInt { int nVal; public: MyInt(int n) { nVal = n; } int ReturnVal() { return nVal; }
// 在此处补充你的代码
}; int main () { MyInt objInt(10); objInt-2-1-3; cout << objInt.ReturnVal(); cout <<","; objInt-2-1; cout << objInt.ReturnVal(); return 0; }
输入无输出4,1样例输入
无
样例输出
4,1
Code:
#include <iostream> using namespace std; class MyInt { int nVal; public: MyInt(int n) { nVal = n; } int ReturnVal() { return nVal; } // 在此处补充你的代码 MyInt& operator-(int s) { nVal -= s; return *this; } }; int main () { MyInt objInt(10); objInt-2-1-3; cout << objInt.ReturnVal(); cout <<","; objInt-2-1; cout << objInt.ReturnVal(); return 0; }
4w5:第四周程序填空题3
描述
写一个二维数组类 Array2,使得下面程序的输出结果是:
0,1,2,3,
4,5,6,7,
8,9,10,11,
next
0,1,2,3,
4,5,6,7,
8,9,10,11,
程序:
#include <iostream> #include <cstring> using namespace std; class Array2 {
// 在此处补充你的代码
}; int main() { Array2 a(3,4); int i,j; for( i = 0;i < 3; ++i ) for( j = 0; j < 4; j ++ ) a[i][j] = i * 4 + j; for( i = 0;i < 3; ++i ) { for( j = 0; j < 4; j ++ ) { cout << a(i,j) << ","; } cout << endl; } cout << "next" << endl; Array2 b; b = a; for( i = 0;i < 3; ++i ) { for( j = 0; j < 4; j ++ ) { cout << b[i][j] << ","; } cout << endl; } return 0; }
输入无输出0,1,2,3,
4,5,6,7,
8,9,10,11,
next
0,1,2,3,
4,5,6,7,
8,9,10,11,样例输入
None
样例输出
0,1,2,3, 4,5,6,7, 8,9,10,11, next 0,1,2,3, 4,5,6,7, 8,9,10,11,
Code:
#include <iostream> #include <cstring> using namespace std; class Array2 { // 在此处补充你的代码 private: int row, column; int **p; //定义一个指针,指的是*int类型的东西 public: Array2(int row_, int column_):row(row_), column(column_) { p = new int*[row]; //左右两边都是int** for (int i = 0; i < column; ++i) { p[i] = new int[column]; } } Array2(){} int* operator[](int a) { return p[a]; //只要重载第一个[]就可以,返回的是int*的数组指针 } int operator()(int a, int b) { return p[a][b]; } Array2(const Array2& a) { //深拷贝 row = a.row; column = a.column; p = new int*[row]; for (int i = 0; i < column; ++i) { p[i] = new int[column]; } for (int i = 0; i < row; ++i) { for (int j = 0; j < column; ++j) { p[i][j] = a.p[i][j]; } } } void operator=(const Array2 &a) { //深拷贝 row = a.row; column = a.column; p = new int*[row]; for (int i = 0; i < column; ++i) { p[i] = new int[column]; } for (int i = 0; i < row; ++i) { for (int j = 0; j < column; ++j) { p[i][j] = a.p[i][j]; } } } }; int main() { Array2 a(3,4); int i,j; for( i = 0;i < 3; ++i ) for( j = 0; j < 4; j ++ ) a[i][j] = i * 4 + j; for( i = 0;i < 3; ++i ) { for( j = 0; j < 4; j ++ ) { cout << a(i,j) << ","; } cout << endl; } cout << "next" << endl; Array2 b; b = a; for( i = 0;i < 3; ++i ) { for( j = 0; j < 4; j ++ ) { cout << b[i][j] << ","; } cout << endl; } return 0; }
永远渴望,大智若愚(stay hungry, stay foolish)