我想获取编码字符串作为输出。但不能够
输入:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确地看看这些行:
看到了吗?您正在使用
==
,而您应该使用=
。==
是一个比较运算符,它返回一个布尔值
(true/false)。它不分配值。因此,要解决您的问题,请将上面的行替换为:这应该可以解决您的问题。
Look at these lines properly:
See? You are using
==
while you should be using=
.==
is a comparison operator which returns aboolean
(true/false). It does not assign values. So to fix your problem, replace the above lines with:This should fix your problem.
我给你的建议是熟悉调试。尝试在代码中添加一些断点,以便您可以实际看到后面发生了什么。检查代码后,这条线似乎给出了无限循环
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.