Ionizing_Radiation 2016-07-01 15:36 采纳率: 100%
浏览 1141
已采纳

C艹菜鸟请教一下大数相乘问题

本人大二狗,学过一点C基础,最近在学C艹,偶然看到大数相乘问题,想试试,于是写出了下面的代码。

#include<iostream>
#include<cstring>
#include<fstream>

void Multiply(char* str1, char* str2, char* Result);//相乘函数
void strRev(char* str);//字符串Reverse函数
void str2int(char* str);//将char - '0' 得到其代表的数值
void Carry(char* str); //计算进位函数
void int2str(char* str); //将所数值转换回 char

#define debug

int main()
{
    #ifdef debug
    std::ifstream in("Pr4_in.txt");
    std::ofstream out("Pr4_out.txt");
    std::cin.rdbuf(in.rdbuf());
    std::cout.rdbuf(out.rdbuf());
    #endif // debug

    char Number_1[102], Number_2[102], Result[204]={'0'};
    std::cin >> Number_1 >> Number_2;
    Multiply(Number_1, Number_2, Result);
//    std::cout << Number_1 << std::endl << Number_2 << std::endl;
    std::cout << Result << std::endl;
    return 0;
}

void Multiply(char str1[], char str2[], char Result[])
{
    int len1 = strlen(str1), len2 = strlen(str2); //Get the length.
    strRev(str1); strRev(str2); //Reverse the string.
//Simulate the manual computing process;
    str2int(str1); str2int(str2);
//Initiate the result.
    for(int i=0; i<203; ++i)
        Result[i] = '*';
        Result[203] = 0;


    for(int i=0; i<len1; ++i){
        for(int j=0; j<len2; ++j){
            int tmp= (int)str1[i] * (int)str2[j];
            if(Result[i+j] != '*')
                Result[i+j] += tmp;
            else Result[i+j] = tmp;
//            if(tmp < 10){
//                Result[i+j] += tmp;
//                Carry(Result);
//            }
//            else{
//                int teens = tmp/10;
//                tmp %= tmp;
//                Result[i+j+1] += teens;
//                Carry(Result);
//            }
            Carry(Result);
        }
    }
    int2str(Result);
    strRev(Result);
}

void strRev(char* str)
{
    int low = 0, high = strlen(str) - 1;
    while(low < high)
    {
        char tmp = str[low];
        str[low] = str[high];
        str[high] = tmp;
        ++low; --high;
    }
}

void str2int(char* str)
{
    int len = strlen(str);
    for(int i=0; i < len; ++i){
        str[i] -= '0';
    }
}

void Carry(char* str)
{
    int i=0;
    while(str[i]){
        if( str[i] >= 10 ){
            int tmp = str[i];
            str[i] = tmp % 10;
            str[i+1] += tmp / 10;
        }
        ++i;
    }
}

void int2str(char* str)
{
    int i = 0;
    while(str[i] != '*'){
        str[i] += '0';
        ++i;
    }
    str[i] = '\0';
}

本人所用IDE: Code::Blocks, Compiler: g++;
所出现的问题:图片说明
输出空串。

  • 写回答

1条回答

  • devmiao 2016-07-01 15:55
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog