C語言程序設計-筆記04-函數
例5-1 計算圓柱體的體積。輸入圓柱的高和半徑,求圓柱體積volume=πxr^2xh。要求定義和調用函數cylinder(r,h)計算圓柱體的體積。
#include<stdio.h>
double cylinder(double r,double h);
int main(void)
{
double height,radius,volume;
printf("Enter radius and height:");
scanf("%lf%lf",&radius,&height);
volume=cylinder(radius,height);
printf("volume=%.3f\n",volume);
return 0;
}
double cylinder(double r,double h)
{
double result;
result=3.1415926*r*r*h;
return result;
}
|
|
例5-2 計算五邊形的面積。將一個五邊形分割成3個三角形,輸入這些三角形的7條邊長,計算該五邊形的面積。要求定義和調用函數area(x,y,z)計算邊長為x,y,z的三角形面積。
|
|||||||
|
|||||||
|
|||||||
#include<stdio.h>
#include<math.h>
int main(void)
{
double a1,a2,a3,a4,a5,a6,a7,s;
double area(double x,double y,double z);
printf("Please input 7 side lengths in the order a1 to a7:\n");
scanf("%lf%lf%lf%lf%lf%lf%lf",&a1,&a2,&a3,&a4,&a5,&a6,&a7);
s=area(a1,a5,a6)+area(a6,a7,a4)+area(a7,a2,a3);
printf("The area of the pentagon is %.2f\n",s);
return 0;
}
double area(double x,double y,double z)
{
double p=(x+y+z)/2;
return sqrt(p*(p-x)*(p-y)*(p-z));//海倫-秦九韶公式
}
例5-3 使用函數判斷完全平方數。定義一個判斷完全平方數的函數IsSquare(n),當n為完全平方數時返回1,否則返回0,不允許調用數學庫函數。
#include<stdio.h>
int IsSquare(n)
{
int i;
for(i=1;n>0;i=i+2)
{
n=n-i;
}
if(n==0)
{
return 1;
}
else
{
return 0;
}
}
int main(void)
{
int n;
printf("Enter n:");
scanf("%d",&n);
if(IsSquare(n)==1)
{
printf("%d is a square number.\n",n);
}
else
{
printf("%d is not a square number.\n",n);
}
}
例5-4 使用函數求最大公約數。定義函數gcd(int m,int n),計算m和n的最大公約數。
#include<stdio.h>
int gcd(int m,int n)
{
int r,temp;
if(m<n)
{
temp=m;
m=n;
n=temp;
}
r=m%n;
while(r!=0)
{
m=n;
n=r;
r=m%n;
}
return n;
}
int main(void)
{
int m,n;
printf("Enter m,n:");
scanf("%d%d",&m,&n);
printf("%d\n",gcd(m,n));
return 0;
}
例5-5 使用函數判斷素數。定義函數prime(m)判斷m是否為素數,當m為素數時返回1,否則返回0.
#include<stdio.h>
#include<math.h>
int prime(int m)
{
int i,limit;
if(m<=1)
{
return 0;
}
else if(m==2)
{
return 1;
}
else
{
limit=sqrt(m)+1;
for(i=2;i<=limit;i++)
{
if(m%i==0)
{
return 0;
}
}
return 1;
}
}
int main(void)
{
int n;
printf("Enter n:");
scanf("%d",&n);
if(prime(n)==1)
{
printf("%d is prime.\n",n);
}
else
{
printf("%d is not prime.\n",n);
}
return 0;
}
例5-6 數字金字塔。輸入一個正整數n,輸出n行數字金字塔。
#include<stdio.h>
int main(void)
{
int n;
void pyramid(int n);
printf("Enter n:");
scanf("%d",&n);
pyramid(n);
return 0;
}
void pyramid(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)//輸出每行數字前的空格
{
printf(" ");
}
for(j=1;j<=i;j++)//輸出每行的數字和數字后的空格
{
printf("%d",i);
printf(" ");
}
putchar('\n');
}
}
例5-7 計算2個復數之和與之積。分別輸入2個復數的實部與虛部,用函數實現計算2個復數之和與之積。
#include<stdio.h>
double result_real,result_imag; //全局變量
int main(void)
{
double imag1,imag2,real1,real2;
void complex_prod(double real1,double real2,double imag1,double imag2);
void complex_add(double real1,double real2,double imag1,double imag2);
printf("Enter 1st complex number(real and imaginary):");
scanf("%lf%lf",&real1,&imag1);
printf("Enter 2nd complex number(real and imaginary):");
scanf("%lf%lf",&real2,&imag2);
complex_add(real1,imag1,real2,imag2);
printf("addition of complex is %f+%fi\n",result_real,result_imag);
complex_prod(real1,imag1,real2,imag2);
printf("product of complex is %f+%fi\n",result_real,result_imag);
return 0;
}
void complex_add(double real1,double imag1,double real2,double imag2)
{
result_real=real1+real2;
result_imag=imag1+imag2;
}
void complex_prod(double real1,double imag1,double real2,double imag2)
{
result_real=real1*real2-imag1*imag2;
result_imag=real1*imag2+real2*imag1;
}
例5-8 用函數實現財務現金記賬。先輸入操作類型(1 收入,2 支出,0 結束),再輸入操作金額,計算現金剩余金額,經多吃操作直到輸入操作類型為0時結束。要求定義並調用函數,其中現金收入與現金支出分別用不同函數實現。
#include<stdio.h>
double cash;
int main(void)
{
int choice;
double value;
void income(double number),expend(double number);
cash=0;
printf("Enter operate choice(0--end,1--income,2--expend):");
scanf("%d",&choice);
while(choice!=0)
{
if(choice==1||choice==2)
{
printf("Enter cash value:");
scanf("%lf",&value);
if(choice==1)
{
income(value);
}
else
{
expend(value);
}
printf("current cash %.2f\n",cash);
}
printf("Enter operate choice(0--end,1--income,2--expend):");
scanf("%d",&choice);
}
return 0;
}
void income(double number)
{
cash=cash+number;
}
void expend(double number)
{
cash=cash-number;
}
例5-9 輸入正整數n,輸出1!-n!的值。要求定義並調用含靜態變量的函數fact_s(n)計算n!.
#include<stdio.h>
double fact_s(int n);
int main(void)
{
int i,n;
printf("Input n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%3d!=%.0f\n",i,fact_s(i));
}
return 0;
}
double fact_s(int n)
{
static double f=1;
f=f*n;
return (f);
}
參考資料
C語言程序設計/何欽銘,顏暉主編.---4版.---北京:高等教育出版社,2020.9