| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 296 人关注过本帖
标题:大整数的运算帮忙看看那里错了啊
取消只看楼主 加入收藏
Arise
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2012-12-2
结帖率:60%
收藏
已结贴  问题点数:5 回复次数:0 
大整数的运算帮忙看看那里错了啊
#include <stdio.h>
#include <string.h>

void ReadBigInt(char number[])
{
    int length = 0, index = 0;
    printf("请输入大整数: ");
    fflush(stdin);
    gets(number);
    length = strlen(number);
    for(index = 0; index < length / 2; index++)
    {
        int tmp = number[index];
        number[index] = number[length - 1 - index];
        number[length - 1 - index] = tmp;
    }
}

void PrintBigInt(char number[])
{
    int index = 0,length = strlen(number);
    for(index = length -1 ;index >= 0;index--)
    {
        printf("%c", number[index]);
    }
   
}



void AddString(char small[], char big[], char result[])
{
    int i = 0 ,smallLen = strlen(small),
        bigLen = strlen(big);
    int carry =0;
    for(i = 0; i < smallLen; i++)
        {
            result[i] = small[i] - '0'
                + big[i] - '0' + carry;
            carry = result[i] / 10;
            result[i] = result[i] % 10 + '0';
        }
        for(; i < bigLen; i++)
        {
            result[i] = big[i] - '0' + carry;
            carry = result[i] / 10;
            result[i] = result[i] % 10 + '0';
        }
        if(carry)
            result[i++] = carry + '0';
        result[i] = '\0';   
}

void Add(char left[], char right[], char result[])
{
    int leftLen = strlen(left);
    int rightLen = strlen(right);
    if(leftLen > rightLen)
    {
        AddString(right, left, result);
    }
    else
    {
        AddString(left, right,result);
    }

}

void Substract(char big[], char small[], char result[])
{
    int borrow = 0;
    int index = 0;
    int smallLength = strlen(small);
    int bigLength = strlen(big);
    for(; index < smallLength; index++)
    {
        result[index] = (big[index] - '0') -
        (small[index] - '0') - borrow;
        if(result[index] < 0)
        {
            result[index] =result[index] + 10 ;
            borrow = 1;
        }
        else
            borrow = 0;
        result[index] = result[index] + '0';
    }
    for(; index < bigLength; index++)
    {
        result[index] = (big[index] - '0')  - borrow;
        if(result[index] < 0)
        {
            result[index] =result[index] + 10 ;
            borrow = 1;
        }
        else
            borrow = 0;        
        result[index] = result[index] + '0';
    }
    result[index] = 0;
}


void main()
{
    char choice = 0;
    char left[1000] = {0}, right[1000] = {0},
        result[1000] = {0};
    printf("请输入运算符: (+,-,*,/)");
    scanf("%c", &choice);


    ReadBigInt(left);
    ReadBigInt(right);
    Substract(left,right,result);
    PrintBigInt(result);

}
搜索更多相关主题的帖子: index void include number 
2013-01-04 14:15
快速回复:大整数的运算帮忙看看那里错了啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038773 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved