019:别叫,这个大整数已经很简化了!
总时间限制: 1000ms 内存限制: 65536kB 描述 程序填空,输出指定结果 #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
这题需要写三个构造函数,分别是无参,int参数,char *参数。还需要重载3个+运算符,分别对应CHugeInt+int,int+CHugeInt,CHugeInt+CHugeInt,还需要重载前置++和后置++,最后还需要一个<<的重载。代码如下:
#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; }
分类:
中国大学mooc郭炜C++入门
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话