Click here to Skip to main content
15,912,932 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include<stdio.h>
#include<string>
#include<iostream>

using namespace std;

const int SIZE = 6;

// parse one equation
void Equation(string line, double coefRow[], double& coefB, string& coefVal){

    int len = int(line.length());
    bool firstItem = true;
    int j=0, m=0;
    coefVal = " ";
    
    string eq = "", item="";
    for (int i = 0; i &lt; len; i++){

        // the case like +x or -x in the start of the line
        if (line[i] == '+' || line[i] == '-'){
            if (isalpha(line[i + 1])){
                coefVal += line[i + 1];
                coefRow[j++] = (line[i] == '+') ? 1. : -1.;

                i++;
                continue;
            }
        }

        // the  last coefficient and the coefficient after the sign "="
        if (line[i] == '='){
                coefRow[j++] = atof(item.c_str()); // the  last coefficient
                item = "";
                for (int l = i + 1; l &lt; len; l++)
                    item += line[l];
                coefB = atof(item.c_str());  // the coefficient after the sign "="
                break;
            }

        if (isalpha(line[i])){
                if (firstItem){ // the case like x  in the start of the line
                firstItem = false;
                coefRow[j++] =  1.;
                }
                else coefRow[j++] = atof(item.c_str()); // the coefficient after the sign "+" or "-"
                item = "";
                m = 0;
                coefVal += line[i];
                firstItem = false;
                continue;
            }
        
        // the case when the  current symbol is not letter or sign (digit and decimal)
        if (m&gt;8)cerr &lt;&lt; "The maximum length of coefficients &gt;8\n";
                item += line[i];
                m++;
                firstItem = false;
        }

    
    
}

//Recursive function for the determinant calculation
double Determinant(double A[SIZE][SIZE], int size){

    double res = 0;
    if (size == 2)return A[1][1];  // real matrix size =1
    if (size == 3)return A[1][1] * A[2][2] - A[2][1] * A[1][2]; // real matrix size=2

    // use the first column and the new matrix with  size=size-1
    int newSize = size - 1;
    double NewA[SIZE][SIZE];
    for (int i = 1; i&lt;size;&gt;      for (int j = 1; j &lt; newSize; j++){
            for (int k = 1; k &lt; newSize; k++)
                if (k &gt;= i) NewA[j][k] = A[j+ 1][k + 1];
                else NewA[j][k] = A[j + 1][k ];
        }
        int p = (i % 2) ? 1:-1;
        res += p * A[1][i] * Determinant(NewA, newSize); //  recursive call function with smaler size
    }
        return res;
}

// Cramer's rule
void Solve(double A[SIZE][SIZE], double b[SIZE], double sol[SIZE], int size){

    double temp[SIZE][SIZE], det;
    

    //call function for the determinant calculation
    det = Determinant(A, size);


    for (int j = 1; j &lt; size; j++){


        //temp = A
        // copy A to temp
        for (int k = 1; k &lt; size; k++)
            for (int l = 1; l &lt; size; l++)
                temp[k][l] = A[k][l];


        // replace column number j to new values (b)
        for (int i = 1; i &lt; size; i++)
            temp[i][j] = b[i];
        
        /* print for the program testing 
        cout &lt;&lt; "\ntemp: \n";
            for (int k = 1; k &lt; size; k++){
                for (int l = 1; l &lt; size; l++)
                    cout &lt;&lt; temp[k][l] &lt;&lt; " ";
                cout &lt;&lt; endl;
            }
            cout &lt;&lt; "det(temp)=" &lt;&lt; Determinant(temp, size) &lt;&lt; endl;
        */  

        //the solution of the system
        sol[j] = Determinant(temp, size) / det;

    }

}



