实验2 C语言分支与循环基础应用编程

task1.c

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 5
 6 
 7 int main(){
 8     int number;
 9     int i;
10 
11     srand(time(0));
12     for(i = 0;i < N;++i){
13         number = rand()%100 + 1;
14         printf("20490042%04d\n",number);
15     }
16     system("pause");
17     return 0;
18 }

运行结果截图

df1430f8aceebf44d50a3c087b276026

问题一:让程序生成5个1~100的随机整数,并且固定格式打出20490042....

问题二:line13的作用是先将标准库函数rand()取出的一个随机整数对100取余,那么数字的范围就确定在了1~99,再加上1就实现了1~100的随机数生成,然后赋值给变量number储存。

问题三:line14控制了输出格式,4是指输出宽度为4个字符,不足4位时会默认在左侧补空格,而0就表示用0代替空格补位。

问题四:去掉srand(time(0))后发现每次生成的5个随机数完全相同!time(0)调用了时间函数,这个值每时每刻都在变化,这样将其作为随机数种子就可以初始化rand()的随机序列。

task2.c

源代码

#include <stdio.h>

int main(){
    int choice,quantity;
    float total_price = 0,amount_paid,change;

    while(1){
        printf("\n自动饮料售卖机菜单:\n");
        printf("1. 可乐 - 3 元/瓶\n");
        printf("2. 雪碧 - 3 元/瓶\n");
        printf("3. 橙汁 - 5 元/瓶\n");
        printf("4. 矿泉水 - 2 元/瓶\n");
        printf("0. 退出购买流程\n");
        printf("请输入饮料编号: ");
        scanf("%d", &choice);
        if (choice ==0)
            break;
        if (choice<1 ||choice>4){
        printf("无效饮料编号,请重新输入。\n");
            continue;}
        printf("请输入购买数量:");
        scanf("%d",&quantity);

        if (quantity<0){
            printf("购买数量不可以为负数,请重新输入。\n");
            continue;}
        if (choice==1 ||choice==2)
            total_price +=3*quantity;
        else if (choice==3)
            total_price+=5*quantity;
        else 
            total_price+=2*quantity;

        printf("请投入金额:");
        scanf("%f",&amount_paid);

        change = amount_paid - total_price;
        printf("本次购买总价:%.2f元\n",total_price);
        printf("找零:%.2f元\n",change);
        total_price = 0;
}
       printf("感谢您的购买,欢迎下次光临!\n");
       return 0;
 }
View Code

运行效果截图

1cc7d7f104ce8348173d5c1633434825

问题一:去掉后会使每次剩下的金额不断累加,无法保证每次开始前金额为0。

问题二:表示结束本次循环,重新开始一次循环。

task3.c

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main(){
 4     char c;
 5     while (scanf("%c", &c) != EOF) {
 6         if (c == '\n') {
 7             continue;
 8         }
 9         if(c=='r')
10             printf("stop!");
11         else if (c=='g')
12             printf("go go go");
13         else if (c=='y')
14             printf("wait a minute");
15         else 
16             printf("something must be wrong");}
17     
18         system("pause");
19         return 0;
20 }

运行效果截图

5db5efff1f2be9bd3387f609c9647ae1

task4.c

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main(){
 4     double expense;
 5     double total = 0.0;
 6     double max_exp = 0.0;
 7     double min_exp = 2000.0;
 8     printf("输入今日开销,直到-1终止:");
 9         while(1){
10             scanf("%lf",&expense);
11             if(expense==-1)
12                     break;
13 
14         total +=expense;
15         if (expense > max_exp) 
16             max_exp = expense;
17         
18         if (expense < min_exp) 
19             min_exp = expense;
20         }
21         printf("今日累计消费总额: %.1f\n", total);
22         printf("今日最高一笔开销: %.1f\n", max_exp);
23         printf("今日最低一笔开销: %.1f\n", min_exp);
24     system("pause");
25     return 0;
26 }
View Code

运行效果截图

a93d70bf7c7b6f200dfb2228988b1ce4

 task5.c

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main() {
 4     int a, b, c, max, temp;
 5     while (scanf("%d %d %d", &a, &b, &c) == 3) {
 6         max = a;
 7         if (b > max) max = b;
 8         if (c > max) max = c;
 9         if (max >= a + b + c - max) {
10             printf("不能构成三角形\n");
11             continue; 
12         }
13         if (a == b && b == c) {           
14             printf("等边三角形\n");
15         } else if (a == b || b == c || a == c) {           
16             printf("等腰三角形\n");
17         } else if (a*a + b*b == c*c || a*a + c*c == b*b || b*b + c*c == a*a) {           
18             printf("直角三角形\n");
19         } else {            
20             printf("普通三角形\n");
21         }
22     }
23     system("pause");
24     return 0;
25 }
View Code

运行效果截图

b1a6a30e5ee64be3a3749581fc9d031d

task6.c

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 int main() {
 6     int lucky_day, guess, i;
 7     srand((unsigned int)time(NULL));
 8     lucky_day = rand() % 30 + 1;
 9     printf("猜猜2026年4月哪一天是你的lucky day\n");
10     for (i = 0; i < 3; i++) {
11         if (i == 0) {
12             printf("开始喽,你有3次机会,猜吧(1~30):");
13         } else {
14             printf("再猜(1~30):");
15         }
16         scanf("%d", &guess);
17         if (guess == lucky_day) {
18             printf("哇,猜中了:)\n");
19             return 0; 
20         } else if (guess > lucky_day) {
21             printf("你猜的日期晚了,你的lucky day在前面哦\n");
22         } else {
23             printf("你猜的日期早了,你的lucky day还没到呢\n");
24         }
25     }
26 
27     printf("次数用光啦。4月你的lucky day是%d号\n", lucky_day);
28     system("pause");
29     return 0;
View Code

运行效果截图

b9ef9f228254cb69d1cd1674101edead

总结:

1.本次实验新掌握了随机函数生成,并发现自己对多组输入直到自己按下z停止程序这一点掌握不够熟练。

2.相比于第一次实验,这次学过的知识点更多了,在编写代码时有种力不从心的感觉,很多算法不知道该怎么设计,也不能准确将算法转成c语言,可能是练习不够,觉得自己还需要熟练。

posted @ 2026-04-03 22:08  everleaf1616  阅读(4)  评论(0)    收藏  举报