| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 572 人关注过本帖
标题:一个求根的程序,搞不定啊!
只看楼主 加入收藏
birdfling
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2006-4-1
收藏
 问题点数:0 回复次数:3 
一个求根的程序,搞不定啊!

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
float res(int,int,int);
float main()
{
int A,B,C;
cout<<"请输入A,B,C的值:"<<endl;
cin>>A>>B>>C;
float x,y;
if(A<0)
{
if(B*B-4*(-A)*(-C)>0)
{
x=(B+sqrt(B*B-4*(-A)*(-C)))/(2*A);
y=(B-sqrt(B*B-4*(-A)*(-C)))/(2*A);
cout<<"方程的两个根x,y为:"<<" x="<<setiosflags(ios::fixed)<<setprecision(3)<<x<<" y="<<setiosflags(ios::fixed)<<setprecision(3)<<y<<endl;
}
else if(B*B-4*(-A)*(-C)==0)
{
x=B/(2*A);
y=x;
cout<<"方程的两个根x,y为:"<<" x=y="<<setiosflags(ios::fixed)<<setprecision(3)<<y<<endl;
}
else cout<<"此方程无根!"<<endl;
}
else if(A=0)
{
x=-(C/B);
y=x;
}
else
{
if(B*B-4*A*C>0)
{
x=((-B)+sqrt(B*B-4*A*C))/(2*A);
y=((-B)-sqrt(B*B-4*A*C))/(2*A);
cout<<"方程的两个根x,y为:"<<" x="<<setiosflags(ios::fixed)<<setprecision(3)<<x<<" y="<<setiosflags(ios::fixed)<<setprecision(3)<<y<<endl;
}
else if(B*B-4*A*C==0)
{
x=B/(2*A);
y=x;
cout<<"方程的两个根x,y为:"<<" x=y="<<setiosflags(ios::fixed)<<setprecision(3)<<y<<endl;
}
else cout<<"此方程无根!"<<endl;
}
return 0;
}

编译没错,可是结果不对,不知道怎么改了啊。

搜索更多相关主题的帖子: include 
2006-04-06 13:48
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
我觉得你是不是不熟悉初中的方程啊? 这是不对的思维方法

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-04-06 14:59
skyfire
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-4-5
收藏
得分:0 

#include<iostream.h>
#include<math.h>
#include<iomanip.h>
void main()
{
float A,B,C;
cout<<"请输入A,B,C的值"<<endl;
cin>>A>>B>>C;
float x,y;
if(A!=0)
{
if(B*B-4*A*C>0)
{
x=((-B)+sqrt(B*B-4*A*C))/(2*A);
y=((-B)-sqrt(B*B-4*A*C))/(2*A);
cout<<"方程的两个根为:"<<" x="<<setiosflags(ios::fixed)<<setprecision(3)<<x<<" y="<<setiosflags(ios::fixed)<<setprecision(3)<<y<<endl;
}
else if(B*B-4*A*C==0)
{
x=(-B)/(2*A);
y=x;
cout<<"方程的两个根为:"<<" x=y="<<setiosflags(ios::fixed)<<setprecision(3)<<y<<endl;
}
else cout<<"此方程无根!"<<endl;
}
else if(A==0)
{
x=-(C/B);
y=x;
cout<<"x=y="<<x;
}
}



楼主看看我这个程序可不可以完成你的功能!


广结天下好友,共同努力进步!
2006-04-06 15:15
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

#include<iostream>
#include<cmath>
#include<iomanip>
#include<conio.h>
using namespace std;

int main()
{
double A,B,C;
cout<<"请输入A,B,C的值:"<<endl;
cin>>A>>B>>C;
double x,x1,x2;
char ch;

if(B*B-4*A*C>=0)
{
if(A!=0)
{
x1=(-B+sqrt(B*B-4*A*C))/(2*A);
x2=(-B-sqrt(B*B-4*A*C))/(2*A);
cout<<"方程的两个根x,y为:"<<" x1="<<fixed<<setprecision(3)<<x1<<" x2="<<fixed<<setprecision(3)<<x2<<endl;
}
else
{
x = -(C/B);cout<<"该方程为一元一次方程,有唯一解:"<<fixed<<setprecision(3)<<x<<endl;
}
}
else
{
cout<<"输入一个字符,如果是求虚数根,则输入!,否则任意输入:"<<endl;
cin>>ch;
if (ch != '!')
{cout<<"此方程无根!"<<endl;}
else // 求虚根
{
double P,T;
T = -(B*B-4*A*C);
P = 2 * A;
cout<<"x1 =(-"<<B<<"+"<<sqrt(T)<<"i)/"<<P<<endl;
cout<<"x2 =(-"<<B<<"-"<<sqrt(T)<<"i)/"<<P<<endl;
}
}
getch(); // 可在结果出现后暂停运行截面,按任意键退出,与上面的conio.h同时用 。
return 0;
}


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-04-06 15:32
快速回复:一个求根的程序,搞不定啊!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039930 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved