永发信息网

c语言编程 高精度加减法

答案:4  悬赏:60  手机版
解决时间 2021-01-06 16:30
  • 提问者网友:眉目添风霜
  • 2021-01-06 05:32
c语言编程 高精度加减法
最佳答案
  • 五星知识达人网友:像个废品
  • 2021-01-06 05:57
本来以为很简单,结果写着写着发现也不简单,用了2个小时。
#include
#include
#include
#include "stdlib.h"

void main()
{
int n=0,i=0,j=0,k=0,b=0;
char a[3][500]={0};
int n1=0,n2=0;
char s[500]={0};
int n3=0;
int c=0,c1=0;
int temp=0;
char op;
char str[1001]={0};
char *result;

scanf("%d",&n);
result=(char *)malloc(501*n);//根据输入的n申请内存空间
*result='\0';


for(;i {
//gets(str);
for(j=0;j<500;j++)
{
a[0][j]='\0';a[1][j]='\0';a[2][j]='\0';
s[j]='\0';
str[j]='\0';
str[1000-j]='\0';
}
c=0;c1=0;
k=0;
n1=0;n2=0;n3=0;


scanf("%s",&str);
for( j=0;str[j];j++ )
{
if( str[j]!='+' && str[j]!='-')
a[k][j-n1]=str[j];
else
{
op=str[j];
k=1;
n1=strlen(a[0])+1;
}
}//for j
n1-=2;
n2=strlen(a[1])-1;
n3=n1>n2?n1:n2;


if(op=='+')
{
for(;n1>=0&&n2>=0;n1--,n2--,n3--)
{
temp=a[0][n1]+a[1][n2]-96;
temp+=c;
if(temp>=10)
{
s[n3]=temp%10+48;
c=1;
}
else
{
s[n3]=temp+48;
c=0;
}
}//for
while(n1>=0)
{
temp=a[0][n1]-48;
temp+=c;
if(temp==10)
{
s[n3]=48;
c=1;
}
else
{
s[n3]=temp+48;
c=0;
}
n1--;
n3--;
}//while n1
while(n2>=0)
{
temp=a[1][n2]-48;
temp+=c;
if(temp==10)
{
s[n3]=48;
c=1;
}
else
{
s[n3]=temp+48;
c=0;
}
n2--;
n3--;
}//while n2
if(c)
strcat(result,"1");
strcat(result,s);
strcat(result,"\n");
}//if op


else
{
if(strcmp(a[0],a[1])<0)
{
//a[2]=a[0];a[0]=a[1];a[1]=a[2];
for(b=0;b<3;b++)
{
j=(b+2)%3;
for(k=0;k<=n2;k++)
a[j][k]=a[b][k];
}
n2=n1;n1=n3;
c1=1;//正为0,负为1
}


for(;n2>=0;n1--,n2--,n3--)
{
temp=a[0][n1]-a[1][n2];
temp-=c;
if(temp>=0)
{
s[n3]=temp+48;
c=0;
}
else
{
s[n3]=temp+58;
c=1;
}
}//for
while(n1>=0)
{
temp=a[0][n1]-48;
temp-=c;
if(temp>=0)
{
s[n3]=temp+48;
c=0;
}
else
{
s[n3]=temp+58;
c=1;
}
n1--;
n3--;
}

if(c1)
strcat(result,"-");


j=0;
while(s[j]==48)
j++;
strcat(result,s+j);
strcat(result,"\n");
}//else op
}//for i
printf("%s",result);
getch();
}
全部回答
  • 1楼网友:行雁书
  • 2021-01-06 07:54
用字符串存储数据 并且初始化数组全部为'\0' 比如你把213914存储到数组a[10]里面 那么结果就是 a="000213914\0" 同理 b="023466123\0" 然后对应位相加 加完后逢十进一 进完后再保留个位数就好了 就和你小时候列竖式算加法一个道理 减法一个道理 但要注意进位
  • 2楼网友:掌灯师
  • 2021-01-06 07:37
汗....这个已经很简单了,用字符数组存储数据进行处理.再用switch算加减法
  • 3楼网友:煞尾
  • 2021-01-06 07:28
#include"stdio.h"
#include"stdlib.h"
#include"ctype.h"
#include"string.h"
#define ERROR -999999999
int
cal(int fri,int sec,char som)
{
if(som=='+')
return (fri +sec );
else if(som=='-')
return (fri - sec);
else
return ERROR;
}
int explain(char *str,int &num1,int &num2,char &som)
{
char *str1,*pstr1;
char *str2,*pstr2;
str1=(char*)malloc(20);
str2=(char*)malloc(20);
pstr1=str1;
pstr2=str2;
while(isdigit(*str))
{
*pstr1=*str;
str++;
pstr1++;
}
*pstr1='\0';
num1=(int)strtol(str1,NULL,10);
while((*str!='+')&&(*str!='-'))
str++;
if(isdigit(*str))
return ERROR;
else
som=*str;
str++;
while(isdigit(*str))
{
*pstr2=*str;
str++;
pstr2++;
}
*pstr2='\0';
num2=(int)strtol(str2,NULL,10);
free(str1);
free(str2);
}
int
main(void)
{
int i;
int num,num1,num2;
char *string;
char somble;
string=(char*)malloc(100);
printf("enter the num:\n");
scanf("%d",&num);
fflush(stdin);
for(i=0;i {
fgets(string,100,stdin);
explain(string,num1,num2,somble);
printf("%d\n",cal(num1,num2,somble));
}
return 0;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