1:补足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;
}
~MyString() { if(p) delete [] p; }
// 在此处补充你的代码
};
int main()
{
char w1[200],w2[100];
while( cin >> w1 >> w2) {
MyString s1(w1),s2 = s1;
MyString s3(NULL);
s3.Copy(w1);
cout << s1 << "," << s2 << "," << s3 << endl;
s2 = w2;
s3 = s2;
s1 = s3;
cout << s1 << "," << s2 << "," << s3 << endl;
}
}
输入
多组数据,每组一行,是两个不带空格的字符串
输出
对每组数据,先输出一行,输入侧侧侧侧侧的打印第中一个字符串三次
然后再输出一行,打印输入中的第二个字符串三次
样例输入
abc def
123 456
样例输出
abc,abc,abc
def,def,def
123,123,123
456,456,456
#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(const MyString &a) {
if (a.p == NULL) { p = NULL; }
else {
p = new char[strlen(a.p) + 1];
strcpy(p, a.p);
}
}
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& a);
MyString& operator =(const char *c);
MyString& operator =(const MyString& a);
};
MyString& MyString::operator =(const MyString& a) {
if (this == &a) { return *this; }
if (a.p) {
if(p) delete []p;
p = new char[strlen(a.p) + 1];
strcpy(p, a.p);
}
else {
p = NULL;
}
return *this;
}
MyString& MyString::operator =(const char *s) {
if (p) delete[]p;//这句话容易漏
if (s) {
p = new char[strlen(s) + 1];
strcpy(p, s);
}
else {
p = NULL;
}
return *this;
}
ostream& operator <<(ostream& o, const MyString& a) {
o << a.p;
return o;
}
int main()
{
char w1[200], w2[100];
while (cin >> w1 >> w2) {
MyString s1(w1), s2 = s1;
MyString s3(NULL);
s3.Copy(w1);
cout << s1 << "," << s2 << "," << s3 << endl;
s2 = w2;
s3 = s2;
s1 = s3;
cout << s1 << "," << s2 << "," << s3 << endl;
}
}
第二次写,多写了一个函数:
MyString&operator =(const char * c){
if(p)delete [] p;
if(c == NULL){
p = NULL;
返回*这个;
}
p = new char [strlen(c)+ 1];
的strcpy(P,C);
}
之所以不需要这个函数,是因为有类型转换构造函数:MyString(const char * s){}
核心在于:
- 判断p是否为空
- 浅拷贝和深拷贝
- operator =必须为成员函数
- 朋友ostream&operator <<友元函数
第二题:
2:看上去好坑的运算符重载
描述
程序填空
#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 << Inc(objInt);
cout <<",";
objInt-2-1;
cout << Inc(objInt) << endl;
}
return 0;
}
输入
多组数据,每组一行,整数ñ
输出
对每组数据,输出一行,包括两个整数,n-5和n - 8
样例输入
20
30
样例输出
15,12
25,22
#include <iostream>
using namespace std;
class MyInt
{
int nVal;
public:
MyInt(int n) { nVal = n; }
MyInt& operator -(int i) {
nVal = nVal - i;
return *this;
}
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;
cout << Inc(objInt);
cout << ",";
objInt - 2 - 1;
cout << Inc(objInt) << endl;
}
return 0;
}
二刷依然存在的问题:
operator int() 不定义回值类型(返回值类型就是int),且无参数。
第三题:
3:惊呆点竟然能这样输入输出!
描述
程序填空
#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 3
4 5
样例输出
2,3
4,5
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() { };
// 在此处补充你的代码
friend istream& operator >>(istream &i, Point &p) {//注意一定要是istream& i
i >> p.x >> p.y;
return i;
}
friend ostream& operator<<(ostream &o,const Point &p) {//注意friend,Point最好有个const
o << p.x << "," << p.y;
return o;
}
};
int main()
{
Point p;
while (cin >> p) {
cout << p << endl;
}
return 0;
}
二刷出错原因:朋友istream&operator >>(istream&i,const Point&p)// const ...都要cin了还写const ....
第四题:
4:第四周程序填空题3
描述
写一个二维数组类数组2,使得下面程序的输出结果是:
0,1,2,3,
4,5,6,7,
8,9,10,11,
下一个
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,
未来
0,1,2,3,
4,5,6,7,
8,9,10,11,
二刷错误原因:
1,p + x * j是int * p [x * j]不是int *类型
2,memcpy(p,ap,sizeof(int) * i * j)这个函数没记号
#include <iostream>
#include <cstring>
using namespace std;
class Array2 {
// 在此处补充你的代码
private:
int x, y;
int *p;
public:
Array2(int i, int j) :x(i), y(j) { p = new int[i*j]; }
Array2() :x(0), y(0), p(NULL) {}
int* operator [](int i) {
return (p + i*y);//注意这里是y不是x
}
int& operator()(int i, int j) {
return p[y*i + j];//注意这里是y不是x
}
Array2& operator=(const Array2& a) {
if (a.p == NULL) {
p = NULL;
return *this;
}
if (p) delete[]p;
x = a.x;
y = a.y;
p = new int[x*y];
memcpy(p, a.p, sizeof(int)*x*y);
return *this;
}
};
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;
}
getchar();
return 0;
}
第五题:
5:别叫,这个大整数已经很简化了!
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int MAX = 110;
class CHugeInt {// 在此处补充你的代码
};
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;
}
输入
多组数据,每组数据是两个非负整数小号和纳秒最多可能200位,正用INT能表示
输出
对每组数据,输出6行,内容分别是:
样例输入
99999999999999999999999999888888888888888812345678901234567789 12
6 6
样例输出
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
99999999999999999999999999888888888888888812345678901234567801
25
25
26
12
12
12
13
13
14
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int MAX = 110;
class CHugeInt {
// 在此处补充你的代码
char c[200];
int len = 0;//最好还是不要len,len现算,不然程序很多bug都是因为len未更新导致
public:
void reverse(char *a) {
int i = 0, j = len - 1;
while (i < j) {
swap(a[i], a[j]);
i++; j--;
}
}
CHugeInt(const char* a) {
memset(c, 0, 200);//把c全置0
strcpy(c, a);
len = strlen(c);
reverse(c);
}
CHugeInt(int n) {
memset(c, 0, 200);//把c全置0
sprintf(c, "%d", n);//把n转换为char存在c中
len = strlen(c);
reverse(c);
}
CHugeInt operator +(const CHugeInt &b) const {
CHugeInt a(0);
if (b.c[0] == '0'&&b.len == 1) {
return *this;
}
if (c[0] == '0'&&len == 1) {
strcpy(a.c, b.c);
a.len = b.len;//别漏了a.len
return a;
}
int j = 0;
for (int i = 0; i < len || i < b.len; i++) {
int x = 0;
char c1 = c[i], c2 = b.c[i];
if (c[i] == 0) c1 = '0';//这两句很关键,不然c[i]=0,c[i]-'0'为负数
if (b.c[i] == 0) c2 = '0';
x = c1 - '0' + c2 - '0' + j;
a.c[i] = (x % 10) + '0';
j = x / 10;
}
a.len= len > b.len ? len : b.len;
if (j != 0) {
int a0 = len > b.len ? len : b.len;
a.c[a0] = j+'0';
a.len = a0 + 1;
}
return a;
}
//还有这种操作???666
CHugeInt operator +(int n) {
return *this + CHugeInt(n);
}
//还有这种操作???666
friend CHugeInt operator +(int n, const CHugeInt &h) {
return h + n;
}
CHugeInt& operator +=(int n) {
*this=*this + CHugeInt(n);//直接调用默认复制构造函数,因为char c[200]是数组不是指针
return *this;
}
/*void operator +=(int n) {//反正这个没报错
*this=*this + CHugeInt(n); }*/
friend ostream& operator<<(ostream& o, const CHugeInt& a) {
//char cc[210];
//strcpy(cc, a.c);
//reverse(cc);这样写是错的,
//reverse()是CHugeInt的成员函数必须用a.reverse()的方式调用
for (int i = a.len - 1; i >= 0; --i) {
cout << a.c[i];
}
return o;
}
friend CHugeInt& operator ++(CHugeInt &a) {
a=a + 1;
return a;
}
friend CHugeInt operator ++(CHugeInt &a,int) {
CHugeInt b = a;
a = (b + 1);
return b;
}
};
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;
}
二刷错误原因:
memset的函数没记住
sprintf的函数没记住