Become a C + + master of macros and enumerations

Source: Internet
Author: User

Macro

Our calculator program, with 1234 corresponding to subtraction, is a bit of a hindrance to people reading. After one months to look at this code may not be able to think of 0123 or 1234, but also to find in the code, if you can represent the four number of arithmetic to take a meaningful alias, a look is to know what corresponds. No problem, use a macro. The following is the fifth edition of our Calculator program:

//define arithmetic macros#define JIA 1#define JIAN 2#define CHENG 3#define CHU 4intMainvoid){//Save the number of user input    intNumber1;intnumber2;intOpt//Operator    //Prompt user to enter first number:    printf("Please enter the first number: \ n");intR =scanf("%d", &number1);///See if the scanf received the correct number    if(r==0){printf("Don't be naughty, it's not fun, I'm gone.");//Program Exit        return 0; }//Prompts the user to enter a second number    printf("Please enter a second number: \ n"); R =scanf("%d", &number2);if(r==0){printf("Don't be naughty, it's not fun, I'm gone.");//Program Exit        return 0; }//Prompt user input operator    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? I'm gone. ");//Program Exit        return 0; }//program run here to show that two times the input is correct    intResult//Store calculation results    //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!" Gone. ");return;//Exit}//Output Results    printf("%d+%d =%d\n", Number1,number2,result);return 0;}

Macro names are habitually capitalized, and defining macros does not require a semicolon to end. The essence of a macro is to replace it. At compile time, the first step is to replace the macro in the code with the actual value. With a macro also a benefit, when you change the value of the macro, the rest of the program do not move. For example, you can change 1234 to 0123, and the program will run without error.

Enumeration

However, you can use enumerations in addition to macros. As:

enumopt{Jia =1, Jian, Cheng, chu};intMainvoid){//Save the number of user input    intNumber1;intnumber2;intOpt//Operator    //Prompt user to enter first number:    printf("Please enter the first number: \ n");intR =scanf("%d", &number1);///See if the scanf received the correct number    if(r==0){printf("Don't be naughty, it's not fun, I'm gone.");//Program Exit        return 0; }//Prompts the user to enter a second number    printf("Please enter a second number: \ n"); R =scanf("%d", &number2);if(r==0){printf("Don't be naughty, it's not fun, I'm gone.");//Program Exit        return 0; }//Prompt user input operator    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? I'm gone. ");//Program Exit        return 0; }//program run here to show that two times the input is correct    intResult//Store calculation results    //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!" Gone. ");return;//Exit}//Output Results    printf("%d+%d =%d\n", Number1,number2,result);return 0;}

Enumeration has a name, we have this enumeration called OPT. Enumerations look like structs, but enumerations are not exactly the same as structs. Each item of an enumeration is a name for an integer, and each item is a constant, not a member variable. The usefulness of enumerations is more like organizing the related values that represent something, and the macro cannot. For example, arithmetic macros, each can be found at random location, they are not close to and no grammatical errors. Enumerations, in turn, force the associated items to be put together.

The name of the enumerated item is generally not written, and of course you can capitalize like a macro. As long as you can stick to the unified style in the whole project.

Note that this series of articles must be combined with video tutorial http://edu.csdn.net/course/detail/2057 to get a quick start effect. Welcome to the group 535807023 discussion.
Previous: Become a C + + master if and switch

Become a C + + master of macros and enumerations

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.