2015-09-27 126 views
-4

我試圖寫一個程序,反覆提示用戶3倍的係數值a,b和c,然後將評價使用下面的表達式和對於x顯示兩個可能的值編程問題。 X = 2C±√2a-35/B-5CC++計算表達式

如果用戶輸入-999一個程序應該終止。此外,必須在開始時清除屏幕,並且如果未爲a輸入-999,則在分開的線路上提示用戶輸入b和c。

此外,如果輸入是像被零除或負的sqrt無效必須顯示一個錯誤消息。這是我到目前爲止,我已經改變了它百萬次,以便讓它編譯和不斷收到錯誤。

#include <iostream.h>              
#include <cmath> 
#include <conio.h> 

using namespace std; 

int main (void) 
{ 
clrscr(); 

float a, b, c, x1, x2, discriminant, divisor; 

//Getting values for a,b,c 
cout<<"Enter the value for A (-999 to quit): "<<endl; 
cin >> a; 

if(a ==-999)return 0; 

cout<<"Enter the value for B: "<<endl; 
cin>>b; 

cout<<"Enter the value for C: "<<endl; 
cin>>c; 

// Evaluating 
discriminant=(2*a-35); 
divisor=(b-5*c) 
if (discriminant >0); 
{ 
    x1=(2*c+sqrt(discriminant))/(divisor); 
    x2=(2*c-sqrt(discriminant))/(divisor); 
} 
else (discriminant ==0) 
{ 
    x1=x2=(2*c)/(divisor); 
} 
else if (sqrt(discriminant <= -1); 
cout<<"Invalid Value"<<endl; 
{ 
else if (divisor ==0) 
cout<<"Invalid Value"<<endl; 
} 
else 
{ 
cout<<"x1= "<<"("<<(2*c)<<"+ i"<<sqrt(discriminant)<<")/"<<divisor<<endl; 
cout<<"x2= "<<"("<<(2*c)<<"+ i"<<sqrt(discriminant)<<")/"<<divisor<<endl; 
} 
     system ("pause"); 

     return 0; 
} 

這一切從改變很多次了搞砸了,我這個只是有點新來這個東西道歉。

+0

所以按預期這是行不通的,但它有什麼作用呢?它編譯失敗嗎?它會崩潰嗎?它會返回錯誤的值嗎? – Quentin

+0

問題是.....? –

+0

此行小艾破,只是說...'否則,如果(開方(判別<= -1);'你可能要一個')',而不是';'有 –

回答

0

該代碼編譯,但結果似乎不正確。

//#include <iostream.h>              
#include <iostream> // delete .h 
#include <cmath> 
//#include <conio.h> // Wandbox gcc 4.8.2 doesn't seem supporting this 

using namespace std; 

int main (void) 
{ 
//clrscr(); // Wandbox gcc 4.8.2 doesn't seem supporting this 

float a, b, c, x1, x2, discriminant, divisor; 

//Getting values for a,b,c 
cout<<"Enter the value for A (-999 to quit): "<<endl; 
cin >> a; 

if(a ==-999)return 0; 

cout<<"Enter the value for B: "<<endl; 
cin>>b; 

cout<<"Enter the value for C: "<<endl; 
cin>>c; 

// Evaluating 
discriminant=(2*a-35); 
//divisor=(b-5*c) 
divisor=(b-5*c); // add semicolon here 
//if (discriminant >0); 
if (discriminant >0) // delete semicolon here 
{ 
    x1=(2*c+sqrt(discriminant))/(divisor); 
    x2=(2*c-sqrt(discriminant))/(divisor); 
} 
//else (discriminant ==0) 
else if (discriminant ==0) // add if 
{ 
    x1=x2=(2*c)/(divisor); 
} 
//else if (sqrt(discriminant <= -1); 
else if (discriminant <= -1) // delete sqrt(and semicolon 
cout<<"Invalid Value"<<endl; 
//{ // delete this 
else if (divisor ==0) 
{ // add this 
cout<<"Invalid Value"<<endl; 
} 
else 
{ 
cout<<"x1= "<<"("<<(2*c)<<"+ i"<<sqrt(discriminant)<<")/"<<divisor<<endl; 
cout<<"x2= "<<"("<<(2*c)<<"+ i"<<sqrt(discriminant)<<")/"<<divisor<<endl; 
} 
     //system ("pause"); // include cstdlib or delete this 

     return 0; 
} 
+0

我給你建議的修改,我仍然得到錯誤 38:代碼在主函數)沒有影響( 41:別人放錯了位置在函數main() 43:X1和X2分配有從未在函數main() 44中使用的值:聲明終止incorreclty 51:聲明終止錯誤 52:意外} – Perc

