实验二

task1
1.用当前系统时间计算number
2.强制保留四位整数
3.随机抽4个人的学号

task2
1.在循环结束后重置总价值的值为零。去掉会使下次循环的初始总价值为上次循环的总价值,导致计算结果出错。
2.break使当前循环结束并执行下一行代码;continue会重新执行当前代码。7
3.没有必要。因为只有choise为1,2,3,4的情况,程序才会运行到line33-44的switch部分。

task3

include <stdio.h>

int main() {
char input;

while (1) {
    input = getchar();
    while (getchar() != '\n');
    switch (input) {
    case 'r':
    case 'R':
        printf("stop!\n");
        break;
    case 'g':
    case 'G':
        printf("go go go\n");
        break;
    case 'y':
    case 'Y':
        printf("wait a minute\n");
        break;
    case EOF: 
        printf("\n程序已终止。\n");
        return 0;
    default:
        printf("something must be wrong...\n");
        break;
    }
}

return 0;

}

task4

include <stdio.h>

int main() {
float exp, total = 0.0f, max = 0.0f, min = 20000.0f;

while (1) {
    scanf_s("%f", &exp);

    if (exp == -1) {
        break;
    }

    if (exp > max) {
        max = exp;
    }

    if (exp < min) {
        min = exp;
    }

    total += exp;
}

printf("最高开销: %.1f\n", max);
printf("最低开销: %.1f\n", min);
printf("总开销: %.1f\n", total);

return 0;

}

task5

include <stdio.h>

include <stdlib.h>

include <time.h>

int main() {
int luckyDay, guess, attempts = 0;
srand(time(0));
luckyDay = rand() % 30 + 1;

printf("欢迎来到4月幸运日猜测游戏!你有3次机会猜出你的幸运日。\n");

while (attempts < 3) {
    printf("请输入你猜测的日期(1-30):");
    scanf_s("%d", &guess);

    if (guess == luckyDay) {
        printf("恭喜你,猜中了!\n");
        return 0;
    }
    else if (guess < luckyDay) {
        printf("你猜的日期早了。\n");
    }
    else {
        printf("你猜的日期晚了。\n");
    }

    attempts++;
}
printf("很遗憾,你的3次机会已用完。你在4月的幸运日是:%d\n", luckyDay);

return 0;

}

task6

include <stdio.h>

void printCharacter(int n) {
for (int i = n; i > 0; i--) {
int characters = 2 * i - 1;

    int spaces = (2 * n - 1 - characters) * 3; 
    for (int s = 0; s < spaces; s++) {
        printf(" ");
    }

    for (int j = 0; j < characters; j++) {
        printf("  0   ");
    }
    printf("\n");

    for (int s = 0; s < spaces; s++) {
        printf(" ");
    }
    for (int j = 0; j < characters; j++) {
        printf(" <H>  ");
    }
    printf("\n");

    for (int s = 0; s < spaces; s++) {
        printf(" ");
    }
    for (int j = 0; j < characters; j++) {
        printf(" I I  ");
    }
    printf("\n\n");
}

}

int main() {
int n;
printf("input n: ");
scanf_s("%d", &n);

printCharacter(n);

return 0;

}


posted @ 2025-03-24 11:13  风火klg  阅读(9)  评论(0)    收藏  举报