Advertisement
a53

e_c_2_i

a53
Dec 28th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring> /// strlen,strcpy
  3. #include <cmath> /// sqrt
  4. #include <cstdlib> /// atoi
  5. using namespace std;
  6.  
  7. /// determinam coeficientul (de tip int) din sirul initial (de tip char*)
  8. /// verificare=1 se foloseste la a si b, unde sirInitial poate contine doar '+' (sau nu contine nimic) pentru 1 si '-' pentru -1
  9. int determinare(char sirInitial[],int verificare=1)
  10. {
  11. int lungime=strlen(sirInitial);
  12. if(verificare)
  13. {
  14. if(lungime==0)
  15. return 1;
  16. if(lungime==1&&sirInitial[0]=='-')
  17. return -1;
  18. }
  19. if(lungime==1&&sirInitial[0]=='+')
  20. return 1;
  21. return atoi(sirInitial);
  22. }
  23. /// -x-x^2
  24. void separare(char ecuatie[],int &a,int &b,int &c) /// atribuim variabilelor a,b,c, valorile aferente
  25. {
  26. int lungime=strlen(ecuatie),lungimeCoeficient,pozitie=0;
  27. char coeficient[10],coeficienti[3][10]; /// coeficienti[0] -> a, coeficient[1] -> b, coeficienti[2] -> c
  28. while(pozitie<lungime)
  29. {
  30. lungimeCoeficient=0;
  31. while(!isalpha(ecuatie[pozitie])&&pozitie<lungime)
  32. {
  33. if(ecuatie[pozitie+1]=='+'||ecuatie[pozitie+1]=='-'||pozitie==lungime-1)
  34. {
  35. coeficient[lungimeCoeficient++]=ecuatie[pozitie++],--pozitie;
  36. break;
  37. }
  38. coeficient[lungimeCoeficient++]=ecuatie[pozitie++];
  39. }
  40. coeficient[lungimeCoeficient]='\0';
  41. ///cout<<endl<<"Coef= "<<coeficient<<" POZ = "<<pozitie<<" URM CAR = "<<ecuatie[pozitie]<<endl;
  42. if(ecuatie[pozitie+1]=='^'&&ecuatie[pozitie+2]=='2') /// este a
  43. {
  44. strcpy(coeficienti[0],coeficient);
  45. pozitie +=2;
  46. }
  47. else
  48. if(isalpha(ecuatie[pozitie])) /// este b
  49. {
  50. strcpy(coeficienti[1],coeficient);
  51. ///++pozitie;
  52. }
  53. else /// este c
  54. if(ecuatie[pozitie+1]=='-'||ecuatie[pozitie+1]=='+'||pozitie<lungime)
  55. {
  56. strcpy(coeficienti[2],coeficient);
  57. }
  58. ++pozitie;
  59. }
  60. ///cout<<coeficienti[0]<<' '<<coeficienti[1]<<' '<<coeficienti[2]<<endl;
  61. a=determinare(coeficienti[0]);
  62. b=determinare(coeficienti[1]);
  63. c=determinare(coeficienti[2],0);
  64. ///cout<<a<<' '<<b<<' '<<c<<endl;
  65. }
  66.  
  67. int main(void)
  68. {
  69. char ecuatie[100];
  70. int a=0,b=0,c=0;
  71. cin.get(ecuatie,100);
  72. separare(ecuatie,a,b,c);
  73. double x1,x2; /// Solutiile ecuatiei
  74. int delta=b*b-4*a*c;
  75. if(delta<0 ) /// Solutii imaginare
  76. cout<<"imaginare\n"<<(double)-b/(2*a)<<' '<<(double)sqrt(-delta)/(2*a)<<'i'; /// ecuatia are solutii imaginare
  77. else
  78. if(delta==0) /// Ecuatia are o singura solutie
  79. cout<<"real\n"<<(double)-b/(2*a);
  80. else /// Ecuatia are doua solutii -> o afisam pe cea mai mica, urmata de cea mai mare
  81. {
  82. double radDelta=sqrt(delta);
  83. x1=(-b+radDelta)/(2*a);
  84. x2=(-b-radDelta)/(2*a);
  85. if(x1>x2)
  86. swap(x1,x2);
  87. cout<<"reale\n"<<x1<<' '<<x2;
  88. }
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement