【问题标题】:Change a bunch of numbers into corresponding alphabets把一堆数字变成对应的字母
【发布时间】:2022-01-04 15:41:39
【问题描述】:

位编码有五种:11、10、01、001、000。每种位编码对应的信息是

11 -> 一个

10 -> B

01 -> C

001 -> D

000 -> E

编译结果如下:

Enter a sequence of bits: 01100001100110
CBEADB

我已经写了这个,但它不能工作。

#include<iostream>
#include<string>
using namespace std;

int main()
{
    int A, B, C, D, E ;
    string code;
    cout << "Enter a sequence of bits: ";
    cin >> code;
    int i = 0;
    while (i < code.length())
    {
        if( code[i]== 1 && code[i + 1]== 1 )
        {
            cout << 'A';
            i += 2;
        }
        else if ( code[i]== 1 && code[i + 1]== 0)
        {
            cout << 'B';
            i += 2;
        }
        else if ( code[i]== 0 && code[i + 1]== 1)
        {
            cout << 'C';
            i += 2;
        }
        else if ( code[i]== 0 && code[i + 1]== 0 && code[ i + 2]== 1)
        {
            cout << 'D';
            i += 3;
        }
        else if ( code[i]== 0 && code[i + 1]== 0 && code[ i + 2]== 0)
        {
            cout << 'E';
            i += 3;
        }
    }
    cout << endl;
    return 0;
}

【问题讨论】:

  • 向我们详细解释为什么这不起作用,你得到了什么,等等
  • code[i] 始终是一个字符,您正在将它与一个整数进行比较。
  • code[i] 将是 '0' (0x30) 或 '1' (0x31),而不是 0 和 1。

标签: c++ string if-statement decoder


【解决方案1】:

您的输入是字符串而不是数字,因此您必须检查字符。不要使用== 0 之类的东西,而应该使用== '0' 之类的东西。此外,您可能希望确保输入具有正确的格式并且不会陷入无限循环(通过永远运行while 而不更新i)或缓冲区溢出(又名。索引超出范围,通过检查code[i]i &gt;= code.length())。您可以执行以下操作:

#include<iostream>
#include<string>
using namespace std;

int main()
{
    int A, B, C, D, E ;
    string code;
    cout << "Enter a sequence of bits: ";
    cin >> code;
    int i = 0;
    while (i+1 < code.length())
    {
        if( code[i]== '1' && code[i + 1]== '1' )
        {
            cout << 'A';
            i += 2;
        }
        else if ( code[i]== '1' && code[i + 1]== '0')
        {
            cout << 'B';
            i += 2;
        }
        else if ( code[i]== '0' && code[i + 1]== '1')
        {
            cout << 'C';
            i += 2;
        }
        else if (i+2 < code.length()) 
        {
            if (code[i]== '0' && code[i + 1]== '0' && code[ i + 2]== '1')
            {
                cout << 'D';
                i += 3;
                }
                else if ( code[i]== '0' && code[i + 1]== '0' && code[ i + 2]== '0')
                {
                    cout << 'E';
                    i += 3;
                } 
                else 
                {
                    return 1;
                }
        }
        else 
        {
            return 1;
        }
    }
    if (i < code.length()) {
        return 1;
    }
    cout << endl;
    return 0;
}

你可以试试here

【讨论】:

    【解决方案2】:

    您的代码是正确的,您只需要使用字符文字而不是 int 文字,例如:

    if (code[i] == '1' && code[i + 1] == '1')
    

    字符串中的数字具有与其整数值不匹配的代码。 '0' 不是 0。因此,您必须使用'0''1'

    【讨论】:

      猜你喜欢
      • 2013-10-03
      • 1970-01-01
      • 2020-06-09
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      相关资源
      最近更新 更多