首页 > 其他分享 >实验三

实验三

时间:2023-11-05 13:33:48浏览次数:33  
标签:return int long char 实验 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 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,Novmber~

task2_1.c源代码

 1 // 利用局部static变量的特性,计算阶乘
 2 #define _CRT_SECURE_NO_WARNINGS
 3 #include <stdio.h>
 4 long long fac(int n); // 函数声明
 5 int main() {
 6     int i, n;
 7     printf("Enter n: ");
 8     scanf("%d", &n);
 9     for (i = 1; i <= n; ++i)
10         printf("%d! = %lld\n", i, fac(i));
11     return 0;
12 }
13 // 函数定义
14 long long fac(int n) {
15     static long long p = 1;
16     printf("p = %lld\n", p);
17     p = p * n;
18     return p;
19 }

运行结果

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

运行截图

 task3.c源代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 long long func(int n); // 函数声明
 4 int main() {
 5     int n;
 6     long long f;
 7     while (scanf("%d", &n) != EOF) {
 8         f = func(n); // 函数调用
 9         printf("n = %d, f = %lld\n", n, f);
10     }
11     return 0;
12 }
13 long long func(int n)
14 {
15     long long  a = 1;
16     for (int i = 1; i <= n; ++i)
17     {
18         a *= 2;
19     }
20     return a - 1;
21 }

运行截图

 task4_1.c源代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 int func(int n, int m);
 4 int main() {
 5     int n, m;
 6     while (scanf("%d%d", &n, &m) != EOF)
 7         printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
 8     return 0;
 9 }
10 int func(int n, int m)
11 {
12     int a = 1, b = 1, c = 1,sum;
13     if (n >= m) {
14         for (int i = 1; i <= n; ++i)
15         {
16             a *= i;
17         }
18         for (int i = 1; i <= m; ++i)
19         {
20             b *= i;
21         }
22         for (int i = 1; i <= n-m; ++i)
23         {
24             c *= i;
25         }
26         sum = a / b / c;
27         return sum;
28     }
29     else
30         return 0;
31 }

运行结果

 

 task4_2.c源代码

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int func(int n, int m);
int main() {
    int n, m;
    while (scanf("%d%d", &n, &m) != EOF)
        printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
    return 0;
}
int func(int n, int m)
{
    if (m > n)
        return 0;
    else if ( n==m||m == 0)
        return 1;
    else
        return func(n - 1, m) + func(n - 1, m - 1);
}

运行结果

 task5.c源代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void hanoi(unsigned int n, char from, char temp, char to);
void move(unsigned int n, char from, char to);
int a;
int main()
{
    unsigned int n;
    while (scanf("%d", &n) != EOF)
    {
        hanoi(n, 'A', 'B', 'C');
        printf("共移动了%d次\n", a);
        a = 0;
    }
    return 0;
}
void hanoi(unsigned int n, char from, char temp, char to)
{
    a++;
    if (n == 1)
        move(n, from, to);
    else
    {
        
        hanoi(n - 1, from, to, temp);
        move(n, from, to);
        hanoi(n - 1, temp, from, to);
        
    }
}
void move(unsigned int n, char from, char to)
{
    printf("%u:%c-->%c\n", n, from, to);
}

运行截图

 task6,c源代码

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

运行结果

 

标签:return,int,long,char,实验,func,include
From: https://www.cnblogs.com/ctl050312/p/17798387.html

相关文章

  • 实验3 类与数组、指针
    实验任务1point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmo......
  • 实验三-类与数组、指针
    point.hpp1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13intget_y()const;1......
  • 实验2 类和对象_基础编程2
    实验任务1demo1.dev方法一#ifndefT_H#defineT_H#include<iostream>#include<string>usingnamespacestd;//类T的声明classT{public:T(intx=0,inty=0);//带有默认形值的构造函数T(constT&t);//复制构造函数T(T&&......
  • 实验3
    实验1源代码1#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);10......
  • 实验3 类与数组、指针
    实验任务1point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmov......
  • 实验三 类与数组、指针
    实验任务1task1.cpp源码:Point.hpp:1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13......
  • 实验3
    任务1源代码1#include<stdio.h>2#include<time.h>3#include<windows.h>4#include<stdlib.h>5#defineN806voidprint_line(intn);7voidprint_col(intn);8voidprint_text(intline,intcol,chartext[]);9intmain(){10......
  • 实验三
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmain......
  • 第三次实验
    任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明intmain(){ ......
  • 实验3
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intco1,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){intline,co1,i;......