Advertisement
Guest User

27 ian

a guest
Jan 27th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. string raspuns_altfel = "normal";
  9.  
  10. float rezolva( string ec )
  11. {
  12. bool gasit_egal = false, gasit_minus = false;
  13. int termen_liber_st = 0, termen_liber_dr = 0;
  14. int coef_st = 0, coef_dr = 0, s = 0;
  15. int i, j, n = ec.size();
  16. cout << "Ecuatia introdusa: " << ec << endl << endl;
  17. int A, B;
  18. i = 0;
  19. while( i < n )
  20. {
  21. //cout << "ec[" << i << "] = " << ec[i] << endl;
  22. //cin.get();
  23. if( ec[ i ] == '=' )
  24. {
  25. gasit_egal = true;
  26. ++i;
  27. continue;
  28. }
  29. if( ec[i] == '-' )
  30. gasit_minus = true;
  31.  
  32.  
  33. if( ec[i] >= '0' && ec[i] <= '9' )
  34. {
  35. s = ec[i] - '0';
  36. //cout << " Plec cu S = " << s << endl;
  37. ++i;
  38.  
  39. /// Partea asta de while creeaza probleme: ar trebui sa prelucreze doar cifrele
  40. /// de extinderea mai departe trebuie sa se ocupe while-ul mare.
  41. while( i < n )
  42. {
  43. if( ec[i] >= '0' && ec[i] <= '9' )
  44. s = s * 10 + (ec[i] - '0');
  45. if( ec[i] == 'x' ) /// Atentie la ceva de tipul: 2x=3
  46. {
  47. coef_st += s;
  48. ++i; /// Trec la urmatorul caracter care poate fi: +, -, = sau sfarsit
  49. break; /// i indica pe x
  50. }
  51. if( ec[i] == '=' )
  52. {
  53. termen_liber_st += s;
  54. gasit_egal = true;
  55. cout << endl << "DREAPTA" << endl << endl;
  56. break;
  57. }
  58. if( ec[i] == '+' || ec[i] == '-' )
  59. {
  60. if(gasit_egal == false )
  61. termen_liber_st += s;
  62. else
  63. termen_liber_dr += s;
  64. break;
  65. }
  66.  
  67. ++i;
  68. }
  69. }
  70. cout << "Pana acum, numarul format: " << s << endl;
  71. cin.get();
  72. if(gasit_minus == true )
  73. {
  74. s = -s;
  75. gasit_minus == true;
  76. }
  77. if( gasit_egal == false ) /// tin cont de ultimul numar (coef ori termen liber) de dinainte de egal
  78. {
  79. coef_st += s;
  80. termen_liber_st += s;
  81. }
  82. else
  83. coef_dr += s;
  84.  
  85. cout << "Termen liber st: " << termen_liber_st << endl;
  86. cout << "Termen liber dr: " << termen_liber_dr << endl;
  87. cout << "Coef st: " << coef_st << endl;
  88. cout << "Coef dr: " << coef_dr << endl;
  89. ++i;
  90. } /// se inchide while-ul mare
  91.  
  92.  
  93. if( ec[ n - 1 ] != 'x' ) /// Daca e termen liber
  94. termen_liber_dr += s;
  95.  
  96. A = coef_st - coef_dr;
  97. B = termen_liber_dr - termen_liber_st;
  98.  
  99. cout << "Ecuatia restransa este:" << endl;
  100. cout << A << " * x = " << B << endl;
  101.  
  102.  
  103. if( A == 0 && B != 0 )
  104. raspuns_altfel = "imposibil";
  105. if( A == 0 && B == 0 )
  106. raspuns_altfel = "infinit";
  107. if( raspuns_altfel == "normal" )
  108. return (float) B / A;
  109. else
  110. return -999999;
  111. }
  112.  
  113.  
  114.  
  115.  
  116. int main()
  117. {
  118. string ecuatie;
  119. cin >> ecuatie;
  120. //cout << "Semnul = are codul: " << (int)'=' << endl;
  121. //cout << "Semnul + are codul: " << (int)'+' << endl;
  122. //cout << "Semnul - are codul: " << (int)'-' << endl;
  123. float solutie = rezolva( ecuatie );
  124.  
  125. if( raspuns_altfel != "normal" )
  126. cout << raspuns_altfel << endl;
  127. else
  128. cout << solutie << endl;
  129.  
  130. return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement