C语言程序设计基础 实验3 函数
 
一、实验目的
1. 理解函数的本质:模块化,实现代码复用
2. 掌握函数定义、声明、调用的语法
3. 理解并掌握函数的形参、实参,以及函数调用和返回的过程
4. 综合应用分支、循环和函数编程解决常见实际问题
 
实验一
(1)
#include <stdio.h>
long long fac(int n);
int main() {
int i, n;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) printf("%d! = %lld\n", i, fac(i));
return 0;
} long long fac(int n) {
static long long p = 1;
p = p * n;
return p;
}

感悟:这个代码非常的细节,考虑到了同学们可能会输入较大的n从而导致对应的阶乘超出int的范围。已知long long的范围下最大的阶乘表示为26

在line22之后插入打印

#include <stdio.h>
long long fac(int n);
int main() {
int i, n;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) printf("%d! = %lld\n", i, fac(i));
return 0;
} long long fac(int n) {
static long long p = 1;
printf("p = %lld\n", p);
p = p * n;
return p;
}

 (2)

#include <stdio.h>
int func(int, int);
int main() {
int k = 4, m = 1, p1, p2;
p1 = func(k, m);
p2 = func(k, m);
printf("%d,%d\n", p1, p2);
return 0;
}
int func(int a, int b) {
static int m = 0, i = 2;
i += m + 1;
m = i + a + b;
return (m);
}

实验二

#include <stdio.h>
void printSymbol(int n, char symbol);
int main() {
int n;
char symbol;
while( scanf("%d %c", &n, &symbol) != EOF ) {
printSymbol(n, symbol);
printf("\n");
}
return 0;
} void printSymbol(int n, char symbol){
if(!n) return ;
printf("%c",symbol);
printSymbol(n-1,symbol);
}

递归版

#include <stdio.h>
void printSymbol(int n, char symbol);
int main() {
int n;
char symbol;
while( scanf("%d %c", &n, &symbol) != EOF ) {
for(int i=1;i<=n;++i) printf("%c",symbol);
printf("\n");
}
return 0;
} void printSymbol(int n, char symbol){
if(!n) return ;
printf("%c",symbol);
printSymbol(n-1,symbol);
}

我认为递归好一点,因为代码短

 
实验三
#include <stdio.h>
long long fun(int n);
int main() {
int n; long long f;
while (scanf("%d", &n) != EOF) {
f = fun(n);
printf("n = %d, f = %lld\n", n, f);
}
return 0;
} long long fun(int n){
if(n==1) return 1ll;
return 2ll*fun(n-1)+1;
}
 
 
实验四
#include <stdio.h>
#define true 0
#define false 1
#define bool int
bool is_prime(int x) {
for(int i=2; i<=x/i; ++i)
if(x%i==0)
return true;
return false;
} int main() {
int cnt = 0;
for(int i=101; i<=200; ++i) {
if(!is_prime(i)) {
cnt ++;
printf("%d ",i);
if(cnt%20 == 0) puts("");
}
}
puts("");
printf("101~200之间一共有%d个非素数\n",cnt);
return 0;
}
 

实验五
#include <stdio.h>
long fun(long s);
int main() {
long s, t;
printf("Enter a number: ");
while (scanf("%ld", &s) != EOF) {
t = fun(s);
printf("new number is: %ld\n\n", t);
printf("Enter a number: ");
}
return 0;
} long fun(long s){
long base = 1,ans=0;
while(s){
long num = s%10;
if(num&1){
ans += base*num;
base*=10;
}
s/=10;
}
return ans;
}
 

实验六
#include <stdio.h>
double fun(int n); // 函数声明
int main() {
int n;
double s;
printf("Enter n(1~10): ");
while (scanf("%d", &n) != EOF) {
s = fun(n); // 函数调用
printf("n = %d, s= %f\n\n", n, s);
printf("Enter n(1~10): ");
}
return 0;
}
double fun(int n){
if(n==1) return 1;
double ans = 1.0;
for(int i=1;i<=n;++i) ans*=i;
if(n&1) return fun(n-1) + 1.0/ans;
else return fun(n-1) - 1.0/ans;
}
 

 
 
 

