Score addition and subtraction-Common Errors and defects in C Language beginner code (12)

Source: Internet
Author: User

I discard the precondition that "a, B, c, and d are 0-9 integers" in the original question because such restrictions are unnecessary. Assume that a, B, c, and d are character sequences in the form of decimal integers. I do not know how to end the input of such questions. The following code ends if the complete formula is not entered correctly. Data Structure: typedef struct {int numer; // int denom; // denominator} frac_t; // The score type data requires three variables, two record scores, and one record operator. # Include <stdio. h> int main (void) {frac_t frc1, frc2; // The two operands char op; // return 0;} copy the overall structure of the Code # define FAIL 0 int main (void) {frac_t frc1, frc2; // two scores char op; // operator while (input_exp (& frc1, & op, & frc2 )! = FAIL) // input formula {// calculation, output} return 0;} implement int input_exp () (frac_t *, char *, frac_t *); int input_frac (frac_t *); int input_exp (frac_t * p_f1, char * p_o, frac_t * p_f2) {if (input_frac (p_f1 )! = 2) return FAIL; if (scanf ("% c", p_o )! = 1) return FAIL; switch (* p_o) {default: return FAIL; // not add or subtract case '+': case '-':;} if (input_frac (p_f2 )! = 2) return FAIL; return! FAIL;} int input_frac (frac_t * p_f) {return scanf ("% d/% d", & p_f-> numer, & p_f-> denom);} // calculates, output first exclude meaningless input if (frc1.denom = 0 | frc2.denom = 0) // meaningless input {puts ("score meaningless"); continue ;} change subtraction to addition switch (op) {case '-': frc2.numer =-frc2.numer; // convert subtraction to addition case '+': add_to (& frc1, & frc2 ); // put the calculation result in frc1 break;} final output result output (frc1); putchar ('\ n'); complete code:/* add and subtract points to write a C program, implements addition and subtraction of two scores Input: the format of each line of data that contains multiple rows is a/boc/d. Here, a, B, c, and d are decimal integers, and o is the operator "+" or "-". Output: outputs two scores for each row of the input data. Note that the results should conform to the writing habits. There are no redundant symbols, molecules, and denominator, and the score is simplified to the simplest. Example input: 1/8 + 3/8 1/4-1/2 1/3-1/3 output: 1/2-1/4 0 */# include <stdio. h> # include <stdlib. h> typedef struct {int numer; // int denom; // denominator} frac_t; // score type # define FAIL 0 int input_exp (frac_t *, char *, frac_t *); int input_frac (frac_t *); void add_to (frac_t *, frac_t const *); int find_lcm (int, int); int find_gcd (int, int ); void reduce (frac_t *); void Output (frac_t); int main (void) {frac_t frc1, frc2; // two scores: char op; // operator while (input_exp (& frc1, & op, & frc2 )! = FAIL) // input formula {// calculation, output if (frc1.denom = 0 | frc2.denom = 0) // meaningless input {puts ("score meaningless"); continue;} switch (op) {case '-': frc2.numer =-frc2.numer; // convert subtraction to addition case '+': add_to (& frc1, & frc2); // place the calculation result in frc1 break;} output (frc1 ); putchar ('\ n');} return 0;} void output (frac_t fr) {if (fr. numer <0) {putchar ('-'); fr. numer =-fr. numer;} if (fr. denom = 1) {printf ("% d ", Fr. numer); return;} printf ("% d/% d", fr. numer, fr. denom);} void reduce (frac_t * p_f) {int gcd = find_gcd (abs (p_f-> numer), abs (p_f-> denom )); p_f-> denom/= gcd; p_f-> numer/= gcd;} int find_gcd (int m, int n) {int t; return (t = m % n) = 0? N: find_gcd (n, t);} int find_lcm (int m, int n) {return m/find_gcd (m, n) * n;} void add_to (frac_t * p_f1, frac_t const * p_f2) {int lcm = find_lcm (abs (p_f1-> denom), abs (p_f2-> denom )); p_f1-> numer = lcm/p_f1-> denom * p_f1-> numer + lcm/p_f2-> denom * p_f2-> numer; p_f1-> denom = lcm; // The denominator is always positive reduce (p_f1); // approx.} int input_frac (frac_t * p_f) {return scanf ("% d/% d ",& P_f-> numer, & p_f-> denom);} int input_exp (frac_t * p_f1, char * p_o, frac_t * p_f2) {if (input_frac (p_f1 )! = 2) return FAIL; if (scanf ("% c", p_o )! = 1) return FAIL; switch (* p_o) {default: return FAIL; // not add or subtract case '+': case '-':;} if (input_frac (p_f2 )! = 2) return FAIL; return! FAIL ;}

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.