int  main(){
// Tests for the program
// string line[] = { "1578x+2345y+8452z=467", "6543x+2348y+9873z=7654", "4230x+6740y+5433z=4546" }; // size = 3
// string line[] = {"78x-45y+52z=7", "43x+48y-73z=54", "30x+40y+33z=46"}; // size = 3
// string line[] = {"50a+37b+c=-44.39", "43a+39b+c=-45.31", "52a+41b+c=-44.96"}; //size = 3
// string line[] = { "a+0b+0c=-44.39", "0a+b+0c=-45.31", "0a+0b+c=-44.96" }; //size = 3
// string line[] = {"2.25x+2.5y+4z+3t=0.5", "0.25x+42y+3.1z+0t=1.33", "4.2x+11y+0z+3t=1.5", "10x+3.2y+0z+2t=1"};  //size = 4
    string row, coefVals;
    double coefRow[SIZE], coefB;
    int i, j, size;
    double det;
    double A[SIZE][SIZE],  b[SIZE], sol[SIZE];

        do{
            cout &lt;&lt; "Number of Input equations (minimum 1 ~ maximum 5): ";
            cin &gt;&gt; size;
        } while (size &lt; 1 || size &gt; 5);
        
    cout &lt;&lt; "Please, enter the " &lt;&lt; size++ &lt;&lt; " equations \n"
         &lt;&lt; "(Equations to be entered as strings, without spaces)\n";


    for (i = 1; i &lt; size; i++){
    cin &gt;&gt; row; //
    //row=line[i - 1]; // for tests(instead input lines)
    Equation(row, coefRow, coefB, coefVals);
    for (j = 1; j &lt; size; j++)
                A[i][j] = coefRow[j - 1];
        b[i] = coefB;
    }



    cout &lt;&lt; "\nA: \n";
    for (i = 1; i &lt; size; i++){
        for (j = 1; j &lt; size; j++)
            cout &lt;&lt; A[i][j] &lt;&lt; " ";
        cout &lt;&lt; endl;
        }

    cout &lt;&lt; "\nb: \n";
    for (i = 1; i &lt; size; i++)
        cout &lt;&lt; b[i] &lt;&lt; endl;



    cout &lt;&lt; "\nVariables: \n";
    for (i = 1; i &lt; size; i++)
        cout &lt;&lt; coefVals[i] &lt;&lt; endl;



    cout &lt;&lt; "\nDeterminant= ";


    det = Determinant(A, size);
    cout &lt;&lt; det;
    if (det != 0){
        Solve(A, b, sol, size);
        cout &lt;&lt; "\nSolution: \n";
        for (i = 1; i &lt; size; i++)
            cout &lt;&lt; sol[i] &lt;&lt; endl;
    }

    else
        cout &lt;&lt; "\nSystem has not solution ";


    cin.get();
    cin.get();
    
return 0;
}


What I have tried:

Its my final project and i couldn't change it to c its so difficult so if any one can help
Posted
Updated 2-Jul-16 3:44am
v2
Comments
Richard MacCutchan 28-Jun-16 3:49am    
Apart from the cout and cin calls, it is already C.
Jounir 28-Jun-16 5:48am    
The time the professor saw it he say its C++ and i want just C
Richard MacCutchan 28-Jun-16 5:52am    
You have been given the answer below.
Jounir 28-Jun-16 5:58am    
I just saw it thank you very much

delete
C#
using namespace std;

replace "cout" with "printf"
 
Share this answer
 
Comments
Jounir 28-Jun-16 4:14am    
Do just i change the cout to printf that's it
[no name] 28-Jun-16 4:38am    
cout and cin is C++, for C you must use printf and scanf
Jounir 28-Jun-16 5:58am    
Thank you very much sir
Patrice T 28-Jun-16 13:48pm    
Use Improve question to update your question.
So that everyone can pay attention to this code.
Read in the wikipedia the Compatibility of C and C++ article to understand that issue.

Compile the code and repair the compiler bugs. The most will be the ordering and the use of objects which arent allowed. So you need to work with char arrays...
 
Share this answer
 
C++
int a=10;
char b='a';
//C++
cout<<"Hello"<<a<<b<<endl;
//C
printf("Hello%i%c",a,b);

also remove:
C++
#include<iostream>
//and
using namespace std;


regards
Ratul
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900