f(x)=x^2+5
#include <iostream>
using namespace std;
int main()
{
int f(int);
cout<<f(5)<<endl;
system("pause");
return 0;
}
int f(int x)
{
int ans;
ans=x*x+5;
return ans;
}
-----------------------------------------------------------------------------------------------------
//a^b
int power(int a,int b)
{
int i,s=1;
for(i=1;i<=b;i++)
s*=a;
return s;
}
int f(int x)
{
int ans;
//int power(int,int); //宣告函數 可省略
ans=power(x,2)+5;
return ans;
}
int main()
{
// int f(int); //宣告函數 可省略
cout<<f(5)<<endl;
cout<<f(3)<<endl;
system("pause");
return 0;
}
------------------------------------------------------------------------------------
BMI計算
#include <iostream>
using namespace std;
int main()
{
double BMI(double,double);
double h,w;
cout<<"Input h,w:";
cin>>h>>w;
cout<<BMI(h,w)<<endl;
system("pause");
return 0;
}
double BMI(double h,double w)
{
h/=100;
return w/(h*h);
}
----------------------------------------------------------------------------------------------
標準體重
#include <iostream>
using namespace std;
double stdweight(double h,bool sex)
{
double w;
if(sex)
w=(h-80)*0.7;
else
w=(h-70)*0.6;
return w;
}
int main()
{
double h;
bool sex;
cout<<"Input h:";
cin>>h;
cout<<"Input sex(1,0):";
cin>>sex;
cout<<"標準體重"<<stdweight(h,sex)<<endl;
system("pause");
return 0;
}
-------------------------------------------------------------------------------------
圓面積
#include <iostream>
using namespace std;
#include <math.h>
double circle(int r)
{
return M_PI*pow(r,2);
}
int main()
{
int a;
cout<<"半徑:";
cin>>a;
cout<<circle(a)<<endl;
system("pause");
return 0;
}
----------------------------------------------------------------------------------
梯形面積
#include <iostream>
using namespace std;
#include <math.h>
double trape(int up,int base,int h)
{
double area;
area=(up+base)*h/2.0;
return area;
}
int main()
{
int up,base,h;
cout<<"Input up,base,h:";
cin>>up>>base>>h;
cout<<trape(up,base,h)<<endl;
system("pause");
return 0;
}
------------------------------------------------------------------------------------------------------------------
計算1+2+3+....+n
存檔到標頭檔
mytool.h
int sum(int n)
{
int i,s=0;
for(i=1;i<=n;i++)
s+=i;
return s;
}
#include <iostream>
using namespace std;
#include "mytool.h"
int main()
{
cout<<sum(100)<<endl;
system("pause");
return 0;
}
----------------------------------------------------------------------------
isTriangle判斷是不是三角形
TriangleArea計算面積
myprogram.h
#include <math.h>
bool isTriangle(int a,int b,int c)
{
if(a+b>c && a+c>b && b+c>a)
return 1;
else
return 0;
}
double TriangleArea(int a,int b,int c)
{
double s,Area;
s=(a+b+c)/2.0;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
return Area;
}
#include <iostream>
using namespace std;
#include "myprogram.h"
int main()
{
int a,b,c;
cin>>a>>b>>c;
if(isTriangle(a,b,c)==1)
cout<<TriangleArea(a,b,c)<<endl;
else
cout<<"不是三角形"<<endl;
system("pause");
return 0;
}