C++:从英语到Pig拉丁语
C++: English to Pig Latin
当我尝试动态分配数组时,收到以下代码中的一个错误(在我尝试使用bool函数递增users数组中的每个字母之后可以看到(。这就是错误:
main.cpp:在函数"Word*splitSentene(std::string,int&("中:main.cpp:81:32:错误:无法在赋值中将"std::string*{aka std::basic_string*}"转换为"Word*"words=新字符串[i]
我正在尝试计算用户输入的单词数量,并为单词串动态分配一个数组。这是我迄今为止的代码:
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
struct Word
{
string english; // English sentence
string piglatin; // Pig latin sentence
};
// PT 1. Function prototype
Word * splitSentence(const string words, int &size){};
int main()
{
string userSentence;
int size;
// Get the users sentence to convert to pig latin
cout << "Please enter a string to convert to pig latin:n";
getline(cin, userSentence);
// Directs to Word * splitSentence function
Word* tempptr = splitSentence(userSentence, size);
delete [] tempptr;
return 0;
}
//PT 1. Analyze the sentence
Word * splitSentene(const string words, int &size)
{
bool flag = true;
int num = 0;
for (int i = 0; i < words.length() + 1; i++)
{
//test for white space, then when you hit the first alphabetical character after a space,
//increment up the size of the array
if (isspace(words[i]))
flag = true;
if (isalpha(words[i]));
{
if (flag == true)
{
flag = false;
cout << words[i++];
}
}
// Dynamically allocate the array for the words
Word *sentence = nullptr;
sentence = new string[i];
}
}
以下是pt 1的说明以供进一步澄清:
PT。1( 编写一个函数,将一个英语句子作为一个字符串。这个函数应该首先计算句子中有多少"单词"(单词是由空格分隔的子字符串(。然后,它应该分配一个大小等于单词数量的动态数组。数组包含Word结构(即Word类型的数组(。然后,该函数会将该句子的每个单词存储到相应结构的英语字段中。然后,该函数应使用return语句将此数组返回给调用函数,并使用引用参数将数组大小返回给该函数。
此函数还应删除除字母以外的所有大写字母和特殊字符。使用以下原型实现功能:
Word * splitSentence(const string words, int &size);
这是我在这里的第一篇文章,所以我会感谢任何关于如何动态分配数组和格式化它的输入(如果我已经成功地编码了如何计算用户输入的句子中的单词(。如果需要提供更多信息,请告诉我!
编译器错误是因为您试图将string[]
数组分配给Word*
指针。您需要改为分配一个Word[]
数组。
您的代码中还有其他错误:
-
在
splitSentence()
的声明末尾有一个错误的{}
。 -
你在
splitSentence()
的定义中拼错了splitSentene
。 -
您在
if (isalpha(words[i]));
上有一个错误的;
-
您不是在
return
'中分配的数组。
事实上,您甚至根本没有正确遵循说明。你不是";计算句子中有多少单词;在分配数组之前(你尝试过,但你在错误的地方进行了分配(,你根本没有填充数组,更不用说"删除除字母以外的所有大写和特殊字符";。
试试类似的东西:
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
struct Word
{
string english; // English sentence
string piglatin; // Pig latin sentence
};
// PT 1. Function prototype
Word* splitSentence(const string words, int &size);
int main()
{
string userSentence;
int size;
// Get the users sentence to convert to pig latin
cout << "Please enter a string to convert to pig latin:n";
getline(cin, userSentence);
// Directs to Word * splitSentence function
Word* tempptr = splitSentence(userSentence, size);
delete [] tempptr;
return 0;
}
//PT 1. Analyze the sentence
Word* splitSentence(const string words, int &size)
{
bool flag = true;
int num = 0;
char ch;
for (int i = 0; i < words.length(); ++i)
{
ch = words[i];
if (isalpha(ch))
{
if (flag)
{
flag = false;
++num;
}
}
else if (isspace(ch))
{
flag = true;
}
}
Word *sentence = new Word[num];
int index = -1;
flag = true;
num = 0;
for (int i = 0; i < words.length(); ++i)
{
ch = words[i];
if (isalpha(ch))
{
if (flag)
{
flag = false;
++num;
++index;
}
if (isupper(ch))
{
ch = tolower(ch);
}
sentence[index].english += ch;
}
else if (isspace(ch))
{
flag = true;
}
}
size = num;
return sentence;
}
实时演示
也就是说,如果你可以使用std::istringstream
和std::vector
以及其他C++习惯用法,而不是使用C习惯用法,这将更容易实现splitSentence()
,例如:
#include <iostream>
#include <sstream>
#include <cctype>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Word
{
string english; // English sentence
string piglatin; // Pig latin sentence
};
// PT 1. Function prototype
Word* splitSentence(const string words, int &size);
int main()
{
string userSentence;
int size;
// Get the users sentence to convert to pig latin
cout << "Please enter a string to convert to pig latin:n";
getline(cin, userSentence);
// Directs to Word * splitSentence function
Word* tempptr = splitSentence(userSentence, size);
delete [] tempptr;
return 0;
}
//PT 1. Analyze the sentence
Word* splitSentence(const string words, int &size)
{
istringstream iss(words);
vector<string> vec;
string s;
while (iss >> s)
{
remove_if(s.begin(), s.end(),
[](unsigned char ch){ return !isalpha(ch); });
if (!s.empty())
{
transform(s.begin(), s.end(), s.begin(),
[](unsigned char ch){ return tolower(ch); });
vec.push_back(s);
}
}
Word *sentence = new Word[vec.size()];
transform(vec.begin(), vec.end(), sentence,
[](const string &s){
Word w;
w.english = s;
return w;
}
);
size = vec.size();
return sentence;
}
实时演示
- C++:从英语到Pig拉丁语
- C++中的双边友语函数:如何使其编译?
- 英语到猪拉丁语在C++使用特定功能
- 罗马数字到阿拉伯语 (vinculum) - 读取字符串中的字符
- 概念可以与 CRTP 习语一起使用吗?
- 复制和交换习语和迭代器
- 为什么当我做复制和交换习语时不调用我的复制构造函数?
- 漂亮的计数器习语的错误或格式错误的静态订单惨败?
- QT 和 JIRA Rest API: /rest/api/2/issue/createmeta 意外返回韩语的 iss
- 拉丁语的二进制字典 - 如何构建或在哪里找到
- 无法通过字符串循环我的程序以将其从英语翻译成猪拉丁语
- 将CJK翻译成拉丁语——最好是C++
- C++Pig拉丁代码,只删除文件信息
- Pig拉丁语转换器使用toupper
- 如何在C++中更改拉丁语 UTF-8 字符串的大小写
- 猪拉丁语 - 字符串
- 俄语(或非拉丁语)文件名加载Qt Windows
- 转换成猪拉丁语
- DirectWrite:拉丁语和复杂脚本使用不同的字体
- 如何将 UTF-8 形式转换为拉丁语/阿拉伯语,反之亦然