实验任务1
源代码task1.c
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #include<windows.h> 5 #define N 80 6 7 void print_text(int line, int col, char text[]); 8 void print_spaces(int n); 9 void print_blank_lines(int n); 10 11 int main(){ 12 int line,col,i; 13 char text[N] = "hi, November~"; 14 15 srand(time(0)); 16 17 for(i = 1; i <= 10; ++i){ 18 line = rand() % 25; 19 col = rand() % 80; 20 print_text(line,col,text); 21 sleep(1000); 22 } 23 24 return 0; 25 } 26 27 void print_spaces(int n){ 28 int i; 29 30 for(i = 1;i <= n; ++i) 31 printf(" ") 32 } 33 34 void print_blank_lines(int n){ 35 int i; 36 37 for(i = 1; i <= n; ++i) 38 print("\n") 39 } 40 41 void print_text(int line,int col,char text[]){ 42 print_blank_lines(line-1); 43 print_spaces(col-1); 44 printf("%s",text); 45 }
运行结果
功能
在屏幕中随机一个位置生成hi, November,重复执行十次
实验任务2
源代码task2_1.c
1 #include<stdio.h> 2 long long fac(int n); 3 4 int main(){ 5 int i,n; 6 7 printf("Enter n:"); 8 scanf("%d",&n); 9 10 for(i = 1; i <= n; ++i) 11 printf("%d! = %lld\n",i,fac(i)); 12 13 return 0; 14 } 15 16 long long fac(int n){ 17 static long long p = 1; 18 19 p = p * n; 20 21 return p; 22 }
运行结果
源代码task2_1.c(改)
1 #include<stdio.h> 2 long long fac(int n); 3 4 int main(){ 5 int i,n; 6 7 printf("Enter n:"); 8 scanf("%d",&n); 9 10 for(i = 1; i <= n; ++i) 11 printf("%d! = %lld\n",i,fac(i)); 12 13 return 0; 14 } 15 16 long long fac(int n){ 17 static long long p = 1; 18 19 printf("p = %lld\n",p); 20 21 p = p * n; 22 23 return p; 24 }
运行结果
源代码task2_2.c
1 #include<stdio.h> 2 int func(int, int); 3 4 int main(){ 5 int k = 4,m = 1,p1,p2; 6 7 p1 = func(k,m); 8 p2 = func(k,m); 9 printf("%d,%d\n",p1,p2); 10 11 return 0; 12 } 13 14 int func(int a, int b){ 15 static int m = 0, i = 2; 16 17 i += m + 1; 18 m = i + a +b; 19 20 return m; 21 }
运行结果
一致
实验任务3
源代码task3.c
1 #include<stdio.h> 2 long long func(int n); 3 4 int main(){ 5 int n; 6 long long f; 7 8 while(scanf("%d",&n) != EOF){ 9 f = func(n); 10 printf("n = %d, f = %lld\n",n,f); 11 } 12 13 return 0; 14 } 15 16 long long func(int n){ 17 int i; 18 long long s = 1; 19 20 21 for(i = 1; i <= n; ++i) 22 s = s * 2; 23 24 return s - 1; 25 }
运行结果
实验任务4
源代码task4_1.c(迭代)
1 #include<stdio.h> 2 int func(int n, int m); 3 4 int main(){ 5 int n, m; 6 7 while(scanf("%d%d",&n, &m) != EOF) 8 printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m)); 9 10 return 0; 11 } 12 13 int func(int a, int b){ 14 int s = 1; 15 int t = 1; 16 int ans,i,c; 17 18 c = b; 19 20 if(a < b) 21 return 0; 22 23 else if(b == 0 || b == a) 24 return 1; 25 26 else{ 27 for(i = 1;i <= b; ++i){ 28 s = s * a; 29 t = t * c; 30 a--; 31 c--; 32 ans = s / t; 33 } 34 35 return ans; 36 } 37 }
运行结果
标签:源代码,return,函数,int,编程,long,C语言,func,include From: https://www.cnblogs.com/chenziming114514/p/17798291.html