/******************************************************* * MP4.cpp * ******************************************************* * Solving of multivariable equations using fractions. * * The coefficients for the equation will be read from * * the user defined file. * * The first number in the file is N, number of * * variables, constrained 1<=N<=10 * * It should be followed by the description of coef. * * of the set of algebraic equations. * * The output solution to the system of equations * * will be given in terms of fractions * ******************************************************* * Written by: Olesia Kovalenko * * For: CS 107 * * Date: 2/17/2003 * *******************************************************/ #include #include #include #include #include using namespace std; //constants const int DIM_ARRAY=100; //definition of the class; class Fraction { public: //default constructor Fraction(); //sets the fraction void setFrac(int valnum,int valden); //sets the numerator void setNumer(int numer); //returns numerator int getNum(); //returns denominator int getDen(); //displays fraction void dispFrac(int space1,int space2); //substraction void sub(Fraction frac1,Fraction frac2); // multiplication void mul(Fraction frac1,Fraction frac2); //division void div(Fraction frac1,Fraction frac2); //addition void add(Fraction frac1,Fraction frac2); //reduce the fraction void reduce(); //returns double equivalent of the fraction double retdouble(); private: int num; //numerator int den; //denominator }; //function prototypes //1. reads the data from the file. int readarray(Fraction array[][DIM_ARRAY+1],char file[]) ; //2. displays the equations. void dispEq(Fraction array[][DIM_ARRAY+1],int n_of_var ); //3. finds maximum between two integers int maximum(int num1,int num2); //3a. uses function 3 with doubles double maximum_double(double num1,double num2); //4. displays a single equation void dispOneEq(Fraction array[DIM_ARRAY+1],int n_of_var ); //5. checks if the ith coeficient in ith row is zero int checkIfZero(Fraction array[][DIM_ARRAY+1],int start_at_row,char file[30],int n_of_var ); //6. Performs the swap of two rows void swap(Fraction array[][DIM_ARRAY+1],int old_row, int new_row, int n_of_var); //7. displays the solution void dispSoln(Fraction array[][DIM_ARRAY+1],int n_of_var); //8. gets the maximum space needed to display the number in the collumn void getspace(Fraction array[][DIM_ARRAY+1],int space1[DIM_ARRAY+1],int space2[DIM_ARRAY+1],int n_of_var); //9. finds Greatest Common Denominator int gcd(int num,int den); int main() { cout<>filenameR; cout<>sORc; //absorb the rest of the input while (junk !='\n') { cin.get(junk); } junk=0; //loop untill the correct input is entered while(sORc!='s' && sORc!='c' && sORc!='S' && sORc!='C') { cout<>sORc; while (junk !='\n') { cin.get(junk); } junk=0; } //Notify the user of the selection made: cout<> junk; junk_int=static_cast(junk); if(junk-junk_int !=0) { cout<<"ERROR: In the \""<10) { cout<<"ERROR: In the \""<>junk) { // check if integer junk_int=static_cast(junk); if(junk-junk_int !=0) { cout<<"ERROR: In the \""<(n_of_var*(n_of_var+1))) { cout<<"ERROR: In the \""<= 1) { powOf10++; } } maxspaceNum=maximum(maxspaceNum,powOf10+flagNeg); flagNeg=0; } powOf10=1; //find the maximum space needed to display the denumerator for (int rownext=0; rownext= 1) { powOf10++; } } maxspaceDen=maximum(maxspaceDen,powOf10+flagNeg); flagNeg=0; } //display the solution cout<<"The solution of this system of equations is as follows:"<1) { divisor=gcd(abs(num),abs(den)); num=num/ divisor; den=den/ divisor; } // check if the denominator smaller than zero and if it is //multiply the numerator by -1 if(den<0) { num=-1*num; den=-1*den; } //check if the num equals to zero, that is the fruction is a zero fraction //if it is set numerator==1 if(num==0) { den=1; } } //12. returns double equivalent of the fraction // Inputs: none // Outputs: fractions value as a double double Fraction:: retdouble() { double value=static_cast(num)/den; return(value); }