那天有人出的题目,我回去写了,精度可以任意.这里的除法用到了乘和减,但是又点问题,我过几天再贴.还有先申明,我得英文不好.还有这个程序写的不太好有待完善的地方.
include <stdio.h>
#include <stdlib.h>
#define LONG (100) //加法或乘法结果的精度
void Make(char *p);
void GetString(char *p);
void Add(int *sum,char *number1,char *number2);//加法
void ChenFa(int *sum,char *number1,char *number2);//乘法
void JianFa(int *sum,char *number1,char *number2);//减法
int BiJiao(char *number1,char *number2);//比较两个字符串数字的大小
int p,p1,p2;
void main()
{
char string1[LONG];
char string2[LONG];
int temp;
int sum[2*LONG-1],sum1[LONG-1],sum2[LONG-1],sum3[LONG-1];
GetString(string1);
GetString(string2);
Make(string1);
Make(string2);
Add(sum2,string1,string2);
JianFa(sum1,string1,string2);
ChenFa(sum,string1,string2);
for(temp=0;temp<LONG-1;temp++)
{
if(sum[temp]!=0){
p=temp;
break;
}
}
for(temp=0;temp<LONG-1;temp++)
{
if(sum1[temp]!=0){
p1=temp;
break;
}
}
for(temp=0;temp<LONG-1;temp++)
{
if(sum2[temp]!=0){
p2=temp;
break;
}
}
JianFa(sum1,string1,string2);
ChenFa(sum,string1,string2);
Add(sum2,string1,string2);
printf("The JianFa result is:");
for(temp=p1;temp<LONG-1;temp++)
{
printf("%d",sum1[temp]);
}
printf("\n");
printf("The ChenFa result is:");
for(temp=p;temp<LONG-1;temp++)
{
printf("%d",sum[temp]);
}
printf("\n");
printf("The Add result is:");
for(temp=p2;temp<LONG-1;temp++)
{
printf("%d",sum2[temp]);
}
printf("\n");
}
void Make(char *p){//对字符串重新排列
int i,j;
for(i=0;p[i]>47;i++){
}
for(j=LONG-1;j>=0;j--,i--){
if(i<0){
p[j]=48;
}
else
p[j]=p[i];
}
}
void GetString(char *p){//输入字符串
printf("please input number2:");
for(int temp=0;temp<LONG-1;temp++){
scanf("%c",&p[temp]);
if(p[temp]<=47){
p[temp]=0;
break;
}
}
}
void Add(int *sum,char *number1,char *number2){//加法
int i[LONG-1],j[LONG-1];
for(int temp=0;temp<LONG-1;temp++){
sum[temp]=0;
}
for(temp=LONG-2;temp>=0;temp--){
i[temp]=number1[temp]-48;
j[temp]=number2[temp]-48;
sum[temp]=sum[temp]+i[temp]+j[temp];
sum[temp-1]=sum[temp-1]+(sum[temp])/10;
sum[temp]=(sum[temp])%10;
for(int m=0;m<temp+1;m++)
{
sum[temp-2-m]=sum[temp-2-m]+(sum[temp-1-m])/10;
sum[temp-1-m]=sum[temp-1-m]%10;
}
}
}
void JianFa(int *sum,char *number1,char *number2){
int i[LONG-1],j[LONG-1];
int temp;
for(temp=LONG-2;temp>=0;temp--){
if(BiJiao(number1,number2)){
i[temp]=i[temp]+number1[temp]-48;
j[temp]=j[temp]+number2[temp]-48;
}
else{
j[temp]=j[temp]+number1[temp]-48;
i[temp]=i[temp]+number2[temp]-48;
}
if(i[temp]-j[temp]<0){
i[temp]=i[temp]+10;
i[temp-1]=i[temp-1]-1;
}
sum[temp]=(i[temp]-j[temp]);
if(sum[temp-1]==0) break;
}
if(BiJiao(number1,number2)==0) sum[p1]=-sum[p1];
}
int BiJiao(char *number1,char *number2){
int i[LONG-1],j[LONG-1];
for(int temp=0;temp<LONG-1;temp++){
i[temp]=i[temp]+number1[temp]-48;
j[temp]=j[temp]+number2[temp]-48;
if(i[temp]>j[temp]){
return 1;
}
else if(i[temp]<j[temp]){
return 0;
}
}
}
void ChenFa(int *sum,char *number1,char *number2){//乘法
int i[LONG-1],j[LONG-1];
int m;
for(int s=0;s<2*LONG-1;s++){
sum[s]=0;
}
for(int temp=0;temp<LONG-1;temp++){
for(int temp1=0;temp1<LONG-1;temp1++){
if((number1[temp]>47)&&(number2[temp1]>47)&&(number1[temp]<59)&&(number2[temp1]<59)){
i[temp]=number1[temp]-48;
j[temp1]=number2[temp1]-48;
sum[temp1+temp-LONG+2]=sum[temp1+temp-LONG+2]+(i[temp]*j[temp1])%10;
sum[temp1+temp-LONG+1]=sum[temp1+temp-LONG+1]+(i[temp]*j[temp1])/10+(sum[temp1+temp-LONG+2])/10;
sum[temp1+temp-LONG+2]=sum[temp1+temp-LONG+2]%10;
}
}
for(m=0;m<temp+1;m++)
{
sum[temp1+temp-LONG+1-m]=sum[temp1+temp-LONG+1-m]+(sum[temp1+temp-LONG+2-m])/10;
sum[temp1+temp-LONG+2-m]=sum[temp1+temp-LONG+2-m]%10;
}
}
}