我想获取编码字符串作为输出。但不能够

发布于 2025-01-13 05:13:55 字数 1078 浏览 5 评论 0原文

输入:

101101110
1101

预期输出:

000000000011

我的输出:

它只是继续接受输入,而不显示任何输出。

请帮我 。我的代码有什么问题。任何帮助将不胜感激。我已经给出了变量的名称,以便于理解。此代码仅适用于发送方。

 #include <iostream>

using namespace std;

int main()
{
        string input;
        string polynomial;
        string encoded="";
        cin>>input;
        cin>>polynomial;
        int input_len=input.length();
        int poly_len=polynomial.length();
        encoded=encoded+input;
        for(int i=1;i<=poly_len-1;i++){
            encoded=encoded+'0';
        }
        for(int i=0;i<=encoded.length()-poly_len;){
            for(int j=0;j<poly_len;j++){
                if(encoded[i+j]==polynomial[j]){
                    encoded[i+j]=='0';
                }
                else{
                    encoded[i+j]=='1';
                }
            }
            while(i<encoded.length() && encoded[i]!='1'){
                i++;
            }
        }
        cout<<encoded;
    return 0;
}

Input:

101101110
1101

Expected Ouput:

000000000011

My output:

It just keeps on taking the input.and not showing any output.

Please help me . what is wrong with my code. Any help would be aprreciated.I have given the names of the variables such that its easy to understand.This code is only for the senders side.

 #include <iostream>

using namespace std;

int main()
{
        string input;
        string polynomial;
        string encoded="";
        cin>>input;
        cin>>polynomial;
        int input_len=input.length();
        int poly_len=polynomial.length();
        encoded=encoded+input;
        for(int i=1;i<=poly_len-1;i++){
            encoded=encoded+'0';
        }
        for(int i=0;i<=encoded.length()-poly_len;){
            for(int j=0;j<poly_len;j++){
                if(encoded[i+j]==polynomial[j]){
                    encoded[i+j]=='0';
                }
                else{
                    encoded[i+j]=='1';
                }
            }
            while(i<encoded.length() && encoded[i]!='1'){
                i++;
            }
        }
        cout<<encoded;
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千と千尋 2025-01-20 05:13:55

正确地看看这些行:

if (encoded[i + j] == polynomial[j]) {
    encoded[i + j] == '0'; // Line 1
}
else {
    encoded[i + j] == '1'; // Line 2
}

看到了吗?您正在使用 ==,而您应该使用 === 是一个比较运算符,它返回一个布尔值(true/false)。它不分配值。因此,要解决您的问题,请将上面的行替换为:

if (encoded[i + j] == polynomial[j]) {
    encoded[i + j] = '0'; // Replaced == with =
}
else {
    encoded[i + j] = '1'; // Replaced == with =
}

这应该可以解决您的问题。

Look at these lines properly:

if (encoded[i + j] == polynomial[j]) {
    encoded[i + j] == '0'; // Line 1
}
else {
    encoded[i + j] == '1'; // Line 2
}

See? You are using == while you should be using =. == is a comparison operator which returns a boolean (true/false). It does not assign values. So to fix your problem, replace the above lines with:

if (encoded[i + j] == polynomial[j]) {
    encoded[i + j] = '0'; // Replaced == with =
}
else {
    encoded[i + j] = '1'; // Replaced == with =
}

This should fix your problem.

む无字情书 2025-01-20 05:13:55

我给你的建议是熟悉调试。尝试在代码中添加一些断点,以便您可以实际看到后面发生了什么。检查代码后,这条线似乎给出了无限循环for(int i=0;i<=encoded.length()-poly_len;)。输入您提供给我们的信息后,条件在任何时候都不会成立。

My tip for you is to get familiar with debugging. Try adding some breakpoints in your code so you can actually see what is happening behind. After checking your code it seems that this line is giving an infinite loop for(int i=0;i<=encoded.length()-poly_len;). The condition won't be true at any point after entering the input you gave us.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文