In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
C++ how to achieve a simple calculator function, many novices are not very clear, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Restrictions:
1. Only branch statements and loop statements can be used
2. Arrays and custom functions cannot be used
(that is, think of yourself as a beginner who only learns branch statements and loop statements)
Calculator requirements:
1. Simple addition, subtraction, multiplication and division can be carried out.
2. If there are multiple operation symbols, the operation order of multiplication and division and then addition and subtraction must be considered.
3. No parenthesis operator is required
A relatively simple program, but with the above restrictions, let me spend a lot of effort.
The source code is as follows: # includeusing namespace std;int main () {/ / first declare variables: double a0Powera1recovera2ash / to handle the operation of multiple decimal places, float is used here; char ca1,ca2;// character type is used to store operation symbols; / / consider the simple case first-enter a number and then directly enter the equal sign and output the result Cin > > a0 > > ca1; if (ca1=='=') coutca2; while (1) / enter the first loop here to handle operations with only addition and subtraction; {if (ca1=='=') {coutca2;break;// continues the loop because the value assigned by ca2 to ca1 is not an equal sign, so the values of A1 and ca2 must be updated Case'*': case'/': / / if the second operator is a multiplication sign or division sign, enter the second loop to deal with the problem of continuous multiplication or division; while (1) {/ / continuous multiplication or division Switch (ca2) {/ / the third variable is assigned here, and the result of multiplication and division of A1 and a2 is stored in A1. Case'*': cin > > ca2;break; case'/': cin > > ca2;break; case'/': cin > > a2 * *: ca2; / / and update the value of ca2 to handle the following operations. } if (ca2=='=') / / if the value of ca2 becomes the equal sign {switch (ca1) {/ / add and subtract the result A1 of the previous multiplication and division with the previous a0, and store the result in a0 Case'+': case'-': a0rooma0Mura1umbbreak.a0plaina0Mura1politicbreak.} ca1=ca2; break / / this is also a condition for jumping out of the second loop, and you have to jump out of the loop when ca2 is plus or minus. } if (ca2=='+' | | ca2=='-') {switch (ca1) {case'+': a 0 percent a 0 percent A1 break Case'-': return 0: return 1;} ca1=ca2;cin > > A1 > > ca2; break;}}
Note:
1. When the value of ca2 is plus sign or minus sign or equal sign, there must be ca2 assignment to ca1, otherwise there is no way to deal with the next operation or jump out of the loop to output the result.
2. The function of a0 is to store the result of addition and subtraction, while the function of A1 is to store the result of multiplication and division. Considering the order of operation, the final output can only be a0.
Running result:
Verify the results with a calculator:
The result is correct!
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.