导航:首页 > 编程知识 > 编程高精度加法怎么写char

编程高精度加法怎么写char

发布时间:2023-08-30 01:58:48

『壹』 c语言编程 高精度加减法

本来以为很简单,结果写着写着发现也不简单,用了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();
}

阅读全文

与编程高精度加法怎么写char相关的资料

热点内容
海康威视手机app怎么看不了 浏览:482
wordpress下载中心插件 浏览:402
微信限制字数是多少 浏览:20
策划输出主要从哪些文件来 浏览:174
网络营销找什么工作 浏览:372
tcl匹配文件名的正则表达式 浏览:461
音频文件数据量为何8 浏览:534
有哪些分享学习的网站 浏览:174
小程序文件发到微信电脑如何打开 浏览:265
四星五星做号工具 浏览:920
qq邮箱怎么发送视频文件 浏览:453
能免费开店的网站有哪些 浏览:548
中国网络营销发展史 浏览:828
违法红头文件有哪些 浏览:43
浏览器单文件版 浏览:982
腾讯视频离线缓存文件 浏览:944
如何让教育过好网络关 浏览:906
新版本vk10001p 浏览:13
现在哪里开设了编程课 浏览:230
漫一网络培训学校有哪些 浏览:752

友情链接