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

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

时间:2024-10-13 12:43:17浏览次数:1  
标签:random int 编程 expense %. 循环 printf include 分支

实验任务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 }

 

标签:random,int,编程,expense,%.,循环,printf,include,分支
From: https://www.cnblogs.com/wyhzmd/p/18462156

相关文章

  • 实验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......
  • 实验1 现代C++编程初体验
    实验任务1:代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;7template<typenameT>8voidoutput(constT&c);9voidtest1();10voidtest2();11vo......
  • JavaScript中的流程控制(顺序结构、分支结构、循环结构)
    流程控制1.概念在一个程序执行的过程中,各条代码的执行顺序对程序的结果是有直接影响的,很多时候要通过控制代码的执行顺序来实现我们完成的功能简单的理解:流程控制就是控制代码,按照一定的结构顺序来执行流程控制的分类:顺序结构分支结构循环结构2.顺序流程控......
  • for循环、break和continue、二重循环
    for循环、break和continue、二重循环循环语句循环语句可以反复多次执行同一组语句,for关键字可以用来编写循环;可以在for循环里让一个变量依次代表一组数字,然后使用同一组语句处理这个变量代表的每个数字。这个变量叫做循环变量,按照统一的规律从第一个数字开始把每个数字都计算出......
  • 用C/C++构建自己的Redis——第六章、事件循环和计时器
    用C/C++构建自己的Redis——第六章、事件循环和计时器文章目录用C/C++构建自己的Redis——第六章、事件循环和计时器前言一、超时和计时器二、链表三、事件循环四、链表排序4.1寻找最近的计时器4.2激活计时器4.3维护计时器五、测试总结前言这一章我们将一起学......
  • JavaScript 异步编程入门
    最近开始不断学习实践JavaScript,出于性能测试的敏感,首先研究了JavaScript的异步编程实践,目前看跟之前学过的Java和Go都不太一样。使用的语法上相对更加复杂,也可能因为我实践得还太少。异步编程异步编程是一种在程序执行过程中,不阻塞主线程的任务处理方式。相较于同步编程......
  • for/while循环与if条件语句
    1.if语句2.while循环3.for循环一.if1.单分支语法格式:if条件:           代码age=18ifage<18:print('666')print('888')8882.双向分支语法格式:if条件:            代码        e......
  • 实验一 现代C++编程初体验
    case1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>usi......
  • 实验1 现代C++编程初体验
    实验1现代C++编程初体验 task1:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参6#include<iostream>7#include<strin......
  • 实验1 现代C++基础编程
    task1实验代码:#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;//声明//模板函数声明template<typenameT>voidoutput(constT&c);//普通函数声明voidtest1();voidtest2();voidtest3......