C语言程序设计-笔记04-函数
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
C语言程序设计-笔记04-函数的更多相关文章
- Go语言学习笔记(7)——函数和方法
Go语言中同时有函数和方法! 函数: go程序必须要包含一个main函数.main函数不能有任何参数和返回值! 1. 定义方法示例: func max(num1, num2 int) int { // ...
- 【Go语言学习笔记】函数做参数和闭包
函数做参数 在Go语言中,函数也是一种数据类型,我们可以通过type来定义它,它的类型就是所有拥有相同的参数,相同的返回值的一种类型.类似于重写(同名覆盖). 回调函数:函数有一个参数是函数类型,这个 ...
- Go语言学习笔记(五) [函数]
日期:2014年7月29日 1.函数定义:func (p type) funcname(q int) (r,s int) {return 0,0 } func: 保留字,用于定义一个函数 ...
- JavaScript语言精粹 笔记04 数组
数组1 数组字面量2 长度3 删除4 列举5 混淆的地方6 方法7 维度 数组1 数组字面量 var empty = []; var numbers = [ 'zero', 'one', 'two', ...
- JavaScript语言精粹 笔记02 函数
函数函数对象函数字面量调用参数返回异常给类型增加方法递归作用域闭包回调模块级联套用记忆 函数 1 函数对象 在JS中函数就是对象.对象是“名/值”对的集合并拥有一个连接到原型对象的隐藏连接.对象字 ...
- JAVA语言程序设计-笔记摘录
JAVA 程序语言设计(基础篇) 笔记摘录 为避免输入错误, 不要在nextByte().nextShort().nextInt()等等后面使用nextLine() nextXXXXX()都称为令牌读 ...
- C语言学习笔记--字符串函数
字符串函数 需要包含头文件#include<stdio.h> strlen strcmp strcpy strchr strstr strcasestr
- Mysql 数据库学习笔记04 函数
一.创建自定义函数 * 使用自定义函数,可以返回字符串.整型.实数或者其他类型: create [aggregate] function 名称 (参数列表) return type begin //函 ...
- C语言学习笔记之函数指针与函数指针数组
指针函数:本质是一个函数: 是一个返回指针类型的函数int * sum(){ } 函数指针:本质是一个指针: 是一个指向函数的指针 int (*p)(int,int) = sum; p(5,6); i ...
- Python语言程序设计(笔记)
1.平方根的格式化 知识点:平方根计算 pow(a,0.5)[可以计算负数,结果为复数] a**b 例题: 获得用户输入的一个整数a,计算a的平方根,保留小数点后3位,并打印输出. ...
随机推荐
- QT 智能指针 QPointer QScopedPointer QSharedPointer QWeakPointer QSharedDataPointer 隐式共享 显示共享
QPointer QPointer 使一种受保护的指针,当其引用的对象被销毁时,它会被自动清除(但是,销毁引用对象还是必须手动delete).QPointer所指向的对象必须是QObject或其派生类 ...
- Kafka之Producer网络传输
一.背景 在Kafka的组成部分(Broker.Consumer.Producer)中,设计理念迥异,每个部分都有自己独特的思考.而把这些部分有机地组织起来,使其成为一个整体的便是「网络传输」.区别于 ...
- 性能测试系列:Jmeter使用记录
jmeter配置环境变量vi /etc/profileexport PATH=$PATH:/tmp/jmeter/apache-jmeter-5.4.1/binsource /etc/profile ...
- vscode中Vetur插件关闭组件自动导入路径
vscode配置项中加入 "vetur.completion.autoImport": false, 或者,将图中4处勾去掉即可
- FPGA 原语之一位全加器
FPGA原语之一位全加器 1.实验原理 一位全加器,三个输入,两个输出.进位输出Cout=AB+BC+CA,本位输出S=A异或B异或C.实验中采用三个与门.一个三输入或门(另外一个是两个或门,功能一致 ...
- KingbaseES 复制冲突之锁类型冲突
背景 昨天遇到客户现场的一个有关复制冲突的问题 备库报错:ERROR: canceling statement due to conflict with recovery,user was holdi ...
- redis的延迟双删策略
1,redis数据为什么会存在和数据库数据不一致的问题 在多线程并发情况下,假设有两个数据库修改请求,为保证数据库与redis的数据一致性,修改请求的实现中需要修改数据库后,级联修改redis中的数据 ...
- 实用 Linux 命令 Windos 命令 实例演示 持续更新中
实用 Linux 命令 Windos 命令 实例演示 持续更新中 目录 实用 Linux 命令 Windos 命令 实例演示 持续更新中 Linux 命令 [Command [options] [lo ...
- #高精度,排列组合、dp#JZOJ 2755 树的计数
题目 求\(n\)个点直径为\(d\)的标号树个数(多组数据) (\(0\leq d\leq n\leq 50,n>0\)) 分析 首先特判一下\(n==d\)无解,\(d=0\)除非只有一个点 ...
- 掌握 C++ 中 static 关键字的多种使用场景
static是什么 在最开始C中引入了static关键字可以用于修饰变量和函数,后来由于C++引入了class的概念,现在static可以修饰的对象分为以下5种: 成员变量,成员函数,普通函数,局部变 ...