fork(9) download
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. long long int t,a,b,c,flagb,flagc;
  6. cin>>t;
  7. string s;
  8. while(t--)
  9. {
  10. a=b=c=flagb=flagc=0;
  11. cin>>s;
  12. if(s[0]=='x')
  13. {
  14. a=1;
  15. flagb=3;
  16. if(s[flagb]=='=')
  17. {
  18. b=c=0;
  19. goto solve;
  20. }
  21. }
  22. else
  23. {
  24. a=(long long int)(s[0]-'0');
  25. flagb=5;
  26. int t1=1;
  27. while(s[t1]!='*')
  28. {
  29. flagb++;
  30. a=a*10+(long long int)(s[t1]-'0');
  31. t1++;
  32. }
  33. if(s[flagb]=='=')
  34. {
  35. b=c=0;
  36. goto solve;
  37. }
  38. }
  39. if(s[flagb]=='+')
  40. {
  41. if(s[flagb+1]=='x')
  42. {
  43. b=1;
  44. flagc=flagb+2;
  45. if(s[flagc]=='=')
  46. {
  47. c=0;
  48. goto solve;
  49. }
  50. }
  51. else
  52. {
  53. b=(long long int)(s[flagb+1]-'0');
  54. flagc=flagb+4;
  55. int t1=flagb+2;
  56. while(s[t1]!='*')
  57. {
  58. if(s[t1]=='=')
  59. {
  60. c=b;
  61. b=0;
  62. goto solve;
  63. }
  64. flagc++;
  65. b=b*10+(long long int)(s[t1]-'0');
  66. t1++;
  67. }
  68. if(s[flagc]=='=')
  69. {
  70. c=0;
  71. goto solve;
  72. }
  73. }
  74. }
  75. if(s[flagb]=='-')
  76. {
  77. if(s[flagb+1]=='x')
  78. {
  79. b=-1;
  80. flagc=flagb+2;
  81. if(s[flagc]=='=')
  82. {
  83. c=0;
  84. goto solve;
  85. }
  86. }
  87. else
  88. {
  89. b=(long long int)(s[flagb+1]-'0');
  90. flagc=flagb+4;
  91. int t1=flagb+2;
  92. while(s[t1]!='*')
  93. {
  94. if(s[t1]=='=')
  95. {
  96. c=-b;
  97. b=0;
  98. goto solve;
  99. }
  100. flagc++;
  101. b=b*10+(long long int)(s[t1]-'0');
  102. t1++;
  103. }
  104. b=-b;
  105. if(s[flagc]=='=')
  106. {
  107. c=0;
  108. goto solve;
  109. }
  110. }
  111. }
  112. if(s[flagc]=='+')
  113. {
  114. c=(long long int)(s[flagc+1]-'0');
  115. int t1=flagc+2;
  116. while(s[t1]!='=')
  117. {
  118. c=c*10+(long long int)(s[t1]-'0');
  119. t1++;
  120. }
  121. }
  122. else
  123. {
  124. c=(long long int)(s[flagc+1]-'0');
  125. int t1=flagc+2;
  126. while(s[t1]!='=')
  127. {
  128. c=c*10+(long long int)(s[t1]-'0');
  129. t1++;
  130. }
  131. c=-c;
  132. }
  133. solve:
  134. //cout<<a<<" "<<b<<" "<<c<<endl;
  135. if(b==0&&c==0)
  136. cout<<"Equal roots."<<endl;
  137. else if(b==0&&c>0)
  138. cout<<"Imaginary roots."<<endl;
  139. else
  140. {
  141. b*=b;
  142. b-=4*a*c;
  143. if(b<0)
  144. cout<<"Imaginary roots."<<endl;
  145. if(b>0)
  146. cout<<"Distinct real roots." <<endl;
  147. if(b==0)
  148. cout<<"Equal roots."<<endl;
  149. }
  150. }
  151. return 0;
  152. }
Success #stdin #stdout 0s 3036KB
stdin
2
x*x-2*x+1=0
2*x*x+5*x-3=0
stdout
Equal roots.
Distinct real roots.