《C++程序设计POJ》《WEEK4 运算符重载 》《第四周-编程填空》
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
-
下面程序的输出是:
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 - 样例输入
-
无
- 样例输出
-
3+4i 5+6i
#include <iostream> #include <cstring> #include <cstdlib> 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 <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;
#include <iostream> using 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 <iostream> #include <cstring> 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, next 0,1,2,3, 4,5,6,7, 8,9,10,11,
#include <iostream> #include <cstring> 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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix