实验二

发布时间 2023-10-16 17:39:38作者: Sn002

一.实验任务一

源码

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

#define N 5
#define N1 374
#define N2 465
int main(){
    int number;
    int i;

    srand( time(0));
    for( i=0;i<N; ++i){
        number = rand()%(N2 -N1 + 1) + N1;
        printf("202383290376%04d\n",number);
    }

    system("pause");
    return 0;
}

运行结果

作用

产生[N1,N2]间的随机整数

二.实验任务二

源码

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

三.实验任务三

源码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include<time.h>
 4 
 5 int main(){
 6     int date,i,guess;
 7 
 8     srand(time(0));
 9     date = 1 + rand() % 30;
10     printf("猜猜2023年11月哪一天会是你的lucky day\n");
11     printf("开始喽,你有三次机会,猜吧(1-30):");
12     scanf("%d",&guess);
13     for(i = 1;i <= 3;i++){
14         if(guess == date){
15             printf("哇,猜中了\n");
16             break;
17         }
18         else if(guess >= date){
19             printf("你猜的日期晚了,你的lucky day已经过啦\n");
20         }
21         else{
22             printf("你猜的日期早了,你的lucky day还没到呢");
23         }
24         printf("再猜(1-30):");
25         scanf("%d",&guess);
26 
27     }
28     if(guess !=date){
29         printf("次数用完了你的lucky day是%d号\n",date);
30     }
31     system("pause");
32 
33     return 0;
34 }

效果

 

 四.实验任务四

源码