易截截图软件、单文件、免安装、纯绿色、仅160KB

编程实现一元二次方程的解 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


相关文档:

C编译基础

int main(int argc,char *argv[])
argc(argument count):参数的个数;
argv(argument value):参数值
argv[0] :文件名
argv[1]:第一个参数,argv[2];第二个参数,以此类推。
编译C
1 单源程序到可执行程序
            编译     & ......

使用C/C++运行时库函数操作线程

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 ......

编译出来的c/c++程式的参数压栈顺序只和编译器相关!

下面列举了一些常见的编译器的调用约定
VC6:
调用约定        堆栈清除    参数传递
__cdecl         调用者      从右到左,通过堆栈传递
__stdcall       函数 ......

简述C和C++程序员学习历程

哈哈!有幸在某网站发现这篇文章,读罢,觉得蛮有道理,发来大家一起共勉之   
总是被同学们问到,如何学习C和C++才不茫然,才不是乱学,想了一下,这里给出一个总的回复。
  一家之言,欢迎拍砖哈。
  1、可以考虑先学习C.
  大多数时候,我们学习语言的目的,不是为了成为一个语言专家,而是希望 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号