C++学习篇
2015-06-03 20:34
288 查看
刚创的blog,来一发最近正在上的一门课:郭炜老师的程序设计实习。
刚入门C++所以还是有蛮多错误的,写了也漫长时间的。欢迎大神们指教,共同前行。
原题:
描述
给出两个正整数以及四则运算操作符(+ - * /),求运算结果。
输入
第一行:正整数a,长度不超过100
第二行:四则运算符o,o是“+”,“-”,“*”,“/”中的某一个
第三行:正整数b,长度不超过100
保证输入不含多余的空格或其它字符
输出
一行:表达式“a o b”的值。
补充说明:
1. 减法结果有可能为负数
2. 除法结果向下取整
3. 输出符合日常书写习惯,不能有多余的0、空格或其它字符
刚入门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; }
相关文章推荐
- Balanced Binary Tree --Leetcode C++
- C语言的代码内存布局详解
- c++中运算符重载
- 12个有趣的C语言面试题
- c++输入文件处理按行读取并且将实现对一行数据分别处理
- Convert Sorted List to Binary Search Tree ------C++ 递归创建平衡二叉查找树
- 黑马程序员——C语言基础——结构体以及结构体指针实战
- C++环形矩阵填充实现
- c++ 时间类型详解 time_t
- C/C++中extern关键字详解
- C语言指针的初始化和赋值
- 【Minimum Path Sum】cpp
- C语言中的union用法
- 12个有趣的C语言面试题
- C语言中的enum(枚举)用法
- poj1258_prime
- C++:模板
- C语言中对大文件操作遇到的问题
- 动作手游源码分析3 classes/layer/GameHeroLayer.cpp
- C++实现费马小定理素数测试