1:MyString
补足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 & S )
{
p = new char[strlen(S.p)+1];
strcpy(p,S.p);
}
MyString & Copy( const char * s )
{
if(p) delete [] p;
p = new char[strlen(s)+1];
strcpy(p,s);
return *this;
}
MyString & operator=( const char * s )
{
if(p) delete [] p;
p = new char[strlen(s)+1];
strcpy(p,s);
return *this;
}
MyString & operator=( const MyString & S )
{
if( this == & S ) return *this;
if(p) delete [] p;
p = new char[strlen(S.p)+1];
strcpy(p,S.p);
return *this;
}
friend ostream & operator<<( ostream & os, const MyString & S )
{
os << S.p;
return os;
}
};
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;
}
}
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
输出
对每组数据,输出一行,包括两个整数, 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 r)
{
nVal -= r;
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;
}
3:惊呆!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 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 & is, Point & p)
{
int a,b;
is>>a>>b;
p.x=a;
p.y=b;
return is;
}
friend ostream & operator <<(ostream & os, const Point & p)
{
os<<p.x<<","<<p.y;
return os;
}
};
int main()
{
Point p;
while(cin >> p) {
cout << p << endl;
}
return 0;
}
4:第四周程序填空题3
描述
写一个二维数组类 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; 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,
next
0,1,2,3,
4,5,6,7,
8,9,10,11,
答案:
#include <iostream>
#include <cstring>
using namespace std;
class Array2 {
int **ptr;
int sizex,sizey;
public:
Array2()
{
ptr = NULL;
sizex = 0;
sizey = 0;
}
Array2(int x, int y)
{
sizex = x;
sizey = y;
ptr = new int*[x];
for(int i=0;i<x;i++)
{
ptr[i] = new int [y];
}
}
Array2(Array2 & a)
{
if(!a.ptr)
{
ptr=NULL;
return;
}
ptr = new int*[a.sizex];
for(int i=0;i<a.sizex;i++)
ptr[i] = new int[a.sizey];
}
int *& operator [](const int & i)
{
return ptr[i];
}
int & operator()(const int & m,const int & n)
{
return ptr[m][n];
}
};
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;
}
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; }
输入
多组数据,每组数据是两个非负整数s和 n。s最多可能200位, n用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 {
private:
char maxNum[210];
int len;
public:
CHugeInt(char * s){
strcpy(maxNum,s);
int i=0,j=strlen(s)-1;
while(i<j)
{
swap(maxNum[i],maxNum[j]);
i++;
j--;
}
//cout<<"init:"<<maxNum<<endl;
len=strlen(s);
//cout<<"Init success"<<endl;
}
CHugeInt(){
len=0;
}
CHugeInt(int n){
int i=0;
if(n==0)
{
maxNum[i++]='0';
}else{
while(n)
{
maxNum[i++]=n%10+'0';
n=n/10;
}
}
maxNum[i]='\0';
len=i;
//cout<<maxNum<<endl;
}
CHugeInt operator+(CHugeInt & a)
{
//cout<<"hrer"<<endl;
int i=0,j=0;
int t,sum=0;
CHugeInt temps;
strcpy(temps.maxNum,maxNum);
temps.len=len;
//cout<<"before:"<<temps.maxNum<<endl;
//maxNum=new char[strlen(a.maxNum)+1];
//cout<<a.len<<","<<len<<endl;
int flag=0;
while(j<a.len&&i<temps.len)
{
t=a.maxNum[j]-'0';
int te=temps.maxNum[i]-'0';
sum=t+te;
//cout<<t<<"+"<<te<<":"<<sum<<endl;
if(sum>=10)
{
temps.maxNum[i]=sum%10+'0';
//cout<<temps.maxNum[i]<<endl;
temps.maxNum[i+1]=sum/10+temps.maxNum[i+1];
if(i+1>=temps.len)
{
temps.maxNum[i+1]+='0';
}
flag=1;
}else{
//cout<<"sum:"<<sum<<endl;
flag=0;
temps.maxNum[i]=sum+'0';
}
//cout<<temps.maxNum[i]<<endl;
i++,j++;
sum=0;
}
while(j<a.len)
{
if(flag==1)
{
temps.maxNum[i+1]=a.maxNum[j];
i++,j++;
}else{
temps.maxNum[i]=a.maxNum[j];
i++,j++;
}
}
if(i>=len)
{
if(flag==1){
temps.maxNum[i+1]='\0';
temps.len=i+1;
}
else{
temps.maxNum[i]='\0';
temps.len=i;
}
}
return temps;
}
/*operator char *()
{
return maxNum;
}*/
CHugeInt & operator +=(int n)
{
CHugeInt temps(n);
*this=this->operator+(temps);
//cout<<this->maxNum<<endl;
return *this;
}
friend ostream & operator<<(ostream & os,const CHugeInt & s)
{
int i=0,j=s.len-1;
//cout<<"len:"<<s.len<<endl;
//cout<<"输出:"<<s.maxNum<<endl;
for(;j>=i;j--)
os<<s.maxNum[j];
return os;
}
friend CHugeInt operator+(int n,CHugeInt s)
{
CHugeInt temps(n);
s=s+temps;
return s;
}
friend CHugeInt operator+(CHugeInt s,int n)
{
CHugeInt temps(n);
s=s+temps;
return s;
}
CHugeInt & operator++()
{
(*this)+=1;
//cout<<"前置自增后:"<<this->maxNum<<endl;
return *(this);
}
CHugeInt operator++(int n)
{
CHugeInt temps;
strcpy(temps.maxNum,maxNum);
temps.len=len;
this->operator +=(1);
//cout<<temps.maxNum<<endl;
//cout<<"len:"<<temps.len<<endl;
return temps;
}
};
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;
}