首页 > 其他分享 >实验3

实验3

时间:2023-10-30 17:36:17浏览次数:27  
标签:10 12 int long 实验 func include

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);  // 暂停1000ms
22     }
23     
24     return 0; 
25 }
26 
27 // 打印n个空格 
28 void print_spaces(int n) {
29     int i;
30     
31     for(i = 1; i <= n; ++i)
32         printf(" ");
33 }
34 
35 // 打印n行空白行
36 void print_blank_lines(int n) {
37     int i;
38     
39     for(i = 1; i <= n; ++i)
40         printf("\n");
41  } 
42 
43 // 在第line行第col列打印一段文本 
44 void print_text(int line, int col, char text[]) {
45     print_blank_lines(line-1);      // 打印(line-1)行空行 
46     print_spaces(col-1);            // 打印(col-1)列空格
47     printf("%s", text);         // 在第line行、col列输出text中字符串
48 }

运行截图

 task2-1.c

static变量的特性:在·计算机中开辟一块特定的内存空间,储存的值一直保留
 1 // 利用局部static变量的特性,计算阶乘
 2 
 3 #include <stdio.h>
 4 long long fac(int n); // 函数声明
 5 
 6 int main() {
 7     int i, n;
 8 
 9     printf("Enter n: ");
10     scanf("%d", &n);
11 
12     for (i = 1; i <= n; ++i)
13         printf("%d! = %lld\n", i, fac(i));
14 
15     return 0;
16 }
17 
18 // 函数定义
19 long long fac(int n) {
20     static long long p = 1;
21  printf("p = %lld\n", p);
22     p = p * n;
23 
24     return p;
25 }

运行截图

task2-2.c

 1 // 练习:局部static变量特性
 2 
 3 #include <stdio.h>
 4 int func(int, int);        // 函数声明
 5 
 6 int main() {
 7     int k = 4, m = 1, p1, p2;
 8 
 9     p1 = func(k, m);    // 函数调用
10     p2 = func(k, m);    // 函数调用
11     printf("%d, %d\n", p1, p2);
12 
13     return 0;
14 }
15 
16 // 函数定义
17 int func(int a, int b) {
18     static int m = 0, i = 2;
19 
20     i += m + 1;
21     m = i + a + b;
22 
23     return m;
24 }
25 //i=3m=8;p1=8;i=12,m=17,p2= 17

运行截图

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 sum=1;
18     int i;
19     for(i=0;i<n;i++){
20     sum*=2;
21     }
22 return sum-1;
23 }

运行截图

task4.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 n, int m){
14     if(n<m){
15         return 0;
16     }
17     else if(m==1){
18         return n;
19     }
20     else if(m==0){
21         return 1;
22     }
23     else{
24         return func(n-1,m)+func(n-1,m-1);
25     }
26     
27     
28 }

运行截图

task5.c

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 void hanoi(int n,char a,char b,char c);
 4 void move(int n,char a,char c);
 5 int main() {
 6     int n;
 7     int cnt=1;
 8     int i;
 9     while(scanf("%d",&n)!=EOF){
10         hanoi(n,'A','B','C');
11         for(i=0;i<n;i++){
12             cnt*=2 ;
13         }
14         printf("一共移动了%d次\n",cnt-1);
15         cnt=1;
16     }
17     return 0;
18 }
19 
20 void hanoi(int n,char a,char b,char c){
21     int cnt =0;
22     if(n==1){
23         move(n,a,c);
24     }else{
25         hanoi(n-1,a,c,b);
26         move(n,a,c);
27         hanoi(n-1,b,a,c);
28     }
29 } 
30 void move(int n,char a,char c){
31     printf("%d:%c-->%c\n",n,a,c);
32 }

运行截图

 task6.c

 1 #include <stdio.h>
 2 #include <math.h>
 3 long func(long s);   // 函数声明
 4 
 5 int main() {
 6 
 7     long s, t;
 8 
 9     printf("Enter a number: ");
10     while (scanf("%ld", &s) != EOF) {
11         t = func(s); // 函数调用
12         printf("new number is: %ld\n\n", t);
13         printf("Enter a number: ");
14     }
15 
16     return 0;
17 }
18 
19 long func(long s){
20     static long sum = 0;
21     while(s!=0){
22         long a=s%10;
23         if(a%2!=0){
24             sum=sum*10+a; 
25         }
26         s/=10;
27     }
28     long t=0;
29     while(sum!=0){
30         long b=sum%10;
31         t=t*10+b;
32         sum/=10;
33     }
34 return t;    
35 }

运行截图

 

 

标签:10,12,int,long,实验,func,include
From: https://www.cnblogs.com/homeh/p/17798367.html

相关文章

  • 大数据实验
       ......
  • 实验3 C语言函数应用编程
    实验任务1源代码task1.c1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(......
  • 实验3
    实验任务1:通过三个函数,达成每隔相同的时间在随机的行和位置输出相同的文本的效果#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);......
  • 实验3
    任务1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_blank......
  • 实验三
    #include<stdio.h>longlongfunc(intn);//函数声明intmain(){intn;longlongf;while(scanf("%d",&n)!=EOF){f=func(n);//函数调用printf("n=%d,f=%lld\n",n,f);}return0;......
  • 实验三
    #include<stdio.h>longlongfunc(intn);//函数声明intmain(){intn;longlongf;while(scanf("%d",&n)!=EOF){f=func(n);//函数调用printf("n=%d,f=%lld\n",n,f);}return0;......
  • 实验3
    实验11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn);101......
  • 实验3
    task1line17-19循环10次并随机赋给line0~24其中一个数,给col0~79其中一个数voidprintf_spaces打印n个空格 voidprintf_blank_lines打印n行空白voidprintf_text利用随机生成的line与col打印出line-1与col-1空白行与空格后,光标在第line行第col列打印出文本。......
  • 实验3
    task11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_blank_l......
  • 基于LiteOS的智慧农业案例实验分享
    最近在指导一位读者朋友做毕业设计,该毕设是关于端云互通的,基于小熊派+LiteOS+华为云。在指导他的过程中我也学到了不少东西,这里通过一个案例实验(智慧农业)给大家分享一些知识。实验框图相关模块简介1、STM32L431RCT62、LiteOSLiteOS是华为开发的轻量级实时操作系统:LiteOS源码GitHub......