0

I have actually this code for an embedded application. I am 1st trying to make it for plain c++ so that I may have concept clear.

英語のアルファベットごとに数字コードが割り当てられています。私のプログラムが文に対して同等の数字コードを出力することを望みます。ループは、実際には文のコードの各桁に対して何回も反復する必要があります。桁ごとに組み込みアプリケーションのピンをオン/オフする必要があるためです。同じ文字の桁間、2 つの文字間、および 2 つの文間の時間差を後で追加する必要があります。1 つ目は、結果のコードの桁ごとに単純な出力が必要でした。

これが私のコードです。アルファベットを文字列に割り当て、コードを文字列配列型に割り当てました。次に、文字列配列から文字の同等の数字コードを検索し、文字列型で保存します。今、私は文字列からintに各数字を割り当て、そのためにループしたいと考えています。しかし、文字列値をintに割り当てるのに問題があります。私は C++ プログラミングの経験があまりありません。

編集 最初に文字列をintに変換するのに問題があります.全体的に、私の問題を解決するためのこのロジックは問題ないようです.

これが私のコードです。これが私の問題を解決しようとしている方法です。

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


        string texttopattern(char c)
        {

    //Hello world again
    //125-15-123-123-135 1346-135-1235-123-145 1-1245-1-24-1345

        string alphabet = "abcdefghijklmnopqrstuvwqyz"; //osv
        string code[] = {"1","12","14","145", "15", "124", "1245",
                          "125", "24", "245", "13", "123", "134",
                          "1345", "135", "1234", "12345", "1235", "234", "2345",
                          "136", "1236", "1346", "13456", "1356", "12346"};
          int index = alphabet.find(c);
            if(index!=-1)
                return code[index];
             else
                return " ";
        }

int main()
{
    string ord;
    getline(cin, ord);
    string code="";
        for(int i=0; i<ord.length(); i++)
        {
        code += texttopattern(ord[i]);
        }

        for(int i=0; i<code.length(); i++) {
            int n = code[i]; //assign a single digit value from string to an   
                             //int,string2int assign value is problem here !!
              while(n>0){    //loop for n times & manipulate PIN in each iteration
                cout<<"loop";
                n--;      }
        //cout<<code[i]<<endl;
        }

        return 0;
}
4

4 に答える 4

0

intC++ および C のキーワードであるという名前の変数を使用しないでください。

したがって、少なくとも while ループでエラーが発生します。while ループを次のように変更します。

       int j = code[i] - '0'; // this will get the number from the char 
       while(j>0){
           cout<<"loop";
           --j;      
       }
于 2013-08-28T13:19:34.817 に答える
0

編集:あなたのコメントに応えて、私は文字列をパターンとして使用することに戻りました:

#define UNKNOWN_LETTER_PATTERN ""

string texttopattern(char c)
{
    string code[] = {"1","12","14","145", "15", "124", "1245",
                      "125", "24", "245", "13", "123", "134",
                      "1345", "135", "1234", "12345", "1235", "234", "2345",
                      "136", "1236", "1346", "13456", "1356", "12346"};
    if (c >= 'a' && c <= 'z')
        return code[c - 'a'];
    else
        return UNKNOWN_LETTER_PATTERN ;
}

の最初の部分はmain()同じままです:

int main()
{
    string ord;
    getline(cin, ord);
    vector<string> code;
    for(int i=0; i<ord.length(); i++)
    {
        code.push_back(texttopattern(ord[i]));
    }

残りを更新し、いくつかのコメントを追加して、うまくいけば私がやっていることを説明します:

    // Loop patterns
    for (vector<string>::iterator it = code.begin(); it != code.end(); it++) 
    {
        if (*it == UNKNOWN_LETTER_PATTERN )
        {
            // Don't know what to do with unknown chars like spaces - your problem
            cout << "unknown letter" << endl;
        }
        else
        {
            // Loop every digit in the current  pattern
            for (int i = 0; i < it->size(); i++)
            {
                char c = it->at(i);

                // There surely is a better way to do the following, atoi or
                // stringstream come to mind
                // But for a single digit number, this should be enough for
                // now to convert a char to a number.
                int number = c - '0'; 

                // loop until number is reached
                for (int j = 0; j < number; j++)
                    cout << "I";
                cout << endl;
            }
            cout << "End of letter" << endl;
        }
    }

    cin.get();

    return 0;
}

編集:上記のサンプルを変更しました。たとえば、入力は 'd' です -> コード "145" に解決します -> すべての桁をループし、数値 [1, 4, 5] に変更します ->桁ごとに 'I' をnumber回出力します」 ⅠⅢⅢⅢ」。あなたのコメントを読み直した後、これはあなたが望んでいたものだと思います.

于 2013-08-28T14:31:48.690 に答える