首页 > 其他分享 >实验3

实验3

时间:2022-11-06 22:47:49浏览次数:49  
标签:return int char 实验 printf include define

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#include<windows.h>
void print_spaces(int n);
void print_blank_lines(int n);
void print_text(int line, int col, char text[]);
#define N 80
int main()
{
    int line, col, i;
    char text[N] = "hi,Novermber~";
    srand(time(0));
    for (i = 1; i <= 10; ++i)
    {
        line = rand()% 25;
        col = rand() % 80;
        print_text(line, col, text);
        Sleep(1000);
    }
    return 0;
}
void print_spaces(int n)
{
    int i;
    for (i = 1; i <= n; ++i)
        printf(" ");

}
void print_blank_lines(int n)
{
    int i; 
    for (i = 1; i <= n; ++i)
        printf("\n");
}
void print_text(int line, int col, char text[])
{
    print_blank_lines(line-1);
    print_spaces(col - 1);
    printf("%s", text);
}

随机打印空格和空行,从而实现在屏幕中随机打印一串字符串

 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int 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;
}
int fac(int n)
{
    static long long  p=1;
    printf("\np=%lld\n", p);
    p = p * n;
    return p;
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int fac(int,int);
int main()
{
    int k = 4, m = 1, p1, p2;
    p1 = fac(k, m);
    p2 = fac(k, m);
    printf("%d,%d\n", p1, p2);
    return 0;
}
int fac(int a,int b)
{
    static int m = 0, i = 2;
    i += m + 1;
    m = i + a + b;
    return m;
}

static局部变量在编译时赋值,数据每改变一次,下次的数值即改变后的数值,不会再重新初始化

 

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

}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.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)
{
    int s;
    if (n == m || m == 0)
        s = 1;
    else if (n < m)
        s = 0;
    else if (m == 1)
        s = n;
    else
        s = func(n - 1, m) + func(n - 1, m - 1);
    return s;
}

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

 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void hanoi(unsigned int n, char from, char temp, char to);
void moveplate(unsigned int n, char from, char to);
int count = 0;
int main()
{
    unsigned int n;
    while (scanf("%d", &n) != EOF)
    {
        hanoi(n, 'A', 'B', 'C');
        printf("一共移动了%d次", count);
        count = 0;
    }
    return 0;
}
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);
    count++;
}

#define   _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int is_prime(int j);
int main() 
{
    int m,n, i;
    while (scanf("%d", &i) != EOF) 
    {
        for (m = 1;; m++)
        {
            if (is_prime(m)) 
            {
                if (is_prime(n = i - m)) 
                {
                    printf("%d=%d+%d\n", i, m, n); 
                    break;
                }
            }
        }

    } return 0;
}
int is_prime(int j)
{

    int i;
    for (i = 2; i < j; i++)
    {
        if (j % i == 0)
            break;
    }
    if (i == j)
        return 1;
    else
        return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
long fun(long s);
int main()
{
    long int 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) 
{
    int a, b = 1;
    long m = 0;
    while (s > 0)
    {
        a = s % 10;
        if (a % 2 != 0) {
            m = a * b + m;
            b *= 10;
        }
        s = s / 10;
    }
    return m;
}

 

标签:return,int,char,实验,printf,include,define
From: https://www.cnblogs.com/wr236/p/16862830.html

相关文章

  • 实验三
    task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint......
  • 实验4:开源控制器实践——OpenDaylight
    (一)基本要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器2.通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s(......
  • 实验2:Open vSwitch虚拟交换机实践
    一、基础要求a)/home/用户名/学号/lab2/目录下执行ovs-vsctlshow命令、以及p0和p1连通性测试的执行结果截图b)/home/用户名/学号/lab2/目录下开启MininetCLI并执行......
  • 实验1:SDN拓扑实践
    实验要求(一)基本要求1.使用Mininet可视化工具,生成下图所示的拓扑,并保存拓扑文件名为学号.py。2.使用Mininet的命令行生成如下拓扑:a)3台交换机,每个交换机连接1台主机,3......
  • 实验7:基于REST API的SDN北向应用实践
    1.基础要求a)编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;#创建拓扑sudomn--topo=single,3--co......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验......
  • 实验四 类与数组、指针
    一.实验任务1-4总结:通过完成实验任务1-4,我对深复制和浅复制有了更加深刻的印象,加深对指针、引用作为形参用法的理解,理解了参数传递的过程。二.实验任务51.源代码:(1)vecto......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验......
  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;建立拓扑sudomn--topo=single,3--cont......