#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
void sub(const char *big, const char *small, char *result);
void reverse_str(char * str);
void two_positive_add(const char *num1, const char *num2, char *result)
{
int count = 0;
int sum = 0; //每一位的和
int carry = 0; //进位
int end1 = strlen(num1)-1; //num1的个位
int end2 = strlen(num2)-1; //num2的个位
//计算相同位的和
while (end1>=0 && end2>=0)
{
//带进位的求和
sum = (num1[end1] ^ 0x30) + (num2[end2] ^ 0x30) +carry;
//处理进位
if (sum >9)
{
sum -= 10;
carry = 1;
}
else
carry = 0;
result[count++] = sum | 0x30;
--end1;
--end2;
}
//num1的位数大于num2的位数
while (end1>=0)
{
sum = (num1[end1] ^ 0x30) +carry;
if (sum >9)
{
sum -= 10;
carry = 1;
}
else
carry = 0;
result[count++] = sum | 0x30;
--end1;
}
//num2的位数大于num1的位数
if (end2>=0)
{
sum = (num2[end2] ^ 0x30) +carry;
if (sum >9)
{
sum -= 10;
carry = 1;
}
else
carry = 0;
result[count++] = sum | 0x30;
--end2;
}
//最后一步需要判断进位是否为0
if (carry!=0)
result[count++] = '1';
//主要添加是字符串结束符''
result[count] = '\0';
}
void positive_negactive_add(const char *positive, const char *negactive, char *result)
{
int len1 = strlen(positive) ; //positive的位数
int len2= strlen(negactive+1); //negactive除的位数
if (len1< len2)
result[0] = '-'; //结果为负数
if (len1== len2)
{
for (int i=0;i<len1;++i) //比较每一位的大小
if (positive[i]<negactive[i+1])
{
result[0] = '-';
break;
}
}
if (result[0] == '-') //结果为负数
sub(negactive+1,positive,result+1);
else //结果为正数
sub(positive,negactive+1,result);
}
void sub(const char *big, const char *small, char *result)
{
int count = 0; //result的位数
int borrow =0; //借位
int sum=0; //每一位的差
int end1 = strlen(big)-1; //big的个位
int end2 = strlen(small)-1; //small的个位
int data1 = 0;
int data2 = 0;
while (end1>=0 && end2>=0)
{
data1 = (big[end1] ^ 0x30)-borrow;
data2 = small[end2] ^ 0x30;
if (data1<data2)
borrow = 1;
else
borrow = 0;
if (borrow == 1)
sum = data1+10-data2;
else
sum = data1-data2;
result[count++] = sum | 0x30;
--end1;
--end2;
}
//将big的剩下的位添加上
while (end1>=0) //该处应为while
{
sum = (big[end1] ^ 0x30) -borrow;
borrow = 0;
result[count++] = sum | 0x30;
--end1;
}
//去掉结果前面无效的0
while (result[count-1] == '0' && count!=1)
--count;
result[count] = '\0';
}
void add (const char *num1, const char *num2, char *result)
{
if (num1 ==NULL || num2 ==NULL || result ==NULL)
return;
bool minus1 = false;
bool minus2 = false;
minus1 = (num1[0] == '-') ? true : false;
minus2 = (num2[0] == '-') ? true : false;
//两正数求和
if (minus1 == false && minus2 == false)
{
two_positive_add(num1,num2,result);
}
//一个正数与一个负数求和
if ((minus1 == false && minus2 ==true) || (minus1 == true && minus2 == false))
{
if ((minus1 == false && minus2 ==true))
positive_negactive_add(num1,num2,result);
else
positive_negactive_add(num2,num1,result);
result = (result[0] == '-') ? result+1 : result;
}
//两个负数求和
if (minus1 == true && minus2 ==true)
{
result[0] = '-';
result = result+1;
//转换为两个正数求和,从符号位后一位开始求和
two_positive_add(num1+1,num2+1,result);
}
//将result逆序过来
reverse_str(result);
}
void reverse_str(char * str)
{
char* p = str;
char* q = str+strlen(str)-1;
while (p<q)
{
char temp = *p;
*p = *q;
*q = temp;
++p;
--q;
}
}
int main()
{
char result[100];//保存结果的字符串
char num1[100];
char num2[100];
printf("请输入两个整数(整数之间用空格作为间隔符):");
scanf("%s %s",num1,num2);
add(num1,num2,result);
printf("The result is:");
printf("%s\n",result);
system("pause");
return 0;
}
#include <string.h>
#include <iostream>
using namespace std;
void sub(const char *big, const char *small, char *result);
void reverse_str(char * str);
void two_positive_add(const char *num1, const char *num2, char *result)
{
int count = 0;
int sum = 0; //每一位的和
int carry = 0; //进位
int end1 = strlen(num1)-1; //num1的个位
int end2 = strlen(num2)-1; //num2的个位
//计算相同位的和
while (end1>=0 && end2>=0)
{
//带进位的求和
sum = (num1[end1] ^ 0x30) + (num2[end2] ^ 0x30) +carry;
//处理进位
if (sum >9)
{
sum -= 10;
carry = 1;
}
else
carry = 0;
result[count++] = sum | 0x30;
--end1;
--end2;
}
//num1的位数大于num2的位数
while (end1>=0)
{
sum = (num1[end1] ^ 0x30) +carry;
if (sum >9)
{
sum -= 10;
carry = 1;
}
else
carry = 0;
result[count++] = sum | 0x30;
--end1;
}
//num2的位数大于num1的位数
if (end2>=0)
{
sum = (num2[end2] ^ 0x30) +carry;
if (sum >9)
{
sum -= 10;
carry = 1;
}
else
carry = 0;
result[count++] = sum | 0x30;
--end2;
}
//最后一步需要判断进位是否为0
if (carry!=0)
result[count++] = '1';
//主要添加是字符串结束符''
result[count] = '\0';
}
void positive_negactive_add(const char *positive, const char *negactive, char *result)
{
int len1 = strlen(positive) ; //positive的位数
int len2= strlen(negactive+1); //negactive除的位数
if (len1< len2)
result[0] = '-'; //结果为负数
if (len1== len2)
{
for (int i=0;i<len1;++i) //比较每一位的大小
if (positive[i]<negactive[i+1])
{
result[0] = '-';
break;
}
}
if (result[0] == '-') //结果为负数
sub(negactive+1,positive,result+1);
else //结果为正数
sub(positive,negactive+1,result);
}
void sub(const char *big, const char *small, char *result)
{
int count = 0; //result的位数
int borrow =0; //借位
int sum=0; //每一位的差
int end1 = strlen(big)-1; //big的个位
int end2 = strlen(small)-1; //small的个位
int data1 = 0;
int data2 = 0;
while (end1>=0 && end2>=0)
{
data1 = (big[end1] ^ 0x30)-borrow;
data2 = small[end2] ^ 0x30;
if (data1<data2)
borrow = 1;
else
borrow = 0;
if (borrow == 1)
sum = data1+10-data2;
else
sum = data1-data2;
result[count++] = sum | 0x30;
--end1;
--end2;
}
//将big的剩下的位添加上
while (end1>=0) //该处应为while
{
sum = (big[end1] ^ 0x30) -borrow;
borrow = 0;
result[count++] = sum | 0x30;
--end1;
}
//去掉结果前面无效的0
while (result[count-1] == '0' && count!=1)
--count;
result[count] = '\0';
}
void add (const char *num1, const char *num2, char *result)
{
if (num1 ==NULL || num2 ==NULL || result ==NULL)
return;
bool minus1 = false;
bool minus2 = false;
minus1 = (num1[0] == '-') ? true : false;
minus2 = (num2[0] == '-') ? true : false;
//两正数求和
if (minus1 == false && minus2 == false)
{
two_positive_add(num1,num2,result);
}
//一个正数与一个负数求和
if ((minus1 == false && minus2 ==true) || (minus1 == true && minus2 == false))
{
if ((minus1 == false && minus2 ==true))
positive_negactive_add(num1,num2,result);
else
positive_negactive_add(num2,num1,result);
result = (result[0] == '-') ? result+1 : result;
}
//两个负数求和
if (minus1 == true && minus2 ==true)
{
result[0] = '-';
result = result+1;
//转换为两个正数求和,从符号位后一位开始求和
two_positive_add(num1+1,num2+1,result);
}
//将result逆序过来
reverse_str(result);
}
void reverse_str(char * str)
{
char* p = str;
char* q = str+strlen(str)-1;
while (p<q)
{
char temp = *p;
*p = *q;
*q = temp;
++p;
--q;
}
}
int main()
{
char result[100];//保存结果的字符串
char num1[100];
char num2[100];
printf("请输入两个整数(整数之间用空格作为间隔符):");
scanf("%s %s",num1,num2);
add(num1,num2,result);
printf("The result is:");
printf("%s\n",result);
system("pause");
return 0;
}