+0

此代碼[不編譯](HTTP: //melpon.org/wandbox/permlink/ZaQ8gj9472CNQ2sr)帶有一個警告。請再次檢查您的代碼。 – MikeCAT

-1
int main (void) 
{ 
    clrscr(); 

    float a, b, c, x1, x2, discriminant, divisor; 

    //Getting values for a,b,c 
    cout<<"Enter the value for A (-999 to quit): "<<endl; 
    cin >> a; 

    if(a ==-999)return 0; 

    cout<<"Enter the value for B: "<<endl; 
    cin>>b; 

    cout<<"Enter the value for C: "<<endl; 
    cin>>c; 

    // Evaluating 
    discriminant=(2*a-35); 
    divisor=(b-5*c) 
    if (divisor ==0) 
    { 
     cout<<"Invalid Value"<<endl; 
     return 0; 
    } 
    if (discriminant >0); 
    { 
     x1=(2*c+sqrt(discriminant))/(divisor); 
     x2=(2*c-sqrt(discriminant))/(divisor); 
    } 
    else if (discriminant ==0) 
    { 
     x1=x2=(2*c)/(divisor); 
    } 
    else 
    { 
     cout<<"Invalid Value"<<endl; 
     return 0; 
    } 

    cout<<"x1= "<< x1 <<endl; 
    cout<<"x2= "<< x2 <<endl; 

    return 0; 
} 
+0

我終於通過改變你的建議並修改了我的else語句並添加了一段時間來編譯沒有錯誤。謝謝你的幫助。 – Perc

0
#include <iostream>              
#include <cmath> 

using namespace std; 

int main (void) 
{ 
//clrscr(); 
char exit_char; 
//Why not use double 
double a, b, c, x1, x2, discriminant, divisor; 
//float a, b, c, x1, x2, discriminant, divisor; 

//Getting values for a,b,c 
cout<<"Enter the value for A (-999 to quit): "<<endl; 
cin >> a; 

if(a ==-999) 
    return 0; 

cout<<"Enter the value for B: "<<endl; 
cin>>b; 

cout<<"Enter the value for C: "<<endl; 
cin>>c; 

// Evaluating 
discriminant=(2*a-35); 
divisor=(b-5*c); //Do you want b-(5*c) or (b-5)*c 
/*if (discriminant >0); 
{ 
    x1=(2*c+sqrt(discriminant))/(divisor); 
    x2=(2*c-sqrt(discriminant))/(divisor); 
} 
//This is supposed to be an else if block 
//else (discriminant ==0) 
else 
    if(discriminant == 0) 
{ 
    x1=x2=(2*c)/(divisor); 
} 
else if (sqrt(discriminant <= -1); 
cout<<"Invalid Value"<<endl; 
{ */ 
// 
if(discriminant > 0) 
{ 
    x1=(2*c+sqrt(discriminant))/(divisor); 
    x2=(2*c-sqrt(discriminant))/(divisor); 
} 
else 
{ 
    if(discriminant == 0) 
    { 
     x1=x2=(2*c)/(divisor); 
    } 
    else 
    { 
     if(sqrt(discriminant) <= -1); 
      cout<<"Invalid Value"<<endl; 
    } 
} 

// 
if (divisor == 0) 
    cout<<"Invalid Value"<<endl; 

else 
{ 

    //cout<<"x1= "<<"("<<(2*c)<<"+ i"<<sqrt(discriminant)<<")/"<<divisor<<endl; 
    //cout<<"x2= "<<"("<<(2*c)<<"+ i"<<sqrt(discriminant)<<")/"<<divisor<<endl; 
    cout<<"x1 = "<<x1<<endl; 
    cout<<"x2 = "<<x2<<endl; 
} 
     //system ("pause"); 
    cout<<"Enter any char to quit the program"<<endl; 
    cin>>exit_char; 

    return 0; 
}`enter code here` 
+0

我得到了上面的代碼工作,但我不是特別確定程序邏輯。我不確切知道你打算在計劃的幾個點上做什麼。您似乎也不確定需要分號的位置。 – Jay