C++程序 在纯字符串中执行计算

C++程序 在纯字符串中执行计算

给定一个包含每个操作的三个操作数“命令类型 “,“ 第一个操作数 “和“ 第二个操作数 “的操作字符串。 计算以此字符串格式给出的所有命令。

换句话说,您将获得一个要求您执行操作并按给定命令的顺序打印结果的纯字符串。

  • add - 加法( + )接受两个参数 num1num2 。(减法(-),乘法(x),除法(/)类似)。
  • percent - 对于百分比( % ),使用 100 接受一个参数 num1num2 始终为 ‘-1’
  • log - 对于以 num2 为底的 num1 的对数,接受两个参数。(类似于exp (^) )。
  • sqrt - 对于 num1 的平方根,接受一个参数 num1num2 始终为 ‘-1’
  • cbrt - 对于 num1 的立方根,接受一个参数 num1num2 始终为 ‘-1’
  • 所有三角函数 – 接受一个参数 num1num2 始终为 ‘-1’

注意: 在发生除以零 (0) 和输入一个参数的函数时,请将 ‘-1’ 作为第二个参数。

示例:

输入: “add 4 5”
输出: 9
说明: 4 + 5 = 9,需要打印出来。

输入: “subtract 7 6”
输出: 1
说明: 7 – 6 = 1,需要打印出来。

输入: “divide 2 0 multiply 6 6”
输出: inf 36 //在这种情况下,它只会打印36
说明: 2/0 =无穷大,而6 * 6 = 36,需要打印出来。

输入: “percent 50 -1”
输出: 0.5 说明: 50/100=0.5,需要打印出来。

方法: 这是一种简单的字符串处理情况,我们需要提取每个单词,然后根据 “操作类型” 计算操作数输出。这可以通过创建函数的if-else阶梯或使用开关案例来完成。如果任何函数无效或在 returnOP 函数中找不到,则返回 ‘-1’

例如: “add 5 6” 可以使用stringstream分解为三个字符串 “add”“5”,“6”。 我们将比较add与我们的函数库,并在将它们转换为浮点数后执行5 + 6。因此,如果 “add” 在位置 i ,则5和6将在位置 i+1i+2 中。

按照以下步骤实现上述方法:

  1. 将字符串传递给函数 processOperations 进行处理。
  2. 使用stringstream类从字符串中提取每个单词,并将所有这些单词存储在向量 strarr 中。
  3. 然后检查第三个单词 (3*k) ,查看所执行的 “操作类型” ,并将其与在 returnOP 函数中可用的操作库进行比较。
  4. 如果找到该操作 returnOP ,则在计算后返回其浮点值,否则返回 “-1”
  5. 根据操作计算输出值,并将它们返回给原函数 processOperations

所有这些我们从 returnOP 获得的操作结果将被推入类型为float的 result 向量中。 result 向量将是我们在执行所有操作后要返回的最终答案。

#include <bits/stdc++.h>
using namespace std;
// MACROS for performing commands
#define e 2.718281828459045
#define pi 3.141592653589793
 
// function for comparing the type of operation
double returnOP(string& word,vector<string>&strarr,int& i)
{
    double num1,num2;
    // using MACROS if required
    if(strarr[i+1].compare("e")==0)
    num1=e;
    if(strarr[i+1].compare("pi")==0)
    num1=pi;
    else num1=stof(strarr[i+1]);
     
    if(strarr[i+2].compare("e")==0)
    num2=e;
    if(strarr[i+2].compare("pi")==0)
    num2=pi;
    else num2=stof(strarr[i+2]);
    double degree=pi*num1/180;
     
     // library of functions to calculate result
      // after finding the 'type of operation'
    if(word.compare("add")==0)
    return num1+num2;
    else if(word.compare("subtract")==0)
    return num1-num2;
    else if(word.compare("multiply")==0)
    return num1*num2;
    else if(word.compare("divide")==0)
    return num1/num2;
    else if(word.compare("percent")==0)
    return (num2/num1)*100;
     
    else if(word.compare("log")==0)
    return log(num1)/log(num2);
    else if(word.compare("exp")==0)
    return pow(num1,num2);
    else if(word.compare("sqrt")==0)
    return sqrt(num1);
    else if(word.compare("cbrt")==0)
    return cbrt(num1);
     
    else if(word.compare("sin")==0)
    return sin(num1);
    else if(word.compare("cos")==0)
    return cos(num1);
    else if(word.compare("tan")==0)
    return tan(num1);
    else if(word.compare("sec")==0)
    return 1/cos(num1);
    else if(word.compare("cosec")==0)
    return 1/sin(num1);
    else if(word.compare("cot")==0)
    return 1/tan(num1);
     
    else if(word.compare("sindeg")==0)
    return sin(degree);
    else if(word.compare("cosdeg")==0)
    return cos(degree);
    else if(word.compare("tandeg")==0)
    return tan(degree);
    else if(word.compare("secdeg")==0)
    return 1/cos(degree);
    else if(word.compare("cosecdeg")==0)
    return 1/sin(degree);
    else if(word.compare("cotdeg")==0)
    return 1/tan(degree);
     
    else if(word.compare("sininv")==0)
    return asin(num1);
    else if(word.compare("cosinv")==0)
    return acos(num1);
    else if(word.compare("taninv")==0)
    return atan(num1);
    else if(word.compare("secinv")==0)
    return acos(1/num1);
    else if(word.compare("cosecinv")==0)
    return asin(1/num1);
    else if(word.compare("cotinv")==0)
    return atan(1/num1);
     
    else if(word.compare("sininvdeg")==0)
    return 180*asin(num1)/pi;
    else if(word.compare("cosinvdeg")==0)
    return 180*acos(num1)/pi;
    else if(word.compare("taninvdeg")==0)
    return 180*atan(num1)/pi;
    else if(word.compare("secinvdeg")==0)
    return 180*acos(1/num1)/pi;
    else if(word.compare("cosecinvdeg")==0)
    return 180*asin(1/num1)/pi;
    else if(word.compare("cotinvdeg")==0)
    return 180*atan(1/num1)/pi;
     
    return -1;
}
// function for fetching each word from a string array of operations
// and pushing them into result after performing required operation
vector<double>processOperations(string& operations)
{
    string word;
    vector<string>strarr;
    vector<double>result;
     
  // stringstream class to extract
  // word from string
    stringstream ss(operations);
    while(ss>>word)
    strarr.push_back(word);
     
  // passing each third word to returnOP
  // to calculate final result of that query
    for(int i=0;i<strarr.size();i+=3)
    result.push_back(returnOP(strarr[i],strarr,i));
     
    return result;
}
 
// driver's code
int main()
{
    string operations="add 1 1 multiply -2 -1 cosecinvdeg 1.4142135624 -1 percent 100 20";
     
    vector<double>ans=processOperations(operations);
     
    for(auto& x:ans)
    cout<<x<<endl;
 
    return 0;
}  

输出:

2
2
90
-500
20
2  // 将1和1相加
2  // 将-2和-1相乘
45 // 求出1.4142135624和-1的余割弧度值
20 // 100和20的百分比

时间复杂度: O(N)

辅助空间: O(N),其中 N 是字符串 operations 中字符的数量。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

C++ 示例