Addition and subtraction of scores-Common Errors and defects in C Language beginner code (11)

Source: Internet
Author: User

# Include <stdio. h> # include <string. h> int abs (int a) {return a> 0? A:-a;} int gcd (int a, int B) {if (a <B) a ^ = B ^ = a ^ = B; return B? Gcd (B, a % B): a;} int main () {int a, B, c, d, temp, temp0, temp1; char op; while (scanf ("% d/% d % c % d/% d", & a, & B, & op, & c, & d )! = EOF) {temp1 = B * d/gcd (B, d); if (op = '-') temp0 = a * temp1/B-c * temp1/d; else temp0 = a * temp1/B + c * temp1/d; temp = gcd (abs (temp0), temp1); temp0/= temp; temp1/= temp; if (temp0 & temp1 = 1) printf ("% d \ n", temp0); else if (temp0) printf ("% d/% d \ n ", temp0, temp1); else printf ("0 \ n");} return 1;} copy the code to evaluate it. This is the code provided by another beginner. This Code seems more concise, but there are also many problems. In terms of the overall structure, we cut corners, greedy and cheap (saving the province to write the function type declaration), and improper order (putting main () behind ). # Include <string. h>. In fact, it is unnecessary to write this article, because it is useless at all. Programmers should know what it is called Occam's razor: Plurality shocould not be posited without necessity. (The Latin expression is: Pluralitas non est ponenda sine neccesitate .). Main () int a, B, c, d, temp, temp0, temp1; a, B, c, d, these identifiers are too vulgar, but considering that they come directly from the question, it seems that it is inconvenient to be deeply responsible. Temp, temp0, and temp1 are too bad. It should not be defined in this position at all. It should be defined in the loop body of while. Char op; no problem. op is obviously the abbreviation of operantion. While (scanf ("% d/% d % c % d/% d", & a, & B, & op, & c, & d )! = EOF) the scanf () function call conversion format -- "% d/% d % c % d/% d", or "matching format" is not well designed. To strictly meet the requirements of the question "a, B, c, d is a 0-9 integer, the format should be "% 1d/% 1d % c % 1d/% 1d ". The positive and negative signs can be regarded as part of c: "% 1d/% 1d % 2d/% 1d", which is applicable only to addition and subtraction and is not general. If you want programs to be more tolerant, such as accepting reasonable spaces in the input stream, such as 1/8 + 3/8, the conversion format should be "% d/% d % c % d/% d ". For "% d/% d % c % d/% d", an input such as 1/8 + 3/8 can only be converted to one "% d" at most, that is, the correct one. For "% d/% d % c % d/% d", input like 1/8 + 3/8 can be fully correctly converted (or even adding a few white spaces ). This is because for scanf (), the white-space character in the conversion format means to read to the first non-white-space character. (A directive composed of white-space character (s) is executed by reading input up to the first non-white-space character (which remains unread ), or until no more characters can be read. the directive nev er fails .) generally, no white-space character or non-white-space character (how to use the scanf () function to abuse itself) should be written in the conversion control string of scanf ), but there are exceptions in everything. Temp1 = B * d/gcd (B, d); impatient. He started to deal with public details in a hurry. Careless. Apparently, the value of B or d is not considered as 0. If (op = '-') temp0 = a * temp1/B-c * temp1/d; else temp0 = a * temp1/B + c * temp1/d; it seems a little bulky. If I write if (op = '-') c =-c; temp0 = a * temp1/B + c * temp1/d; temp = gcd (abs (temp0), temp1); it is better to evaluate the small thing like absolute value within the definition of the gcd () function. This article highlights the details and influences the expression of the main ideas. If (temp0 & temp1 = 1) printf ("% d \ n", temp0); else if (temp0) printf ("% d/% d \ n ", temp0, temp1); else printf ("0 \ n"); this code should be abstracted as a function, and it seems to be muddy. Return 1; this is amazing. Should return 0; abs () int abs (int a) {return a> 0? A:-a;} the name of this function is obviously incorrect. This name should not be used, because a library function is the name (declared in stdlib. h ). Of course, this is not impossible, but it involves a series of complex language rules. People who know this rule know that their functions can be the same name as library functions, but they never use it like this. I believe that this beginner does not know this rule, and he or she writes it like this: It's just a blind cat. Int gcd (int a, int B) {if (a <B) a ^ = B ^ = a ^ = B; return B? Gcd (B, a % B): a;} this function is incorrect. If (a <B) a ^ = B ^ = a ^ = B; there are two problems. First, the expression a ^ = B ^ = a ^ = B is an undefined behavior (see the pre-Paster for "a + = a-= a *, and a + = a-= a * "). Second, from the code below, there is no need to sort a and B. (See chapter 5 of pinwu C to draw questions 12 "incomplete thinking", p152)

Related Article

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.