📄 arithmetic.cpp
字号:
\*******************************************************/
Arithmetic operator - (Arithmetic& numa, Arithmetic& numb)
{
std::string strNuma = numa.GetData(), strNumb = "-" + numb.GetData();
Arithmetic result = numa.PlusMinus(strNuma, strNumb);
return result;
}
/*******************************************************\
函数: operator *
功能: 计算大数乘法
输入: Arithmetic对象
输出: 无
返回: 2数之积
备注: 模仿竖式的乘法计算
\*******************************************************/
Arithmetic operator * (Arithmetic& numa, Arithmetic& numb)
{
int digCount = 0, i;
std::string strNuma = numa.GetData(), strNumb = numb.GetData();
int state = 0;
if(strNuma[0] == '-' && strNumb[0] != '-')
{
state = 1;
strNuma.erase(0, 1);
}
if(strNuma[0] != '-' && strNumb[0] == '-')
{
state = 1;
strNumb.erase(0, 1);
}
if((i = strNuma.find(".")) < strNuma.length())
{
digCount += (strNuma.length() - 1 - i);
strNuma.erase(i, 1);
}
if((i = strNumb.find(".")) < strNumb.length())
{
digCount += (strNumb.length() - 1 - i);
strNumb.erase(i, 1);
}
std::string strResult = numa.Multiply(strNuma, strNumb, digCount);
if(state == 1)
strResult.insert(0, "-");
Arithmetic result = strResult;
return result;
}
/*******************************************************\
函数: operator %
功能: 计算大数取模
输入: Arithmetic对象
输出: 无
返回: 模
备注: 类似大数除法运算
\*******************************************************/
Arithmetic operator % (Arithmetic& numa, Arithmetic& numb)
{
if(numa < numb)
return numa;
Arithmetic iresult = numa - numb, ia = "";
if(iresult.GetData() == "0")
return iresult;
std::string strNuma = numa.GetData(), carry = "0";
long lb = numb.GetData().length();
do
{
ia = strNuma.substr(0, lb);
strNuma.erase(0, lb);
iresult = ia - numb;
if(iresult.GetData()[0] == '-')
{
std::string t = ia.GetData();
t += strNuma[0];
ia = t;
strNuma.erase(0, 1);
}
do
{
ia = ia - numb;
if(ia.GetData()[0] == '-')
break;
carry = ia.GetData();
}while(1);
if(carry != "0")
strNuma.insert(0, carry);
ia = strNuma;
}while(ia >= numb);
return ia;
}
/*******************************************************\
函数: operator /
功能: 计算大数除法
输入: Arithmetic对象
输出: 无
返回: 2数差
备注: 用户设置小数点后的精确度.
分段用相减来获得商,从高位开始去减被除数直到差小于被除数,
相减的次数就是商
\*******************************************************/
Arithmetic operator / (Arithmetic& numa, Arithmetic& numb)
{
std::string strNuma = numa.GetData(), strNumb = numb.GetData();
int signstate = 0;
if(strNuma[0] == '-' && strNumb[0] != '-')
{
signstate = 1;
strNuma.erase(0, 1);
}
if(strNuma[0] != '-' && strNumb[0] == '-')
{
signstate = 1;
strNumb.erase(0, 1);
}
while(strNumb[0] == '0')
strNumb.erase(0, 1);
if(strNumb.find(".") < strNumb.length())
{
while(strNumb[strNumb.length()-1] == '0')
strNumb.erase(strNumb.length()-1, 1);
}
if(strNumb == "" || strNumb == ".")//0
{
Arithmetic result = "Error: " + strNuma + " / " + "0";
return result;
}
while(strNuma[0] == '0')
strNuma.erase(0, 1);
if(strNuma.find(".") < strNuma.length())
{
while(strNuma[strNuma.length()-1] == '0')
strNuma.erase(strNuma.length()-1, 1);
}
if(strNuma == "" || strNuma == ".")
{
Arithmetic result = "0";
return result;
}
long ib = strNumb.find("."), ia = strNuma.find(".");
int state = 0;
if(ib < strNumb.length() && ia < strNuma.length())
state = 1;
if(ib > strNumb.length() && ia < strNuma.length())
state = 2;
if(ib < strNumb.length() && ia > strNuma.length())
state = 3;
switch(state)
{
case 1:
{
if(strNumb.length() - 1 - ib > strNuma.length() - 1 - ia)
{
long i = strNumb.length() - 1 - ib - (strNuma.length() - 1 - ia);
while(i--)
strNuma += "0";
}
else
{
long i = strNuma.length() - 1 - ia - (strNumb.length() - 1 - ib);
while(i--)
strNumb += "0";
}
strNuma.erase(ia, 1);
strNumb.erase(ib, 1);
break;
}
case 2:
{
long i = strNuma.length() - 1 - ia;
while(i--)
strNumb += "0";
strNuma.erase(ia, 1);
break;
}
case 3:
{
long i = strNumb.length() - 1 - ib;
while(i--)
strNuma += "0";
strNumb.erase(ib, 1);
break;
}
default:
break;
}
std::string strResult = numa.Dividle(strNuma, strNumb, Arithmetic::m_precision);
if(signstate == 1)
strResult.insert(0, "-");
Arithmetic result = strResult;
return result;
}
/*******************************************************\
函数: RootInteger
功能: 计算大数乘方(指数为整数)
输入: string
输出: 无
返回: 得数
备注: 无
\*******************************************************/
Arithmetic Arithmetic::RootInteger(std::string strNumber, std::string strRootInt)
{
Arithmetic number = strNumber, n = strRootInt, result = "1", one = "1";
for (Arithmetic i = "0";i < n;i = i + one)
result = result * number;
std::string strResult = result.GetData();
Arithmetic t = strResult;
return t;
}
/*******************************************************\
函数: < > <= >=
功能: 比较大小
输入: Arithmetic
输出: 无
返回: bool
备注: 无
\*******************************************************/
bool operator < (Arithmetic& numa, Arithmetic& numb)
{
Arithmetic result = numa - numb;
if(result.GetData()[0] == '-')
return 1;
return 0;
}
bool operator > (Arithmetic& numa, Arithmetic& numb)
{
Arithmetic result = numa - numb;
if(result.GetData()[0] == '-')
return 0;
return 1;
}
bool operator <= (Arithmetic& numa, Arithmetic& numb)
{
Arithmetic result = numa - numb;
if(result.GetData()[0] == '-' || result.GetData() == "0")
return 1;
return 0;
}
bool operator >= (Arithmetic& numa, Arithmetic& numb)
{
Arithmetic result = numa - numb;
if(result.GetData()[0] == '-' || result.GetData() == "0")
return 0;
return 1;
}
/*******************************************************\
函数: Factorial
功能: 计算数的阶乘
输入: 无
输出: 无
返回: string
备注: 无
\*******************************************************/
std::string Arithmetic::Factorial()
{
Arithmetic i = "2", result = "1", one = "1";
if(m_num.find(".") < m_num.length())
{
std::string t = "Error: ";
t += m_num;
return t;
}
if(m_num == "1" || m_num == "0")
{
std::string result = "1";
return result;
}
while(i <= *this)
{
result = result * i;
i = i + one;
}
std::string strResult = result.GetData();
return strResult;
}
/*******************************************************\
函数: Sin
功能: 计算三角函数
输入: 无
输出: 无
返回: string
备注: 将角度化简到360以内 弧度化简到2pai以内
\*******************************************************/
std::string Arithmetic::Sin()
{
if(m_num[0] == '-' && m_Tstate == 0)
{
Arithmetic pai2 = "360", zero = "0";
while(1)
{
*this = *this + pai2;
if(*this >= zero)
break;
}
}
if(m_num[0] == '-' && m_Tstate == 1)
{
Arithmetic pai2 = pai, two = "2", zero = "0";
pai2 = pai2 * two;
while(1)
{
*this = *this + pai2;
if(*this >= zero)
break;
}
}
if(m_Tstate == 0)//角度
{
std::string dig = "";
long i;
if((i = m_num.find(".")) < m_num.length())//取出小数部分,再对整数模360
{
dig = m_num.substr(i+1, m_num.length()-1 - i);
m_num.erase(i, m_num.length());
}
Arithmetic pai2 = "360", paihalf = "180", ipai = pai, strNumi = "";
strNumi = ipai / paihalf;
*this = *this % pai2;
std::string t = this->GetData();
if(dig != "")
{
t += ".";
t += dig;
}
double NumResult = atof(t.c_str());
NumResult = NumResult * atof(strNumi.GetData().c_str());
NumResult = sin(NumResult);
char* p = new char[20];
sprintf(p, "%f", NumResult);
t = p;
delete [] p;
if(t.find(".") < t.length())
{
while(t[t.length()-1] == '0')
t.erase(t.length()-1, 1);
if(t[t.length()-1] == '.')
t.erase(t.length()-1, 1);
}
return t;
}
if(m_Tstate == 1)//弧度
{
Arithmetic i = "2", ipai = pai, t = "";
ipai = ipai * i;
while(1)
{
t = *this - ipai;
if(t < ipai)
break;
*this = t;
}
double NumResult = atof(this->GetData().c_str());
NumResult = sin(NumResult);
char *p = new char [20];
sprintf(p, "%f", NumResult);
std::string result = p;
if(result.find(".") < result.length())
{
while(result[result.length()-1] == '0')
result.erase(result.length()-1, 1);
if(result[result.length()-1] == '.')
result.erase(result.length()-1, 1);
}
delete [] p;
return result;
}
}
/*******************************************************\
函数: Cos
功能: 计算三角函数
输入: 无
输出: 无
返回: string
备注: 将角度化简到360以内 弧度化简到2pai以内
\*******************************************************/
std::string Arithmetic::Cos()
{
if(m_num[0] == '-' && m_Tstate == 0)
{
Arithmetic pai2 = "360", zero = "0";
while(1)
{
*this = *this + pai2;
if(*this >= zero)
break;
}
}
if(m_num[0] == '-' && m_Tstate == 1)
{
Arithmetic pai2 = pai, two = "2", zero = "0";
pai2 = pai2 * two;
while(1)
{
*this = *this + pai2;
if(*this >= zero)
break;
}
}
if(m_Tstate == 0)//角度
{
std::string dig = "";
long i;
if((i = m_num.find(".")) < m_num.length())//取出小数部分,再对整数模360
{
dig = m_num.substr(i+1, m_num.length()-1 - i);
m_num.erase(i, m_num.length());
}
Arithmetic pai2 = "360", paihalf = "180", ipai = pai, strNumi = "";
strNumi = ipai / paihalf;
*this = *this % pai2;
std::string t = this->GetData();
if(dig != "")
{
t += ".";
t += dig;
}
double NumResult = atof(t.c_str());
NumResult = NumResult * atof(strNumi.GetData().c_str());
NumResult = cos(NumResult);
char* p = new char[20];
sprintf(p, "%f", NumResult);
t = p;
delete [] p;
if(t.find(".") < t.length())
{
while(t[t.length()-1] == '0')
t.erase(t.length()-1, 1);
if(t[t.length()-1] == '.')
t.erase(t.length()-1, 1);
}
return t;
}
if(m_Tstate == 1)//弧度
{
Arithmetic i = "2", ipai = pai, t = "";
ipai = ipai * i;
while(1)
{
t = *this - ipai;
if(t < ipai)
break;
*this = t;
}
double NumResult = atof(this->GetData().c_str());
NumResult = cos(NumResult);
char *p = new char [20];
sprintf(p, "%f", NumResult);
std::string result = p;
if(result.find(".") < result.length())
{
while(result[result.length()-1] == '0')
result.erase(result.length()-1, 1);
if(result[result.length()-1] == '.')
result.erase(result.length()-1, 1);
}
delete [] p;
return result;
}
}
/*******************************************************\
函数: Tan
功能: 计算三角函数
输入: 无
输出: 无
返回: string
备注: 将角度化简到180以内 弧度化简到pai以内
\*******************************************************/
std::string Arithmetic::Tan()
{
if(m_num[0] == '-' && m_Tstate == 0)
{
Arithmetic pai1 = "180", zero = "0";
while(1)
{
*this = *this + pai1;
if(*this >= zero)
break;
}
}
if(m_num[0] == '-' && m_Tstate == 1)
{
Arithmetic pai1 = pai, zero = "0";
while(1)
{
*this = *this + pai1;
if(*this >= zero)
break;
}
}
if(m_Tstate == 0)//角度
{
std::string dig = "";
long i;
if((i = m_num.find(".")) < m_num.length())//取出小数部分,再对整数模360
{
dig = m_num.substr(i+1, m_num.length()-1 - i);
m_num.erase(i, m_num.length());
}
Arithmetic pai1 = "360", paihalf = "180", ipai = pai, strNumi = "";
strNumi = ipai / paihalf;
*this = *this % pai1;
if(this->GetData() == "90")
{
std::string t = "None";
return t;
}
std::string t = this->GetData();
if(dig != "")
{
t += ".";
t += dig;
}
double NumResult = atof(t.c_str());
NumResult = NumResult * atof(strNumi.GetData().c_str());
NumResult = tan(NumResult);
char* p = new char[20];
sprintf(p, "%f", NumResult);
t = p;
delete [] p;
if(t.find(".") < t.length())
{
while(t[t.length()-1] == '0')
t.erase(t.length()-1, 1);
if(t[t.length()-1] == '.')
t.erase(t.length()-1, 1);
}
return t;
}
if(m_Tstate == 1)//弧度
{
Arithmetic i = "", ipai = pai, t = "", two = "2";
while(1)
{
t = *this - ipai;
if(t < ipai)
break;
*this = t;
}
t = ipai / two;
if(t.GetData() == this->GetData())
{
std::string t = "None";
return t;
}
double NumResult = atof(this->GetData().c_str());
NumResult = tan(NumResult);
char *p = new char [20];
sprintf(p, "%f", NumResult);
std::string result = p;
if(result.find(".") < result.length())
{
while(result[result.length()-1] == '0')
result.erase(result.length()-1, 1);
if(result[result.length()-1] == '.')
result.erase(result.length()-1, 1);
}
delete [] p;
return result;
}
}
/*******************************************************\
函数: Abs
功能: 取绝对值
输入: 无
输出: 无
返回: 绝对值
备注: 无
\*******************************************************/
Arithmetic Arithmetic::Abs()
{
if(m_num[0] == '-')
{
Arithmetic result = m_num.substr(1, m_num.length());
return result;
}
return *this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -