赞
踩
输入
多组数据,每组一行,是两个不带空格的字符串
输出
对每组数据,先输出一行,打印输入中的第一个字符串三次
然后再输出一行,打印输入中的第二个字符串三次
样例输入
abc def
123 456
样例输出
abc,abc,abc
def,def,def
123,123,123
456,456,456
#include "stdafx.h" #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; } ~MyString() { if (p) delete[] p; } // 在此处补充你的代码 MyString & operator=(const MyString & str ){ if (str.p == p){ return *this;} if (p){ delete [] p;} if (str.p!=NULL) { p = new char[strlen(str.p) + 1]; strcpy(p, str.p); } else p = NULL; return *this; } void Copy(const char * s) { if (p) delete[] p; if (s ) { p = new char[strlen(s) + 1]; strcpy(p, s); } else { p = NULL; } } friend ostream & operator <<(ostream & o,const MyString & s) { o << s.p; return o; } MyString(const MyString &s) { if (s.p) { p = new char[strlen(s.p) + 1]; strcpy(p, s.p); } else { p = NULL; } } MyString & operator=(const char *s) { if (p) delete[] p; if (s != NULL) { p = new char[strlen(s) + 1]; strcpy(p, s); } else p = NULL; return *this; } // 在此处补充你的代码 }; int main() { char w1[200], w2[100]; while (cin >> w1 >> w2) { MyString s1(w1), s2 = s1; // 缺了一个复制构造函数,就会多删除一个指针,然后报错……C++管理内存真麻烦呐…… MyString s3(NULL); s3.Copy(w1); cout << s1 << "," << s2 << "," << s3 << endl; s2 = w2; s3 = s2; s1 = s3; cout << s1 << "," << s2 << "," << s3 << endl; } }
输入
多组数据,每组一行,整数n
输出
对每组数据,输出一行,包括两个整数, n-5和n - 8
样例输入
20
30
样例输出
15,12
25,22
#include "stdafx.h" #include <iostream> using namespace std; class MyInt { int nVal; public: MyInt(int n) { nVal = n; } // 在此处补充你的代码 MyInt & operator -(int a) { nVal = nVal-a; return *this; } /*friend int Inc(MyInt m) { return m.nVal; }*/ operator int() { //太神奇了。 return nVal; } // 在此处补充你的代码 }; int Inc(int n) { return n + 1; } int main() { int n; while (cin >> n) { MyInt objInt(n); objInt - 2 - 1 - 3;// operator - cout << Inc(objInt);//Inc() cout << ","; objInt - 2 - 1; cout << Inc(objInt) << endl; } return 0; }
输入
多组数据,每组两个整数
输出
对每组数据,输出一行,就是输入的两个整数
样例输入
2 3
4 5
样例输出
2,3
4,5
#include "stdafx.h" #include <iostream> using namespace std; class Point { private: int x; int y; public: Point() { }; // 在此处补充你的代码 friend istream & operator >> (istream & in, Point & p) { in >> p.x >> p.y; return in; } friend ostream & operator <<(ostream & os, Point & p) { os << p.x << "," << p.y; return os; } // 在此处补充你的代码 }; int main() { Point p; while (cin >> p) { //>> cout << p << 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,
#include "stdafx.h" #include <iostream> #include <cstring> using namespace std; class Array2 { // 在此处补充你的代码 public: int * p; int column, row; Array2() { p = NULL; } Array2(int a,int b):row(a),column(b) { p = new int[row*column]; } Array2(Array2 &a) :row(a.row),column(a.column){ p = new int[row*column]; //动态数组,真是神奇呐 memcpy(p, a.p, sizeof(int)*row*column);// 数组复制函数 } Array2 & operator =(Array2 & a) { if (p) { delete [] p; } column = a.column; row = a.row; p = new int[row*column]; memcpy(p, a.p, sizeof(int)*row*column);// 数组复制函数 return *this; } ~Array2() { if(p) delete[]p; } int * operator[](int i) { *p; return p + i*column; //第一个[]是重载函数,第二个[]就不是了,就是原生的 [] .原生的 [] 外面本来就是指针 } int & operator()(int i,int j) { return p[i*column+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] << ","; //第一个[]W是重载函数,第二个[]就不是了,就是原生的[].原生的[] 外面本来就是指针 } cout << endl; } return 0; }
输入
多组数据,每组数据是两个非负整数s和 n。s最多可能200位, n用int能表示
输出
对每组数据,输出6行,内容分别是:
样例输入
99999999999999999999999999888888888888888812345678901234567789 12
6 6
样例输出
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
25
25
26
12
12
12
13
13
14
#include "stdafx.h" #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> using namespace std; const int MAX = 110; class CHugeInt { // 在此处补充你的代码 private: char buf[220]; public: void reverse(char *p) {// 倒置后的数值字符串更加方便进行计算,并且容易判断出计算结束。 int len =strlen(p); int i = 0, j = len - 1; while (i<=j) { swap(p[i], p[j]);// 这是一个模板函数 ++i; --j; } } CHugeInt(char * p) { memset(buf, 0, sizeof(buf)); //将buf中的所有元素置成0 strcpy(buf, p); reverse(buf); } CHugeInt(int n) { memset(buf, 0, sizeof(buf)); sprintf(buf, "%d", n); //将n以%d的字符串形式保存buf reverse(buf); } CHugeInt operator+(int n) { return *this + CHugeInt(n); } CHugeInt operator +(const CHugeInt & n)const{ //常量函数,函数内的值不可更改 CHugeInt tmp(0); int carry = 0;//进位 for (int i = 0; i < 210; i++) { char c1 = buf[i]; char c2 = n.buf[i]; if (c1 == 0 && c2 == 0 && carry == 0) //三者都为0时,说明读取到了字符串 break; if (c1==0) { c1 = '0'; } if (c2==0) { c2 = '0'; } int k = c1 - '0' + c2 - '0' + carry;// 这里的K是数值 if (k>=10) { carry = 1; tmp.buf[i] = k - 10 + '0';//又转成字符串 } else { carry = 0; tmp.buf[i] = k + '0'; } } return tmp; //放回值不是引用的原因:tmp在调用后就会消亡 } friend CHugeInt operator +(int n, CHugeInt & h) { //友元函数可以写在类里面 return h + n;//类型转换构造函数变成两个对象相加,其实都是对象相加; } friend ostream &operator<<(ostream &os, const CHugeInt & h) { int len = strlen(h.buf); for (int i =len-1; i >= 0; --i)//本来是倒着的,所以倒着输出。 { cout<< h.buf[i]; } return os; } CHugeInt & operator +=(int n) { *this = *this + n; return *this; } CHugeInt & operator ++() { *this = *this + 1; return *this; } CHugeInt operator ++(int) {//后置++ CHugeInt tmp(*this); *this = tmp + 1; return tmp; } // 在此处补充你的代码 }; int main() { char s[210]; int n; while (cin >> s >> n) { CHugeInt a(s); CHugeInt b(n); cout << a + b << endl; cout << n + a << endl; cout << a + n << endl; b += n; cout << ++b << endl; cout << b++ << endl; cout << b << endl; } return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。