C语言程序设计基础 实验3 函数的更多相关文章

  1. C语言程序设计基础知识点概括

    C语言程序设计基础知识点概括 C语言程序设计基础知识点1.函数是C语言的基本构成单位.main函数是C语言程序的唯一入口.2.C语言程序开发过程. 编译过程:将以.c或.cpp结尾的源程序文件经过编译 ...

  2. C语言程序设计基础

    C语言程序设计基础 目录 C语言 C语言基础 C语言编程注意 C语言 C语言基础 C语言编程注意 0<9<9和0<9&&9<9是不同的 数组的声明和定义 con ...

  3. C语言程序设计基础-第1周作业-初步

    1.安装带有计算机术语的翻译软件 2.在自己电脑上安装C编译器,windows系统建议安装dev-c++,其他系统自行查找. 3.加入课程小组,有任何疑问可以在小组中提问:https://group. ...

  4. 《C语言程序设计基础1》第二学期第一周学习总结

    **<C语言程序设计基础1>第二学期第一周学习总结 一. 本周学习内容总结 一维数组,了解了一维数组的定义(定义一个数组,需要明确数组变量名,数组元素的类型和数组大小,即数组中元素的数量) ...

  5. C语言程序真正的启动函数

    为什么要用”真正”这个词?因为我们从学C语言开始,都会先明白这个道理,即C语言有且仅有一个main函数,main函数是C语言的入口点和出口点!(可以参考>http://www.dotcpp.co ...

  6. Python语言程序设计基础(5)—— 函数和代码复用

    lambda sum = lambda x,y : x + y print(sum(3,3),type(sum)) 默认参数 def prints(str,times = 2) : print(str ...

  7. 嵌入式LinuxC语言程序设计基础教程

    第1章 嵌入式LinxuC语言开发工具 第2章 数据 第3章 数据的输入输出 第4章 运算符和表达式 第5章 程序结构和控制语句 第6章 数组 第7章 指针 第8章 函数 第9章 用户自定义数据类型 ...

  8. 清华大学《C++语言程序设计基础》线上课程笔记03---数据的共享和保护&数组

    数据的共享和保护 对象的生存期 static类型的局部变量,生存期在整个程序,局部可见. void example() { static a=1; int b=2 } 当调用完example函数后,b ...

  9. 清华大学《C++语言程序设计基础》线上课程笔记04---指针

    指针 static int i; static int* ptr = &i; 此处的*表示ptr是指针类型(地址类型),用来存放目标数据的地址 其本身也有地址,所以又指向指针的指针; *前面的 ...

  10. 清华大学《C++语言程序设计基础》线上课程笔记02---类与对象

    类与对象 public是类的对外访问接口: 类内初始值 在定义类时对数据成员写初始值,在创建对象的时候,会使用类内初始值初始化数据成员: class Clock { public: void show ...

随机推荐

  1. Access to the path 'C:\Windows\TEMP\XXX.tmp' is denied.

    System.UnauthorizedAccessException: Access to the path 'C:\Windows\TEMP\ASPNETCORE_935a19f1-814f-4b3 ...

  2. Java DelayQueue包装类

    public class DelayQueueWrapper<T> { private TimeUnit timeUnit; private final Long capacity; pr ...

  3. using Spire.Pdf 合并文件夹下.pdf 文件

    using Spire.Pdf private void mergePDF() { List<string> filesList = new List<string>(); D ...

  4. STM32中HAL库和标准库的区别

    转载自:https://www.lmonkey.com/t/RwykY8bBX STM32标准库与HAL库比较 ST为开发者提供了非常方便的开发库.到目前为止,有标准外设库(STD库).HAL库.LL ...

  5. 【C学习笔记】day2-3 求10 个整数中最大值

    #include <stdio.h>#define n 10 int main() { int max=0; int a[n] = {12,15,16,546,165,654,612,23 ...

  6. 公共的common.scss (覆盖部分element组件主题色)

    公共的 common.scss (包含主题色 覆盖部分element组件主题色) $theme: #D50000; $litterTheme: #ec6059; .text-theme { color ...

  7. 058_Component Bundles

  8. NX二次开发,对象上色

    #include <uf_defs.h> #include <uf_ui_types.h> #include <uf.h> #include <uf_ui.h ...

  9. mongodb地理位置坐标加了索引,操作时报错 Location object expected, location array not in correct format

    别犹豫了,将坐标中的数据改为数字类型即可,如: location:[113.45,34,191]

  10. GAN的两种训练方式,以及梯度求导问题——detch(),retain_graph

    http://t.zoukankan.com/LXP-Never-p-13951578.html detach():截断node反向传播的梯度流,将某个node变成不需要梯度的Varibale,因此当 ...