1、输出一个语句
#include<iostream>
using namespace std;
int main()
{
cout<<"hello world!";
return 0;
}
2、求一个表达式的值
#include<iostream>
using namespace std;
int main()
{
int a,b,result;
cout<<"please input two numbers:\n";
cin>>a>>b;
result=3*a+4*b-9;
cout<<"result is:"<<result<<endl;
return 0;
}
3、简单函数调用
#include<iostream>
#include<math.h>
using namespace std;
double max(double x,double y);
int main()
{
double a,b,c;
cout<<"please input two numbers:\n";
cin>>a>>b;
c=max(a,b);
cout<<"the squart of maximum(最大值开方)="<<sqrt(c);
return 0;
}
double max(double x,double y)
{
if(x>y)
return x;
else
return y;
}
4、省略函数声明的情况(3的改进版)
#include<iostream>
#include<math.h>
using namespace std;
double max(double x,double y)
{
if(x>y)
return x;
else
return y;
}
int main()
{
double a,b,c;
cout<<"please input two numbers:\n";
cin>>a>>b;
c=max(a,b);
cout<<"the squart of maximum(最大值开方)="<<sqrt(c);
return 0;
}