#include <iostream>
#include <string>
#include "HugeInteger.h"
#include <cmath>
using namespace std;
HugeInteger temp1,temp2;//左右操作数
HugeInteger result;//计算的结果
HugeInteger::HugeInteger(){
flag=true;
for(int i=0;i<40;i++)
store[i]='0';
}
HugeInteger::HugeInteger(const HugeInteger& h){
flag=h.flag;
for(int i=0;i<40;i++)
store[i]=h.store[i];
}
HugeInteger HugeInteger::input(string s){
const char* ch=s.c_str();
bool overflow=false;//判断是否越界的标志,初始为false
if(ch[0]=='-'){
flag=false;
if(s.length()>41)
overflow=true;
}
else{
flag=true;
if(s.length()>40)
overflow=true;
}
if(flag==true)
{
for(int j=0;j<s.length();j++)
{
store[j]=ch[s.length()-1-j];
}
for(int j=s.length();j<40;j++)
{
store[j]='0';
}
}
else
{
for(int i=0;i<s.length()-1;i++)
{
store[i]=ch[s.length()-1-i];
}
for(int i=s.length()-1;i<40;i++)
{
store[i]='0';
}
}
if(overflow)
cout<<"大整数越界"<<endl;
return *this;
}
string HugeInteger::output(){
string s="";
int i=39;
while(this->store[i]=='0')
i--;
if(flag==true)//如果是正数或0,直接输出
{
for(int j=i;j>=0;j--)
s+=store[j];
}
else//如果是负数,先输出负号
{
s+='-';
for(int j=i;j>=0;j--)
s+=store[j];
}
return s;
}
HugeInteger HugeInteger::add(HugeInteger huge){
temp1=*this;
temp2=huge;
bool overflow=false;
int int_store[40];
int int_store1[40];
int int_store2[40];
for(int i=0;i<40;i++)
{
//将字符转化成对应的正数,方便进行加减操作
int_store[i]=result.store[i]-48;
int_store1[i]=temp1.store[i]-48;
int_store2[i]=temp2.store[i]-48;
}
//正数加正数的情况
if(flag==true&&huge.flag==true){
result.flag=true;//正数加正数一定得正数
for(int i=0;i<39;i++)
{
int_store[i]=int_store1[i]+int_store2[i];
if(int_store[i]>=10)
{
int_store[i]%=10;
int_store1[i+1]+=1;
}
}
int_store[39]=int_store1[39]+int_store2[39];
if(int_store[39]>=10)//最高位如果大于10,越界
overflow=true;
}
else if(flag==false&&huge.flag==false)//负数加负数的情况
{
result.flag=false;
for(int i=0;i<39;i++)
{
int_store[i]=int_store1[i]+int_store2[i];
if(int_store[i]>=10)
{
int_store[i]%=10;
int_store1[i+1]+=1;
}
}
int_store[39]=int_store1[39]+int_store2[39];
if(int_store[39]>=10)//最高位如果大于10,越界
overflow=true;
}
else if(flag==true&&huge.flag==false)//正数加负数
{
temp2.flag=true;//将加数变为正数
return temp1.substract(temp2);//调用减法函数里的“正数减正数”部分
}
else//负数加正数
{
temp1.flag=true;//将被加数变为正数
return temp2.substract(temp1);//调用减法函数里的“正数减负数”部分
}
for(int i=0;i<40;i++)
result.store[i]=int_store[i]+48;
return result;
}
HugeInteger HugeInteger::substract(HugeInteger huge)
{
bool overflow=false;
int int_store[40];
int int_store1[40];
int int_store2[40];
for(int i=0;i<40;i++)
{
int_store[i]=result.store[i]-48;
int_store1[i]=temp1.store[i]-48;
int_store2[i]=temp2.store[i]-48;
}
if(flag==true&&huge.flag==true)//正数减正数
{
if(this->isGreaterThanOrEqualTo(huge))//被减数比减数大或相等
{
result.flag=true;
for(int i=0;i<40;i++)
{
int_store[i]=int_store1[i]-int_store2[i];
if(int_store[i]<0)
{
int_store[i]=10+int_store1[i]-int_store2[i];
int_store1[i+1]-=1;
}
}
}
else//被减数比减数小
{
result.flag=false;
for(int i=0;i<40;i++)
{
int_store[i]=int_store2[i]-int_store1[i];
if(int_store[i]<0)
{
int_store[i]=10+int_store2[i]-int_store1[i];
int_store2[i+1]-=1;
}
}
}
}
else if(flag==false&&huge.flag==false)//负数减负数
{
if(this->isGreaterThanOrEqualTo(huge))//被减数比减数大或相等
{
result.flag=true;
for(int i=0;i<40;i++)
{
int_store[i]=int_store2[i]-int_store1[i];
if(int_store[i]<0)
{
int_store[i]=10+int_store2[i]-int_store1[i];
int_store2[i+1]-=1;
}
}
}
else//被减数比减数小
{
result.flag=false;
for(int i=0;i<40;i++)
{
int_store[i]=int_store1[i]-int_store2[i];
if(int_store[i]<0)
{
int_store[i]=10+int_store1[i]-int_store2[i];
int_store1[i+1]-=1;
}
}
}
}
else if(flag==true&&huge.flag==false)//正数减负数
{
temp2.flag=true;//将减数变为正数
return temp1.add(temp2);//调用加法函数的“正数加正数部分”
}
else//负数减正数
{
temp2.flag=false;//将减数变为负数
return temp1.add(temp2);//调用加法函数的“负数加负数部分”
}
for(int i=0;i<40;i++)
result.store[i]=int_store[i]+48;
return result;
}
bool HugeInteger::isEqualTo(HugeInteger huge)
{
bool judge=true;
if((flag==true&&huge.flag==false)||(flag==false&&huge.flag==true))//不同号
judge=false;
else
{
int i=39,j=39;
while(store[i]=='0')
i--;
while(huge.store[j]=='0')
j--;
if(i!=j)//位数不同
judge=false;
else
{
for(int k=0;k<40;k++)
if(store[k]!=huge.store[k])
{
judge=false;
break;
}
}
}
return judge;
}
bool HugeInteger::isNotEqualTo(HugeInteger huge)
{
return !(this->isEqualTo(huge));
}
bool HugeInteger::isGreaterThan(HugeInteger huge)
{
if(flag==true&&huge.flag==false)
return true;
else if(flag==false&&huge.flag==true)
return false;
else if(flag==true&&huge.flag==true)//同为正数
{
int i=39,j=39;
while(store[i]=='0')
i--;
while(huge.store[j]=='0')
j--;
if(i>j)//数字的位数多,自然大一些
return true;
if(i<j)
return false;
if(i==j)
{
int k;
for(k=i;k>=0;k--)
{
if(store[k]!=huge.store[k])
{
if(store[k]>huge.store[k])
return true;
if(store[k]<=huge.store[k])
return false;
break;
}
}
if(k=-1) return false;
}
}
else//同为负数
{
//将两个数都转化为正数
temp1=*this;
temp2=huge;
temp1.flag=true;
temp2.flag=true;
if(temp1.isEqualTo(temp2))//调用是否相等的函数
return false;
else
return !(temp1.isGreaterThan(temp2));
}
}
bool HugeInteger::isLessThan(HugeInteger huge)
{
if((this->isEqualTo(huge))||(this->isGreaterThan(huge)))
return false;
else
return true;
}
bool HugeInteger::isGreaterThanOrEqualTo(HugeInteger huge)
{
if(this->isLessThan(huge))
return false;
else
return true;
}
bool HugeInteger::isLessThanOrEqualTo(HugeInteger huge)
{
if(this->isGreaterThan(huge))
return false;
else
return true;
}
bool HugeInteger::isZero()
{
for(int i=0;i<40;i++)
if(store[i]!='0')
{
return false;
break;
}
return true;
}
void HugeInteger::compare(HugeInteger huge)
{
cout<<this->output();
if(this->isEqualTo(huge))
cout<<"=";
else if(this->isGreaterThan(huge))
cout<<">";
else if(this->isLessThan(huge))
cout<<"<";
cout<<huge.output()<<endl;
}
没有合适的资源?快使用搜索试试~ 我知道了~
HugeInteger大整数计数器作业
共17个文件
obj:3个
cpp:2个
user:2个
需积分: 19 27 下载量 138 浏览量
2017-08-08
11:41:17
上传
评论 6
收藏 378KB RAR 举报
温馨提示
HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and subtract. For comparing HugeInteger objects, provide functions isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo and isLessThanOrEqualTo — each of these is a “predicate” function that simply returns true if the relationship holds between the two HugeIntegers and returns false if the relationship does not hold. Also, provide a predicate function isZero. If you feel ambitious, provide member functions multiply, divide and modulus
资源推荐
资源详情
资源评论
收起资源包目录
HugeInteger大整数计数器作业.rar (17个子文件)
HugeInteger大整数计数器作业
HugeInteger
HugeInteger.sln 898B
HugeInteger
HugeInteger.h 659B
HugeInteger.vcproj 3KB
HugeInteger.vcproj.PC-201103072012.Administrator.user 1KB
Implementation.cpp 7KB
HugeInteger.vcproj.XIAOXIN.Administrator.user 1KB
Test.cpp 1KB
Debug
Test.obj 58KB
Implemention.obj 63KB
BuildLog.htm 7KB
HugeInteger.exe.intermediate.manifest 145B
mt.dep 67B
vc80.idb 139KB
Implementation.obj 63KB
debug
HugeInteger.exe 116KB
HugeInteger.suo 14KB
HugeInteger.ncb 1.81MB
共 17 条
- 1
资源评论
huaixiaohuai
- 粉丝: 3
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功