需要帮助这个文本信息解码器(c++)练习
Need help on this Text message decoder (C++) exercise
说明如下:
-
使用
getline()
获取一行用户输入到字符串中。输出行。(3点)
,
输入text: IDK如果我要去。今天是我最好的朋友的生日。您输入: IDK if I'll go。今天是我最好的朋友的生日。 -
搜索字符串(使用
find()
)查找常见缩写,并打印每个找到的缩写及其解码的含义的列表。(3点)
,
输入text: IDK如果我要去。今天是我最好的朋友的生日。您输入: IDK if I'll go。今天是我最好的朋友的生日。
支持以下缩写:
-永远的好朋友
IDK——我不知道
JK——开个玩笑
TMI—信息过多
TTYL——待会再聊
我一直在尝试编码并获得6/6可能的点,但我最终获得3/6。
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "Enter text: ";
getline(cin, input);
cout << endl <<"You entered: " << input << endl;
if (input.find("BFF") && input.find("IDK"))
{
cout << "BFF: best friend forever" << endl;
cout << "IDK: I don't know" << endl;
}
if (input.find("JK") && input.find("TMI") && input.find("TTYL"))
{
cout << "JK: just kidding" << endl;
cout << "TMI: too much information" << endl;
cout << "TTYL: talk to you later" << endl;
}
return 0;
}
我没有得到最后3分:
比较输出0/2
输入我不知道我是否要去。今天是我闺蜜的生日。
输出
输入文字:
你输入:IDK如果我要去。今天是我闺蜜的生日。
永远的好朋友
IDK:我不知道
JK:开玩笑的TMI: too much information
TTYL:回头聊
预期输出输入文本:
你输入:IDK如果我要去。今天是我闺蜜的生日。
永远的好朋友
IDK:我不知道
比较输出0/1
输入不错的照片,TMI哈哈JK。TTYL
输出
输入文字:
你输入:漂亮的照片,TMI哈哈JK。TTYL
永远的好朋友
IDK:我不知道
期望输出
输入文字:
你输入:漂亮的照片,TMI哈哈JK。TTYL
JK:开玩笑的TMI: too much information
TTYL:回头聊
显然你的函数调用在某些点为真,在其他点为假。Find()返回找到的子字符串的第一个字符的位置,如果没有找到这样的子字符串,则返回npos = -1。true语句是计算结果为非零的语句。False的值为0。
在你的比较输出0/2中,所有的计算结果都是非零值。因此,所有内容都被打印出来。在0/1中,一定有一个值是0。BFF和IDK的指纹是-1,其他的没有。(只是我的2美分,也许有人可以确认你代码中的值是多少)
#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
cout << "Input an abbreviation:" << endl;
getline(cin, text);
if (text.find("LOL") != string::npos){
cout << "laughing out loud" << endl;
}
else if (text.find("IDK") != string::npos){
cout << "I don't know" << endl;
}
else if (text.find("BFF") != string::npos){
cout << "best friends forever" << endl;
}
else if (text.find("IMHO") != string::npos){
cout << "in my humble opinion" << endl;
}
else if (text.find("TMI") != string::npos){
cout << "too much information" << endl;
}
else{
cout << "Unknown" << endl;
}
return 0;
}
//for already defined String Value of BFF, IDK, JK, TMI and TTYL.
if (userIn.find("BFF") != string::npos){
cout << "BFF: " << BFF << endl;
}
if (userIn.find("IDK") != string::npos){
cout << "IDK: " << IDK << endl;
}
if (userIn.find("JK") != string::npos){
cout << "JK: " << JK << endl;
}
if (userIn.find("TMI") != string::npos){
cout << "TMI: " << TMI << endl;
}
if (userIn.find("TTYL") != string::npos){
cout << "TTYL: " << TTYL << endl;
}
.find()
不返回布尔值,所以它不是真或假。find(item)
返回第一个项目出现的索引,否则返回string::npos(在字符串库中定义的常量)。因此,你将它与string::npos
进行比较,看看你想要的字符串是否存在。
希望能解释清楚
删除条件:input.find("IDK"
- 错误:(-210:不支持的格式或格式组合)功能'create'中的硬件视频解码器不支持视频源
- 如何从WIC解码器确定自上而下/自下而上?
- 无法在 HEVC 解码器上设置输出类型 IMFTransform
- 连接无线电流和 MP3 解码器
- h264_cuvid编解码器未找到
- C++17 编解码器在将标准::字符串转换为标准::字符串时抛出"bad conversion"
- 如何使用 ffmpeg 将 3840 nb_samples编码为请求 1024 的编解码器
- FFMPEG:为什么当我调用av_codec_next()时,我所有的编解码器都没有显示出来
- OpenCV 检查计算机上是否安装了视频编解码器 (C++)
- FFMPEG 解码器似乎会丢帧
- 直接显示通用媒体解码器
- OpenCV:FFMPEG:编解码器 ID 12 和格式 'mp4 / MP4 不支持标记
- 带有 H264 编解码器的 libav 错误消息。"非严格单调PTS"
- C++ 文本消息解码器脚本 编译器 "expects unqualified id" 之前 if 和 else 语句
- 为什么编解码器X264/X265忽略了输入框架的PT和DTS
- 如何修复"UnicodeDecodeError:"utf-8"编解码器无法解码字节时使用Python C扩展?
- IWICImagingFactory::CreateDecoderFromFilename() 即使在解码器被销毁后,也
- 致命的Python错误:Py_Initialize:无法加载文件系统编解码器&ModuleNotFoundError
- C++ avformat_open_input返回空编解码器、宽度和高度
- 超级强大-导出到文件与混合器和解码器问题(不同的sampleRates和samplesPerFrame)