求用C语言写的“大整数的四则运算”程序源代码,请看详细描述,刷分勿近!

求用C语言写的“大整数的四则运算”程序源代码,请看详细描述,刷分勿近!,第1张

/////这个是C++的,C++下比较模块化一点用的是字符串存储整数,比数组存储容易实现点。

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////1. 输入形式为:A[空格或换行]O[空格或换行]B

//// 2. 1中A、B为大数,O为运算符(如输入:123456789 / 432432)

//// 3. 既然处理大数,就没必要输入小数点位了

//// 4.加减不能处理负号,乘除可以

//// 5. 用于学习交流,若发现错误或不妥之处可联系

////////////////////////////////////////////////////////////////////////////////////歼激////////////////////////////////////////////

#include<iostream>

#include<string>

using namespace std

class BigFigure{

string num1,num2

string outcome

int precision

char operation

public:

BigFigure(){

num1=num2="0"

outcome="0"

precision=5

}

string&plus(string &, string &)

string&subtration(string &, string &)

string&multiplication(string &, string &)

string&division(string &, string &)

void show()

void BigFigureInterface()

string &revese(string&)

friend istream&operator>>氏滚袜(istream&i, BigFigure&a){

return i>>a.num1>>a.operation>>a.num2

}

~BigFigure(){ }

}

void BigFigure::show(){

cout<<"Result: "<<outcome<<endl

}

void BigFigure::BigFigureInterface(){

BigFigure a

cout<<备数"*********************************************************/n"

cout<<" Welcome... /n"

cout<<"Four Arithmetic Operations of Big Figures/n"

cout<<"*********************************************************/n/n"

cout<<"Notes:/n"

cout<<" 1. 输入形式为:A[空格或换行]O[空格或换行]B。/n"

cout<<" 2. 1中A、B为大数,O为运算符(如输入:123456789 / 432432)。/n"

cout<<" 3. 既然处理大数,就没必要输入小数点位了。/n"

cout<<" 4. 加减不能处理负号,乘除可以。/n"

cout<<" 5. 用于学习交流,若发现错误可联系[email protected]。/n/n"

cout<<"Now Start, Input 0 0 0 to end if you want to quit!/n/n"

cout<<"[BigFigure #] "

cin>>a

while(a.operation!='0'){

switch(a.operation){

case '+': a.plus(a.num1, a.num2)

a.show()break

case '-': a.subtration(a.num1, a.num2)

a.show()break

case '*': a.multiplication(a.num1, a.num2)

a.show()break

case '/': a.division(a.num1, a.num2)

a.show()break

default:cout<<a.operation<<" is not Arithmetic Operation./n"

}

cout<<"[BigFigure #] "

cin>>a

}

system("cls")

cout<<"/n/n/n/n/n/n/t/t Quited.../n/n/n/n/n/n/n"

system("pause")

}

string& BigFigure::revese(string&s){

char c

int t=s.size()

for(int i=0i<t/2i++){

c=s[i]

s[i]=s[t-i-1]

s[t-i-1]=c

}

return s

}

string&BigFigure::plus(string &str1, string &str2){//加法运算,未处理符号

int min=0,i,t=0

string temp

outcome.clear()

str1=revese(str1)

str2=revese(str2)

min=str1.size()<str2.size() ? str1.size() : str2.size()

for(i=0i<mini++){

temp+=(str1[i]+str2[i]-96+t)%10+48

t=(str1[i]+str2[i]-96+t)/10

}

if(min==str1.size()){

while(i<str2.size()){

temp+=(str2[i]+t-48)%10+48

t=(str2[i]-48+t)/10

i++

}

if(t) temp+='1'

}

else{

while(i<str1.size()){

temp+=(str1[i]+t-48)%10+48

t=(str1[i]-48+t)/10

i++

}

if(t) temp+='1'

}

outcome=revese(temp)

return outcome

}

string &BigFigure::subtration(string &str1, string &str2){//减法运算,未处理符号

int min=0,flag,i,t=0

string temp

outcome.clear()

if(str1.size()>str2.size() || (str1.size()==str2.size() &&str1.compare(str2)==1)){

str1=revese(str1)

str2=revese(str2)

flag=0

min=str2.size()

}

else if(str1.size()==str2.size()){

if(!str1.compare(str2)){

outcome='0'

return outcome

}

if(str1.compare(str2)==-1){

temp=str1

str1=revese(str2)

str2=revese(temp)

flag=1

min=str2.size()

}

}

else{

temp=str1

str1=revese(str2)

str2=revese(temp)

flag=1

min=str2.size()

}

temp.clear()

for(i=0i<mini++){

if(str1[i]-t<str2[i]){

temp+=str1[i]+10-t-str2[i]+48

t=1

}

else{

temp+=str1[i]-t-str2[i]+48

t=0

}

}

while(i<str1.size()){

if(!t){

while(i<str1.size()){

temp+=str1[i]

i++

}

break

}

if(str1[i]!='0'){ temp+=str1[i]-tt=0}

else temp+='9'

i++

}

string s

for(unsigned int k=temp.size()-1k>=0k--){

if(temp[k]!='0'){

for(int n=kn>=0n--)

s+=temp[n]

break

}

}

if(flag) s='-'+s

outcome=s

return outcome

}

