函数程序举例(初学者)
2019-01-30 22:07
176 查看
例1、实现pow函数并尝试。
验证头文件:#include <math.h>
pow() 函数用来求 x 的 y 次幂(次方),x、y及函数值都是double型 ,其原型为:
double pow(double x, double y);
pow()用来计算以x 为底的 y 次方值,然后将结果返回。
直接调用库函数:
include <stdafx.h> #include<stdio.h> #include<math.h> void main() { double x=2.0,y=3.0,z; z = pow (x,y); printf("%lf to the power of %lf is %lf\n",x,y,z); }
自己定义函数:
#include <stdafx.h> #include<stdio.h> void main() { double pow(double x,double y); double x=2.0,y=3.0,z; z = pow (x,y); printf("%lf to the power of %lf is %lf\n",x,y,z); } double pow(double x,double y) { double z=1; for(;y>0;y--) { z*=x; } return z; }
注:新增变量最好赋初值,否则系统会随机给它一个值。
例2:猜想sqrt()函数的原理并尝试编程。(暂时只要求整型数据)
#include <stdafx.h> #include<stdio.h> void main() { int sqrt(int x); int x=49,z; z = sqrt (x); if(x<0) printf("Error:sqrt returns %d\n",x); else printf("%d\n",z); } int sqrt(int x) { int temp=1; while(1) { if(temp*temp==x) return temp; else ++temp; } }
相关文章推荐
- 程序举例(初学者)
- 【郝斌数据结构自学笔记】49-52_递归的定义和不同函数之间相互调程序举例_一个函数自己调自己程序举例
- Python中zip()函数用法举例
- 编写程序,实现strcpy()相同功能myStrcpy()函数
- 第11周项目1函数版星号图(1填充程序输出星号图)
- winform程序实现ListBox 双击选中项执行函数
- 深入了解汇编的第一个简单程序(对初学者很重要)
- 程序入口函数和glibc及C++全局构造和析构
- toupper函数及一些小程序
- 初学者写程序
- 函数可以作为另一个函数的参数(举例)
- Ruby编写的取得Pyton所有函数并导出到XML文档的程序
- VS平台中的控制台程序所支持的数据类型以及printf函数
- C 程序的外部变量与函数
- 您可以在您的asp程序中使用如下函数来进行在线发信
- H面试程序(1)编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的 下一秒
- 在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”声明?
- 简单LinuxC程序关于简单计算器(函数指针与回调函数)
- 怎样在TI stellaris lm3sxxx 程序中使用C语言printf()函数进行串口输出
- [C++] 用Xcode来写C++程序[5] 函数的重载与模板