Advertisement
Guest User

etapa a II-a

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