0

## 一元二次方程式公式解C++ oop

#include<math.h>
#include
using namespace std;

public:
Quadratic(double _a, double _b, double _c){
a=_a;
b=_b;
c=_c;
};
void solver();
//一元二次方程式公式解解出方程式
void print();
// print() print the answers in the form m+ni,
//where m is the real part and n is the imaginary part

``````private:
double a;
double b;
double c;
double real, imag;
``````

};

double ans;
int j;
double m,n;
ans=bb-4ac;
if(ans<0)
ans=abs(ans);
ans=pow(ans,0.5);
for(int i;i<b
b;i++){
if(ans==ii)
j=j+1;
}
if(j==0){
cout<<-b<<"_"<<ans/2
a<<endl;
}
else{
m=(-b+ans)/2a;
cout<<m<<"_0"<<endl;
n=(-b-ans)/2
a;
cout<<n<<"_0"<<endl;
}
}

}

//我不知道如何把solver的cout移動到print()裡

int main(){
double a, b, c;
cout<<"input a,b,c";
cin >> a >> b >> c;
formula.solver();
return 0;
}
//main檔不可以動

### 2 個回答

2

iT邦大神 1 級 ‧ 2019-03-20 20:49:06

``````#include <math.h>
#include <iostream>
using namespace std;

public:
Quadratic(double _a, double _b, double _c){
a=_a;
b=_b;
c=_c;
};
void solver();
//一元二次方程式公式解解出方程式
void print();
// print() print the answers in the form m+ni,
//where m is the real part and n is the imaginary part

private:
double a;
double b;
double c;
double real, imag;
int flag;
double real_1, imag_1;
double real_2, imag_2;
};

double ans;
int j;
double m,n;
ans=b*b-4*a*c;
if(ans<0)
ans=abs(ans);
ans=pow(ans,0.5);
for(int i;i<(b*b);i++){
if(ans==(i*i))
j=j+1;
}
flag = j;
if(j==0){
//std::cout<<-b<<"_"<<ans/(2*a)<<endl;
real_1 = -1 * b;
imag_1 = ans / (2*a);
}
else{
m=(-b+ans)/(2*a);
// cout<<m<<"_0"<<endl;
real_1 = m;
imag_1 = 0;
n=(-b-ans)/(2*a);
// cout<<n<<"_0"<<endl;
real_2 = n;
imag_2 = 0;
}
print();
}

if (flag==0) {
cout << real_1 << "+" << imag_1 << "i" << endl;
} else {
cout << real_1 << "+" << imag_1 << "i" << endl;
cout << real_2 << "+" << imag_2 << "i" << endl;
}
}

//我不知道如何把solver的cout移動到print()裡

int main(){
double a, b, c;
cout<<"input a,b,c";
cin >> a >> b >> c;
formula.solver();
return 0;
}
//main檔不可以動
``````

sunnyyeh iT邦新手 5 級 ‧ 2019-03-21 23:23:35 檢舉

oop初學 有很多疑惑QQ

formula 是變數名稱,

2.買了一個商品，為了跟別人買的做區隔，取個名字(叫 formula)
3.下標時要告訴賣家三個數字，賣家把他設定進去(formula(a,b,c))
4.使用方法就是按下那個按鈕 solver，然後答案就會顯示在螢幕上

sunnyyeh iT邦新手 5 級 ‧ 2019-03-23 18:35:05 檢舉

sunnyyeh iT邦新手 5 級 ‧ 2019-03-23 18:35:36 檢舉

1

iT邦大師 1 級 ‧ 2019-03-20 18:46:13

print的時候直接將變數列印出來.

sunnyyeh iT邦新手 5 級 ‧ 2019-03-21 23:21:49 檢舉

sunnyyeh iT邦新手 5 級 ‧ 2019-03-21 23:22:15 檢舉