实验3 函数

1. 实验任务1

task1.c

程序功能:在屏幕上 25行,80列 的范围内任意位置打印十次字符串”hi,May~“,每次打印间隔1000ms

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define N 80 void printText(int line, int col, char text[]);  // 函数声明 void printSpaces(int n);  // 函数声明 void printBlankLines(int n); // 函数声明 int main() { int line, col, i; char text[N] =hi, May~; srand(time(0)); // 以当前系统时间作为随机种子 for (i = 1; i <= 10; ++i) {     line = rand() % 25;     col = rand() % 80;     printText(line, col, text);     Sleep(1000); // 暂停1000ms }return  0; } // 打印n个空格 void printSpaces(int n) { int i; for(i=1; i<=n; ++i) printf( ); } // 打印n行空白行 void printBlankLines(int n) { int i; for(i=1; i<=n; ++i) printf(\n);  } // 在第line行第col列打印一段文本 void printText(int line, int col, char text[]) {     printBlankLines(line-1);  // 打印n-1行空行     printSpaces(col - 1);// 打印n-1列空格     printf(%s, text); }

2. 实验任务2

task2_1.c

#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; }

task2_2.c

// 练习:局部static变量特性 #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; } 

总结 局部static变量的特性:static局部变量的初始化语句只会执行一次,下次再运行该初始化语句就不会被初始化,但是它可以被多次赋值。

3. 实验任务3

task3.c

 
#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) {     int a;     long long r;     int i = 1;     if (n == 0||n==1)         r = 1;     else         r= 2 * fun(n - 1)+1;     return r; }

 

 
4. 实验任务4
task4.c

#include <stdio.h> void hanoi(unsigned int n, char from, char temp, char to); void moveplate(unsigned int n, char from, char to); int sum(unsigned int n); int main() {    unsigned int n;    int s;    while (scanf(%u, &n) != EOF)    {        hanoi(n, 'A', 'B', 'C');        printf(\n);        printf(一共移动了%d次\n, sum(n));        printf(\n);    } } void hanoi(unsigned int n, char from, char temp, char to) {     if (n == 1)         moveplate(n, from, to);     else     {         hanoi(n - 1, from, to, temp);         moveplate(n, from, to);         hanoi(n - 1, temp, from, to);     }  } void moveplate(unsigned int n, char from, char to) {        printf(第%u个盘子:%c-->%c\n, n, from, to); } int sum(unsigned int n) {     if (n == 1)         return 1;     else         return 2 * sum(n - 1) + 1; }

5. 实验任务5

task5.c

 

#include <stdio.h> int is_prime(int n);  int main() {     int i, a, c1, c2;     for (i = 4;i <= 20;i += 2)     {         for (a = 2;a <= i / 2;a++)         {             c1 = is_prime(a);             c2 = is_prime(i - a);             if (c1 == 1 && c2 == 1)                 printf(%d=%d+%d\n, i, a, i - a);         }     } }      int is_prime(int n)     {         int i;         for (i = 2;i < n;i++)             if (n % i == 0)                 return 0;         return 1;     }

 

6.实验任务6

task6.c

#include <stdio.h> #include<math.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 r=0;     int i=0;     while (s != 0)     {         long t;         t = s % 10;         if (t % 2 == 1)         {             r += pow(10, i) * t;             i++;         }         s = s / 10;     }          return r; }