實驗2 c語言分支與迴圈基礎應用程式設計-1

张朦丹發表於2024-10-13

實驗任務1

task1.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 5
 6 #define N1 397
 7 #define N2 476
 8 #define N3 21
 9 
10 int main() {
11     int cnt;
12     int random_major, random_no;
13 
14     srand(time(NULL));      
15 
16     cnt = 0;
17     while(cnt < N) {
18         random_major = rand() % 2;
19 
20         if(random_major) {
21             random_no = rand() % (N2 - N1 + 1) + N1;
22             printf("20248329%04d\n", random_no);
23         }
24         else {
25             random_no = rand() % N3 + 1;
26             printf("20248395%04d\n", random_no);
27         }
28 
29         cnt++;
30     }
31 
32     return 0;
33 }

回答問題

問題1. line 21使抽取到的在202483290397-202483290476的範圍內隨機。

問題2. line 24使抽取到的在202483950001-202483950021的範圍內隨機。

問題3. 該程式可以在202483290397-202483290476和202483950001-202483950021的範圍內隨機抽取5個學號。

實驗任務二

task2.c

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 int main() {
 5     double a, b, c;
 6     double delta, p1, p2; 
 7 
 8     while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
 9         if(a == 0) {
10             printf("a = 0, invalid input\n");
11             continue;
12         }
13 
14         delta = b*b - 4*a*c;
15         p1 = -b/2/a;
16         p2 = sqrt(fabs(delta))/2/a;
17 
18         if(delta == 0)
19             printf("x1 = x2 = %.2g\n", p1);
20         else if(delta > 0)
21             printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2);
22         else {
23             printf("x1 = %.2g + %.2gi, ", p1, p2);
24             printf("x2 = %.2g - %.2gi\n", p1, p2);
25         }
26     }
27 
28     return 0;
29 

實驗任務3

task3.c

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

實驗任務4

 1 #include<stdio.h>
 2 int main(){
 3     double expense,max=0,min=20000,total=0;
 4     printf("輸入今日開銷,直到輸入-1終止:\n");
 5     while (1) {
 6         scanf("%lf",&expense);
 7         if (expense==-1){
 8             break;
 9         }
10         total=total+expense;
11         if (expense >max){
12             max=expense;
13         }
14         if (expense <min){
15             min=expense;
16         }
17     }
18     printf("今日累計消費總額:%.1lf\n",total);
19     printf("今日最高一筆開銷:%.1lf\n",max);
20     printf("今日最低一筆開銷:%.1lf\n",min);
21     return 0;
22 }

實驗任務5

task5.c

 1 #include<stdio.h>
 2 int main(){
 3     int a,b,c;
 4     while (scanf("%d%d%d",&a,&b,&c) != EOF){
 5         if(a+b<=c || a+c<=b ||b+c<=a){
 6             printf("不能構成三角形\n"); 
 7         }
 8         else{
 9             if (a==b && b==c) printf("等邊三角形\n");
10             else if ((a*a +b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b)&&(a==b ||b==c||a==c)) printf("等腰直角三角形\n");
11             else if (a*a +b*b==c*c||b*b+c*c==a*a||a*a+c*c==b*b) printf("直角三角形\n");
12             else if (a==b ||b==c||a==c) printf("等腰三角形\n");
13             else printf("普通三角形\n"); 
14         }
15     }
16     return 0;
17 }

實驗任務6

task6.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 int main(){
 5     int i=3,n,n1,rdm;
 6     srand(time(NULL));
 7     rdm=1+rand()%30;
 8     printf("猜猜2024年11月哪一天會是你的lucky day\n開始嘍,你有三次機會,猜吧(1~30):");
 9     while(scanf("%d",&n) != EOF){
10         if (n==rdm) {
11             printf("哇,猜中了:)\n");
12             return 0;
13         }
14         else if (n<rdm) {
15             printf("你猜的日期早了,你的lucky day還沒到呢\n");
16             printf("再猜(1~30):");  
17         }    
18         else{
19             printf("你猜的日期晚了,你的lucky day在前面哦\n");
20             printf("再猜(1~30):");
21         }
22         i--;
23         if (i<=0) {
24             break;
25         }
26     }
27     printf("次數用光啦。偷偷告訴你,11月你的lucky day是%d號",rdm); 
28     return 0;
29 }

相關文章