/* * Copyright (c) 2014,烟台大学计算机学院 * All right reserved. * 作者:邵帅 * 文件:temp.cpp * 版本号:v1.0 */ #include<iostream> #include<math.h> using namespace std; int main() { cout << "请输入a,b,c值:" << endl; cout << "例如:x²+3x-2=0" << endl; cout << "a=1 b=3 c=2" << endl; cout << "--------------------------" << endl; cout << "请输入:" << endl; float a, b, c, d, x1, x2; /* 定义变量 */ cin >> a >> b >> c; /* 输入一元二次方程的三个参数 */ if (a != 0) { d = b * b - 4 * a * c; // 判断德它 if (d > 0) { x1 = (-b + sqrt(d)) / 2 * a; x2 = (-b - sqrt(d)) / 2 * a; cout << "方程根为:" << "x1=" << x1 << " x2=" << x2; } else if (d == 0) { x1 = -b / (2 * b); cout << "方程根为:x1=x2=" << x1; } else { cout << "无实根!"; } } else if (a == 0) { cout << "非一元二次方程!"; } return 0; }