1.实验任务1
task1.c源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <windows.h> 5 #define N 80 6 void print_text(int line, int col, char text[]); // 函数声明 7 void print_spaces(int n); // 函数声明 8 void print_blank_lines(int n); // 函数声明 9 int main() { 10 int line, col, i; 11 char text[N] = "hi, November~"; 12 srand(time(0)); // 以当前系统时间作为随机种子 13 for(i = 1; i <= 10; ++i) { 14 line = rand() % 25; 15 col = rand() % 80; 16 print_text(line, col, text); 17 Sleep(1000); // 暂停1000ms 18 } 19 return 0; 20 } 21 // 打印n个空格 22 void print_spaces(int n) { 23 int i; 24 for(i = 1; i <= n; ++i) 25 printf(" "); 26 } 27 // 打印n行空白行 28 void print_blank_lines(int n) { 29 int i; 30 for(i = 1; i <= n; ++i) 31 printf("\n"); 32 } 33 // 在第line行第col列打印一段文本 34 void print_text(int line, int col, char text[]) { 35 print_blank_lines(line-1); // 打印(line-1)行空行 36 print_spaces(col-1); // 打印(col-1)列空格 37 printf("%s", text); // 在第line行、col列输出text中字符串 38 }
在屏幕上随机出现hi,November~
2.实验任务2
task2_1.c源代码
1 // 利用局部static变量的特性,计算阶乘 2 #include <stdio.h> 3 long long fac(int n); // 函数声明 4 int main() { 5 int i, n; 6 printf("Enter n: "); 7 scanf("%d", &n); 8 for (i = 1; i <= n; ++i) 9 printf("%d! = %lld\n", i, fac(i)); 10 return 0; 11 } 12 // 函数定义 13 long long fac(int n) { 14 static long long p = 1; 15 p = p * n; 16 return p; 17 }
task2_2.c源代码
1 #include <stdio.h> 2 int func(int, int); 3 int main() 4 { 5 int k = 4, m = 1, p1, p2; 6 p1 = func(k, m); 7 p2 = func(k, m); 8 printf("%d, %d\n", p1, p2); 9 return 0; 10 } 11 12 int func(int a, int b) 13 { 14 static int m = 0, i = 2; 15 i += m + 1; 16 m = i + a + b; 17 return m; 18 }
局部变量在函数内做定义说明,仅限于函数内部使用。
3.实验任务3
task3.c源代码
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 long long func(int n); 5 int main() 6 { 7 int n; 8 long long f; 9 while (scanf("%d",&n)!=EOF) 10 { 11 f = func(n); 12 printf("n=%d, f=%lld\n",n,f); 13 } 14 system("pause"); 15 return 0; 16 } 17 18 long long func(int n) 19 { 20 int i; 21 long long s,m=1; 22 for(i=1;i<=n;i++) 23 { 24 m=m*2; 25 } 26 s=m-1; 27 return s; 28 }
4.实验任务4
task4.c源代码
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 int func(int n,int m); 5 int main() 6 { 7 int n, m; 8 while(scanf("%d%d", &n, &m) != EOF) 9 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 10 system("pause"); 11 return 0; 12 } 13 14 int func(int n,int m) 15 { 16 int i,d=1,f=1,c,a=n-m,b=1; 17 if(n==m) 18 return 1; 19 else if(n<m) 20 return 0; 21 else 22 { 23 for(i=1;i<=m;i++) 24 { 25 d*=(a+1); 26 f*=b; 27 a++; 28 b++; 29 } 30 c=d/f; 31 return c; 32 } 33 }
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 int func(int n,int m); 5 int main() 6 { 7 int n, m; 8 while(scanf("%d%d", &n, &m) != EOF) 9 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 10 system("pause"); 11 return 0; 12 } 13 14 int func(int n,int m) 15 { 16 int i,d=1,f=1,c,a=n-m,b=1; 17 if(m==0) 18 return 1; 19 else 20 { 21 if(n==1) 22 return 1; 23 else 24 { 25 if(m==n) 26 return 1; 27 else 28 return func(n-1,m-1)+func(n-1,m); 29 } 30 } 31 }
5.实验任务5
task5.c源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 void hanoi(unsigned int n, char from, char temp, char to); 4 void moveplate (unsigned int n,char from,char to) ; 5 int N=0; 6 int main() 7 { 8 unsigned int n; 9 while(1) 10 { 11 N=0; 12 scanf("%u",&n); 13 hanoi(n,'A','B','C'); 14 printf("一共移动了%d次\n",N); 15 } 16 return 0; 17 } 18 19 void hanoi (unsigned int n,char from,char temp,char to) 20 { 21 if(n==1) 22 moveplate(n,from,to); 23 else 24 { 25 hanoi(n-1,from,to,temp); 26 moveplate(n,from,to); 27 hanoi(n-1,temp,from,to); 28 } 29 N++; 30 } 31 32 void moveplate (unsigned int n,char from,char to) 33 { 34 printf("%u:%c-->%c\n",n,from,to); 35 }
6.实验任务6
task6.c源代码
1 #include <stdio.h> 2 #include <math.h> 3 long func(long s); 4 int main() 5 { 6 long s, t; 7 printf("Enter a number: "); 8 while (scanf("%ld", &s) != EOF) 9 { 10 t = func(s); 11 printf("new number is: %ld\n\n", t); 12 printf("Enter a number: "); 13 } 14 return 0; 15 } 16 17 long func(long s) 18 { 19 int a; 20 int b=1,c=0; 21 22 while (s!=0) 23 { 24 a=s%10; 25 if(a%2!=0&&s>10) 26 { 27 c=c+b*a; 28 b=b*10; 29 } 30 else if(s<=10&&s%2!=0) 31 { 32 c=c+b*a; 33 b=b*10; 34 } 35 s=s/10; 36 } 37 38 return c; 39 }