| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1378 人关注过本帖
标题:求教:这个程序在用 DEV debug时,有死循环,执行时不会出现死循环。。。
只看楼主 加入收藏
Arther0919
Rank: 1
等 级:新手上路
帖 子:9
专家分:7
注 册:2019-9-11
结帖率:100%
收藏
 问题点数:0 回复次数:2 
求教:这个程序在用 DEV debug时,有死循环,执行时不会出现死循环。。。
//大整数乘法(传统法)
//用string类 元素之间无法相乘
#include<iostream>
#include<string>
using namespace std;

int b1  = 0;
int max1 = 0;
int index = -1;

int main(){
    string s1, s2, s3, s4;
    int a[210] = {0}, b[210] = {0}, c[40010] = {0}, d[40010] = {0};
    cin >> s1 >> s2;
    int i = 0, j = 0 ;
    i = s1.length();
    j = s2.length();                   //字符转数字。字符串类字符不能运算
    for(int t = 0; t < i; t++){
        s1[t] -= 48;
        a[t+1] = s1[t];

    }
    for(int t = 0; t < j; t++){
        s2[t] = s2[t] - 48;
        b[t+1] = s2[t];
    }
    while(j > 0){
        index++;
        int z = 0;
        d[40010] = {0};
        b1 = 0;
        i = s1.length();
        for(int l = 0; l < index; l++){
            d[z] = 0;
            z++;
        }
        while(i+1 > 0){                                 //因为进位,第一位元素定义为0
            d[z++] = (a[i]*b[j] + b1)%10;
            b1 =(a[i--]*b[j--])/10;
        }
        if(max1 < z){
            max1 = z;
        }
        int b2 = 0;
        for(int k = 0; k < z+1; k++){
            c[k] = (c[k] + d[k] + b2)%10;
            b2 = (c[k] + d[k])/10;
        }
    }
    if(c[max1] != 0){
        for(int i = 0; i < max1 + 1; i++){
            cout << c[max1-i];
        }
    }
    else{
        for(int i = 0; i < max1; i++){
            cout << c[max1-i-1];
        }
    }
    return 0;
}
搜索更多相关主题的帖子: debug index 死循环 int for 
2020-05-18 10:36
Arther0919
Rank: 1
等 级:新手上路
帖 子:9
专家分:7
注 册:2019-9-11
收藏
得分:0 
回复 楼主 Arther0919
这个大整数乘法程序,5*7 = 45,5*9 = 55;哥哥们能看看哪的问题吗?
2020-05-18 10:47
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
你这代码,若此多的变量,相当于进行了一次代码混淆,看不懂。

程序代码:
#include <iostream>
#include <string>
using namespace std;

std::string foo( const std::string& a, const std::string& b )
{
    std::string r( a.size()+b.size(), '0' );
    for( size_t i=0; i!=a.size(); ++i )
    {
        char carry = 0;
        for( size_t j=0; j!=b.size(); ++j )
        {
            carry += (a[a.size()-1-i]-'0') * (b[b.size()-1-j]-'0') + (r[r.size()-1-i-j]-'0');
            r[r.size()-1-i-j] = carry%10 + '0';
            carry /= 10;
        }
        r[a.size()-1-i] = carry%10 + '0';
    }
    size_t index = r.find_first_not_of("0");
    return index==std::string::npos ? "0" : r.substr(index);
}

int main( void )
{
    cout << foo("00099","000") << endl;
    cout << foo("00099","00098") << endl;
    cout << foo("5","7") << endl;
    cout << foo("5","9") << endl;
}

2020-05-18 13:40
快速回复:求教:这个程序在用 DEV debug时,有死循环,执行时不会出现死循环。。 ...
数据加载中...
 
   



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

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