当前位置:网站首页>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
*/
边栏推荐
- Google's display of Chrome browser icons was abandoned, and it was intended to be a small rocket
- (forward) usage of PostMessage
- Vscode connected to the remote server cannot format the code / document (resolved)
- 1. Mx6u-alpha development board (key input experiment)
- A new UI testing method: visual perception test
- BGP - border gateway protocol
- How to learn automated testing? Can you teach yourself?
- Failed to create a concurrent index, leaving an invalid index. How to find it
- Istio二之流量劫持过程
- The difference between token and session, this classic interview question deserves a more in-depth answer
猜你喜欢

Pix2seq: Google brain proposes a unified interface for CV tasks!

Open source demo | release of open source example of arcall applet
![[msp430g2553] graphical development notes (1) configuration environment](/img/42/479c96d1e7f6747f893d1a0b65be3f.png)
[msp430g2553] graphical development notes (1) configuration environment

Lunch break train & problem thinking: thinking about the problem of converting the string formed by hour: minute: second to second

Synthesis route of ALA PNA alanine modified PNA peptide nucleic acid | AC ala PNA

Cloud native observability tracking technology in the eyes of Baidu engineers

147 set whether to cache by using the routing meta information - use of include and exclude - use of activated and deactivated

English grammar_ Demonstrative pronoun this / these / that / those

clip:learning transferable visual models from natural language supervision

TCP sliding window, singleton mode (lazy and hungry) double checked locking / double checked locking (DCL)
随机推荐
Monotone stack and monotone queue (linear complexity optimization)
How to use named slots
Teach you five ways to crack the computer boot password
Valdo2021 - vascular space segmentation in vascular disease detection challenge (2)
Luogu - p1616 crazy herb picking
How to set appium script startup parameters
How to view the execution plan of a stored procedure in Youxuan database
clip:learning transferable visual models from natural language supervision
Modulenotfounderror: no module named 'pysat.solvers' (resolved)
Hook 32-bit function using the method modified to JMP instruction
Safe way -- Analysis of single pipe reverse connection back door
(posted) differences and connections between beanfactory and factorybean
Methods of using tyrosine modified peptide nucleic acid PNA | Tyr PNA | BZ Tyr PNA | 99Tcm survivinmrna antisense peptide nucleic acid
C# 窗体应用TreeView控件使用
Azide labeled PNA peptide nucleic acid | methylene blue labeled PNA peptide nucleic acid | tyrosine modified PNA | Tyr PNA Qiyue Bio
What does software testing need to learn?
Flink window & time principle
Huawei set up login with account and password
Modbus communication protocol specification (Chinese) sharing
Richview table table alignment