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

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

#define N 5
#define N1 397
#define N2 476
#define N3 21

int main()
{
    int cnt;
    int random_major,random_no;
    
    srand(time(NULL));     //以当前系统时间作为随机种子
    cnt = 0;
    while(cnt < N)
    {
        random_major = rand() % 2;
        
        if(random_major)
        {
        random_no = rand() % (N2 - N1 + 1) + N1;
        printf("20248329%04d\n",random_no);
        }
        else
        {
            random_no = rand() % N3 + 1;
            printf("20248395%04d\n",random_no);
        }
        cnt++;
     } 
     return 0;
}

问题1:将随机数范围限定在397到476之间

问题2:将随机数范围限定在1到21之间

问题3:随机生成学号,一种是以2024832 开头,范围在397到476之间;另一种是以20248395开头,随机数范围在 1 到21之间

实验二

//医院二次方程组
#include<stdio.h>
#include<math.h>
 int main(){
     double a, b, c;
     double delta, p1, p2;//用于保存中间结果
     
     while(scanf("%lf%lf%lf", &a,&b,&c) != EOF){
         if(a == 0){
             printf("a = 0,invalid input\n");
             continue;
         }
         delta = b*b - 4*a*c;
         p1 = -b/2/a;
         p2 = sqrt(fabs(delta))/2/a;
         
         if(delta == 0)
         printf("x1 = x2 = %.2g\n",p1);
         else if(delta > 0)
         printf("x1 = x2 = %.2g.x2 = %.2g\n",p1+p2,p1-p2);
         else{
             printf("x1 = %.2g + %.2gi,",p1,p2);
             printf("x2 = %.2g - %.2gi\n,",p1,p2);
             
         }
         
     }
     return 0;
      
 } 

实验三

#include <stdio.h>

int main() {
    char color;
    printf("请输入交通信号灯颜色字符(r/g/y),按 CTRL+Z 和回车可终止程序:\n");
    while (scanf("%c", &color)!= EOF) {
        if (color == 'r' || color == 'R') {
            printf("stop!\n");
        } else if (color == 'g' || color == 'G') {
            printf("go go go\n");
        } else if (color == 'y' || color == 'Y') {
            printf("wait a minute\n");
        } else {
            printf("something must be wrong...\n");
        }
        // 清空输入缓冲区
        while (getchar()!= '\n');
    }
    return 0;
}

实验四

#include<stdio.h>
#include<math.h>
int main(){
    double min = 20000.0,max = 0.0,total,ex;
    printf("请输入当天的开销,输入 -1 终止程序。\n");
    while(scanf("%lf",&ex)!= EOF){
        if(ex == -1)
        break;
        else if(ex > 0&&ex > max )
        max = ex;
        else if(ex > 0&&ex < min)
        min = ex;
        total += ex;
        
    }
    if(total > 0)
    printf("最高一笔开销:%.1f 元\n", max);
    printf("最低一笔开销:%.1f 元\n", min);
    printf("一天总开销:%.1f 元\n", total);
    return 0;
    
    
}

实验五

#include <stdio.h>

int main() {
int a, b, c;
while (scanf("%d %d %d", &a, &b, &c) == 3) {
if ((a + b > c) && (a + c > b) && (b + c > a)) {
if (a == b && b == c) {
printf("等边三角形\n");
} else if ((a == b && a!= c) || (a == c && a!= b) || (b == c && b!= a)) {
if ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (b * b + c * c == a * a)) {
printf("等腰直角三角形\n");
} else {
printf("等腰三角形\n");
}
} else if ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (b * b + c * c == a * a)) {
printf("直角三角形\n");
} else {
printf("普通三角形\n");
}
} else {
printf("不能构成三角形\n");
}
}
return 0;
}

 

实验六

#include<stdio.h>
#include<math.h>
#include<time.h>
int main(){
    srand(time(NULL));
    int n = rand() % 30 + 1;
    int i,j;
    for(i = 1;i < 4;++i){
        scanf("%d",&j);
    getchar();
        if(j > n)
        printf("晚了\n");
      else if(j < n)
        printf("早了\n");
      else if(j == n){
          printf("猜中了\n");
        break;
      }
        
    }
     printf("很遗憾,你三次都没猜对。正确答案是 %d\n", n);
      

    return 0;
    
}

 

posted @ 2024-10-13 15:42  杨玉鹏  阅读(12)  评论(0编辑  收藏  举报