⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xt4-2.cpp

📁 最新版谭浩强《c++语言程序设计》所有习题的原代码
💻 CPP
字号:
#include <iostream>
#include <math.h>
using namespace std;
float x1,x2,disc,p,q;
int main()
{void greater_than_zero(float,float);
 void equal_to_zero(float,float); 
 void smaller_than_zero(float,float); 
 float a,b,c;
 cout<<"input a,b,c:";
 cin>>a>>b>>c;
 disc=b*b-4*a*c;
 cout<<"root:"<<endl;
 if (disc>0)
  {
   greater_than_zero(a,b);
   cout<<"x1="<<x1<<",x2="<<x2<<endl;
  }
 else if (disc==0)
  {equal_to_zero(a,b);
   cout<<"x1="<<x1<<",x2="<<x2<<endl;
  }
 else
  {smaller_than_zero(a,b);
   cout<<"x1="<<p<<"+"<<q<<"i"<<endl;
   cout<<"x2="<<p<<"-"<<q<<"i"<<endl;
  }
 return 0;
}

void greater_than_zero(float a,float b)   /* 定义一个函数,用来求disc>0时方程的根 */
 {x1=(-b+sqrt(disc))/(2*a);
  x2=(-b-sqrt(disc))/(2*a);
 }

void equal_to_zero(float a,float b)     /* 定义一个函数,用来求disc=0时方程的根 */
 {
  x1=x2=(-b)/(2*a);
 }

void smaller_than_zero(float a,float b)  /* 定义一个函数,用来求disc<0时方程的根 */
 {
  p=-b/(2*a);
  q=sqrt(-disc)/(2*a);
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -