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
。
我为每个英文字母分配了一个数字代码。我希望我的程序输出一个句子的等效数字代码。实际上,循环应该为句子的每个数字迭代次数。我需要为每个数字的嵌入式应用程序打开/关闭引脚。我必须在相同字母的数字之间,两个字母之间添加时差,&之后两句话之间。第一,我希望得到的代码的每个数字都是纯输出。
这是我的代码。我已经为String&分配了字母表。代码为String数组类型。然后我从String array&中搜索字符的等效数字代码。将其保存为字符串类型。现在我想将每个数字从字符串分配给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;
}
答案 0 :(得分: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;
}
答案 1 :(得分: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' - &gt;解析代码“145” - &gt;循环每个数字并转到数字[1,4,5] - &gt;为每个数字“I IIII IIIII”打印'I'数字次。重读评论后,我相信这就是你想要的。
答案 2 :(得分:0)
我真的希望我理解你的意思。 我试图制作一个你在这里展示的程序,我想出了这个:
Macros.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
Header.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
实施文件:(当然我也打印过,并没有对输出做过任何重要的事情)。
Main.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();
}
答案 3 :(得分:0)
以下是我如何解决问题的代码。感谢所有回复的人,虽然一些回复对我来说太复杂了。请看它&amp;通过我正在寻找的输出来指出任何缺点。
#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;
}