- 总时间限制:
- 1000ms 内存限制:
- 65536kB
- 描述
-
下面程序的输出是:
3+4i
5+6i
请补足Complex类的成员函数。不能加成员变量。
-
#include
#include #include 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 样例输入
-
无
样例输出 -
3+4i5+6i
#include#include #include using namespace std;class Complex {private: double r, i;public: void Print() { cout << r << "+" << i << "i" << endl; } // 在此处补充你的代码#if 0 void operator=(string s) { //由原型知,若省略第2个参数,则默认从位置0(即第1个字符)起开始查找 int position = s.find("+",0); string firstPart = s.substr(0, position); string secondPart = s.substr(position + 1, s.length() - position - 2); r = atof(firstPart.c_str()); i = atof(secondPart.c_str()); }#endif Complex& operator=(string s) { //由原型知,若省略第2个参数,则默认从位置0(即第1个字符)起开始查找 int position = s.find("+", 0); string firstPart = s.substr(0, position); string secondPart = s.substr(position + 1, s.length() - position - 2); r = atof(firstPart.c_str()); i = atof(secondPart.c_str()); return *this; }};int main() { Complex a; a = "30+40i"; a.Print(); a = "500+6i"; a.Print(); while (1); return 0;}
- 描述
-
下面的MyInt类只有一个成员变量。MyInt类内部的部分代码被隐藏了。假设下面的程序能编译通过,且输出结果是:
4,1
请写出被隐藏的部分。(您写的内容必须是能全部放进 MyInt类内部的,MyInt的成员函数里不允许使用静态变量)。
-
#include
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;
#includeusing namespace std;class MyInt { int nVal;public: MyInt(int n) { nVal = n; } int ReturnVal() { return nVal; } // 在此处补充你的代码 MyInt& operator-(int w) { nVal -= w; return *this; }};int main() { MyInt objInt(10); objInt - 2 - 1 - 3; cout << objInt.ReturnVal(); cout << ","; objInt - 2 - 1; cout << objInt.ReturnVal(); while (1); return 0;}
注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
写一个二维数组类 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#include using namespace std;// 在此处补充你的代码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,
样例输入
无
样例输出
0,1,2,3,4,5,6,7,8,9,10,11,next0,1,2,3,4,5,6,7,8,9,10,11,
#include#include using namespace std;// 在此处补充你的代码class Array2{private: int i; int j; int * a;public: //constructor function Array2() { a = NULL; } Array2(int i_, int j_) { i = i_; j = j_; a = new int[i*j]; } // copy constructor ? is needed??? Array2(Array2 &t) { i = t.i; j = t.j; a = new int[i*j]; memcpy(a, t.a, sizeof(int)*i*j); } // overload [] and = and ( ) Array2 & operator=(const Array2 &t) { if (a != NULL) delete[] a; i = t.i; j = t.j; a = new int[i*j]; memcpy(a, t.a, sizeof(int)*i*j); return *this; } ~Array2() { if (a != NULL) delete[] a; } // 将返回值设为int的指针,则可以应用第二个【】,不用重载第二个【】操作符 int *operator[] (int i_) { return a + i_*j; // 觉得有问题 } int &operator() (int i_, int j_) { return a[i_*j + 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; } while (1); return 0;}