当前位置:网站首页>表达式求值
表达式求值
2022-07-21 05:15:00 【龙星尘】
描述:
输入为四则运算表达式,仅由数字,+,-,*,/,()组成,没有空格,要求求其值。假设运算符算出来的结果都为整数,‘/’也是整数。
输入:
一串四则运算表达式
输出:
该表达式的值
样例输入1:
(6+4)*5
样例输出1:
50
样例输入2:
5+9/3-4
样例输出2:
4
这道题我们可以用递归的方法来解决,我们可以用三个函数来进行解决,第一个函数是来判断用括号内的,也是来判断哪几个数相加相乘......转移到相对于的函数上面,第二个函数是专门来处理运算符是乘除*/的,第三个函数是将分开的表达式加在一起,是负责加减+-的。
代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int factor_value();
int term_value();
int expression_value();
int factor_value()
{
int result1=0;
char c=cin.peek();
if(c=='(')
{
cin.get();
result1=expression_value();
cin.get();
}
else
{
while(isdigit(c))
{
result1=10*result1+c-'0';
cin.get();
c=cin.peek();
}
}
return result1;
}
int term_value()
{
int result2=factor_value();
while(true)
{
char op = cin.peek();
if(op=='*'||op=='/')
{
cin.get();
int value1=factor_value();
if(op=='*')
result2*=value1;
else result2/=value1;
}
else
break;
}
return result2;
}
int expression_value()
{
int result=term_value();
bool more=true;
while(more)
{
char op=cin.peek();
if(op=='+'||op=='-')
{
cin.get();
int value=term_value();
if(op=='+') result+=value;
else result-=value;
}
else more=false;
}
return result;
}
int main()
{
cout << expression_value() << endl;
return 0;
}
这里面有多个比较陌生的函数,我们一起来看看。
cin.penk观看一个字符串中的第一个字符,不取走(程序中的意思)
cin.peek()的返回值是一个char型的字符,其返回值是指针指向的当前字符,但它只是观测
指针停留在当前位置并不后移;如果要访问的字符是文件结束符,则函数值是-1
cin.get将一个字符串的第一个字符取走(程序中的意思)
1、cin.get()
用来从指定的输入流中提取一个字符(包括空白字符),
函数的返回值就是读入的字符。若遇到输入流中的文件结束符,
则函数值返回文件结束标志EOF(End Of File),一般以-1代表EOF
2、 cin.get(字符数组, 字符个数n, 终止字符)
其作用是从输入流中读取n-1个字符,赋给指定的字符数组(或字符指针指向的数组),如果在读取n-1个字符之前遇到指定的终止字符,则提前结束读取。如果读取成功则函数返回true(真),如失败(遇文件结束符) 则函数返回false(假)。
isdigit(“一个cahr类型变量”)判断这个字符中是否是数字(程序中的意思)
判断一个变量是否是数字
在这里我还要提一下,如果想要这个程序的详细解读,可以看一下我的“哔哩哔哩”看一下我的第一个视频“表达式求值”,谢谢了。
小小问答;
请大家看看这三个函数,将三个函数名翻译成中文,里面的内容就是我下一章要讲的了,拜拜了。
边栏推荐
- 概率论-假设检验
- What is the Internet of things control system? What are its characteristics?
- The importance of PLC industrial control board development
- Mstest cannot exit
- 01 knapsack interview questions series (I)
- What is PCBA? What is the importance of PCBA testing?
- 竟然有人把VSCode玩成了IDEA的效果,有点厉害
- 12 solutions d'optimisation SQL super pratiques
- 110. 平衡二叉树
- Smart devices are coming, making our lives more automated
猜你喜欢
火爆社区的开源数据可视化工具 datart 新用户体验教程
The evolution of data warehouse in recent 10 years and suggestions on database learning
好看又有趣的数据可视化图表制作,膜拜教程
Paoding solves the fiboracci sequence and knapsack problem - analyze the optimization process of the two problems in detail, and take you to understand dynamic programming from the most basic problem!
05-unittest扩展
Easily self-made Pascal VOC dataset
2020普及组总结
01- fundamentals of automated testing -- (selenium eight part + environment configuration + eight positioning)
Liteos connector script (2)
Online JVM memory problem location
随机推荐
Do you really understand the 01 backpack problem? Can you answer these questions of 01 backpack?
组合学总结
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
西瓜书第三章-线性模型
Easily self-made Pascal VOC dataset
2021普及组总结
重磅:国产IDE发布,由阿里研发,完全开源了(高性能+高定制性)
Online JVM memory problem location
深入剖析多重背包问题(上篇)
Automated test model of 03 selenium
Teach you how to use Charles to grab bags
堆-原理到应用——堆排序、优先级队列
「跑象科技」获得天使+融资,打造新一代实时数据基础平台
datart 自定义插件,不改动源代码,让 BI 顺利完成又一次创新
1046. 最后一块石头的重量
Heap - principle to application - heap sort, priority queue
Liteos opening
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
01 knapsack interview questions series (I)
轻松自制PASCAL VOC数据集