C++ 转换c++;使用字符串和流头文件和函数编码为arduino
我正在我的arduino板上开发一个项目,为了表达我在C++ 转换c++;使用字符串和流头文件和函数编码为arduino,c++,function,c++11,arduino,header-files,C++,Function,C++11,Arduino,Header Files,我正在我的arduino板上开发一个项目,为了表达我在C++中编写代码的想法。但据我所知,在arduino IDE上找不到某些库文件和函数,这些文件和函数可以在C++中找到 我在下面附上代码。 我想将整个代码转换为arduino,其中只有convertToEnglish将作为函数保留在arduino中。 我尝试用字符串库和其他Stream.h头文件替换头文件和其他函数,但几乎所有操作都以失败告终。 因此,为了解决这个问题,请给我一个解决方案。 我尝试使用as quoted,但getline函数仍
C++
中编写代码的想法。但据我所知,在arduino IDE上找不到某些库文件和函数,这些文件和函数可以在C++
中找到
我在下面附上代码。
我想将整个代码转换为arduino,其中只有convertToEnglish
将作为函数保留在arduino中。
我尝试用字符串库和其他Stream.h
头文件替换头文件和其他函数,但几乎所有操作都以失败告终。
因此,为了解决这个问题,请给我一个解决方案。
我尝试使用as quoted,但getline函数仍然报告一个错误,指出cin未在范围内声明
#include <StandardCplusplus.h>
#include <system_configuration.h>
#include <unwind-cxx.h>
#include <utility.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string convertToEnglish(string morse, string const morseCode[]);
int main()
{
string input = "";
cout << "Please enter a string in morse code: ";
getline(cin, input);
string const morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
cout << convertToEnglish(input, morseCode) << endl;
return 0;
}
string convertToEnglish(string morse, string const morseCode[])
{
string output = "";
string currentLetter = "";
istringstream ss(morse);
size_t const characters = 26;
while(ss >> currentLetter)
{
size_t index = 0;
while(currentLetter != morseCode[index] && index < characters)
{
++index; //increment here so we don't have to decrement after the loop like if we put in the condition
}
output += 'A' + index;
}
return output;
}
退出状态1
“cin”未在此范围内声明
getline(cin, input);
^
本报告将提供更多信息
“在编译期间显示详细输出”
在File->Preferences中启用选项。我不清楚您可以使用什么,不能使用什么 假设您不能使用
std::vector
,但可以使用良好的旧C字符串(char*)函数和输入/输出函数,我准备了以下C风格的示例
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
char * convertToEnglish (char * output,
std::size_t dimOut,
char const * const input,
char const * const * const morseCode,
std::size_t numMorseCodes)
{
if ( (nullptr == output) || (0U == dimOut) || (nullptr == input)
|| (nullptr == morseCode) || (0U == numMorseCodes) )
throw std::runtime_error("invalid input in cTE()");
std::size_t posOut = 0U;
std::size_t index;
char const * ptrI0;
char const * ptrI1;
char currentLetter[5];
std::memset(output, 0, dimOut);
for ( ptrI0 = input ; nullptr != ptrI0 ; ptrI0 = ptrI1 )
{
ptrI1 = std::strpbrk(ptrI0, " \n");
if ( nullptr == ptrI1 )
{
// last character if the string isn't cr terminated
if ( sizeof(currentLetter) <= strlen(ptrI0) )
throw std::runtime_error("to length last char in cTE()");
std::strcpy(currentLetter, ptrI0);
}
else
{
if ( sizeof(currentLetter) <= (ptrI1 - ptrI0) )
throw std::runtime_error("to length char in cTE()");
std::memset(currentLetter, 0, sizeof(currentLetter));
std::strncpy(currentLetter, ptrI0, (ptrI1 - ptrI0));
if ( '\n' == *ptrI1 )
ptrI1 = nullptr;
else
++ptrI1;
}
for ( index = 0U
; (index < numMorseCodes)
&& strcmp(currentLetter, morseCode[index])
; ++index )
;
if ( numMorseCodes <= index )
throw std::runtime_error("no morse code in cTE()");
output[posOut] = 'A' + index;
if ( ++posOut >= dimOut )
throw std::runtime_error("small out buffer in cTE()");
}
return output;
}
int main ()
{
constexpr char const * morseCode[] = {".-", "-...", "-.-.",
"-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-", ".--", "-..-", "-.--", "--.."};
constexpr std::size_t numMorseCodes
= sizeof(morseCode)/sizeof(morseCode[0]);
char * input = nullptr;
std::size_t dim = 0U;
if ( getline(&input, &dim, stdin) == -1 )
throw std::runtime_error("error reading input");
char output[1024];
std::cout << convertToEnglish(output, sizeof(output), input,
morseCode, numMorseCodes) << std::endl;
return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
字符*转换英语(字符*输出,
标准:尺寸尺寸尺寸,
字符常量*常量输入,
字符常量*常量*常量莫尔斯电码,
标准::大小(数字代码)
{
if((nullptr==output)| |(0U==dimOut)| |(nullptr==input)
||(nullptr==morseCode)| |(0U==numMorseCodes))
抛出std::runtime_错误(“cTE()中的输入无效”);
标准:尺寸\u t posOut=0U;
标准:尺寸指数;
字符常量*ptrI0;
字符常量*ptrI1;
字母[5];
std::memset(输出,0,dimOut);
for(ptrI0=input;nullptr!=ptrI0;ptrI0=ptrI1)
{
ptrI1=std::strpbrk(ptrI0,“\n”);
if(nullptr==ptrI1)
{
//如果字符串未以cr结尾,则为最后一个字符
if(sizeof(currentLetter)到底是什么问题?在arduino ide中找不到字符串和sstream头文件。如果不是,则使用其他头文件来生成相同的输出。可能的重复项应切换currentLetter
的测试顺序:在index
之前和currentLetter!=morseCode[index]之后
。否则您可以访问摩尔斯电码[26]