C++学习篇

刚创的blog,来一发最近正在上的一门课:郭炜老师的程序设计实习。
刚入门C++所以还是有蛮多错误的,写了也漫长时间的。欢迎大神们指教,共同前行。

原题:
描述
给出两个正整数以及四则运算操作符(+ - * /),求运算结果。

输入
第一行:正整数a,长度不超过100
第二行:四则运算符o,o是“+”,“-”,“*”,“/”中的某一个
第三行:正整数b,长度不超过100

保证输入不含多余的空格或其它字符
输出
一行:表达式“a o b”的值。

补充说明:
1. 减法结果有可能为负数
2. 除法结果向下取整
3. 输出符合日常书写习惯,不能有多余的0、空格或其它字符

//by vito
#include <iostream>
#include <string>
#include <cstring>

using namespace std;


class big{
public:
    int length;
    string origin;

    //构造函数
    big(){
        origin.clear();

    }
    //构造函数
    big(string a){
        if(!origin.empty()) origin.clear();
        length=(int)a.size();
        origin=a;
    }

    string operator+(const big& comp){
        string temp;
        temp=comp.origin;
        int carry=0;
        int maxlen;
        //补0
        //左边的大于右边的时候
        if(length>comp.length){
            maxlen=length;
            for(int i=0;i<length-comp.length;i++){
                temp.insert(temp.begin(), '0');
            }
        }
        //右边大于左边的时候
        else{
            maxlen=comp.length;
            for(int i=0;i<comp.length-length;i++){
                origin.insert(origin.begin(), '0');
            }
        }
        string sum(maxlen ,'0');
        for(int j=maxlen-1;j>=0;j--){
            carry=(int)(origin[j]-'0')+(int)(temp[j]-'0')+carry;
            sum[j]=(carry%10+'0');
            carry/=10;
            //cout << temp << endl;
        }
        if(carry!=0)
            sum.insert(sum.begin(),(carry%10+'0'));
        return sum;
    }

    string operator-(const big& comp)
    {

        string head;    //是否需要添加负号
        string temp;
        temp=comp.origin;
        int borrow=0;
        int maxlen;

        //先判断是不是相等的两个数字
        if(origin==temp)
            return "0";

        //补0
        //左边的大于右边的时候
        if(length>comp.length){
            maxlen=length;
            for(int i=0;i<length-comp.length;i++){
                temp.insert(temp.begin(), '0');
            }

        }
        //右边大于左边的时候
        else{
            maxlen=comp.length;
            for(int i=0;i<comp.length-length;i++){
                origin.insert(origin.begin(), '0');
            }
        }

        //初始化差的大小
        string result(maxlen ,'0');

        //判断是不是大数减去小数;
        for(int i=0;i<maxlen;i++){
            if(origin[i]>temp[i])
                break;
            else if(origin[i]==temp[i])
                continue;
            else{
                string ch;
                ch=origin;
                origin=temp;
                temp=ch;
                head="-";
                break;
            }
        }
        //只适用于大数减小数
        for (int j=maxlen-1;j>=0;j--){
            borrow=10+(int)(origin[j]-'0')-(int)(temp[j]-'0')-borrow;
            result[j]=(borrow%10+'0');
            borrow=1-borrow/10;
        }
        if(result[0]=='0')
            result.erase(result.begin());
        return (head+result);
    }

    string operator*(const big& comp){
        string temp;
        temp=comp.origin;
        int maxlen;
        int carry = 0;

        if(origin=="0"||temp=="0")
            return "0";

        maxlen=length+comp.length;
        string result(maxlen,'0');

        for(int j=comp.length-1;j>=0;j--){
            carry=0;        //每次运行都要重置carry为0
            for(int i=length-1;i>=0;i--){
                carry=(origin[i]-'0')*(temp[j]-'0')+carry+(result[i+j+1]-'0');  //该carry会被上一级相乘影响,所以需要重置为)
                result[i+j+1]=(carry%10+'0');
                // 测试用的:cout << carry << "  "<<result[i+j+1] << "       " << endl;
                carry=carry/10;
            }
            //最前一位乘有进位,将其加上并将carry化为0方便计算下一位操作
            if(carry!=0){
                result[j]=carry+'0';
            }
            // 测试用的:cout<<result << endl;
            // 测试用的:cout << "next" << endl;
        }
        //有多的0就除去
        if(carry==0)
            result=result.substr(1,maxlen-1);
        else{
            result[0]=carry+'0';
        }
        return result;



}

    string operator/(const big& comp){
        string temp=comp.origin,
        temp_origin=origin;
        //minus;  //用于记录循环中每次相减的数字

        int borrow= 0;

        //除数小于等于被除数的情况
        if(temp_origin<=temp&&length<=comp.length){
            if(temp_origin==temp)
                return "1";
            else
                return "0";
        }

        int dif=length-comp.length;     //相差多少位
        string result(dif+1,'0');
        string minus(length,'0');


        for(int i=0;i<=dif;i++)     //for循环相差位数次。i表示进行的是第几位
        {
            //后面补0,前面也得补0
            temp.append(dif-i,'0');
            temp.insert(temp.begin(),i,'0');


            for(int add=1;temp_origin>=temp;add++)  //补位后判断多少个被除数等于除数,然后加入结果中
            {
                //做一次减法
                for (int j=length-1;j>=0;j--){
                    borrow=10+(int)(temp_origin[j]-'0')-(int)(temp[j]-'0')-borrow;
                    minus[j]=(borrow%10+'0');
                    borrow=1-borrow/10;
                }
                temp_origin=minus;
                result[i]='0'+add;
            }
            temp=comp.origin;   //重置被除数
        }


        if(result[0]=='0')
            result.erase(result.begin());
        return result;

    }

};


int main(){
    string a,b;
    cin >> a;
    char o;
    cin >>o;
    cin >>b;
    big first(a);
    big second(b);
    string answer;
    if(o=='+')
        answer=first+second;
    if(o=='-')
        answer=first-second;
    if(o=='*')
        answer=first*second;
    if(o=='/')
        answer=first/second;
    cout << answer;
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值