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

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

时间:2024-10-13 15:32:16浏览次数:1  
标签:%. int 编程 C语言 else ex printf include 分支

#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>
#include<math.h>
int main(){
    int a,b,c;
    printf("从键盘输入三角形三边边长a, b, c")
    while(scanf("%d%d%d",&a&b&c)!= EOF){
        if(a+b<c||a+c<b||b+c<a)
        printf("不能构成三角形");
        else if(a==b||a==c||b==c)
        printf("等腰三角形") ;
        else(a==b==c) 
        printf("等边三角形");
        else(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) 
        printf("直角三角形"); 
         
    }
    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;
    
}

 

标签:%.,int,编程,C语言,else,ex,printf,include,分支
From: https://www.cnblogs.com/yangyupeng2024/p/18462441

相关文章

  • 【python基础】Python基础入门:从零开始学习编程
    Python基础入门:从零开始学习编程Python是一种广泛应用于各个领域的高级编程语言,因其简洁、易读、功能强大而受到开发者的青睐。从数据分析、Web开发到人工智能和自动化,Python提供了丰富的工具和库,帮助开发者快速构建项目。本篇博客将为你详细介绍Python的基础语法和核......
  • 豆包MarsCode编程助手(插件)
    1、豆包MarsCode(浏览器复制该链接即刻体验:sourl.cn/pdDZ6S)是基于豆…CloudIDE及AI编程助手两种使用形态,具备代码补全、智能问答、代码解释和代码修复等多项功能。原生的AI能力,让编程变得更加智能化。2、安装方法:点击上面的链接,会出现一个画面,画面如下:之后,鼠标向......
  • Shell 编程:流程控制
    Shell编程:流程控制if语句ifconditionthencommand1command2...commandNfiifconditionthencommand1command2...commandNelsecommandfiifcondition1thencommand1elifcondition2thencommand2else......
  • 实验1 现代C++编程初体验
    Task1code1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 面向对象编程系列3
    多态理解多态之前,要先明白什么是向上转型和动态绑定。这个向上转型字面上的意思就是子类--->父类。我们在实例化一个鸟类时,可以这样写:Birdbird=newBird("jj");或者:​Birdbird=newBird("jj");Animalbird1=bird;//两行代码组合起来......
  • 实验2 c语言分支与循环基础应用编程-1
    实验任务1task1.c1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314s......
  • 实验2 c语言分支与循环基础应用编程1
    task1:问题1随机数求余后结果为1,生成0397到0476中的随机数问题2随机数求余后结果为0,生成0001到0021中的随机数问题3随机生成5个不同的学号task2: 实验3: task4:1#include<stdio.h>2intmain()3{4doublex,sum,max,min;5sum=0;6......
  • 【C语言】爱心代码与EasyX的安装
    前几天学习了C语言上爱心代码:1.静态的#include<stdio.h>#include<stdlib.h>#include<windows.h>intmain(intargc,char*argv[]){ floatx,y,a; for(y=1.5;y>-1.5;y-=0.1){ for(x=-1.5;x<1.5;x+=0.05){ a=x*x+y*y-1; putchar(a*a*a-x*x*y*......
  • 实验1 现代C++编程初体验
    实验任务1:代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;7template<typenameT>8voidoutput(constT&c);9voidtest1();10voidtest2();11vo......
  • 【C语言基础】核心关键字详解与应用
    目录一、void1.1.作用1.2.代码示例二、基本数据类型(char、int、float、double等)2.1.char(字符类型)2.2.int(整型)2.3.float(单精度浮点型)2.4.double(双精度浮点型)2.5.代码示例三、控制流程语句(if、else、switch、case、default等)3.1.if和else语句3.2.switch......