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