C ++编程问题评估表达式

时间:2015-09-27 00:23:59

标签: c++

我正在尝试编写一个程序,该程序反复提示用户输入3个系数值a,b和c,然后使用下面的表达式计算并显示x的两个可能值。 X = 2C±√2a-35 / B-5C

如果用户输入-999,程序应该终止。此外,必须在开头清除屏幕,如果没有输入-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;
}

这一切都搞砸了很多次改变它,我为这个东西做了一些新的道歉。

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

    #logo {
        background: no-repeat url("http://s.ytimg.com/yts/imgbin/www-hitchhiker-vflNAOpbO.webp") -167px -205px;
        width: 72px;
        height: 30px;
        background-size: auto;
    }

答案 2 :(得分:-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;
}