当前位置:网站首页>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
*/
边栏推荐
- Are network security and data security indistinguishable? Why is data security important?
- Sql164 next day retention rate of new users per day in November 2021
- API data interface for historical data of A-share index
- 2022 chemical automation control instrument test question simulation test platform operation
- Fluoronisin peptide nucleic acid oligomer complex | regular active group alkyne, SH thiol alkynyl modified peptide nucleic acid
- BGP - border gateway protocol
- Generate self signed certificate: generate certificate and secret key
- Covid-19-20 - basic method of network segmentation based on vnet3d
- MySQL data recovery
- Transport layer protocol parsing -- UDP and TCP
猜你喜欢

Actual measurement of Qunhui 71000 Gigabit Network
![[training Day10] silly [simulation] [greed]](/img/31/94c32e05b498f8ad192f8ec2c500ca.png)
[training Day10] silly [simulation] [greed]

Generate self signed certificate: generate certificate and secret key
![[training Day6] game [mathematics]](/img/b2/09c752d789eead9a6b60f4b4b1d5d4.png)
[training Day6] game [mathematics]

Do you want to enroll in a training class or study by yourself?
![[msp430g2553] graphical development notes (2) system clock and low power consumption mode](/img/4e/c08288c3804d3f1bcd5ff2826f7546.png)
[msp430g2553] graphical development notes (2) system clock and low power consumption mode

Valdo2021 - vascular space segmentation in vascular disease detection challenge (I)

C form application treeview control use

Bypass using the upper limit of the maximum number of regular backtracking

In the era of new knowledge economy, who is producing knowledge?
随机推荐
Hook 32-bit function using the method modified to JMP instruction
Upgrade appium automation framework to the latest 2.0
Actual measurement of Qunhui 71000 Gigabit Network
Methods of using tyrosine modified peptide nucleic acid PNA | Tyr PNA | BZ Tyr PNA | 99Tcm survivinmrna antisense peptide nucleic acid
Safe way -- Analysis of single pipe reverse connection back door
[Extension Program - cat scratch 1.0.15 _ online video and audio acquisition artifact _ installation tutorial plus acquisition]
[JVM] selection of garbage collector
1. Mx6u-alpha development board (buzzer experiment)
Software testing interview tips | if you don't receive the offer, I'll wash my hair upside down
Substr and substring function usage in SQL
[training Day10] linear [mathematics] [thinking]
Analysis of xmldecoder parsing process
Istio二之流量劫持过程
微服务架构 | 服务监控与隔离 - [Sentinel] TBC...
Leetcode 1928. minimum cost of reaching the destination within the specified time
The difference between token and session, this classic interview question deserves a more in-depth answer
How to integrate Kata in kubernetes cluster
[msp430g2553] graphical development notes (2) system clock and low power consumption mode
Install MySQL 5.7.37 on windows10
MySQL docker installation master-slave deployment