高精度计算器

#include<bits/stdc++.h>
#include<ctime>
#include<cstdlib>
#include<unistd.h>
#include <windows.h>
using namespace std;
string A,B;
char fh;
int a[1000000],b[1000000],c[1000000],q,w,e,d[1000000],len,f[1000000];
void into() {
	q=w=0;
	for(int i=A.size()-1; i>= 0; --i) {
		a[q++]=A[i]-48;
	}
	for (int i=B.size()-1; i>=0; --i) {
		b[w++]=B[i]-48;
	}
}
void add() {
	memset(c,0,sizeof(c));
	e=max(q,w);
	for(int i=0; i<e; ++i) {
		c[i]=a[i]+b[i];
	}
	for(int i=0; i<e; ++i) {
		c[i+1]+=c[i]/10;
		c[i]%=10;
	}
	while(c[e]) {
		++e;
		c[e]=c[e-1]/10;
		c[e-1]%=10;
	}
	for(int i=e-1; i>=0; --i) {
		printf("%d",c[i]);
	}
	cout<<endl;
}
int cmp() {
	if(A.size()==B.size()) {
		for(int i=q-1; i>=0; --i) {
			if(a[i]<b[i]) {
				return-1;
			} else if(a[i]>b[i]) {
				return 1;
			}
		}
	} else {
		int x=A.size(),y=B.size();
		if(x>y) {
			return 1;
		} else if(x==y) {
			return 0;
		} else {
			return-1;
		}
	}
	return 0;
}
void sub() {
	memset(c,0,sizeof(c));
	e=max(q,w);
	int x=cmp();
	if(x==0) {
		putchar('0');
		putchar(10);
		return;
	} else if(x>0) {
		for(int i=0; i<e; ++i) {
			c[i]=a[i]-b[i];
		}
		while(c[e]==0&&e>1) {
			--e;
		}
		for(int i=0; i<e; ++i) {
			if(c[i]<0) {
				--c[i+1];
				c[i]+=10;
			}
		}
		if(c[e]) {
			++e;
			if(c[e-1]<-9) {
				c[e-1]+=10;
				--c[e];
			}
		}
	} else {
		for(int i=0; i<e; ++i) {
			c[i]=b[i]-a[i];
		}
		while(c[e]==0&&e>1) {
			--e;
		}
		for(int i=0; i<e; ++i) {
			if(c[i]<0) {
				--c[i+1];
				c[i]+=10;
			}
		}
		if(c[e]) {
			++e;
			if(c[e-1]<-9) {
				c[e-1]+=10;
				--c[e];
			}
		}
		putchar('-');
	}
	for(int i=e-1; i>=0; --i) {
		printf("%d",c[i]);
		cout<<endl;
	}
}
void mul() {
	for(int i=0; i<q; ++i) {
		for(int j=0; j<w; ++j) {
			d[i+j]+=a[i]*b[j];
		}
	}
	e=q+w-1;
	for(int i=0; i<e; ++i) {
		d[i+1]+=d[i]/10;
		d[i]%=10;
	}
	while(d[e]) {
		d[e+1]=d[e]/10;
		d[e]%=10;
		++e;
	}
	for(int i=e-1; i>=0; --i) {
		printf("%d",d[i]);
		cout<<endl;
	}
}
bool check(const int x, const int w1) {
	for (int i=0; i<w1; ++i) {
		if (a[x+i]>b[i]) {
			return true;
		} else if(b[i]>a[x+i]) {
			return false;
		}
	}
	return true;
}
void div() {
	int x=cmp();
	if(x<0) {
		putchar('0');
		putchar(10);
		for(int i=q-1; i>=0; --i) {
			printf("%d",a[i]);
			cout<<endl;
		}
	} else if(x==0) {
		putchar('1');
		putchar(10);
		putchar('0');
	} else {
		memset(c,0,sizeof(c));
		for(int i=0; i<(int)B.size(); ++i) {
			b[i]=B[i]-48;
		}
		for(int i=0; i<(int)A.size(); ++i) {
			a[i]=A[i]-48;
		}
		int x=0;
		while(q-x>=w) {
			while(check(x,w)) {
				for(int i=w-1; i>=0; --i) {
					a[x+i]-=b[i];
					if(a[x+i]<0) {
						--a[x+i-1];
						a[x+i]+=10;
					}
				}
				++f[x];
			}
			a[x+1]+=a[x]*10;
			a[x]=0;
			++x;
		}
		bool b1=1;
		int la;
		for(int i=0; i<x; ++i) {
			if(b1&&!f[i])continue;
			if(b1) {
				la=i;
			}
			b1=0;
			printf("%d",f[i]);
			cout<<endl;
		}
	}
}
int main() {
	cout<<"********************\n";
	cout<<"****高精度计算器****\n";
	cout<<"********************\n";
	int sr;
	cout<<"请输入你要干什么(1.计算,2.退出)\n";
	cin>>sr;
	while(sr==1) {
		cout<<"请输入要参与运算的两个数(用空格或换行隔开):\n";
		cin>>A>>B;
		cout<<"请输入运算符:\n";
		cin>>fh;
		while(fh!='+'&&fh!='-'&&fh!='*'&&fh!='/') {
			cout<<"运算符不存在,请重新输入运算符\n";
			cin>>fh;
		}
		into();
		len=A.size()-B.size();
		if(fh=='+') {
			add();
			Sleep(1000);
			system("cls");
		} else if(fh=='-') {
			sub();
			Sleep(1000);
			system("cls");
		} else if(fh=='*') {
			mul();
			Sleep(1000);
			system("cls");
		} else if(fh=='/') {
			div();
			Sleep(1000);
			system("cls");
		}
		cout<<"请输入你要干什么(1.计算,2.退出)\n";
		cin>>sr;
	}
	cout<<"欢迎下次光临!!!\n";
	cout<<"再见!!!";
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值