/* Program: Project 2 Author: Chi Tat Leung Class: CSCI 110 Date: 10/11/03 Description: Rational Numbers I certify that: - All code used in my program is either my original work or was modified by me from the source code published in the text of this course or provided by my instructor. - I have not used code obtained from another student, or any other unauthorized source, either modified or unmodified. - I have not discussed or shared coding details of this program with anyone other than my instructor or MT. SAC tutors. I understand that I may discuss the concepts/ideas of this program with other students as long as we do not get down to the coding details. */ #include int main() { struct fraction { int num; int den; double answer; }; int counter; struct fraction f1, f2, f3, user; char operation, divider; /* -----------------------------first fraction-----------------------*/ do { printf ("\nplease enter the first rational--->"); scanf("%d",&f1.num); scanf ("%c", ÷r); scanf("%d",&f1.den); if (divider != '/') printf("\nplease enter the rational in the form of A/B---> "); }while (divider != '/'); /* -----------------------------operation-----------------------*/ do { printf ("\nplease enter one operator [ + - * / ]---> "); scanf ("%c", &operation); scanf ("%c", &operation); printf ("\nYou entered %c\n",operation); /* if (operation != '+' || operation != '-' || operation != '*' || operation != '/'); { printf("\n Cannot determine your given operation"); } */ }while (operation != '+' && operation != '-' && operation != '*' && operation != '/'); /* -----------------------------second fraction-----------------------*/ do { printf ("\nplease enter the the second rational---> "); scanf("%d",&f2.num); scanf ("%c", ÷r); scanf("%d",&f2.den); if (divider != '/') printf("\nplease enter the rational in the form of A/D---> "); }while (divider != '/'); /* -----------------------------users fraction-----------------------*/ do { printf ("\nplease enter the the your answer--->"); scanf("%d",&user.num); scanf ("%c", ÷r); scanf("%d",&user.den); if (divider != '/') printf("\nplease enter the rational in the form of A/B---> "); }while (divider != '/'); /* -----------------------------Operations-----------------------*/ /* ----------Addition--------*/ if (operation == '+') { f3.num = f1.num * f2.den + f2.num * f1.den; f3.den = f1.den*f2.den; } /* ----------Subtraction--------*/ else if (operation == '-') { f3.num = f1.num * f2.den - f2.num * f1.den; f3.den = f1.den*f2.den; } /* ----------Multiply--------*/ else if (operation == '*') { f3.num = f1.num * f2.num; f3.den = f1.den * f2.den; } /* ----------Division--------*/ else if (operation == '/') { f3.num = f1.num * f2.den; f3.den = f2.num * f1.den; } else { printf("\nBad operator"); } /* -----------------------------Compare Answers-----------------------*/ f3.answer= (f3.num / f3.den); user.answer= (user.num / user.den); if (f3.answer == user.answer) { printf("\nCongratulations, you got it right!"); printf("\n\nYou entered %d%c%d%c%d%c%d=%d%c%d\n",f1.num, divider, f1.den, operation, f2.num, divider, f2.den, user.num, divider, user.den); printf("\nIts floating-point value is %d\n" ,user.num / user.den); } else { printf("Sorry, your answer did not match the calculated answer"); printf("\nProgram is written by anc.\n"); } return 0; }