编程实现一元二次方程的解 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
相关文档:
VB
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
MSComm1.CommPort = i1
MSComm1.PortOpen = True
MSComm1.InputMode = comInputModeBinary
MSComm1.InBufferCount = 0
& ......
我们已经了解如何定义线程入口点函数、调用系统API创建执行指定函数的线程。本节将揭示这一切在系统内部是如何完成的。
图6-1描述了线程创建并完成初始化后的状态。调用CreateThread会使系统产生一个线程内核对象,其引用计数(Usage count)被初始化为2(创建线程的进程和线程本身都引用了该内核对象),其它属性也完成了 ......
Visual Studio包含了4个本机C/C++运行时库和2个用来管理MS.NET的C/C++运行时库。所有这些库都支持多线程编程环境:目前已经没有专门为单线程开发设计的C/C++运行时库了。表6-1对这些库进行了描述:
Libray Name
Description
LibCMt.lib
Statically linked release version of the library.
Lib ......
下面列举了一些常见的编译器的调用约定
VC6:
调用约定 堆栈清除 参数传递
__cdecl 调用者 从右到左,通过堆栈传递
__stdcall 函数 ......