Become a C + + Master of the final version of the calculator

Source: Internet
Author: User
Tags switch case

Below is the final version of our calculator.

The problem now is that the user can only count once. If someone buys a disposable calculator, then he must be a local tyrants. Our calculator can not be used only for local tyrants, so we should change it to be used repeatedly.

You can use loop statements, but what code loops? From user input to printing results this process is repeated, and the code is:

enumopt{Jia =1, Jian, Cheng, chu};intMainvoid){//Save the number of user input    intNumber1;intnumber2;intOpt//Operator   //loop the process from input to output results     while(1){//Prompts the user to enter the first number, which needs to be looped until the user enters the correct number:        intR while(1){printf("Please enter the first number: \ n"); R =scanf("%d", &number1);///See if the scanf received the correct number            if(r==0){printf("Don't be naughty, it's not good to play, again \");//clears the input stream. Without this sentence, the remaining characters in the input stream will cause scanf not to wait for user input.                 intC while((c = GetChar ())! =' \ n '&& c! = EOF); }Else{//Lose the right, jump out of the loop                 Break; }        }//Prompts the user to enter a second number         while(1){printf("Please enter a second number: \ n"); R =scanf("%d", &number2);///See if the scanf received the correct number            if(r==0){printf("Don't be naughty, it's not good to play, again \");//clears the input stream. Without this sentence, the remaining characters in the input stream will cause scanf not to wait for user input.                 intC while((c = GetChar ())! =' \ n '&& c! = EOF); }Else{//Lose the right, jump out of the loop                 Break; }        }intResult//Store calculation results        //Prompt user input operator         while(1){printf("Please enter operator (%d%d%d%d corresponds to subtraction): \ n", Jia,jian,cheng,chu); R =scanf("%d", &opt);if(r==0){printf("Operators are numbers too, don't be mistaken, okay? How do you make me do this? Re-enter it \ n ");//clears the input stream. Without this sentence, the remaining characters in the input stream will cause scanf not to wait for user input.                 intC while((c = GetChar ())! =' \ n '&& c! = EOF);//back to loop start                Continue; }//follow-up operator for different operations            Switch(opt) { CaseJia//Plusresult = Number1+number2; Break; CaseJian//Minusresult = Number1-number2; Break; CaseCheng//Multiplyresult = Number1*number2; Break; CaseChu//Except, now only divisibleresult = Number1/number2; Break;default:printf("operator must be one of the 1,2,3,4!" Again \ n ");Continue;//Go back to the loop and start over again}//When executing here, the operator check succeeds and should jump out while.              Break; }//Output Results        printf("Result:%d\n", Number1,number2,result); }return 0;}

To complete this program, you have mastered the most basic knowledge of getting started. Summing up, the following concepts should be mastered: functions, variables, header files, entry functions, parameters, returns, while loops, if judgments, switch case, macros, enumerations, structures, declarations, definitions.

To learn the next step, you need to be proficient in basic grammar, especially when you can quickly distinguish between definitions and declarations, calling functions and accessing variables. Key points are as follows:

    • function definition syntax four elements: return value type, function name, parameter, function body, asint add(int a,int b){ return a+b;}
    • function call syntax two elements: function name, parameter, plus an optional: return value. Such asint r = add(2,4);
    • function calls cannot be written outside the function.
    • Variable definition syntax Two elements: type, variable name, plus an optional: initial value. Such as:int r;
    • The difference between a function declaration and a definition is that there is no curly brace.
    • Distinguish between calling a function or using a variable: with or without parentheses. For example: int a=abc; ABC must be a variable; int a=abc(); ABC must be a function.
    • The difference between a variable declaration and a definition is that there is no extern, which is declared when extern , and cannot be initialized at the time of declaration. For example: extern int a; It is a declaration, but a int a; definition.
      Previous: Become a C + + master while loop

Become a C + + Master of the final version of the calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.