实验任务一:
task1.c
1 #include <stdio.h> 2 #include <time.h> 3 #include <windows.h> 4 #include <stdlib.h> 5 #define N 30 6 void print_spaces(int); 7 void print_blank_lines(int); 8 void print_text(int, int, char); 9 int main() { 10 int line, col; 11 int i; 12 char text[N] = "hello November"; 13 srand(time(0)); 14 for (i = 1; i <= 10; i++) { 15 line = rand() % 25; 16 col = rand() % 80; 17 print_text(line, col, text); 18 Sleep(1000); 19 } 20 return 0; 21 } 22 23 void print_spaces(int n) { 24 for (int i = 0; i < n; i++) { 25 printf(" "); 26 } 27 28 } 29 void print_blank_lines(int n) { 30 for (int i = 0; i < n; i++) 31 printf("\n"); 32 33 } 34 void print_text(int line, int col, char text[]) { 35 print_blank_lines(line - 1); 36 print_spaces(col - 1); 37 printf("%s", text); 38 }
文字描述:以当前系统时间作为随机种子,行为0~24的随机数,列为0~79的随机数,打印文档的函数里嵌套了打印空白行和打印空格的函数,将产生随机数的行和列和输入的文档作为实参传入函数,实现在某一随机区域打印文档的效果。
实验任务二:
task2_1.c
1 #include <stdio.h> 2 long long fac(int); 3 int main() { 4 int n; 5 scanf("%d", &n); 6 int i; 7 for (i = 1; i <= n; i++) { 8 printf("%3d的阶乘 = %lld\n", i, fac(i)); 9 } 10 return 0; 11 } 12 long long fac(int x) { 13 static long long p = 1; 14 printf("p = %d\n", p); 15 p *= x; 16 return p; 17 }
运行截图:
task2_2.c
1 #include <stdio.h> 2 int func(int, int); // 函数声明 3 int main() { 4 int k = 4, m = 1, p1, p2; 5 p1 = func(k, m); // 函数调用 6 p2 = func(k, m); // 函数调用 7 printf("%d, %d\n", p1, p2); 8 return 0; 9 } 10 // 函数定义 11 int func(int a, int b) { 12 static int m = 0, i = 2; 13 i += m + 1; 14 m = i + a + b; 15 return m; 16 }
程序运行截图:
static 变量特性:
static改变了变量的声明周期,这里变量在函数中,函数调用结束后,m和i并不会被释放,他们的值还保留着。等到整个程序运行结束才会释放变量。
实验任务三:
task3.c
1 #include <stdio.h> 2 long long func(int n); // 函数声明 3 int main() { 4 int n; 5 long long f; 6 while (scanf("%d", &n) != EOF) { 7 f = func(n); // 函数调用 8 printf("n = %d, f = %lld\n", n, f - 1); 9 } 10 return 0; 11 } 12 long long func(int n) { 13 if (n == 1) 14 return 2; 15 return func(n - 1) * 2; 16 }
运行截图:
实验任务四:
task4.c
1 #include <stdio.h> 2 int func(int n, int m); 3 int main() { 4 int n, m; 5 while (scanf("%d%d", &n, &m) != EOF) 6 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 7 return 0; 8 } 9 int func(int n, int m) { 10 if ((n < m)) { 11 return 0; 12 } 13 else if ((n == m) || (m == 0)) { 14 return 1; 15 } 16 else if (m == 1) { 17 return n; 18 } 19 return func(n - 1, m) + func(n - 1, m - 1); 20 }
程序运行截图:
实验任务五:
task5.c
1 #include <stdio.h> 2 int mul(int n, int m); 3 int main() { 4 int n, m; 5 while (scanf("%d%d", &n, &m) != EOF) 6 printf("%d * %d = %d\n", n, m, mul(n, m)); 7 8 return 0; 9 } 10 int mul(int n, int m) { 11 if (m == 1) { 12 return n; 13 } 14 else if ((m == 0) || (n == 0)) { 15 return 0; 16 } 17 return mul(n, m - 1) + n; 18 19 }
程序运行截图:
实验任务六:
task6.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 int hanoi(int n, char source, char temp, char dest); 4 void move(int m,char a, char b); 5 int main() { 6 int m; 7 scanf("%d", &m); 8 9 int n =hanoi(m, 'A', 'B', 'C'); 10 printf("一共移动了%d次", n); 11 return 0; 12 } 13 void move(int m, char a, char b) { 14 15 printf("%d:%c-->%c\n", m, a, b); 16 } 17 int hanoi(int n, char source, char temp, char dest) { 18 static int count = 0; 19 if (n == 1) { 20 move(n, source, dest); 21 count++; 22 } 23 else { 24 hanoi(n - 1, source, dest, temp); 25 move(n, source, dest); 26 count++; 27 hanoi(n - 1, temp, source, dest); 28 } 29 return count; 30 }
运行截图:
实验任务七:
task7.c
1 #include <stdio.h> 2 #include <math.h> 3 int is_prime(int a) { 4 int i; 5 if (a < 2) 6 return 0; 7 for (i = 2; i <= sqrt(a); i++) { 8 if (a % i == 0) 9 return 0; 10 } 11 return 1; 12 13 } 14 int main() { 15 int m; 16 int i, j; 17 printf("输入一个偶数:"); 18 while ((scanf("%d", &m) != EOF)) { 19 for (i = 1; i < m; i++) { 20 for (j = 1; j < m; j++) { 21 if (is_prime(i) && is_prime(j) && (i + j == m)) { 22 printf("%d = %d + %d\n", m, i, j); 23 } 24 25 } 26 } 27 printf("输入一个偶数:"); 28 29 } 30 return 0; 31 }
运行截图:
标签:语句,return,int,编程,long,C语言,char,func,include From: https://www.cnblogs.com/abcdefg-gaoyuan/p/16849969.html