当我输入“...”时,用户输入不起作用
我正在对单词进行莫尔斯电码并倒着做。该程序可以正常工作,但有一种情况除外,即当我在用户输入中键入“...”然后按空格键时,它会变得非常小并且程序不会返回任何内容。因此,例如,如果我尝试输入 ... 返回 S 它可以工作,但如果我尝试输入 ... ... 对于 SS 它不起作用。我使用斯坦福库来获取用户输入和处理地图,但当我使用标准库时也会发生同样的事情。
#include <iostream>
#include <string>
#include "console.h"
#include "random.h"
#include "map.h"
#include "simpio.h"
using namespace std;
int main() {
string input = getLine("Please enter words or morse code");
Map<string, string> toMorse;
toMorse.put("A", ".-");
toMorse.put("B", "-...");
toMorse.put("C", "-.-.");
toMorse.put("D", "-..");
toMorse.put("E", ".");
toMorse.put("F", "..-.");
toMorse.put("G", "--.");
toMorse.put("H", "....");
toMorse.put("I", "..");
toMorse.put("J", ".---");
toMorse.put("K", "-.-");
toMorse.put("L", ".-..");
toMorse.put("M", "--");
toMorse.put("N", "-.");
toMorse.put("O", "---");
toMorse.put("P", ".--.");
toMorse.put("Q", "--.-");
toMorse.put("R", ".-.");
toMorse.put("S", "...");
toMorse.put("T", "-");
toMorse.put("U", "..-");
toMorse.put("V", "...-");
toMorse.put("W", ".--");
toMorse.put("X", "-..-");
toMorse.put("Y", "-.--");
toMorse.put("Z", "--..");
Map<string, string> toSentence;
for(char c0='A'; c0<='Z'; c0++)
{
string c="";
c.append(1, c0);
//cout<<toMorse.get(c)<<endl;
toSentence.put(toMorse.get(c), c);
}
if(input[0]=='.' || input[0]=='-')
{
string toLetter;
for(int i=0; i<input.length(); i++)
{
if(input[i] != ' ' && i<input.length()-1)
{
toLetter.append(input.substr(i, 1));
}
else if(input[i] != ' ' && i==input.length()-1)
{
toLetter.append(input.substr(i, 1));
cout << toSentence.get(toLetter);
}
else
{
cout << toSentence.get(toLetter);
toLetter = "";
}
}
}
else
{
for(int i=0; i<input.length(); i++)
{
if(toMorse.containsKey(input.substr(i,1)))
{
cout << toMorse.get(input.substr(i,1)) << " ";
}
}
}
return 0;
}
I'm doing a morse to words and backwards. The program works except for one case, when I type "..." in the user input and then press space it becomes extremely small and the program doesn't return anything. So for instance if I try typing ... to return S it works, but if I try typing ... ... for SS it doesn't work. I'm using stanford libraries for getting user input and handling maps, but the same thing happens when I use the standard libraries.
#include <iostream>
#include <string>
#include "console.h"
#include "random.h"
#include "map.h"
#include "simpio.h"
using namespace std;
int main() {
string input = getLine("Please enter words or morse code");
Map<string, string> toMorse;
toMorse.put("A", ".-");
toMorse.put("B", "-...");
toMorse.put("C", "-.-.");
toMorse.put("D", "-..");
toMorse.put("E", ".");
toMorse.put("F", "..-.");
toMorse.put("G", "--.");
toMorse.put("H", "....");
toMorse.put("I", "..");
toMorse.put("J", ".---");
toMorse.put("K", "-.-");
toMorse.put("L", ".-..");
toMorse.put("M", "--");
toMorse.put("N", "-.");
toMorse.put("O", "---");
toMorse.put("P", ".--.");
toMorse.put("Q", "--.-");
toMorse.put("R", ".-.");
toMorse.put("S", "...");
toMorse.put("T", "-");
toMorse.put("U", "..-");
toMorse.put("V", "...-");
toMorse.put("W", ".--");
toMorse.put("X", "-..-");
toMorse.put("Y", "-.--");
toMorse.put("Z", "--..");
Map<string, string> toSentence;
for(char c0='A'; c0<='Z'; c0++)
{
string c="";
c.append(1, c0);
//cout<<toMorse.get(c)<<endl;
toSentence.put(toMorse.get(c), c);
}
if(input[0]=='.' || input[0]=='-')
{
string toLetter;
for(int i=0; i<input.length(); i++)
{
if(input[i] != ' ' && i<input.length()-1)
{
toLetter.append(input.substr(i, 1));
}
else if(input[i] != ' ' && i==input.length()-1)
{
toLetter.append(input.substr(i, 1));
cout << toSentence.get(toLetter);
}
else
{
cout << toSentence.get(toLetter);
toLetter = "";
}
}
}
else
{
for(int i=0; i<input.length(); i++)
{
if(toMorse.containsKey(input.substr(i,1)))
{
cout << toMorse.get(input.substr(i,1)) << " ";
}
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你的控制台正在将 3 个句点更改为省略号,就像文字处理程序可能会做的那样。不知道如何解决这个问题,除了扫描 Unicode 或控制台创建的任何值:)
Sounds like your console is changing 3 periods to an ellipses run, like a word processing program might do. Not sure how to fix this though except to scan for the Unicode or whatever value the console is creating :)
您的控制台“有帮助”地将三个句点字符 (...) 转换为 Unicode 标准所允许的省略号 (...)。由于您使用的是 std::string(我假设是 Linux,因为 Windows 不这样做),因此它必须转换为 UTF-8。 unicode 字符为代码点 U+2026,在 UTF-8 中为
0xE2 0x80 0xA6
,或者作为 cstring"\xE2\x80\xA6"
。来源:“Unicode 将一系列三个句点字符 (U+002E) 识别为水平省略号字符的兼容性等效项(尽管不是规范的)。” -http://en.wikipedia.org/wiki/Ellipsis
Your console is "helpfully" converting three period characters (...) as an ellipsis (…) as allowed by the unicode standard. Since you're using
std::string
, (and I assume linux, since Windows doesn't do this), it must be converting to UTF-8. The unicode character is codepoint U+2026, which in UTF-8 is0xE2 0x80 0xA6
, or, as a cstring"\xE2\x80\xA6"
.Source: "Unicode recognizes a series of three period characters (U+002E) as compatibility equivalent (though not canonical) to the horizontal ellipsis character." -http://en.wikipedia.org/wiki/Ellipsis
我已经遍历并在必要时用 std:: 类替换了你的东西,对我来说,硬编码输入字符串“... ...”会导致“SS”的输出,所以你的实际莫尔斯翻译是可以的(假设你希望它丢弃空格) - 但用 cin 捕获字符串会截断空格处的输入。
I've run through and replaced your stuff with std:: classes where necessary, and for me hardcoding the input string "... ..." results in the output of "SS" so your actual morse translation is ok (assuming you want it to discard spaces) - but capturing the string with cin truncates the input at the space.