// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <iostream>
#include <set>
#include<vector>
#include <stack>
#include <queue>
#include <list>
typedef float (*fun_sin)(float);
int priority(char oper1, char oper2)
{
static std::unordered_map<char, int> priorityMap = {
{ '-',1 },
{ '+',1 },
{ '*',2 },
{ '/',2 },
{ '(',3 },
{ ')',3 },
};
return priorityMap[oper1] - priorityMap[oper2];
}
bool isNumber(const std::string& str)
{
for (auto c : str)
{
if ((c >= '0' && c <= '9') || c == '.')
{
}
else
{
return false;
}
}
return true;
}
bool isOperator(const std::string& str)
{
static std::unordered_set<std::string> operatorSet = {
"+",
"-",
"*",
"/",
"(",
")",
","
};
return operatorSet.count(str) > 0;
}
bool isFuction(const std::string& str)
{
static std::unordered_map<std::string, fun_sin> one_arg = {
{ "sin",sin },
{ "cos",cos },
{ "tan",tan },
{ "tan",tan },
{ "sqrt",sqrt },
};
return one_arg.count(str) > 0;
}
std::vector<std::string> split(const std::string& str, const std::string& sep)
{
//pow(2,3*(40+51))+33*(488+a005)*
std::vector<std::string> ret;
int start = 0;
while (start < str.size())
{
int index = str.find(sep, start);
if (index < 0)
{
index = str.size();
ret.push_back(str.substr(start, index - start));
break;
}
else
{
ret.push_back(str.substr(start, index - start));
ret.push_back(sep);
}
start = index + sep.length();
}
return ret;
}
std::vector<std::string> split(const std::string& str, const std::vector<std::string>& seps)
{
std::list<std::string> retList;
std::vector<std::string> retVector;
retList.push_back(str);
for (auto spe : seps)
{
int size = retList.size();
for (int i = 0; i < size; i++)
{
auto item = retList.front();
auto parts = split(item, spe);
retList.pop_front();
for (auto& part : parts)
{
if (part.empty())
continue;
retList.push_back(std::move(part));
}
}
}
retVector.assign(retList.begin(), retList.end());
return retVector;
}
void calculate(const std::vector<std::string> & exp)
{
std::stack<std::string> operatorStack;
std::stack<std::string> dataStack;
std::stack<std::string> fuctionStack;
for (int i = 0; i < exp.size(); ++i)
{
if (exp[i] == "(")
{
}
else if (isOperator(exp[i]))
{
if (operatorStack.top() == "("
|| priority(operatorStack.top(), exp[i]) < 0)
operatorStack.push(exp[i]);
}
else if (isNumber(exp[i]))
{
}
else if (isFuction(exp[i]))
{
}
else
{
std::cout << "未知的符号:"<< exp[i] <<std::endl;
return;
}
}
}
int main()
{
//auto a = one_arg["sin"](1);
//auto b = one_arg["cos"](1);
//auto c = one_arg["tan"](1);
//auto d = one_arg["sqrt"](2);
std::string str = "pow(2,3*(40+51))+33*(488+a005)";
std::vector<std::string> opers = {
"*",
"/",
"+",
"-",
"(",
")",
",",
};
//第一步, 按符号切割字符串
//第二步, 判断表达式是不是数字或者函数
//第三步,
auto data = split(str, opers);
for (int i = 0; i < data.size(); ++i)
{
if(data[i] == "")
}
while (true)
{
std::cout << ">>>";
std::string line;
std::getline(std::cin, line);
}
return 0;
}