| 5 | 1/1 | 返回列表 |
| 查看: 1124 | 回復(fù): 4 | ||||
| 本帖產(chǎn)生 1 個(gè) 博學(xué)EPI ,點(diǎn)擊這里進(jìn)行查看 | ||||
| 當(dāng)前只顯示滿足指定條件的回帖,點(diǎn)擊這里查看本話題的所有回帖 | ||||
[交流]
C++編程
|
||||
|
求解一元二次方程。 一元二次方程的定義為: ax2+bx+c=0 (1)如果b2 -4ac>0,方程有兩個(gè)不同的實(shí)根,分別是: (2)如果b2 -4ac< 0,方程沒有實(shí)根,但有虛根; (3)如果b2 -4ac= 0,方程有一個(gè)實(shí)根。 請(qǐng)你編寫一個(gè)程序,使其能求出多個(gè)二次方程的根。該程序要詢問用戶是否想繼續(xù)解下一個(gè)方程。用戶輸入1來繼續(xù),輸入其它數(shù)字,則終止程序。程序要求用戶輸入a,b和c,然后根據(jù)前面的條件計(jì)算,并輸出答案。 要求:使用類實(shí)現(xiàn), (1) a,b,c為該類的私有成員變量; (2) 求根的實(shí)現(xiàn)為該類的成員函數(shù),形式為: //函數(shù)返回值:實(shí)根的個(gè)數(shù); //參數(shù):x- 用以返回實(shí)根值的數(shù)組; long CalResult(float x[]); (3) 該類還包含有參構(gòu)造函數(shù)、析構(gòu)函數(shù)。 謝謝 請(qǐng)盡快 |
» 搶金幣啦!回帖就可以得到:
+1/489
+1/86
+1/46
+1/42
+1/41
+1/41
+1/37
+1/35
+1/35
+1/32
+1/28
+1/15
+1/7
+1/7
+1/6
+1/5
+1/4
+1/4
+1/3
+1/2
主管區(qū)長 (文壇精英)
![]() |
專家經(jīng)驗(yàn): +151 |
主管區(qū)長 (文壇精英)
![]() |
專家經(jīng)驗(yàn): +151 |
|
#include #include using namespace std; float x1,x2,disc,p,q; int main() {void greater_than_zero(float,float); void equal_to_zero(float,float); void smaller_than_zero(float,float); float a,b,c; cout<<"input a,b,c:"; cin>>a>>b>>c; disc=b*b-4*a*c; cout<<"root:"< { greater_than_zero(a,b); cout<<"x1="< else if (disc==0) {equal_to_zero(a,b); cout<<"x1="< else {smaller_than_zero(a,b); cout<<"x1="< cout<<"x2="< } return 0; } void greater_than_zero(float a,float b) /* 定義一個(gè)函數(shù),用來求disc>0時(shí)方程的根 */ {x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); } void equal_to_zero(float a,float b) /* 定義一個(gè)函數(shù),用來求disc=0時(shí)方程的根 */ { x1=x2=(-b)/(2*a); } void smaller_than_zero(float a,float b) /* 定義一個(gè)函數(shù),用來求disc<0時(shí)方程的根 */ { p=-b/(2*a); q=sqrt(-disc)/(2*a); } |
|
// 二次根.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "math.h" #include using namespace std; class Cercigeng { public: Cercigeng(float aa, float bb, float cc); ~Cercigeng(); //函數(shù)返回值:實(shí)根的個(gè)數(shù); //參數(shù):x- 用以返回實(shí)根值的數(shù)組; long CalResult(float x[]); private: float a; float b; float c; }; Cercigeng::Cercigeng(float aa, float bb, float cc) { a=aa; b=bb; c=cc; } Cercigeng::~Cercigeng() { } long Cercigeng::CalResult(float x[]) { float delta = 0; long geshu=0; delta = b*b-4.0*a*c; if (delta < 0) { geshu = 0; } else if(fabs(delta)<1e-10) { x[0] = (-b+sqrt(delta))/2.0/a; geshu = 1; } else { x[0] = (-b+sqrt(delta))/2.0/a; x[1] = (-b-sqrt(delta))/2.0/a; geshu = 2; } return geshu; } int main(int argc, char* argv[]) { cout<<"求解方程的根!\n"<<"請(qǐng)輸入a,b,c"; float a,b,c; cin>>a>>b>>c; Cercigeng fangcheng(a,b,c); float xx[1]; long GeShu = fangcheng.CalResult(xx); if (GeShu = 0) { cout<<"無實(shí)根!\n"; } else if(GeShu = 1) { cout<<"方程有一個(gè)實(shí)根為"< else { cout<<"方程有二個(gè)實(shí)根,分別為"< cout<<"如果繼續(xù),請(qǐng)輸入1!否則輸入其它數(shù)字\n"; int jixu=0; cin>>jixu; for (;(jixu-1)!=0 ![]() { cout<<"求解方程的根!\n"<<"請(qǐng)輸入a,b,c"; cin>>a>>b>>c; Cercigeng fangcheng(a,b,c); GeShu = fangcheng.CalResult(xx); if (GeShu = 0) { cout<<"無實(shí)根!\n"; } else if(GeShu = 1) { cout<<"方程有一個(gè)實(shí)根為"< else { cout<<"方程有二個(gè)實(shí)根,分別為"< } return 0; } 試試吧,應(yīng)該沒有問題的,我寫了一下,但是沒有編譯運(yùn)行,好運(yùn)! |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 材料與化工306分找調(diào)劑 +3 | 滄海輕舟e 2026-04-02 | 3/150 |
|
|---|---|---|---|---|
|
[考研] 283分材料與化工求調(diào)劑 +9 | 羅KAKA 2026-04-02 | 9/450 |
|
|
[考研] 265求調(diào)劑 +5 | 梁梁校校 2026-04-01 | 5/250 |
|
|
[考研] 一志愿北京科技大學(xué)085601材料工程英一數(shù)二初試總分335求調(diào)劑 +6 | 雙馬尾痞老板2 2026-04-02 | 7/350 |
|
|
[考研] 材料化工340求調(diào)劑 +5 | jhx777 2026-03-30 | 5/250 |
|
|
[考研] 321求調(diào)劑 +9 | y-yh 2026-04-01 | 10/500 |
|
|
[考研] 286求調(diào)劑 +5 | lim0922 2026-03-26 | 5/250 |
|
|
[考研] 350求調(diào)劑 +7 | 阿佳~ 2026-03-31 | 7/350 |
|
|
[論文投稿]
chinese chemical letters英文版投稿求助
120+4
|
Yishengeryi 2026-03-30 | 5/250 |
|
|
[考研] 291求調(diào)劑 +3 | 迷蒙木木 2026-04-01 | 4/200 |
|
|
[考研] 263求調(diào)劑 +3 | DDDDuu 2026-03-27 | 3/150 |
|
|
[考研] 一志愿中海洋材料357 +4 | 麥恩莉. 2026-03-30 | 4/200 |
|
|
[考研] 085600材料與化工調(diào)劑 +16 | kikiki7 2026-03-30 | 16/800 |
|
|
[考研] 抱歉 +4 | 田洪有 2026-03-30 | 4/200 |
|
|
[考研] 求調(diào)劑 +7 | 青春裁為三截 2026-03-29 | 7/350 |
|
|
[考研] 環(huán)境科學(xué)與工程334分求調(diào)劑 +6 | 王一一依依 2026-03-30 | 8/400 |
|
|
[考研] 343求調(diào)劑 +6 | 愛羈絆 2026-03-29 | 6/300 |
|
|
[考研] 081200-11408-276學(xué)碩求調(diào)劑 +6 | 崔wj 2026-03-26 | 6/300 |
|
|
[考研] 一志愿南京航空航天大學(xué)材料學(xué)碩求調(diào)劑 +3 | @taotao 2026-03-28 | 3/150 |
|
|
[考研] 081200-11408-276學(xué)碩求調(diào)劑 +4 | 崔wj 2026-03-26 | 4/200 |
|