double a = 1377083362513770833626.0, b=1585054852315850548524.0;
printf("res = %.0f\n", a+b);
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ADD_THRES (sizeof("4294967295")-2) //两个9位整数相加不会溢出
#define MUL_THRES (sizeof("65535")-2) //两个4位整数相乘不会溢出
#define OTH_THRES (sizeof("4294967295")-1) //两个10位整数相减或相除不会溢出
typedef struct{
char *leftVal;
char *rightVal;
char operator;
}MATH_OPER;
void Addition(char *leftVal, char *rightVal,
char *resBuf, unsigned int resbufLen) {
unsigned int leftLen = strlen(leftVal);
unsigned int rightLen = strlen(rightVal);
unsigned char isLeftLonger = (leftLen>=rightLen) ? 1 : 0;
unsigned int longLen = isLeftLonger ? leftLen : rightLen;
if(resbufLen < longLen) { //possible carry + string terminator
fprintf(stderr, "Not enough space for result(cur:%u)!\n", resbufLen);
return;
}
char *longAddend = isLeftLonger ? leftVal : rightVal;
char *shortAddend = isLeftLonger ? rightVal : leftVal;
unsigned int diffLen = isLeftLonger ? (leftLen-rightLen) : (rightLen-leftLen);
//a carry might be generated from adding the most significant digit
if((leftLen == rightLen) && (leftVal[0]-'0'+rightVal[0]-'0' >= 9))
resBuf += 1;
unsigned int carry = 0;
int i = longLen-1;
for(; i >= 0; i--) {
unsigned int leftAddend = longAddend[i] - '0';
unsigned int rightAddend = (i<diffLen) ? 0 : shortAddend[i-diffLen]-'0';
unsigned int digitSum = leftAddend + rightAddend + carry;
resBuf[i] = digitSum % 10 + '0';
carry = (digitSum >= 10) ? 1 : 0;
}
if(carry == 1) {
resBuf -= 1;
resBuf[0] = '1';
}
else if(leftVal[0]-'0'+rightVal[0]-'0' == 9) {
resBuf -= 1;
resBuf[0] = ' '; //fail to generate a carry
}
}
void Subtraction(char *leftVal, char *rightVal,
char *resBuf, unsigned int resbufLen) {
int cmpVal = strcmp(leftVal, rightVal);
if(!cmpVal) {
resBuf[0] = '0';
return;
}
unsigned int leftLen = strlen(leftVal);
unsigned int rightLen = strlen(rightVal);
unsigned char isLeftLonger = 0;
if((leftLen > rightLen) || //100-10
(leftLen == rightLen && cmpVal > 0)) //100-101
isLeftLonger = 1;
unsigned int longLen = isLeftLonger ? leftLen : rightLen;
if(resbufLen <= longLen) { //string terminator
fprintf(stderr, "Not enough space for result(cur:%u)!\n", resbufLen);
return;
}
char *minuend = isLeftLonger ? leftVal : rightVal;
char *subtrahend = isLeftLonger ? rightVal : leftVal;
unsigned int diffLen = isLeftLonger ? (leftLen-rightLen) : (rightLen-leftLen);
//a borrow will be generated from subtracting the most significant digit
if(!isLeftLonger) {
resBuf[0] = '-';
resBuf += 1;
}
unsigned int borrow = 0;
int i = longLen-1;
for(; i >= 0; i--)
{
unsigned int expanSubtrahend = (i<diffLen) ? '0' : subtrahend[i-diffLen];
int digitDif = minuend[i] - expanSubtrahend - borrow;
borrow = (digitDif < 0) ? 1 : 0;
resBuf[i] = digitDif + borrow*10 + '0';
//printf("[%d]Dif=%d=%c-%c-%d -> %c\n", i, digitDif, minuend[i], expanSubtrahend, borrow, resBuf[i]);
}
//strip leading '0' characters
int iSrc = 0, iDst = 0, isStripped = 0;
while(resBuf[iSrc] !='\0') {
if(isStripped) {
resBuf[iDst] = resBuf[iSrc];
iSrc++; iDst++;
}
else if(resBuf[iSrc] != '0') {
resBuf[iDst] = resBuf[iSrc];
iSrc++; iDst++;
isStripped = 1;
}
else
iSrc++;
}
resBuf[iDst] = '\0';
}
#include<assert.h>
#define ASSERT_ADD(_add1, _add2, _sum) do{\
char resBuf[100] = {0}; \
Addition(_add1, _add2, resBuf, sizeof(resBuf)); \
assert(!strcmp(resBuf, _sum)); \
}while(0)
#define ASSERT_SUB(_minu, _subt, _dif) do{\
char resBuf[100] = {0}; \
Subtraction(_minu, _subt, resBuf, sizeof(resBuf)); \
assert(!strcmp(resBuf, _dif)); \
}while(0)
void VerifyOperation(void) {
ASSERT_ADD("22", "1686486458", "1686486480");
ASSERT_ADD("8888888888888", "1112", "8888888890000");
ASSERT_ADD("1234567890123", "1", "1234567890124");
ASSERT_ADD("1234567890123", "3333333333333", "4567901223456");
ASSERT_ADD("1234567890123", "9000000000000", "10234567890123");
ASSERT_ADD("1234567890123", "8867901223000", "10102469113123");
ASSERT_ADD("1234567890123", "8000000000000", " 9234567890123");
ASSERT_ADD("1377083362513770833626", "1585054852315850548524", "2962138214829621382150");
ASSERT_SUB("10012345678890", "1", "10012345678889");
ASSERT_SUB("1", "10012345678890", "-10012345678889");
ASSERT_SUB("10012345678890", "10012345678891", "-1");
ASSERT_SUB("10012345678890", "10012345686945", "-8055");
ASSERT_SUB("1000000000000", "999999999999", "1");
}
void CalcOperation(MATH_OPER *mathOper, char *resBuf, unsigned int resbufLen) {
unsigned int leftLen = strlen(mathOper->leftVal);
unsigned int rightLen = strlen(mathOper->rightVal);
switch(mathOper->operator) {
case '+':
if(leftLen <= ADD_THRES && rightLen <= ADD_THRES)
snprintf(resBuf, resbufLen, "%d",
atoi(mathOper->leftVal) + atoi(mathOper->rightVal));
else
Addition(mathOper->leftVal, mathOper->rightVal, resBuf, resbufLen);
break;
case '-':
if(leftLen <= OTH_THRES && rightLen <= OTH_THRES)
snprintf(resBuf, resbufLen, "%d",
atoi(mathOper->leftVal) - atoi(mathOper->rightVal));
else
Subtraction(mathOper->leftVal, mathOper->rightVal, resBuf, resbufLen);
break;
case '*':
if(leftLen <= MUL_THRES && rightLen <= MUL_THRES)
snprintf(resBuf, resbufLen, "%d",
atoi(mathOper->leftVal) * atoi(mathOper->rightVal));
else
break; //Multiplication: product = multiplier * multiplicand
break;
case '/':
if(leftLen <= OTH_THRES && rightLen <= OTH_THRES)
snprintf(resBuf, resbufLen, "%d",
atoi(mathOper->leftVal) / atoi(mathOper->rightVal));
else
break; //Division: quotient = dividend / divisor
break;
default:
break;
}
return;
}
int main(void) {
VerifyOperation();
char leftVal[100] = {0}, rightVal[100] = {0}, operator='+';
char resBuf[1000] = {0};
//As you see, basically any key can quit:)
printf("Enter math expression(press q to quit): ");
while(scanf(" %[0-9] %[+-*/] %[0-9]", leftVal, &operator, rightVal) == 3) {
MATH_OPER mathOper = {leftVal, rightVal, operator};
memset(resBuf, 0, sizeof(resBuf));
CalcOperation(&mathOper, resBuf, sizeof(resBuf));
printf("%s %c %s = %s\n", leftVal, operator, rightVal, resBuf);
printf("Enter math expression(press q to quit): ");
}
return 0;
}
[wangxiaoyuan_@localhost ~]$ gcc -Wall -o BigIntOper BigIntOper.c [wangxiaoyuan_@localhost ~]$ ./BigIntOper Enter math expression(press q to quit): 100+901 100 + 901 = 1001 Enter math expression(press q to quit): 100-9 100 - 9 = 91 Enter math expression(press q to quit): 1234567890123 + 8867901223000 1234567890123 + 8867901223000 = 10102469113123 Enter math expression(press q to quit): 1377083362513770833626 - 1585054852315850548524 1377083362513770833626 - 1585054852315850548524 = -207971489802079714898 Enter math expression(press q to quit): q [wangxiaoyuan_@localhost ~]$
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有