我正在编写一个非常基本的解析器(主要是为了更好地理解它们是如何工作的),它使用户输入选择的几个单词,检测句子结构是否正常,并输出结果.语法是:
句子:Noun Verb
文章句子
句子连词句
连词:"和""或""但是"
名词:"鸟""鱼""C++"
动词:"规则""飞""游泳"
文章:"该"
写语法很简单.它正在实现给我带来麻烦的代码.我的伪代码是:
main()
get user input (string words;)
while loop (cin >> words)
call sentence()
end main()
sentence()
call noun()
if noun() call verb() (if verb is true return "OK" ???)(else "not ok"???)
else if not noun() call article()
if article() call sentence() (if sentence is true "OK"???)(else "not"?)
else if not noun() call conjunction()
if sentence() conjunction() sentence() - no idea how to implement
return "OK"
else "not ok"
Run Code Online (Sandbox Code Playgroud)
所以有我非常邋ps的伪代码.我有一些关于实施它的问题.
对于单词函数(名词,动词等),我该如何检查它们是否属实?(如检查用户的输入是否有鸟类,鱼类,苍蝇,游泳等) …
我正在编写一个程序,用户输入名称然后老化.然后程序按字母顺序对列表进行排序并输出对.但是,我不确定在按字母顺序排序之后如何保持年龄与名称相匹配.我到目前为止所有的一切都是......
编辑:将代码更改为此 -
#include "std_lib_facilities.h"
struct People{
string name;
int age;
};
int main()
{
vector<People>nameage;
cout << "Enter name then age until done. Press enter, 0, enter to continue.:\n";
People name;
People age;
while(name != "0"){
cin >> name;
nameage.push_back(name);
cin >> age;
nameage.push_back(age);}
vector<People>::iterator i = (nameage.end()-1);
nameage.erase(i);
}
Run Code Online (Sandbox Code Playgroud)
我得到了!=运算符和cin运算符的编译器错误.不知道该怎么办.
我在这个程序中返回多个值时遇到了一些问题,这些值计算了min,max,mean,median.我做的第一件事是传递引用参数,它起作用 - 但我读到创建结构或类是返回多个值的首选方法.
所以我尝试了,但是我没能取得好成绩.这是我到目前为止所得到的.
#include "std_lib_facilities.h"
struct maxv{
int min_value;
int max_value;
double mean;
int median;
};
maxv calculate(vector<int>& max)
{
sort(max.begin(), max.end());
min_value = max[0];
int m = 0;
m = (max.size()-1);
max_value = max[m];
for(int i = 0; i < max.size(); ++i) mean += max[i];
mean = (mean/(max.size()));
int med = 0;
if((max.size())%2 == 0) median = 0;
else
{
med = (max.size())/2;
median = max[med];
}
}
int main()
{
vector<int>numbers;
cout << "Input numbers. Press enter, …Run Code Online (Sandbox Code Playgroud) 我从结构中创建了一个向量来存储多种类型的值.但是,我无法获得投入工作.
#include "std_lib_facilities.h"
struct People{
string name;
int age;
};
int main()
{
vector<People>nameage;
cout << "Enter name then age until done. Press enter, 0, enter to continue.:\n";
People name;
People age;
while(name != "0"){
cin >> name;
nameage.push_back(name);
cin >> age;
nameage.push_back(age);}
vector<People>::iterator i = (nameage.end()-1);
nameage.erase(i);
}
Run Code Online (Sandbox Code Playgroud)
我也尝试在main函数中使用name和age变量为string/int类型,虽然这解决了操作符问题,但它会导致push_back行中函数调用的问题.
PS是否可以push_back多个输入,如...
cin >> name >> age;
nameage.push_back(name,age);
Run Code Online (Sandbox Code Playgroud)
?
在函数调用之后,C++是否有任何类型的实用程序可以返回函数的开头?例如,在calculate函数中调用help().
void help()
{
cout << "Welcome to this annoying calculator program.\n";
cout << "You can add(+), subtract(-), multiply(*), divide(/),\n";
cout << "find the remainder(%), square root(sqrt()), use exponents(pow(x,x)),\n";
cout << "use parentheses, assign variables (ex: let x = 3), and assign\n";
cout << " constants (ex: const pi = 3.14). Happy Calculating!\n";
return;
}
void clean_up_mess() // purge error tokens
{
ts.ignore(print);
}
const string prompt = "> ";
const string result = "= ";
void calculate()
{
while(true) …Run Code Online (Sandbox Code Playgroud)