c语言编程 高精度加减法

输入:
输入的第一行是一个正整数,表示下面有几组数据需要计算。之后的每一行是两个十进制的正整数和一个运算符,每个整数可以由最多 500 个数字组成。运算符可以是加号或者减号。

输出:
对应着输入的每一行数据,输出计算的结果,每个结果占一行。

如输入
2
213914+23466123
32862934-23481243
输出
23680037
9381691

这个怎么做,好难啊.....

本来以为很简单,结果写着写着发现也不简单,用了2个小时。
#include<stdio.h>
#include <conio.h>
#include <string.h>
#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<n;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
{ /*保证减数大于被减数
*如果被减数大于减数,则交换2数,并设置变量
*/
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,"-");

/*消除减法结果高位的0*/
j=0;
while(s[j]==48)
j++;
strcat(result,s+j);
strcat(result,"\n");
}//else op
}//for i
printf("%s",result);
getch();
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-03-17
#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<num;i++)
{
fgets(string,100,stdin);
explain(string,num1,num2,somble);
printf("%d\n",cal(num1,num2,somble));
}
return 0;
}
第2个回答  2009-03-17
用字符串存储数据 并且初始化数组全部为'\0' 比如你把213914存储到数组a[10]里面 那么结果就是 a="000213914\0" 同理 b="023466123\0" 然后对应位相加 加完后逢十进一 进完后再保留个位数就好了 就和你小时候列竖式算加法一个道理 减法一个道理 但要注意进位
第3个回答  2019-08-23
等十分钟
在给你写
加法函数
好久没写程序了
本来以为十分钟能写好
。。。。。(修改:修复了个小bug)
void
plus(char
*a,
char
*b,
char
*c){
int
i,index_a,index_b,index_c,carry=0,ten='9'+1,temp_index_c;
index_a=strlen(a)-1;
//
index变量指向最末一个数字
index_b=strlen(b)-1;
index_c=index_a>index_b?
index_a:index_b;
temp_index_c=index_c;
if(index_a>=index_b){
for(i=index_b+1;i>=0;i--){
b[i+(index_a-index_b)]=b[i];
}
for(i=0;i<index_a-index_b;i++)
b[i]='0';
}
else{
for(i=index_a+1;i>=0;i--){
a[i+(index_b-index_a)]=a[i];
}
for(i=0;i<index_b-index_a;i++)
a[i]='0';
}
while(index_c>=0){
c[index_c]=a[index_c]+b[index_c]+carry-'0';
if(c[index_c]>=ten){
c[index_c]-=ten-'0';
carry=1;
}
else
carry=0;
index_c--;
}
if(carry==1){
for(i=temp_index_c;i>0;i--){
c[i+1]=c[i];
}
c[0]=1;
}
c[temp_index_c+1]=0;
}
第4个回答  2009-03-17
汗....这个已经很简单了,用字符数组存储数据进行处理.再用switch算加减法
第5个回答  2009-03-17
#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<num;i++)
{
fgets(string,100,stdin);
explain(string,num1,num2,somble);
printf("%d\n",cal(num1,num2,somble));
}
return 0;
}
第6个回答  2009-03-17
用字符串存储数据 并且初始化数组全部为'\0' 比如你把213914存储到数组a[10]里面 那么结果就是 a="000213914\0" 同理 b="023466123\0" 然后对应位相加 加完后逢十进一 进完后再保留个位数就好了 就和你小时候列竖式算加法一个道理 减法一个道理 但要注意进位
第7个回答  2019-08-23
等十分钟
在给你写
加法函数
好久没写程序了
本来以为十分钟能写好
。。。。。(修改:修复了个小bug)
void
plus(char
*a,
char
*b,
char
*c){
int
i,index_a,index_b,index_c,carry=0,ten='9'+1,temp_index_c;
index_a=strlen(a)-1;
//
index变量指向最末一个数字
index_b=strlen(b)-1;
index_c=index_a>index_b?
index_a:index_b;
temp_index_c=index_c;
if(index_a>=index_b){
for(i=index_b+1;i>=0;i--){
b[i+(index_a-index_b)]=b[i];
}
for(i=0;i<index_a-index_b;i++)
b[i]='0';
}
else{
for(i=index_a+1;i>=0;i--){
a[i+(index_b-index_a)]=a[i];
}
for(i=0;i<index_b-index_a;i++)
a[i]='0';
}
while(index_c>=0){
c[index_c]=a[index_c]+b[index_c]+carry-'0';
if(c[index_c]>=ten){
c[index_c]-=ten-'0';
carry=1;
}
else
carry=0;
index_c--;
}
if(carry==1){
for(i=temp_index_c;i>0;i--){
c[i+1]=c[i];
}
c[0]=1;
}
c[temp_index_c+1]=0;
}
第8个回答  2009-03-17
汗....这个已经很简单了,用字符数组存储数据进行处理.再用switch算加减法
相似回答
大家正在搜