如何将字符 a= '+'(或字符串 []) 变成实际操作?

How can I make a char a= ‘+’(or string[]) into a real operation?

本文关键字:实际操作 字符串 字符      更新时间:2023-10-16

Ex:5 a 5 = 10.我正在尝试做一个线性方程计算器,我从一个字符串开始。我可以将其转换为数字,但不能使聊天等同于 + 号(例如)实际上做 + 号的作用......

在 c 风格的语言中,代码在运行之前就被编译了,因此您不能像运行时的代码一样直接运行字符串。但是,您可以构建一个基本的口译员来完成您的任务。例如:

if (op == '+') {
result = a + b;
} else if (op == '-') {
result = a - b;
}

我认为您可以将字符串中的字符转换为提供更多信息的内容。

我写了一个示例(非常简单),将 A-Z 和 a-z 中的字符处理为变量,将 0-9 作为常量,标准运算符'+''-''*''/'处理。

它只处理整数问题和很少的运算符,但它只是一个简单的例子,只是为了说明我的想法。


法典

工具.h

#ifndef TOOLS_H
#define TOOLS_H
#include <vector>
#include <string>
namespace interp
{
enum class ITEM_TYPE {OPERATOR, OPERAND};
enum class VAR_TYPE {CONSTANT, VARIABLE};
enum class SOLVE_STATUS{SUCCESS, FAILED};
const int solve_init_value = 0; // Value used for initializing variables for the solve algorithm
struct Item
{
ITEM_TYPE item_t;
Item(ITEM_TYPE t);
};
struct Operator : public Item
{
char op;
Operator(char o);
int operate(int a, int b);
};
struct Operand : public Item
{
VAR_TYPE var_t;
int value;
Operand(VAR_TYPE t, int v);
};
std::vector<std::string> split(const std::string & s, char c);
Item * convert(char c);
SOLVE_STATUS solve(std::vector<Item*> lhs, std::vector<Item*> rhs);
}
#endif // TOOLS_H

工具.cpp

#include "tools.h"
namespace interp
{
Item::Item(ITEM_TYPE t) : item_t(t)
{}
// ---------- ---------- ---------- ---------- ----------
Operator::Operator(char o) : Item(ITEM_TYPE::OPERATOR), op(o)
{}
int Operator::operate(int a, int b)
{
int res;
switch(op)
{
case '+': res = a+b; break;
case '-': res = a-b; break;
case '*': res = a*b; break;
case '/': res = a/b; break;
default: /* Do what you want */ throw std::string("Unknown Operator");
}
return res;
}
// ---------- ---------- ---------- ---------- ----------
Operand::Operand(VAR_TYPE t, int v) : Item (ITEM_TYPE::OPERAND), var_t(t), value(v)
{}
// ---------- ---------- ---------- ---------- ----------
std::vector<std::string> split(const std::string & s, char c)
{
std::vector<std::string> splitted;
std::string word;
for(char ch : s)
{
if((ch == c) && (!word.empty()))
{
splitted.push_back(word);
word.clear();
}
else
word += ch;
}
if(!word.empty())
splitted.push_back(word);
return splitted;
}
// ---------- ---------- ---------- ---------- ----------
Item * convert(char c)
{
if((c < 48) || (c > 126)) // check for invalid characters
return nullptr;
if((c >= 48) && (c <= 57)) // constant integer values (0 to 9)
return new Operand(VAR_TYPE::CONSTANT, static_cast<int>(c));
if((c >= 65) && (c <= 90)) // variable integer values (char A-Z)
return new Operand(VAR_TYPE::VARIABLE, solve_init_value);
if((c >= 97) && (c <= 122)) // variable integer values (char a-z)
return new Operand(VAR_TYPE::VARIABLE, solve_init_value);
return new Operator(c);
}
}

主.cpp

#include "tools.h"
#include <iostream>
int main()
{
std::string equation("1+x=5*3");
std::vector<std::string> separatedSides(interp::split(equation, '='));
if(separatedSides.size() != 2)
{
std::cout << "Error: Too many '=' found in the string." << std::endl;
return -1;
}
std::vector<interp::Item *> leftHandSide;
std::vector<interp::Item *> rightHandSide;
// Convert the left hand side
for(char c : separatedSides[0])
{
interp::Item * item(interp::convert(c));
if(item != nullptr)
leftHandSide.push_back(item);
else
{
std::cout << "Error: Unknown character encountered." << std::endl;
return -1;
}
}
// Convert the right hand side
for(char c : separatedSides[1])
{
interp::Item * item(interp::convert(c));
if(item != nullptr)
rightHandSide.push_back(interp::convert(c));
else
{
std::cout << "Error: Unknown character encountered." << std::endl;
return -1;
}
}
// Now you can solve the problem knowing what Item is.
interp::SOLVE_STATUS status = interp::solve(leftHandSide, rightHandSide);
// Release memory at the end
for(interp::Item * item : leftHandSide)
delete item;
for(interp::Item * item : rightHandSide)
delete item;
leftHandSide.clear();
rightHandSide.clear();
return 0;
}

解释

我没有写 solve 方法(只是写在标题中但没有实现),这比我的工作更像是你的工作:)

但是,通过字符的Item表述,您可以轻松识别什么是什么(Operand(常量或变量)或Operator)。

Operator项中,您有可以为您执行操作的方法operate(),因此在您的solve()函数中,您只需调用此函数并检查是否没有发生错误(例如,对于未知运算符)。

因此,您可以根据自己的情况调整此代码,以便处理更复杂的情况,例如优先级规则的(),双精度值而不仅仅是整数(.),...

也许代码需要一些调整,但这不是我的意思。

我希望它能有所帮助。