首页 > 编程语言 >实验2 C语言输入输出和控制语句应用编程

实验2 C语言输入输出和控制语句应用编程

时间:2023-03-17 22:12:47浏览次数:36  
标签:main 编程 int scanf 输入输出 C语言 while printf include

一.实验结论

1.实验任务1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 5
#define R1 586
#define R2 701

int main()
{
    int number;
    int i;

    srand(time(0));

    for (i = 0; i < N; ++i) 
    {
        number = rand() % (R1 - R2 + 1) + R1;
        printf("20228330%04d\n", number);
    }

    return 0;
}

 

 

1.line18可以生成一个586到701之间的随机数
2.这个程序的功能是随机输出五个202283300586到202283300701之间的数

2.实验任务2

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS
int main()
{
    double x, y;
    char c1, c2, c3;
    int a1, a2, a3;

    scanf_s("%d%d%d", &a1, &a2, &a3);//添加三个&符号
    printf("a1=%d,a2=%d,a3=%d\n", a1, a2, a3);
    getchar();//添加一个getchar()吞掉回车键
    scanf_s("%c%c%c", &c1,1, &c2,1, &c3,1);
    printf("c1 = %c,c2 = %c,c3 = %c\n", c1, c2, c3);

    scanf_s("%f,%lf", &x, &y);
    printf("x = %f, y = %lf\n", x, y);

    return 0;
}

 

 3.实验任务3

#include <stdio.h>
#include <math.h>

int main()
{
    double x, y;
    while (scanf_s("%lf", &x) != EOF)
    {
        y = 9.0 * x / 5.0 + 32;
        printf("摄氏度c = %.2f,华氏度f = %.2f\n", x, y);
        printf("\n");
    }
    return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
    double x, ans;
    while (scanf_s("%lf", &x) != EOF)
    {
        ans = pow(x, 365);
        printf("%.2f的365次方: %.2f\n", x, ans);
        printf("\n");
    }
    return 0;
}

 

 

 

 

4.实验任务4

#include<stdio.h>

int main()
{
    char x;
    while (scanf_s("%c", &x,1) != EOF)
    {
        getchar();
        switch (x)
        {
        case 'r':printf("stop\n"); break;
        case 'g':printf("go go go\n"); break;
        case 'y':printf("wait a minute\n"); break;
        default:printf("someing must be wrong...\n"); break;
        }
    }
    
    return 0;
}

 

 5.实验任务5

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>

int main() {
    int x, y, t;
    srand(time(0));
    x = rand() % 30 + 1;
    printf("猜猜2023年4月哪一天会是你的lucky day\n开始喽,你有三次机会,猜吧(1~30):");
    scanf_s("%d", &y);
    for (t = 1; t < 4;) 
    {
        if (x == y)
            printf("哇,猜中了!");
        else
            if (x > y)
                printf("你猜的日期早了,你的lucky day还没有到呢\n");
            else
                printf("你猜的日期晚了,你的lucky day已经过啦\n");
        t++;
        if (x == y||t==4)
            break;
        else
        {
            printf("再猜(1~30):");
            scanf_s("%d", &y);
        }
    }
    if (t == 4)
        printf("次数用完啦,偷偷告诉你:4月,你的lucky day是%d号", x);
    return 0;
}

 

 

 

 6.实验任务6

#include<stdio.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>

int main()
{
    int x, y;
    x = 1;
    y = 1;
    while (y < 10)
    {
        while(x<=y)
        {
            printf("%d*%d=%2d   ", x, y, x * y);
            x++;
        }
        x = 1;
        printf("\n");
        y++;
    }
    return 0;
}

 

 7.实验任务7

#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>

int main()
{
    int n, a, b, t, x;
    t = 1;
    a = 1;
    x = 1;
    printf("input n:");
    scanf_s("%d", &n);
    b = 2 * n - 1;
    while(a<=n)
    {
        b = 2 * (n - a + 1) - 1;
        while (x < a) 
        {
            printf("        ");
            x++;
        }
        while(t<=b)
        {
            printf(" o      ");
            t++;
        }
        printf("\n");
        t = 1;
        x = 1;
        while (x < a)
        {
            printf("        ");
            x++;
        }
        while(t<=b)
        {
            printf("<H>     ");
            t++;
        }
        printf("\n");
        t = 1;
        x = 1;
        while (x < a)
        {
            printf("        ");
            x++;
        }
        while(t<=b)
        {
            printf("I I     ");
            t++;
        }
        printf("\n");
        printf("\n");
        t = 1;
        x = 1;
        a++;
    }
        return 0;

}

 

 

 

 规律:

当输入为n时:
第i行,需要打印2*(n-i+1)个字符小人
第i行,前面需要打印i-1个空白

标签:main,编程,int,scanf,输入输出,C语言,while,printf,include
From: https://www.cnblogs.com/lwh442/p/17228375.html

相关文章