Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- using namespace std;
- string raspuns_altfel = "normal";
- float rezolva( string ec )
- {
- bool gasit_egal = false, gasit_minus = false;
- int termen_liber_st = 0, termen_liber_dr = 0;
- int coef_st = 0, coef_dr = 0, s = 0;
- int i, j, n = ec.size();
- cout << "Ecuatia introdusa: " << ec << endl << endl;
- int A, B;
- i = 0;
- while( i < n )
- {
- //cout << "ec[" << i << "] = " << ec[i] << endl;
- //cin.get();
- if( ec[ i ] == '=' )
- {
- gasit_egal = true;
- ++i;
- continue;
- }
- if( ec[i] == '-' )
- gasit_minus = true;
- if( ec[i] >= '0' && ec[i] <= '9' )
- {
- s = ec[i] - '0';
- //cout << " Plec cu S = " << s << endl;
- ++i;
- /// Partea asta de while creeaza probleme: ar trebui sa prelucreze doar cifrele
- /// de extinderea mai departe trebuie sa se ocupe while-ul mare.
- while( i < n )
- {
- if( ec[i] >= '0' && ec[i] <= '9' )
- s = s * 10 + (ec[i] - '0');
- if( ec[i] == 'x' ) /// Atentie la ceva de tipul: 2x=3
- {
- coef_st += s;
- ++i; /// Trec la urmatorul caracter care poate fi: +, -, = sau sfarsit
- break; /// i indica pe x
- }
- if( ec[i] == '=' )
- {
- termen_liber_st += s;
- gasit_egal = true;
- cout << endl << "DREAPTA" << endl << endl;
- break;
- }
- if( ec[i] == '+' || ec[i] == '-' )
- {
- if(gasit_egal == false )
- termen_liber_st += s;
- else
- termen_liber_dr += s;
- break;
- }
- ++i;
- }
- }
- cout << "Pana acum, numarul format: " << s << endl;
- cin.get();
- if(gasit_minus == true )
- {
- s = -s;
- gasit_minus == true;
- }
- if( gasit_egal == false ) /// tin cont de ultimul numar (coef ori termen liber) de dinainte de egal
- {
- coef_st += s;
- termen_liber_st += s;
- }
- else
- coef_dr += s;
- cout << "Termen liber st: " << termen_liber_st << endl;
- cout << "Termen liber dr: " << termen_liber_dr << endl;
- cout << "Coef st: " << coef_st << endl;
- cout << "Coef dr: " << coef_dr << endl;
- ++i;
- } /// se inchide while-ul mare
- if( ec[ n - 1 ] != 'x' ) /// Daca e termen liber
- termen_liber_dr += s;
- A = coef_st - coef_dr;
- B = termen_liber_dr - termen_liber_st;
- cout << "Ecuatia restransa este:" << endl;
- cout << A << " * x = " << B << endl;
- if( A == 0 && B != 0 )
- raspuns_altfel = "imposibil";
- if( A == 0 && B == 0 )
- raspuns_altfel = "infinit";
- if( raspuns_altfel == "normal" )
- return (float) B / A;
- else
- return -999999;
- }
- int main()
- {
- string ecuatie;
- cin >> ecuatie;
- //cout << "Semnul = are codul: " << (int)'=' << endl;
- //cout << "Semnul + are codul: " << (int)'+' << endl;
- //cout << "Semnul - are codul: " << (int)'-' << endl;
- float solutie = rezolva( ecuatie );
- if( raspuns_altfel != "normal" )
- cout << raspuns_altfel << endl;
- else
- cout << solutie << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement