当前位置:网站首页>Expression evaluation
Expression evaluation
2022-07-21 22:38:00 【Long Xingchen】
describe :
Enter four arithmetic expressions , Just numbers ,+,-,*,/,() form , There are no spaces , Ask for its value . Suppose that the result of the operator is an integer ,‘/’ It's also an integer .
Input :
A string of four arithmetic expressions
Output :
The value of the expression
The sample input 1:
(6+4)*5
Sample output 1:
50
The sample input 2:
5+9/3-4
Sample output 2:
4
We can solve this problem recursively , We can use three functions to solve , The first function is to judge with brackets , It is also to judge which numbers are added and multiplied ...... Move to the relative function , The second function is dedicated to dealing with the operator multiplication and division */ Of , The third function is to add the separated expressions together , Is responsible for adding and subtracting +- Of .
Code :
#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;
}
There are many unfamiliar functions , Let's see .
cin.penk Watch the first character in a string , Don't take it away ( Meaning in the program )
cin.peek() The return value of char Type characters , Its return value is the current character pointed to by the pointer , But it's just observation
The pointer stays at the current position and does not move back ; If the character to be accessed is the end of the file , Then the value of the function is -1
cin.get Take the first character of a string ( Meaning in the program )
1、cin.get()
Used to extract a character from the specified input stream ( Include white space characters ),
The return value of the function is the read character . If you encounter the end of the file in the input stream ,
Then the function value returns the end of file flag EOF(End Of File), General with -1 representative EOF
2、 cin.get( A character array , The number of characters n, Stop character )
Its function is to read... From the input stream n-1 Characters , Assign to the specified character array ( Or the array pointed to by the character pointer ), If you're reading n-1 The specified terminating character was encountered before characters , End reading ahead of time . If the reading is successful, the function returns true( really ), If you fail ( End of file ) Then the function returns false( false ).
isdigit(“ One cahr Type variable ”) Determine whether this character is a number ( Meaning in the program )
Determine whether a variable is a number
I would also like to mention here , If you want a detailed interpretation of this program , You can have a look at mine “ Bili, Bili ” Take a look at my first video “ Expression evaluation ”, Thank you for the .
Little question and answer ;
Please take a look at these three functions , Translate the three function names into Chinese , The content inside is what I will talk about in the next chapter , Bye .
边栏推荐
- YOLOv4(darknet版)后处理:显示置信度、保存检测框的内容到本地
- "Running image technology" obtained Angel + financing to build a new generation of real-time data infrastructure platform
- 地产巨头,数据一体化建设项目方案(拿走不谢)
- 堆-原理到應用——堆排序、優先級隊列
- 近10年数据仓库演进之路,以及数据库学习建议
- 2021普及组总结
- In depth analysis of multiple knapsack problem (Part 2)
- Subsequence
- 205. 同构字符串
- Why is Le Xin in a hurry to release esp32-c3 when there are few words
猜你喜欢
Yolov4 (Darknet version) post processing: display confidence and save the contents of the detection box locally
234. 回文链表
手把手教你在服务器上用YOLOv4训练和测试数据集(保姆级)
Source Insight 4.0个性化设置及快捷键
844. 比较含退格的字符串
beta.4 版发布啦,国产开源数据可视化 datart 接下来将会小跑进入 rc 阶段
438. 找到字符串中所有字母异位词
用两个模板(递归法和迭代法)完成二叉树的四种遍历方式
108. 将有序数组转换为二叉搜索树
堆-原理到應用——堆排序、優先級隊列
随机推荐
organize a team
994. 腐烂的橘子
n的阶乘
Open source data visualization tool datart new user experience tutorial for popular communities
近10年数据仓库演进之路,以及数据库学习建议
What is a direct drinking machine? What is its working principle and advantages?
线性回归大结局(岭(Ridge)、 Lasso回归原理、公式推导),你想要的这里都有
西瓜书第二章笔记-评估方法
437. 路径总和 III
深入剖析斐波拉契数列
029:陶陶摘苹果
74. 搜索二维矩阵
火爆社区的开源数据可视化工具 datart 新用户体验教程
一文讲透,分布式系统的数据分片难题
YOLOv4(darknet版)后处理:显示置信度、保存检测框的内容到本地
2021普及组总结
Datart data visualization works are open source | chart plug-in works are all open source, which can be extracted through Baidu cloud download link
概率论-方差和协方差、相关系数
844. 比较含退格的字符串
Good looking and interesting data visualization chart making, worship tutorial