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.

我为每个英文字母分配了一个数字代码。我希望我的程序输出一个句子的等效数字代码。一个循环实际上应该为句子代码的每个数字迭代次数。因为我需要为每个数字的嵌入式应用程序打开/关闭引脚。我必须在同一字母的数字之间,两个字母之间以及两个句子之间添加时间差。第一个我想要结果代码的每个数字的简单输出。

这是我的代码。我已将字母分配给字符串并将代码分配给字符串数组类型。然后我从字符串数组中搜索字符的等效数字代码并将其保存为字符串类型。现在我想将字符串中的每个数字分配给 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

这是我如何解决我的问题的代码。感谢所有回复的人,尽管有些回复对我来说太复杂了。请查看它并指出它的任何缺点,尽管我正在寻找它的输出。

#include <iostream>
#include <string>
#include <sstream>
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()
{
    int n;
    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++){
              n = code[i] - '0';
                for(int i=0; i<n; i++){
                  cout<<"Here you go"<<"-"<<n<<endl;}}

        return 0;
        }
于 2013-09-02T11:57:37.120 回答
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'次数“ I III III III”。重读您的评论后,我相信这就是您想要的。

于 2013-08-28T14:31:48.690 回答
0

我认为您不应该使用名为的变量int,它是 C++ 和 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

我真的希望我明白你的意思。我试着像你在这里展示的那样制作一个程序,我想出了这个:

宏.h:

#ifndef MY_MACROS_H
#define MY_MACROS_H

#define VALUES(base)                \
                ADD_VALUE(0,base)   \
                ADD_VALUE(1,base)   \
                ADD_VALUE(2,base)   \
                ADD_VALUE(3,base)   \
                ADD_VALUE(4,base)   \
                ADD_VALUE(5,base)   \
                ADD_VALUE(6,base)   \
                ADD_VALUE(7,base)   \
                ADD_VALUE(8,base)   \
                VALUE(9,base)

#define VALUE(val,base) +1

#define ADD_VALUE(val,base) VALUE(val, base)


#define DIGIT_COUNT (0 VALUES(1))
#define BASE_COUNT  (DIGIT_COUNT + 1)

#define ALL_VALUES   ADD_ARRAY_VALUES(1)                                  \
                     ADD_ARRAY_VALUES(BASE_COUNT)                         \
                     ADD_ARRAY_VALUES(BASE_COUNT * BASE_COUNT)            \
                     ARRAY_VALUES(BASE_COUNT * BASE_COUNT * BASE_COUNT)

#define ARRAY_VALUES(rank) +1

#define ADD_ARRAY_VALUES(rank) ARRAY_VALUES(rank)

#define UNKNOWN_LETTER_PATTERN ""
#define ARRAYS_COUNT (0 ALL_VALUES)

#endif

页眉.h:

    #ifndef _MY_HEADER_H
#define _MY_HEADER_H

/*****
* Includes
******/
#include <iostream>
#include <string>
#include <vector>
#include "Macros.h"

using namespace std;

/*****
* Definitions
******/
#define FIRST_LETTER 'a'
#define LAST_LETTER  'z'

typedef unsigned char byte;

typedef int NumberValueType;

#define BASE_CHAR_VALUE '0'

// character 
#define CHAR_TO_CHAR_INDEX(character) (character - FIRST_LETTER) 
#define CHAR_TO_NUM_INDEX(character)  (character - BASE_CHAR_VALUE)

#define IS_CHAR_VALID(character) ((character >= FIRST_LETTER) && (character <= LAST_LETTER))

int ParseInput();
string texttopattern(char c);

#endif

和实现文件:(当然我也打印了并且没有对输出做任何重要的事情)。

主要.cpp:

    #include "Header.h"

int s_arValues[ARRAYS_COUNT][DIGIT_COUNT] =
{   
    #undef  VALUE
    #define VALUE(val,base) (base * val)

    #undef ADD_VALUE
    #define ADD_VALUE(val,base) VALUE(val, base),

    #undef ARRAY_VALUES
    #define ARRAY_VALUES(rank) { VALUES(rank) }

    #undef ADD_ARRAY_VALUES
    #define ADD_ARRAY_VALUES(rank) ARRAY_VALUES(rank),

    ALL_VALUES
};


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 (IS_CHAR_VALID(c))
        return code[CHAR_TO_CHAR_INDEX(c)];
    else
        return UNKNOWN_LETTER_PATTERN ;
}

int ParseInput()
{
    string ord;
    getline(cin, ord);
    vector<string> code;

    for(int i=0; i<ord.length(); i++)
    {
        code.push_back(texttopattern(tolower(ord[i])));
    }

    // Loop inputs
    for (vector<string>::iterator itInputs = code.begin(); itInputs != code.end(); itInputs++) 
    {
        if (*itInputs == UNKNOWN_LETTER_PATTERN)
        {
            // Don't know what to do with unknown chars like spaces - your problem
            cout << "unknown letter" << endl;
            continue;
        }

        int number = 0;
        int nInputSize = itInputs->size();

        // Loop every digit in the current pattern
        for (int nDigitIndex = 0; nDigitIndex < nInputSize; nDigitIndex++)
        {
            if(nDigitIndex >= sizeof(NumberValueType))
            {
                cout << number;
                number = 0;
                continue;
            }

            char cChar = itInputs->at(nDigitIndex);

            number += s_arValues[nInputSize - nDigitIndex - 1][CHAR_TO_NUM_INDEX(cChar)]; 
        }

        // At this point you can use number
        cout << number;
        cout << endl;
        cout << "End of letter" << endl;
    }

    cin.get();

    return 0;
}

void main()
{   
    ParseInput();
}
于 2013-08-28T23:12:12.743 回答