C ++ macros and enumeration, enumeration

Source: Internet
Author: User

C ++ macros and enumeration, enumeration
Macro

Our calculator program uses 1234 for addition, subtraction, multiplication, and division, which makes reading a little difficult. After a month, you may not be able to remember whether the code is 0123 or 1234. You still have to search for it in the Code. If you can get a meaningful alias for the four numbers representing the four arithmetic operations, first, you know what it is. No problem. Use macros. The fifth edition of our calculator program is as follows:

Http://hovertree.com/menu/cpp/

// Define the macro of the four arithmetic operations # define JIA 1 # define JIAN 2 # define CHENG 3 # define CHU 4int main (void) {// Save the number of user input int number1; int number2; int opt; // operator // prompt the user to enter the first number: printf ("Enter the first number: \ n"); int r = scanf ("% d ", & number1); // check if Scanf has received the correct number. if (r = 0) {printf ("Don't be naughty, it's not fun. I'm leaving \ n "); // exit the program return 0;} // He asked hovertree.com // the user is prompted to enter the second number of printf ("enter the second number: \ n "); r = scanf ("% d", & number2); if (r = 0) {printf ("Don't be naughty, it's not fun, I leave \ n "); // exit retu Rn 0;} // prompt the user to enter the printf operator ("Enter the operator (% d corresponds to addition, subtraction, multiplication, division): \ n", JIA, JIAN, CHENG, CHU); r = scanf ("% d", & opt); if (r = 0) {printf ("operator is also a number, do not make a mistake? How do you confuse me like this? I walked \ n "); // exit the program return 0;} // The program runs here, indicating that both inputs are correct int result; // store the calculation result // switch (opt) {case JIA: // Add result = number1 + number2; break; case JIAN: // minus result = number1-number2; break; case CHENG: // multiply result = number1 * number2; break; case CHU: // division, now only result = number1/number2 can be divisible; break; default: printf ("the operator must be one of 1, 2, 3, and 4! \ N "); return; // exit} // output result printf (" % d + % d = % d \ n ", number1, number2, result ); return 0 ;}

Generally, macro names are written in upper case. The definition macro does not need to end with a semicolon. The essence of macros is replacement. During compilation, the first step is to replace the macro in the Code with the actual value. Macro is also a benefit. When you change the macro value, the rest of the program does not need to be moved. For example, you can change "1234" to "0123" so that the program runs normally.

Enumeration

However, in addition to macros, you can also use enumeration. Example:

Enum OPT {jia = 1, jian, cheng, chu}; int main (void) {// Save the number of user input int number1; int number2; int opt; // operator // prompt the user to enter the first number: printf ("Enter the first number: \ n"); int r = scanf ("% d", & number1 ); // check if Scanf has received the correct number if (r = 0) {printf ("Don't be naughty, this is not fun, I am leaving \ n "); // exit return 0;} // prompt the user to enter the second number printf ("enter the second number: \ n"); r = scanf ("% d ", & number2); if (r = 0) {printf ("Don't be naughty, this is not fun, I went \ n"); // exit the program and return 0 ;} // prompt the user to enter the printf operator ("Enter the operator (% d corresponds to Subtraction and multiplication): \ n ", jia, jian, cheng, chu); r = scanf (" % d ", & opt); if (r = 0) {printf ("the operator is also a number. Do not make a mistake, OK? How do you confuse me like this? I walked \ n "); // The program exited return 0;} // He asked hovertree.com // when the program runs here, it indicates that both inputs are correct int result; // store the calculation result // switch (opt) {case jia: // Add result = number1 + number2; break; case jian: // minus result = number1-number2; break; case cheng: // multiply result = number1 * number2; break; case chu: // division, now only result = number1/number2 can be divisible; break; default: printf ("the operator must be one of 1, 2, 3, and 4! \ N "); return; // exit} // output result printf (" % d + % d = % d \ n ", number1, number2, result ); return 0 ;}

The enumeration has a name. Our name is OPT. Although enumeration looks like a structure, enumeration and structure are completely different. Each item of enumeration is a name for an integer, and each item is a constant rather than a member variable. The usefulness of enumeration is more like organizing the values that indicate something together, and macros cannot do it. For example, the four arithmetic macros can be randomly located for each item. They are not close to each other and there is no syntax error. Enumeration forces related items to be put together.

The name of an enumerated item is generally not in uppercase. Of course, you can use a macro in uppercase. As long as you can stick to the unified style throughout the project.

Http://www.cnblogs.com/roucheng/p/cpp11.html

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.