roberthsu2003 / cAndC-

48 stars 18 forks source link

x = [-b±√(b2-4ac)]/(2a) #2

Closed RainVaporeon closed 10 months ago

RainVaporeon commented 4 years ago
#include <iostream>
#include <cmath>
using namespace std;

int main() {
  int x,a,b,c;
  int type;
  float result;
  float result2;
  float result3;
  cout << "Type: ax^2+by+c";
    cout << endl << "Input a: ";
  cin >> a;
    cout << endl << "Input b: ";
  cin >> b;
    cout << endl << "Input c: ";
  cin >> c;
  cout << "Calculation: " << "x = (-b+/-sqrt(pow(b, 2)-(4a*c))/2*a";
  if((pow(b, 2) - 4 * a * c) > 0) {
      type=2;
  }
  if((pow(b, 2) - 4 * a * c) == 0) {
      type=1;
  }
  if((pow(b, 2) - 4 * a * c) < 0) {
      type=0;
  }
  result=(-b+sqrt(pow(b, 2)-(4*a*c))/2*a);
  result2=(-b-sqrt(pow(b, 2)-(4*a*c))/2*a);
  result3=-b/2*a;
  if(type == 2) {
    cout << "\n There are two results: " << result << " and " << result2;
    return 0;
  }
  if(type == 1) {
    cout << "\n There is only one result: " << result3;
    return 0;
  }
  if(type == 0) {
    cout << "\n There are no results.";
    return 1;
  }
}
tsai80517 commented 4 years ago

https://repl.it/@tsai80517/cc12021

tsai80517 commented 4 years ago

@RainTheVappy result, result2 小括號括錯位置了, 要改為 (-b+sqrt( pow(b, 2)-(4 a c))) / 2*a;

RainVaporeon commented 4 years ago

@RainTheVappy result, result2 小括號括錯位置了, 要改為 (-b+sqrt( pow(b, 2)-(4 a c))) / 2*a;

有注意到,但是懶得回去改了 不過你有注意到這點也很細心了

RainVaporeon commented 4 years ago

https://repl.it/@tsai80517/cc12021 for(;;) 應該可以變成 while(true)?

tsai80517 commented 4 years ago

https://repl.it/@tsai80517/cc12021 for(;;) 應該可以變成 while(true)?

可以哦, 如果懶得打英文也可以用while(1)表示 哈哈