当前位置:网站首页>Evaluation of four operation expressions
Evaluation of four operation expressions
2022-07-24 20:29:00 【To catty Hawthorn】
/*
Four expressions are evaluated
Ideas , The recursive method , There are two cases of an expression ,1. There is only one item 2. The two terms add and subtract from each other
There are two cases of an item 1. There is only one factor 2. The two factors multiply and divide each other
There are two cases of a factor 1. There's only one number , At this point, recursion terminates , Returns the number return atof()
2.( + exp() + ) Return to the beginning and recurse until there is only one number
Be careful , The naming is not standard , Debug two lines of tears !
*/
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
int term();
int exp();
int factor();
int exp()
{
int value = term();
while(true){
char c = cin.peek();
if(c == '+' || c == '-'){
cin.get();
int val = term();
if(c == '+') value += val;
else value -= val;
}
else break;
}
return value;
}
int term()
{
int value = factor();
while(true){
char c = cin.peek();
if(c == '*' || c == '/'){
cin.get();
int val = factor();
if(c == '*') value *= val;
else value /= val;
}
else break;
}
return value;
}
int factor()
{
int value = 0;
int res = cin.peek();
if(res == '('){
cin.get(); //remove (
value = exp();
cin.get(); //remove )
}
else{
while(isdigit(res)){
value = 10 * value + res - '0';
cin.get();
res = cin.peek();
}
}
return value;
}
int main()
{
printf("%d",exp()); // Why change it to %f became 0, and %d nevertheless 63, Because fixed-point numbers and floating-point numbers
// Are your computer codes different ?
return 0;
}
/*
Input :(2+3)*(5+7)+9/3 Output : 63
*/
边栏推荐
- Machine learning job interview summary: five key points that resume should pay attention to
- MySQL stored procedure
- Redisgraph graphic database multi activity design scheme
- How to use named slots
- Upgrade appium automation framework to the latest 2.0
- Two methods of how to export asynchronous data
- Leetcode 560 and the subarray of K (with negative numbers, one-time traversal prefix and), leetcode 438 find all alphabetic ectopic words in the string (optimized sliding window), leetcode 141 circula
- Inconsistent time
- [sciter]: window communication
- 微服务架构 | 服务监控与隔离 - [Sentinel] TBC...
猜你喜欢

How to use named slots
![[training Day10] linear [mathematics] [thinking]](/img/bf/0082dbe88c579bbb7adc014c60a0be.png)
[training Day10] linear [mathematics] [thinking]

PC port occupation release

API data interface of A-share transaction data

Generate self signed certificate: generate certificate and secret key

Methods of using tyrosine modified peptide nucleic acid PNA | Tyr PNA | BZ Tyr PNA | 99Tcm survivinmrna antisense peptide nucleic acid

Appium element positioning - App automated testing

Covid-19-20 - basic method of network segmentation based on vnet3d
![[training Day8] [luogu_p6335] staza [tarjan]](/img/cf/e2027549c56b8597e7cd579d737392.png)
[training Day8] [luogu_p6335] staza [tarjan]

C# 窗体应用TreeView控件使用
随机推荐
[training Day9] maze [line segment tree]
C# 窗体应用TreeView控件使用
Two methods of how to export asynchronous data
Leetcode 146: LRU cache
Do you want to enroll in a training class or study by yourself?
[training Day8] series [matrix multiplication]
How to learn automated testing
The difference between map and flatmap in stream
Chrome realizes automated testing: recording and playback web page actions
Framework API online viewing source code
Microservice architecture | service monitoring and isolation - [sentinel] TBC
Login Huawei device in SSH mode
微服务架构 | 服务监控与隔离 - [Sentinel] TBC...
How to integrate Kata in kubernetes cluster
C form application treeview control use
Are network security and data security indistinguishable? Why is data security important?
Get the current time in go language, and the simple implementation of MD5, HMAC, SHA1 algorithms
Leetcode 300 longest increasing subsequence (greedy + binary search for the first element subscript smaller than nums[i]), leetcode 200 island number (deep search), leetcode 494 target sum (DFS backtr
Istio一之Envoy工作原理
[training Day10] tree [interval DP]