string&BigFigure::multiplication(string &str1, string &str2){//乘法运算,已处理符号

char c='0',flag=0

string temp1,temp2="0"

if(str1[0]=='0' || str2[0]=='0'){

outcome="0"

return outcome

}

if(str1[0]=='-'){ flag++str1.erase(0,1)}

if(str2[0]=='-'){ flag++str2.erase(0,1)}

str1=revese(str1)

str2=revese(str2)

for(unsigned int i=0i<str2.size()i++){

c='0'

for(unsigned int j=0j<str1.size()j++){

temp1+=((str2[i]-48)*(str1[j]-48)+c-48)%10+48

c=((str2[i]-48)*(str1[j]-48)+c-48)/10+48

}

if(c!='0') temp1+=c

temp1=revese(temp1)

for(int k=0k<ik++)

temp1+='0'

temp2=plus(temp1, temp2)

temp1.clear()

}

if(flag%2) temp2='-'+temp2

outcome=temp2

return outcome

}

string&BigFigure::division(string &str1, string &str2){//除法运算,已处理符号

int str=0,flag=0,flag1=0,flag2=0

string temp,temps,tempt

if(str2=="0"){

outcome="Inf"

return outcome

}

if(str2=="1" || str1=="0"){

outcome=str1

return outcome

}

if(str1[0]=='-'){ flag++str1.erase(0,1)}

if(str2[0]=='-'){ flag++str2.erase(0,1)}

for(unsigned int i=0i<str1.size()i++){//整除处理

str=0

temp+=str1[i]

if(temp[0]=='0') temp.erase(0,1)

if(temp.size()>str2.size() ||

(temp.size()==str2.size() &&temp.compare(str2)>=0)){

while(temp.size()>str2.size() ||

(temp.size()==str2.size() &&temp.compare(str2)>=0)){

tempt=str2

temp=subtration(temp, tempt)

str++

}

temps+=str+48

}

else if(temp.size()==str2.size() &&temp.compare(str2)==0) temps+='1'

else temps+='0'

}

flag1=temps.size()

if(temp!="0" &&precision) temps+='.'

for(int i=0i<=precision &&temp!="0"i++){//小数后位的处理

str=0

temp+='0'

if(temp.size()>str2.size() ||

(temp.size()==str2.size() &&temp.compare(str2)>=0)){

while(temp.size()>str2.size() ||

(temp.size()==str2.size() &&temp.compare(str2)>=0)){

tempt=str2

temp=subtration(temp, tempt)

str++

}

temps+=str+48

}

else if(temp.size()==str2.size() &&temp.compare(str2)==0) temps+='1'

else temps+='0'

}

flag2=temps.size()-2

if(temps[temps.size()-1]>='5' &&flag2-flag1==precision){//四舍五入处理

temps.erase(flag1,1)

temps.erase(temps.size()-1, 1)

temp="1"

temps=plus(temps, temp)

if(temps.size()>flag2) temps.insert(flag1+1, 1, '.')

else temps.insert(flag1, 1, '.')

}

else if(flag2-flag1==precision) temps.erase(temps.size()-1, 1)

temp.clear()

for(unsigned int k=0k<temps.size()k++){//左端去零处理

if(temps[k]!='0' || temps[k+1]=='.'){

for(int n=kn<temps.size()n++)

temp+=temps[n]

break

}

}

if(flag%2) temp='-'+temp

outcome=temp

return outcome

}

int main(){//主函数

BigFigure a

a.BigFigureInterface()

return 0

魇鍅苮孒,你好:

你所谓的100×99×98×97×96×……×2×1,也就是100的阶乘,也可以简写作“100!”,如果你对数学历友有更深入点的接触的话,就会明白阶乘本身是没有简便运算可言的,只可以硬算,如果你想计算更大的数,我建议你上百度寻找计算阶乘的软件,听说多大的数都能计算出来。

如果你对计算机编程有所了解的话,我建议你可以自己编程计算这个问题,对于学过任何一门编程语言的人来说这都应该是一个非常简单的小程序罢了。

我可以跟你说这个阶乘的值为100!=9.332621544 e157 (就是9.332621544乘以10的157次幂)为近似数字,精确到小数点后9位。

下面给你两个解决途径:

1、在你电脑上的开始菜单中,进入附件,调出计算器,并在“查看”选项卡中选择“科学计算器”,你就可以计算阶乘了,方法是:输入数字100,再点击“n!”符号按钮。

2、如果懂得编程的话,下面给你一个程序源代码:

/**

*计算大数的阶乘,算法的主要思想就是将计算结果肢携槐的每一位用数组的一位来表示:如要计算5!,那么首先将

*(1) a[0]=1,然后a[0]=a[0]*2,a[0]=2,

*(2) a[0]=a[0]*3,a[0]=6

*(3) a[0]=a[0]*4,a[0]=24,此时a[1]=2,a[0]=4

*/

public class Factorial

{

static int a[] = new int [10000]

static void factorial(int n)

{

for(int i=2i<a.lengthi++)

a[i] = 0//将数组元素初始化

a[0] = 1//用数组的一项存放计算结果的位数

a[1] = 1//将第一项赋值为一

for(int j= 2j <= nj++)

{

int i=1

int c = 0//c表示向高位的进位

for(i <= a[0]i++)

{

a[i] = a[i] * j + c//将来自低位的计算结果和本位的结果相加

c = a[i] / 10

a[i] = a[i] % 10

}

for(c != 0i++)

{

a[i] = c%10

c = c / 10

}

a[0] = i - 1

}

}

public static void main(String[] args)

{

String num = args[0]

int count = 0

int n = Integer.parseInt(num)

f(n)

for(int i= a[0]i>0i--)

{

count++

System.out.print(/隐档*"a[" + i + "]=" + */a[i]/* + " "*/)

}

System.out.println("\n"+count)

}

}

参考资料:http://blog.csdn.net/hengshan/archive/2005/11/13/528778.aspx

祝你能够早日解决这个问题!如果我的回答对你有帮助,请采纳,谢谢!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12354965.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存