C++ C+中的前缀表达式求值+;[代码评估]
是挑战的一个环节。 为了您的方便: 前缀表达式 说明: 将为您提供一个前缀表达式。编写一个程序来评估它。 输入样本: 第一个参数是一个输入文件,每行有一个前缀表达式。例如 * + 2 3 4 您的程序必须读取并将其插入到您喜欢的任何数据结构中。 遍历该数据结构并计算前缀表达式。每个令牌都是 用空格分隔。您可以假定只有有效的运算符出现 测试中的数据为“+”、“*”和“/” 输出样本: 打印到标准输出,前缀表达式的输出,每行一个。例如 20“ 我的代码有时会被CodeEval拒绝,因为编译时间超过10秒。当它编译时,我得到85/100分,因为我认为在40个测试用例中,我得到了一些错误。我相信我的算法是正确的,问题只是在检查边界/极端情况。有人能帮我优化吗请将此代码设置为在CodeEval中工作C++ C+中的前缀表达式求值+;[代码评估],c++,prefix,C++,Prefix,是挑战的一个环节。 为了您的方便: 前缀表达式 说明: 将为您提供一个前缀表达式。编写一个程序来评估它。 输入样本: 第一个参数是一个输入文件,每行有一个前缀表达式。例如 * + 2 3 4 您的程序必须读取并将其插入到您喜欢的任何数据结构中。 遍历该数据结构并计算前缀表达式。每个令牌都是 用空格分隔。您可以假定只有有效的运算符出现 测试中的数据为“+”、“*”和“/” 输出样本: 打印到标准输出,前缀表达式的输出,每行一个。例如 20“ 我的代码有时会被CodeEval拒绝,因为编译时间超过1
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
void tokenize(string& str, vector<string>& tokens)
{
int pos;
string token;
while ((pos = str.find(" ")) != std::string::npos )
{
token = str.substr(0,pos);
tokens.push_back(token);
str.erase(0, pos + 1);
}
tokens.push_back(str.c_str());
}
bool isOperator(string str)
{
if((str == "+") || (str == "-") || (str == "*") || (str == "/") )
return true;
else
return false;
}
int compute(string oper, int val1, int val2)
{
if(oper == "+")
return (val1 + val2);
else if(oper == "*")
return (val1 * val2);
else if(oper == "/")
return (val1 / val2);
else if(oper == "-")
return (val1 - val2);
}
void evalPrefix(vector<string>& expression)
{
vector<int> numStack;
int num1;
int num2;
for (int i = (expression.size() - 1); i >=0; i--)
{
if(isOperator(expression[i]))
{
num1 = numStack.back();
numStack.pop_back();
num2 = numStack.back();
numStack.pop_back();
numStack.push_back(compute(expression[i], num1, num2));
}
else
{
numStack.push_back(atoi(expression[i].c_str()));
}
}
cout << numStack[0] << endl;
}
int main(int argc, char *argv[])
{
ifstream file(argv[1]);
string line;
string token;
vector<string> tokens;
while (!file.eof()) //processing the file
{
getline(file, line);
if(line.length() == 0)
continue;
else
{
tokens.clear();
tokenize(line, tokens); //tokenizing the file
if(tokens.size())
evalPrefix(tokens);
}
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void标记化(字符串和str、向量和标记)
{
int pos;
字符串标记;
而((pos=str.find(“”)!=std::string::npos)
{
token=str.substr(0,pos);
代币。推回(代币);
str.erase(0,pos+1);
}
代币。推回(str.c_str());
}
布尔等参器(字符串str)
{
如果((str==“+”)| |(str==“-”)| |(str==“*”)| |(str==“/”)
返回true;
其他的
返回false;
}
int compute(字符串运算符,int val1,int val2)
{
如果(操作==“+”)
返回(val1+val2);
如果(oper==“*”),则为else
返回值(val1*val2);
否则如果(操作==“/”)
返回(val1/val2);
如果(oper==“-”),则为else
返回(val1-val2);
}
void evalPrefix(向量和表达式)
{
向量numStack;
int num1;
int num2;
对于(int i=(expression.size()-1);i>=0;i--)
{
if(等运算符(表达式[i]))
{
num1=numStack.back();
numStack.pop_back();
num2=numStack.back();
numStack.pop_back();
push_back(计算(表达式[i],num1,num2));
}
其他的
{
push_back(atoi(表达式[i].c_str());
}
}
完成了。他们想要浮动值。谢谢
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
void tokenize(string& str, vector<string>& tokens)
{
int pos;
string token;
while ((pos = str.find(" ")) != std::string::npos )
{
token = str.substr(0,pos);
tokens.push_back(token);
str.erase(0, pos + 1);
}
tokens.push_back(str.c_str());
}
bool isOperator(string str)
{
if((str == "+") || (str == "*") || (str == "/") )
return true;
else
return false;
}
float compute(string oper, float val1, float val2)
{
if(oper == "+")
return (val1 + val2);
else if(oper == "*")
return (val1 * val2);
else if(oper == "/")
return (val1 / val2);
else
return 0;
}
void evalPrefix(vector<string>& expression)
{
vector<float> numStack;
float num1;
float num2;
for (int i = (expression.size() - 1); i >=0; i--)
{
if(isOperator(expression[i]))
{
num1 = numStack.back();
numStack.pop_back();
num2 = numStack.back();
numStack.pop_back();
numStack.push_back(compute(expression[i], num1, num2));
}
else
{
numStack.push_back(atoi(expression[i].c_str()));
}
}
int i = int (numStack[0] + 0.5);
cout << i << endl;
}
int main(int argc, char *argv[])
{
ifstream file(argv[1]);
string line;
string token;
vector<string> tokens;
while (getline(file, line)) //processing the file
{
if(line.length() == 0)
continue;
else
{
tokens.clear();
tokenize(line, tokens); //tokenizing the file
if(tokens.size())
evalPrefix(tokens);
}
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void标记化(字符串和str、向量和标记)
{
int pos;
字符串标记;
而((pos=str.find(“”)!=std::string::npos)
{
token=str.substr(0,pos);
代币。推回(代币);
str.erase(0,pos+1);
}
代币。推回(str.c_str());
}
布尔等参器(字符串str)
{
如果((str==“+”)| |(str==“*”)| |(str==“/”)
返回true;
其他的
返回false;
}
浮点计算(字符串运算符、浮点值1、浮点值2)
{
如果(操作==“+”)
返回(val1+val2);
如果(oper==“*”),则为else
返回值(val1*val2);
否则如果(操作==“/”)
返回(val1/val2);
其他的
返回0;
}
void evalPrefix(向量和表达式)
{
向量numStack;
浮点数m1;
浮点数m2;
对于(int i=(expression.size()-1);i>=0;i--)
{
if(等运算符(表达式[i]))
{
num1=numStack.back();
numStack.pop_back();
num2=numStack.back();
numStack.pop_back();
push_back(计算(表达式[i],num1,num2));
}
其他的
{
push_back(atoi(表达式[i].c_str());
}
}
int i=int(numStack[0]+0.5);
这是C#works中的解决方案。他们是否检查了一些我没有检查的边界?一个问题是您不应该在(!file.eof()时使用)
,请参阅。另外,如果您只想在空格上分隔标记,则std::ostringstream
和普通输出操作符>
也应该可以工作。或者只需使用std::copy
和std::back\u迭代器和std::back\u插入器将其修复。将其更改为while(getline)(文件,行))通过更改while循环(访问文件),我的分数提高到了97.5分。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
void tokenize(string& str, vector<string>& tokens)
{
int pos;
string token;
while ((pos = str.find(" ")) != std::string::npos )
{
token = str.substr(0,pos);
tokens.push_back(token);
str.erase(0, pos + 1);
}
tokens.push_back(str.c_str());
}
bool isOperator(string str)
{
if((str == "+") || (str == "*") || (str == "/") )
return true;
else
return false;
}
float compute(string oper, float val1, float val2)
{
if(oper == "+")
return (val1 + val2);
else if(oper == "*")
return (val1 * val2);
else if(oper == "/")
return (val1 / val2);
else
return 0;
}
void evalPrefix(vector<string>& expression)
{
vector<float> numStack;
float num1;
float num2;
for (int i = (expression.size() - 1); i >=0; i--)
{
if(isOperator(expression[i]))
{
num1 = numStack.back();
numStack.pop_back();
num2 = numStack.back();
numStack.pop_back();
numStack.push_back(compute(expression[i], num1, num2));
}
else
{
numStack.push_back(atoi(expression[i].c_str()));
}
}
int i = int (numStack[0] + 0.5);
cout << i << endl;
}
int main(int argc, char *argv[])
{
ifstream file(argv[1]);
string line;
string token;
vector<string> tokens;
while (getline(file, line)) //processing the file
{
if(line.length() == 0)
continue;
else
{
tokens.clear();
tokenize(line, tokens); //tokenizing the file
if(tokens.size())
evalPrefix(tokens);
}
}
return 0;
}