实验一:
task1_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; }
结果:
task1_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; printf("p = %d\n", p); p = p * n; return p; }
结果:
局部static变量p在这里起 到的作用:只对p变量初始化一次,每次调用fac()函数时保存的是上次的结果。
task1_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); }
理论分析:k=4,m=1, m=0,i=2 i=2+0+1=3,m=3+4+1=8 p1=8
k=4,m=1, m=8,i=3 i=3+8+1=12,m=12+4+1=17 p2=17
结果:
bingo~
局部static变量的特性:对变量初始化只进行一次,再次调用函数时,保存上次结果。
实验二:
task2_1源码:
#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) { int i; for(i=1;i<=n;i++) printf("%c",symbol); printf("\n"); }
结果:
task2_2源码:
#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==1) printf("%c",symbol); else { printSymbol(n-1,symbol); printf("%c",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 1; else return 2*fun(n-1)+1; }
结果:
实验四:
源码:
#include<stdio.h> int isPrime(int n); const int start=101; const int end=200; int main() { int i, count; count = 0; for(i=start;i<=end;i++) { if(!(isPrime(i))) { printf("%d ",i); count+=1; } } printf("\n"); printf("%d~%dÖ®¼äÒ»¹²ÓÐ%d¸ö·ÇËØÊý\n", start, end, count); return 0; } int isPrime(int n) { int i,flag; for(i=2;i<n/2;i++) { if(n%i==0) { flag = 0; break; } else flag = 1; } return flag; }
结果:
实验五:
源码:
#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 result = 0; int i,j; j = 1; while(s!=0) { i = s%10; if(i%2) { result+=i*j; j*=10; } s/=10; } return result; }
结果:
实验六:
源码:
#include <stdio.h> #include <math.h> double fun(int n); long long fac(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; } long long fac(int n) { if(n==1||n==0) return 1; else return n*fac(n-1); } double fun(int n) { double item,s; int i; s = 0; for(i=1;i<=n;i++) { item = pow(-1,i-1)/fac(i); s+=item; } return s; }
结果:
实验总结:
体会了因函数而使代码模块化带来的逻辑上的清晰;学会了部分算法;体会了同一个问题不同算法带来的直观感受;加深了局部变量和全局变量的理解。