第四周作业
2017-07-05 16:35
429 查看
编程题 #1
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
请补足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
源码:
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; class Complex { private: double r,i; public: void Print() { cout << r << "+" << i << "i" << endl; } Complex & operator=(string a){ r=a[0]-int('0'); i=a[2]-int('0'); return *this; } }; int main() { Complex a; a = "3+4i"; a.Print(); a = "5+6i"; a.Print(); return 0; }
编程题 #2
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
下面的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 a){ nVal-=a; return *this; } }; int main () { MyInt objInt(10); objInt-2-1-3; cout << objInt.ReturnVal(); cout <<","; objInt-2-1; cout << objInt.ReturnVal(); return 0; }
编程题 #3
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)注意: 总时间限制: 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; }
源码:
#include <iostream> #include <cstring> using namespace std; clas d4e6 s Array2{ private: int row,column; int** p;//定义一个指针,指的是 *int 类型的东西 public: Array2(int row_,int column_):row(row_),column(column_){ p=new int*[row];//左右两边都是int** for(int i=0;i<column;++i){ p[i]=new int[column]; } } Array2(){} int* operator[](int a){ return p[a];//只要重载第一个[]就可以,返回的是int*的数组指针 } int operator()(int a,int b){ return p[a][b]; } Array2(const Array2 &a){//深拷贝 row=a.row; column=a.column; p=new int*[row]; for(int i=0;i<column;++i){ p[i]=new int[column]; } for(int i=0;i<row;++i){ for(int j=0;j<column;++j){ p[i][j]=a.p[i][j]; } } } void operator=(const Array2 &a){//深拷贝 row=a.row; column=a.column; p=new int*[row]; for(int i=0;i<column;++i){ p[i]=new int[column]; } for(int i=0;i<row;++i){ for(int j=0;j<column;++j){ p[i][j]=a.p[i][j]; } } } }; int main() { Array2 a(3,4); int i,j; a[3][2]=4;//只需要重载第一个[],返回一个数组指针可以和后面的[]连用而不用重载 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; }
编程题#4:大整数的加减乘除
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
给出两个正整数以及四则运算操作符(+ - * /),求运算结果。
输入
第一行:正整数a,长度不超过100
第二行:四则运算符o,o是“+”,“-”,“*”,“/”中的某一个
第三行:正整数b,长度不超过100
保证输入不含多余的空格或其它字符
输出
一行:表达式“a o b”的值。
补充说明:
减法结果有可能为负数
除法结果向下取整
输出符合日常书写习惯,不能有多余的0、空格或其它字符
源码:
//该程序效率不高,可以使用longlong作为最小单元加减,这里是每个位作为最小单元,效率很差 #include <iostream> #include <string> #include <sstream> #include <math.h> using namespace std; int s2i(const char &a){//char to int int b; b=(int)a-48; return b; } char i2s(const int &a){//int to char char b; stringstream c; c<<a; c>>b; return b; } int str2int(string str){//string to int int sum=0; for(int i=str.size()-1;i>-1;--i){ sum+=((int)str[i]-48)*(pow(10,(str.size()-i-1))); } return sum; } int smalll(string a,string c){//比较两个string类型数的大小,在后续程序加减乘除中起指导作用 int small; int equal(string a,string c); if(equal(a,c)){ return 3; } if(a.size()>c.size()){ small=2; }else if(a.size()==c.size()){ for(int i=0;i<a.size();++i){ if(s2i(a[i])>s2i(c[i])){ small=2; break; }else if(s2i(a[i])<s2i(c[i])){ small=1; break; } } }else if(a.size()<c.size()){ small=1; } return small; } int equal(string a,string c){//判定两string是否相等 if(a.size()!=c.size()){ return 0; } for(int i=0;i<a.size();++i){ if(a[i]!=c[i]){ return 0; } } return 1; } string pluss(string a,string c,const int &small){//加法函数 string answer,temp; int p=0,temp2;//p放的是进位 if(small==2){ temp=a;a=c;c=temp; } //此函数这之后都是a<c; for(int i=a.size()-1,j=c.size()-1;i>-1;--i,--j){ temp2=(s2i(c[j])+s2i(a[i])+p)%10; p=(s2i(c[j])+s2i(a[i])+p)/10; c[j]=i2s(temp2); }//将小的那个数和大的那个数对应位想加 for(int i=c.size()-a.size()-1;i>-1;--i){ temp2=(s2i(c[i])+p)%10; p=(s2i(c[i])+p)/10; c[i]=i2s(temp2); }//处理大的那个数剩下的进位 if(p==0){//判断最后一个有没有进位,如果有,则需要在大的位数前加一 answer=c; }else if(p==1){ answer="1"+c; } return answer; } string minuss(string a,string c,int small){//减法函数 string answer,temp; int p=0,temp2=0; if(small==2){ temp=a;a=c;c=temp; } //此函数这之后都是a<c for(int i=a.size()-1,j=c.size()-1;i>-1;--i,--j){ if((s2i(c[j])-p)<s2i(a[i])){ temp2=10; }else {temp2=0;} c[j]=i2s(s2i(c[j])+temp2-s2i(a[i])-p); if(temp2!=0){ p=1; }else{p=0;} }//处理小的数,用大的数相应位减去小的数 for(int i=c.size()-a.size()-1;i>-1;--i){ if((s2i(c[i])-p)<0){temp2=10;} c[i]=i2s(s2i(c[i])+temp2-p); if(temp2!=0){ p=1; }else{p=0;} }//处理大的数剩下的,是否有退位 if(c[0]-p=='0'){//看最后是否有退位 answer=c.substr(1); }else{answer=c;} if(small==1){ answer="-"+answer; } return answer; } string multii(string a,string c,int small){//加法程序,就是连加 string answer="0",temp; string temp2="0"; int pan=0; if(small==2){ temp=a;a=c;c=temp; } //此函数以后都是a<c while(pan==0){ answer=pluss(answer,c,smalll(answer,c)); temp2=pluss(temp2,"1",smalll(temp2,"1")); if(equal(temp2,a)){ pan=1; } } return answer; } string divisionn(string a,string c,int small){//除法程序,就是连减 if(small==1){ return "0"; }else if (small==3){ return "1"; } //此函数后面都是a>c int pan=0; string answer="0",temp="0"; while(pan==0){ temp=pluss(temp,c,smalll(temp,c)); if(smalll(temp,a)!=1){ break; } answer=pluss(answer,"1",smalll(answer,"1")); } return answer; } int main(){ string a,b,c,answer; int small; cin>>a>>b>>c; //a="10000";c="5671";b="-"; //small为1则a<c,small为2则a>c small=smalll(a,c); if(b=="+"){ answer=pluss(a,c,small); }else if(b=="-"){ answer=minuss(a,c,small); }else if(b=="*"){ answer=multii(a,c,small); }else if (b=="/"){ answer=divisionn(a,c,small); } cout<<answer<<endl; return 0; }