018:别叫,这个大整数已经很简化了!

题目地址:http://cxsjsxmooc.openjudge.cn/2021t3fall/018/

#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
############################################################################

使用stringstream和string实现的,所以不是答案
使用stringstream和string实现的,所以不是答案
使用stringstream和string实现的,所以不是答案

因为既然学C++了,c语言的char就尽量的没用,所以代码里只有一个题目的用的char

代码未作鲁棒性测试
代码未作鲁棒性测试
代码未作鲁棒性测试

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
const int MAX = 110;
class CHugeInt {
  private:
  string content;

  public:
  void reverse( string& content1 ) {
    int i = 0, j = content1.size() - 1;
    while ( i <= j ) {
      swap( content1[ i ], content1[ j ] );
      i++;
      j--;
    }
  }
  CHugeInt( string content_ ) {
    content.clear();
    content.assign( content_ );
    reverse( content );
  }
  CHugeInt( int n ) {
    stringstream in;
    in << n;
    content.assign( in.str() );
    reverse( content );
  }
  CHugeInt operator+( const CHugeInt& t ) {
    CHugeInt     tmp( 0 );
    stringstream tmp_o;
    string       content_, c1, c2;
    int          left = 0;
    // cout << __FUNCTION__ << __LINE__ << ";" << ( int )this->content.size() << ";" << this->content.c_str() << endl;
    // cout << __FUNCTION__ << __LINE__ << ";" << ( int )t.content.size() << ";" << t.content.c_str() << endl;
    for ( int i = 0; i < 220; i++ ) {
      tmp_o.clear();
      tmp_o.str( "" );
      if ( i < ( int )this->content.size() )
        c1 = this->content.at( i );
      else
        c1.assign( "0" );
      if ( i < ( int )t.content.size() )
        c2 = t.content.at( i );
      else
        c2.assign( "0" );
      if ( i >= ( int )this->content.size() && i >= ( int )t.content.size() && left == 0 ) {
        break;
      }
      tmp_o << c1.c_str() << " " << c2.c_str();
      int c1_num, c2_num;
      tmp_o >> c1_num >> c2_num;
      tmp_o.clear();
      tmp_o.str( "" );
      // cout << "tmp_o >> c1_num >> c2_num = " << c1_num << ";" << c2_num << endl;
      int k = c1_num + c2_num + left;
      if ( k >= 10 ) {  //判断有进位
        k = ( k % 10 );
        tmp_o << k;
        content_.append( tmp_o.str().c_str() );
        left = 1;
      }
      else {
        tmp_o << k;
        content_.append( tmp_o.str().c_str() );
        left = 0;
      }
      // cout << "tmp_o.str().c_str() = " << tmp_o.str().c_str() << endl;
    }
    tmp.content.clear();
    tmp.content.assign( content_ );
    // cout << "C + C " << tmp.content << endl;
    return tmp;
  }
  friend CHugeInt operator+( CHugeInt& t, int n ) {
    return ( t + CHugeInt( n ) );
  }
  friend CHugeInt operator+( int n, CHugeInt& t ) {
    // cout << __FUNCTION__ << __LINE__ << endl;
    return ( t + n );
  }
  CHugeInt operator+=( int n ) {
    return ( *this + CHugeInt( n ) );
  }
  CHugeInt operator++() {
    return ( *this + 1 );
  }
  CHugeInt operator++( int ) {
    CHugeInt tmp = *this;
    *this        = *this + 1;
    return ( tmp );
  }
  friend ostream& operator<<( ostream& o, CHugeInt t ) {
    int len = t.content.size();
    for ( int i = len - 1; i >= 0; i-- ) {
      cout << *( t.content.c_str() + i );
    }
    return o;
  }
};
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;
}

二.值得注意的点
1.使用stringstream tmp_o变量做或string或char 和int互相转换的时候,记得使用 tmp_o之前要先 tmp_o.clear();
tmp_o.str( “” );
*

2.使用stringstream tmp_o变量一次将两个int转换char *或string 时候,导入字符串流的时候
tmp_o << c1.c_str() << " " << c2.c_str();
添加间隔空格“ ” ,不然
tmp_o >> c1_num >> c2_num;
会出错

3.看mooc 郭炜老师的“程序设计与算法(三)C++面向对象程序设计”的string那一节的课学习,比自己试错式的学习强多了。
课程地址:https://www.icourse163.org/learn/PKU-1002029030?tid=1207491203#/learn/content?type=detail&id=1212861102&cid=1216308121

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangming12345678

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值