/***** Start of odd.c *****/ #include <stdio.h> int gcd( int m, int n ) { int r; if ((m == 0) && (n == 0)) { printf("Exception: gcd(0,0) is undefined\n"); exit(-1); } if (m < 0) m = -m; if (n < 0) n = -n; while (n != 0) { r = m % n; m = n; n = r; } return(m); } int main () { int a, b, c, d, g; int sum, diff, prod, denom; printf("Input a : "); scanf("%d",&a); printf("%d\n",a); printf("Input b : "); scanf("%d",&b); printf("%d\n",b); if (b <= 0) { printf("Error: Positive denominator expected\n"); exit(-1); } printf("Input c : "); scanf("%d",&c); printf("%d\n",c); printf("Input d : "); scanf("%d",&d); printf("%d\n",d); if (d <= 0) { printf("Error: Positive denominator expected\n"); exit(-1); } sum = a * d + b * c; diff = a * d - b * c; prod = a * c; denom = b * d; printf("\nRaw output:\n"); printf("(a/b)+(c/d) = %d/%d\n",sum,denom); printf("(a/b)-(c/d) = %d/%d\n",diff,denom); printf("(a/b)*(c/d) = %d/%d\n",prod,denom); printf("\nReduced output:\n"); g = gcd(sum,denom); printf("(a/b)+(c/d) = %d/%d\n",sum/g,denom/g); g = gcd(diff,denom); printf("(a/b)-(c/d) = %d/%d\n",diff/g,denom/g); g = gcd(prod,denom); printf("(a/b)*(c/d) = %d/%d\n",prod/g,denom/g); printf("\n\n"); } /***** End of odd.c *****/
Input a : -377 Input b : 481 Input c : -899 Input d : 1147 Raw output: (a/b)+(c/d) = -864838/551707 (a/b)-(c/d) = 0/551707 (a/b)*(c/d) = 338923/551707 Reduced output: (a/b)+(c/d) = -58/37 (a/b)-(c/d) = 0/1 (a/b)*(c/d) = 841/1369 Input a : -100 Input b : 31753 Input c : 125 Input d : 19951 Raw output: (a/b)+(c/d) = 1974025/633504103 (a/b)-(c/d) = -5964225/633504103 (a/b)*(c/d) = -12500/633504103 Reduced output: (a/b)+(c/d) = 25/8023 (a/b)-(c/d) = -21225/2254463 (a/b)*(c/d) = -12500/633504103
/***** Start of even.c *****/ #include <stdio.h> int gcd( int m, int n ) { int r; if ((m == 0) && (n == 0)) { printf("Exception: gcd(0,0) is undefined\n"); exit(-1); } if (m < 0) m = -m; if (n < 0) n = -n; while (n != 0) { r = m % n; m = n; n = r; } return(m); } int main () { int a, b, c, d, g; int real1, imag1, denom1; int real2, imag2, denom2; printf("Input a : "); scanf("%d",&a); printf("%d\n",a); printf("Input b : "); scanf("%d",&b); printf("%d\n",b); if ( (a == 0) && (b == 0) ) { printf("Error: Non-zero complex number expected\n"); exit(-1); } printf("Input c : "); scanf("%d",&c); printf("%d\n",c); printf("Input d : "); scanf("%d",&d); printf("%d\n",d); if ( (c == 0) && (d == 0) ) { printf("Error: Non-zero complex number expected\n"); exit(-1); } real1 = a * c + b * d; imag1 = b * c - a * d; denom1 = c * c + d * d; real2 = real1; imag2 = -imag1; denom2 = a * a + b * b; printf("\nRaw output:\n"); printf("(a+ib)/(c+id) = (%d/%d)+i(%d/%d)\n",real1,denom1,imag1,denom1); printf("(c+id)/(a+ib) = (%d/%d)+i(%d/%d)\n",real2,denom2,imag2,denom2); printf("\nReduced output:\n"); printf("(a+ib)/(c+id) ="); g = gcd(real1,denom1); printf("(%d/%d) + i",real1/g,denom1/g); g = gcd(imag1,denom1); printf("(%d/%d)\n",imag1/g,denom1/g); printf("(c+id)/(a+ib) ="); g = gcd(real2,denom2); printf("(%d/%d) + i",real2/g,denom2/g); g = gcd(imag2,denom2); printf("(%d/%d)\n",imag2/g,denom2/g); printf("\n\n"); } /***** End of even.c *****/
Input a : 37 Input b : -33 Input c : -2 Input d : 35 Raw output: (a+ib)/(c+id) = (-1229/1229)+i(-1229/1229) (c+id)/(a+ib) = (-1229/2458)+i(1229/2458) Reduced output: (a+ib)/(c+id) =(-1/1) + i(-1/1) (c+id)/(a+ib) =(-1/2) + i(1/2) Input a : -15163 Input b : 11387 Input c : 6667 Input d : -4189 Raw output: (a+ib)/(c+id) = (-148791864/61996610)+i(12399322/61996610) (c+id)/(a+ib) = (-148791864/359580338)+i(-12399322/359580338) Reduced output: (a+ib)/(c+id) =(-12/5) + i(1/5) (c+id)/(a+ib) =(-12/29) + i(-1/29)
[Course home] [Home]