![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
OJ习题
MOOC 程序设计与算法(三)
Quanmator
这个作者很懒,什么都没留下…
展开
-
17 二维数组类
#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;原创 2022-03-15 16:05:31 · 514 阅读 · 0 评论 -
14 MyString
#include <iostream>#include <string>#include <cstring>using namespace std;class MyString { char * p;public: MyString(const char * s) { if( s) { p = new char[strlen(s) + 1]; strcpy(p,s); } else p = NULL; } ~MyStrin原创 2022-03-14 20:48:11 · 106 阅读 · 0 评论 -
16 惊呆 Point竟能这样输入输出
#include <iostream> using namespace std;class Point { private: int x; int y; public: Point() { };// 在此处补充你的代码}; int main() { Point p; while(cin >> p) { cout << p << endl; } return 0;}输入2 34 5输出2,原创 2022-03-14 16:48:41 · 551 阅读 · 0 评论 -
15 看上去好坑的运算符重载
#include <iostream> using namespace std;class MyInt { int nVal; public: MyInt( int n) { nVal = n ;}// 在此处补充你的代码}; int Inc(int n) { return n + 1;}int main () { int n; while(cin >>n) { MyInt objInt(n); objInt-2-1-3; cout原创 2022-03-14 15:38:19 · 662 阅读 · 0 评论 -
8 超简单的复数类
#include <iostream>#include <cstring>#include <cstdlib>using namespace std;class Complex {private: double r,i;public: void Print() { cout << r << "+" << i << "i" << endl; }// 在此处补充你原创 2022-03-13 21:34:43 · 154 阅读 · 0 评论 -
11 Big & Base 封闭类问题
#include <iostream>#include <string>using namespace std;class Base {public: int k; Base(int n):k(n) { }};class Big{public: int v; Base b;// 在此处补充你的代码};int main(){ int n; while(cin >>n) { Big a1(n); Big a2 = a1; cou原创 2022-03-12 20:16:35 · 259 阅读 · 0 评论 -
12 这个指针哪来的
#include <iostream>using namespace std;struct A{ int v; A(int vv):v(vv) { }// 在此处补充你的代码};int main(){ const A a(10); const A * p = a.getPointer(); cout << p->v << endl; return 0;}输入无输出10考点:【常对象/常成员】1、const A a(10)原创 2022-03-12 17:45:33 · 58 阅读 · 0 评论 -
9 哪来的输出
#include <iostream>using namespace std;class A { public: int i; A(int x) { i = x; }// 在此处补充你的代码};int main(){ A a(1); A * pa = new A(2); delete pa; return 0;}输入无输出21考点:【析构函数】1、分析A a(1);A * pa = new A(2);发现,创建了两个对象但是没有调用内部的成员函数,原创 2022-03-12 17:01:37 · 137 阅读 · 0 评论 -
7 返回什么才好呢
#include <iostream>using namespace std;class A {public: int val; A(int// 在此处补充你的代码};int main(){ int m,n; A a; cout << a.val << endl; while(cin >> m >> n) { a.GetObj() = m; cout << a.val << endl;原创 2022-03-11 16:47:11 · 120 阅读 · 0 评论 -
6 奇怪的类复制
#include <iostream>using namespace std;class Sample {public: int v;// 在此处补充你的代码};void PrintAndDouble(Sample o){ cout << o.v; cout << endl;}int main(){ Sample a(5); Sample b = a; PrintAndDouble(b); Sample c = 20; PrintAnd原创 2022-03-11 11:28:06 · 208 阅读 · 0 评论 -
5 学生信息处理程序
#include <iostream>#include <string>#include <cstdio>#include <cstring>#include <sstream>#include <cstdlib>using namespace std;class Student {// 在此处补充你的代码};int main() { Student student; // 定义类的对象 stu原创 2022-03-11 11:15:42 · 171 阅读 · 0 评论 -
4 神秘的数组初始化
#include <iostream>using namespace std;int main(){ int * a[] = {// 在此处补充你的代码}; *a[2] = 123; a[3][5] = 456; if(! a[0] ) { cout << * a[2] << "," << a[3][5]; } return 0;}输入无输出123,456考点:【内存分配】1、首先定义了一个指针变量的数组,说明里原创 2022-03-11 10:57:41 · 90 阅读 · 0 评论 -
3 好怪异的返回值
#include <iostream>using namespace std;// 在此处补充你的代码getElement(int * a, int i){ return a[i];}int main(){ int a[] = {1,2,3}; getElement(a,1) = 10; cout << a[1] ; return 0;}输入无输出10考点:【引用作为函数的返回值】1、函数体内部返回了一个数,但是后面又对这个数进行了赋值操作,说原创 2022-03-11 10:49:16 · 81 阅读 · 0 评论 -
2 较难swap
#include <iostream>using namespace std;void swap(在此处补充你的代码){ int * tmp = a; a = b; b = tmp;}int main(){ int a = 3,b = 5; int * pa = & a; int * pb = & b; swap(pa,pb); cout &原创 2022-03-11 10:44:45 · 113 阅读 · 0 评论 -
1 简单swap
#include <iostream>using namespace std;class A{ public: int x; int getX() { return x; } };void swap(在此处补充你的代码){ int tmp = a.x;、 a.x = b.x; b.x = tmp;}int main(){ A a,b;原创 2022-03-11 10:39:58 · 145 阅读 · 0 评论