Nyoj: Topic 111 fractions Plus Subtraction

Source: Internet
Author: User
Tags greatest common divisor min time limit
fractional addition and subtractionTime limit: Ms | Memory limit: 65535 KB Difficulty: 2 Description Write a C program that implements two fractions of the add and subtract input input containing multiple rows of data
Each row of data is a string, and the format is "A/BOC/D".
Where A, B, C, D is an integer of 0-9. O is the operator "+" or "-".

Data ends with EOF
The input data ensures that the legitimate output outputs two fractions of the result for each line of input data.
Note that the results should conform to the writing habit, with no unnecessary symbols, molecules, and denominators, and simplify to the simplest fraction sample input
1/8+3/8
1/4-1/2
1/3-1/3
Sample output
0 -1/4
Source Water Problem Contest uploader HZYQAZASDF

Addition: direct-pass, then add, and then find the numerator denominator of the greatest common divisor numerator on the line.

Subtraction: Judging the size, with a large reduction (if itself is small to reduce the large, the result of the former plus-number),-pass, subtract, and then find the numerator denominator greatest common divisor numerator on the line.

Note: 1.0 and a number can not be found greatest common divisor (so special case another judgment) 2. The denominator is 1 direct output molecule.


#include <stdio.h> int a,b,c,d;
	int yuefen (int fenzi,int fenmu)//Find greatest common divisor {int da=fenzi,xiao=fenmu;
		if (da<xiao) {int t=da;
		Da=xiao;
	xiao=t;
		} while (da%xiao!=0) {Da=da/xiao;
			if (da<xiao) {int t=da;
			Da=xiao;
		xiao=t;
}} return Xiao;
	} int main () {char s[7];
		while (scanf ("%s", s)!=eof) {a=s[0]-48;
		b=s[2]-48;
		c=s[4]-48;
		d=s[6]-48;
			if (s[3]== ' + ') {if (a==0&&c==0)//0 and a number cannot be found greatest common divisor {printf ("0\n");
				} else {int fenzi=a*d+c*b;
				int fenmu=b*d;
				int Gongyueshu=yuefen (FENZI,FENMU);
				Fenzi=fenzi/gongyueshu;
				Fenmu=fenmu/gongyueshu;
				if (fenmu==1)//denominator is 1 output molecule {printf ("%d\n", Fenzi);
				} else {printf ("%d/%d\n", FENZI,FENMU);
				}}} if (s[3]== '-') {if (double) a/b< (double) C/D)//First judge size {int fenmu=b*d;
				int fenzi=b*c-a*d;
				int Gongyueshu=yuefen (FENZI,FENMU);
				Fenzi=fenzi/gongyueshu;
				Fenmu=fenmu/gongyueshu; 
	if (fenmu==1)//denominator is 1 output molecule			{printf ("-%d\n", Fenzi);
				} else {printf ("-%d/%d\n", FENZI,FENMU); }} else {if ((a==c&&b==d) | | | (a==0&&c==0))
				The numerator is 0 {printf ("0\n");
					} else {int fenmu=b*d;
					int fenzi=a*d-b*c;
					int Gongyueshu=yuefen (FENZI,FENMU);
					Fenzi=fenzi/gongyueshu;
					Fenmu=fenmu/gongyueshu;
					if (fenmu==1)//denominator is 1 output molecule {printf ("%d\n", Fenzi);
					} else {printf ("%d/%d\n", FENZI,FENMU);
}}}}} return 0; }


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.