注册 登录
编程论坛 C++教室

一元二次方程的问题

洪夜馨 发布于 2009-07-17 14:22, 1704 次点击
通过一元二次方程如何求出它的根呢?
我做出的是:
#include "math.h"
double main()
{
    do
    {
    double a,b,c,d,x,x1,x2;
    cout<<"请输入一元二次方程的a、b、c"<<endl;
    cin>>a>>b>>c;
    d=b*b-4*a*c;
    if(d<0)
    {
        cout<<"此方程无解"<<endl;
    }
    }while(d>=0)
    {
        if(d==0)
        {
            x=b/2/a;
            cout<<"x1=x2=-"<<x<<endl;
        }
        else
        {
            x1=b/2/a+sqrt(b*b-4*a*c)/2/a;
            x2=b/2/a-sqrt(b*b-4*a*c)/2/a;
            cout<<"x1=-"<<x1<<";"<<"x2=-"<<x2<<endl;
        }
    }
    return 0;
}
可下面显示错误的好多
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
F:\教育教学\练习\1.cpp(7) : error C2065: 'cout' : undeclared identifier
F:\教育教学\练习\1.cpp(7) : error C2297: '<<' : illegal, right operand has type 'char [28]'
F:\教育教学\练习\1.cpp(7) : error C2065: 'endl' : undeclared identifier
F:\教育教学\练习\1.cpp(8) : error C2065: 'cin' : undeclared identifier
F:\教育教学\练习\1.cpp(8) : error C2296: '>>' : illegal, left operand has type 'double'
F:\教育教学\练习\1.cpp(8) : error C2297: '>>' : illegal, right operand has type 'double'
F:\教育教学\练习\1.cpp(12) : error C2297: '<<' : illegal, right operand has type 'char [11]'
F:\教育教学\练习\1.cpp(14) : error C2065: 'd' : undeclared identifier
F:\教育教学\练习\1.cpp(15) : error C2143: syntax error : missing ')' before '{'
F:\教育教学\练习\1.cpp(15) : error C2143: syntax error : missing ';' before ')'
F:\教育教学\练习\1.cpp(15) : error C2143: syntax error : missing ';' before ')'
F:\教育教学\练习\1.cpp(15) : error C2143: syntax error : missing ';' before '{'
F:\教育教学\练习\1.cpp(18) : error C2065: 'x' : undeclared identifier
F:\教育教学\练习\1.cpp(18) : error C2065: 'b' : undeclared identifier
F:\教育教学\练习\1.cpp(18) : error C2065: 'a' : undeclared identifier
F:\教育教学\练习\1.cpp(19) : error C2297: '<<' : illegal, right operand has type 'char [8]'
F:\教育教学\练习\1.cpp(23) : error C2065: 'x1' : undeclared identifier
F:\教育教学\练习\1.cpp(23) : error C2065: 'c' : undeclared identifier
F:\教育教学\练习\1.cpp(23) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
F:\教育教学\练习\1.cpp(24) : error C2065: 'x2' : undeclared identifier
F:\教育教学\练习\1.cpp(24) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
F:\教育教学\练习\1.cpp(25) : error C2297: '<<' : illegal, right operand has type 'char [5]'
执行 cl.exe 时出错.

1.obj - 1 error(s), 0 warning(s)
到底错在哪些地方??
另外这问题可以用到函数的递归这个现象吗?该怎么用?我不清楚啊
能给我一些提示吗 谢谢大家啊
12 回复
#2
wuyun85362009-07-17 15:33
/*总体有三个错误。
1。缺少using namespace std
2.局部变量只在声明的{}里起作用。出了{}就不存在了
3。do wihie() 的使用,在while()后应该有分号*/
#3
lintaoyn2009-07-17 16:19
还有缺少头文件<iostream>
主函数的返回值为什么不弄成int?
#4
洪夜馨2009-07-17 16:37
谢谢LS2位,可我按你们说的弄成了这样:
#include "iostream"
#include "math.h"
using namespace std;
double main()
{
    double a,b,c,d,x,x1,x2;
    do
    {
    cout<<"请输入一元二次方程的a、b、c"<<endl;
    cin>>a>>b>>c;
    d=b*b-4*a*c;
    if(d<0)
    {
        cout<<"此方程无解"<<endl;
    }
    }while(d>=0);
    {
        if(d==0)
        {
            x=b/2/a;
            cout<<"x1=x2=-"<<x<<endl;
        }
        else
        {
            x1=b/2/a+sqrt(b*b-4*a*c)/2/a;
            x2=b/2/a-sqrt(b*b-4*a*c)/2/a;
            cout<<"x1=-"<<x1<<";"<<"x2=-"<<x2<<endl;
        }
    }
    return 0;
}
编译成功了,可是无论我输入什么1 2 1,还是1 4 4这种的都反复叫我重新输入.怎么办?是不是我还有什么地方没按你们改呢?
#5
jackie19182009-07-17 16:50
你的while语句错了你的意思正好反了,while(d>=0)恰恰是当d>=0的时候运行循环了,改为while(d<0)
#6
jackie19182009-07-17 16:54
小问题:不是你自定义的头文件最好用<>,而不是""
标准写
#include <iostream>
#include <math>
using namespace std;
#7
洪夜馨2009-07-17 17:09
额,楼上的jackie1918,您叫我写成#include <math>这样的话在编译的时候还是成为fatal error C1083: Cannot open include file: 'math': No such file or directory了。
#8
jackie19182009-07-17 17:26
#include <iostream>
#include <math.h>
using namespace std;
这样才对,呵呵,刚错了~~
#9
洪夜馨2009-07-17 17:28
哦,谢谢。太谢谢各位了。
#10
洪夜馨2009-07-17 17:31
3楼的lintaoyn,抱歉啊。我本来想给你分数的。可为什么却没有可以给我给你分的空格?
#11
pangding2009-07-18 12:12
回复 7楼 洪夜馨
你可以用 cmath 这个头文件,这个是有名空间概念的。
#12
shaozihaozi2009-07-20 15:19
我改了一下
#include<iostream>
#include "math.h"
using namespace std;
void main()
{
  
    double a,b,c,d,x,x1,x2;
    cout<<"请输入一元二次方程的a、b、c"<<endl;
    cin>>a>>b>>c;
    d=b*b-4*a*c;
    if(d<0)
    {
        cout<<"此方程无解"<<endl;
    }
    else
    {
        if(d==0)
        {
            x=b/2/a;
            cout<<"x1=x2="<<x<<endl;
        }
        else
        {
            x1=b/2/a+sqrt(b*b-4*a*c)/2/a;
            x2=b/2/a-sqrt(b*b-4*a*c)/2/a;
            cout<<"x1=-"<<x1<<";"<<"x2=-"<<x2<<endl;
        }
    }
   
}
#13
zhqer2009-07-20 17:18
要统一用std就include<cmath>
1