编程实现一元二次方程的解 ax^2+bx+c=0
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double a,b,c;
double delta;
double x1,x2;
cout<<"Please input a,b,c:"<<endl;
cin>>a>>b>>c;
if(cin.fail())
{
cout<<"Error:bad input!";
return 1;
}
delta=b*b-4*a*c;
if(delta>0)
{
cout<<"The Equation has two roots:"<<endl;
x1=(-b+sqrt(delta))/(2*a);
x2=(-b-sqrt(delta))/(2*a);
cout<<"x1="<<x1<<"\n"
<<"x2="<<x2<<"\n";
}
if(delta==0)
{
cout<<"The Equation has two equal roots:"<<endl;
x1=(-b+sqrt(delta))/(2*a);
cout<<"x1=x2="<<x1<<endl;
}
if(delta<0)
{
cout<<"The Equation has two virtual roots:"<<endl;
cout<<"x1="<<(-b)/(2*a)<<"+"<<sqrt(-delta)/(2*a)<<"i"<<endl;
cout<<"x2="<<(-b)/(2*a)<<"-"<<sqrt(-delta)/(2*a)<<"i"<<endl;
}
system("pause");
return 0;
}
实现算法:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int sgn(int m)
{
if(m>=0)
return 1;
else return -1;
}
int _tmain(int argc, _TCHAR* argv[])
{
double a,b,c;
double delta;
double q;
double x1,x2;
cout<<"Please input a,b,c:"<<endl;
cin>>a>>b>>c;
delta=b*b-4*a*c;
if(delta>0)
{
cout<<"The equation has two roots:"<<endl;
q=-(b+sgn(b)*sqrt(delta))/2.0;
x1=q/a;
x2=c/q;
cout<<fixed;
cout<<"x1="<<x1<<"\n"
<<"x2="<<x2<<endl;
}
if(delta==0)
{
cout<<"The equation has two equal roots:"<<endl;
q=-(b+sgn(b)*sqrt(delta))/2.0;
x1=q/a;
cout<<"x1=x2="<<x1<<endl;
}
if(delta<0)
{
cout<<"Error!"<<endl;
return 1;
}
system("pause");
retur
相关文档:
1.1.1 格式化输入输出函数
Turbo C2.0 标准库提供了两个控制台格式化输入、 输出函数printf( ) 和
scanf(), 这两个函数可以在标准输入输出设备上以各种不同的格式读写数据。
printf()函数用来向标准输出设备(屏幕)写数据; scanf() 函数用来从标准输入
设备(键盘)上读数据。下面详细介绍这两个函数的用法。
一、pr ......
const是一个C语言的关键字,它限定一个变量不允许被改变。使用const在一定程度上可以提高程序的健壮性,另外,在观看别人代码的时候,清晰理解const所起的作用,对理解对方的程序也有一些帮助。
虽然这听起来很简单,但实际上,const的使用也是c语言中一个比较微妙的地方,微妙在何处呢?请看下面几个问题。
问题 ......
我们已经了解如何定义线程入口点函数、调用系统API创建执行指定函数的线程。本节将揭示这一切在系统内部是如何完成的。
图6-1描述了线程创建并完成初始化后的状态。调用CreateThread会使系统产生一个线程内核对象,其引用计数(Usage count)被初始化为2(创建线程的进程和线程本身都引用了该内核对象),其它属性也完成了 ......
def login():
print 'login'
def logout():
print 'logout'
controllers = {
'in': login,   ......
下面列举了一些常见的编译器的调用约定
VC6:
调用约定 堆栈清除 参数传递
__cdecl 调用者 从右到左,通过堆栈传递
__stdcall 函数 ......