永发信息网

C++程序的问题

答案:6  悬赏:30  手机版
解决时间 2021-07-30 01:35
  • 提问者网友:欺烟
  • 2021-07-29 06:59

#include<iostream>
using namespace std;
void main()
{
double a,b,c,m,n;
char i;
cout<<"请输入a=";
cin>>a;
cout<<"请输入b=";
cin>>b;
cout<<"请输入c=";
cin>>c;
if(a=0)
cout<<"x="<<-c/b<<endl;
else
if(b*b-4*a*c==0)
cout<<"x1=x2="<<-b/2*a<<endl;
else
if(b*b-4*a*c>0)
{
m=(-b+sqrt(b*b-4*a*c))/2*a;
cout<<"x1="<<m<<endl;
n=(-b-sqrt(b*b-4*a*c))/2*a;
cout<<"x2="<<n<<endl;
}
else
{
cout<<"x1="<<-b/(2*a)<<"+"<<sqrt(b*b-4*a*c)/(2*a)<<i<<endl;
cout<<"x2="<<-b/(2*a)<<"-"<<sqrt(b*b-4*a*c)/(2*a)<<i<<endl;
}



} 错在哪

最佳答案
  • 五星知识达人网友:我住北渡口
  • 2021-07-29 07:28
#include<iostream>
using namespace std;
void main()
{
double a,b,c,m,n;
char i;
cout<<"请输入a=";
cin>>a;
cout<<"请输入b=";
cin>>b;
cout<<"请输入c=";
cin>>c;
if(a==0)
cout<<"x="<<-c/b<<endl;
else
if(b*b-4*a*c==0)
cout<<"x1=x2="<<-b/2*a<<endl;
else
if(b*b-4*a*c>0)
{
m=(-b+sqrt(b*b-4*a*c))/2*a;
cout<<"x1="<<m<<endl;
n=(-b-sqrt(b*b-4*a*c))/2*a;
cout<<"x2="<<n<<endl;
}
else
{
cout<<"x1="<<-b/(2*a)<<"+"<<sqrt(-(b*b-4*a*c))/(2*a)<<"i"<<endl;
cout<<"x2="<<-b/(2*a)<<"-"<<sqrt(-(b*b-4*a*c))/(2*a)<<"i"<<endl;
}
}
全部回答
  • 1楼网友:低音帝王
  • 2021-07-29 11:27

等号写成了赋值号

  • 2楼网友:鸠书
  • 2021-07-29 11:21

我在VC下面给你修改了,经测试程序能运行,并能得出正确结果,用到开平方函数一定要有#include<math.h>头文件,其次在最后的输出复数i时,前面的char i;理解有误,这只是定义了一个字符变量,里面没有任何字符,你想输出复数里的i字符,则把'i'赋予它,即char i='i';

#include<iostream> #include<math.h> using namespace std; void main() { double a,b,c,m,n; char i='i'; cout<<"请输入a="; cin>>a; cout<<"请输入b="; cin>>b; cout<<"请输入c="; cin>>c; if(a==0) cout<<"x="<<-c/b<<endl; else if(b*b-4*a*c==0) cout<<"x1=x2="<<-b/(2*a)<<endl; else if(b*b-4*a*c>0) { m=(-b+sqrt(b*b-4*a*c))/(2*a); cout<<"x1="<<m<<endl; n=(-b-sqrt(b*b-4*a*c))/(2*a); cout<<"x2="<<n<<endl; } else { cout<<"x1="<<-b/(2*a)<<"+"<<sqrt(-(b*b-4*a*c))/(2*a)<<i<<endl; cout<<"x2="<<-b/(2*a)<<"-"<<sqrt(-(b*b-4*a*c))/(2*a)<<i<<endl; }

  • 3楼网友:千杯敬自由
  • 2021-07-29 10:21
根据我得理解,这应该是个解一次和二次方程的程序,a,b,c为方程参数。以下是改好了的程序:#include<iostream>#include<cmath>using namespace std;void main(){ double a,b,c,m,n; cout<<"请输入a="; cin>>a; cout<<"请输入b="; cin>>b; cout<<"请输入c="; cin>>c; if(a==0) cout<<"x="<<-c/b<<endl; else if(b*b-4*a*c==0) cout<<"x1=x2="<<-b/2*a<<endl; else if(b*b-4*a*c>0) { m=(-b+sqrt(b*b-4*a*c))/2*a; cout<<"x1="<<m<<endl; n=(-b-sqrt(b*b-4*a*c))/2*a; cout<<"x2="<<n<<endl; } else { cout<<"x1="<<-b/(2*a)<<"+"<<sqrt(4*a*c-b*b)/(2*a)<<'i'<<endl; cout<<"x2="<<-b/(2*a)<<"-"<<sqrt(4*a*c-b*b)/(2*a)<<'i'<<endl; }} 错误如下:1.没有#include<cmath>,因为用sqrt这类数学函数,都要#include<cmath>;2.第一个if 函数条件内:a=0,应该为a==0,相信这是楼主笔误,要仔细呀;3.对于输出虚数i,不必用char i,直接在cout<<'i'就可以了;4.当解为虚数时,不要输出sqrt(b*b-4*a*c)/(2*a),改为输出sqrt(4*a*c-b*b)/(2*a);
  • 4楼网友:孤老序
  • 2021-07-29 09:13

错误太多了,看加粗的部分吧:

#include<iostream> #include<math.h> using namespace std; int main() { double a,b,c,m,n; char i = 'i'; cout<<"请输入a="; cin>>a; cout<<"请输入b="; cin>>b; cout<<"请输入c="; cin>>c; if(a==0) cout<<"x="<<-c/b<<endl; else if(b*b-4*a*c==0) cout<<"x1=x2="<<-b/2*a<<endl; else if(b*b-4*a*c>0) { m=(-b+sqrt(b*b-4*a*c))/2*a; cout<<"x1="<<m<<endl; n=(-b-sqrt(b*b-4*a*c))/2*a; cout<<"x2="<<n<<endl; } else { cout<<"x1="<<-b/(2*a)<<"+"<<sqrt(-(b*b-4*a*c))/(2*a)<<i<<endl; cout<<"x2="<<-b/(2*a)<<"-"<<sqrt(-(b*b-4*a*c))/(2*a)<<i<<endl; } }

  • 5楼网友:酒醒三更
  • 2021-07-29 07:42
#include<iostream> #include<cmath> //sqrt() using namespace std; void main() { double a,b,c,m,n; char i; cout<<"请输入a="; cin>>a; cout<<"请输入b="; cin>>b; cout<<"请输入c="; cin>>c; if(a==0) //判断相等用== 赋值才用= cout<<"x="<<-c/b<<endl; else if(b*b-4*a*c==0) cout<<"x1=x2="<<-b/2*a<<endl; else if(b*b-4*a*c>0) { m=(-b+sqrt(b*b-4*a*c))/2*a; cout<<"x1="<<m<<endl; n=(-b-sqrt(b*b-4*a*c))/2*a; cout<<"x2="<<n<<endl; } else { cout<<"x1="<<-b/(2*a)<<"+"<<sqrt(b*b-4*a*c)/(2*a)<<i<<endl; cout<<"x2="<<-b/(2*a)<<"-"<<sqrt(b*b-4*a*c)/(2*a)<<i<<endl; } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