首页 > 其他分享 >实验2

实验2

时间:2024-10-11 15:45:48浏览次数:1  
标签:%. int else 实验 printf include day

实验任务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 {
12     int cnt;
13     int random_major,random_no;
14     
15     srand(time(NULL));
16     
17     cnt=0;
18     while(cnt<N)
19     {
20         random_major=rand()%2;
21         if(random_major)
22         {
23             random_no=rand()%(N2-N1+1)+N1;
24             printf("20248329%04d\n",random_no);
25         }
26         else
27         {
28             random_no=rand()%N3+1;
29             printf("20248395%04d\n",random_no);
30         }
31         cnt++;
32     }
33     
34     return 0;
35 }

运行结果:

问题1:第23行代码是为了生成从N1到N2内的随机整数

问题2:第28行代码是为了生成从1到N3内的随机整数

问题3:这个程序是为了随机生成在两个范围内的整数

 

 

实验任务2:

task2.c源代码:

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 int main()
 5 {
 6     double a,b,c;
 7     double delta,p1,p2;
 8     
 9     while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
10     {
11         if(a==0)
12         {
13             printf("a=0,invalid input");
14             continue;
15         }
16         delta=b*b-4*a*c;
17         p1=-b/2/a;
18         p2=sqrt(fabs(delta))/2/a;
19         if(delta==0) printf("x1=x2=%.2g\n",p1);
20         else if(delta>0) printf("x1=%.2g,x2=%.2g\n",p1+p2,p1-p2);
21         else
22         {
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 {
 4     char color;
 5     while(scanf("%c",&color)!=EOF)
 6     {
 7         getchar();
 8     
 9         if(color=='r')
10         printf("stop!\n");
11         else if(color=='g')
12         printf("go go go\n");
13         else if(color=='y')
14         printf("wait a minute\n");
15         else printf("something must be wrong");
16     }
17 
18     return 0;
19 }

 

运行结果:

 

 

 

实验任务4:

task4.c源代码:

 

 1 #include <stdio.h>
 2 int main()
 3 {
 4     double money,c;
 5     double max=0,min=20000;
 6     printf("输入今日开销,直到输入-1终止:\n");
 7     while(scanf("%lf",&money),money!=-1)
 8     {
 9         if(money>max) max=money;
10         else min=money;
11         getchar();
12         c=c+money;
13         
14     }
15     printf("今日累计消费总额:%.1f\n",c);
16     printf("今日最高一笔开销:%.1f\n",max);
17     printf("今日最低一笔开销:%.1f\n",min);
18     
19     return 0;
20 }

 

运行结果:

 

 

 

实验任务5:

task5.c源代码:

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

 

运行结果:

 

 

实验任务6:

task6.c源代码:

 1 #include <stdio.h>
 2 #include<time.h>
 3 int main()
 4 {
 5     srand(time(NULL));
 6     int random_number=rand()%30+1;
 7     int day,count=1;
 8     
 9     printf("猜猜2024年11月哪一天是你的lucky day\n");
10     printf("\n");
11     printf("开始喽,你有三次机会,猜吧(1~30):");
12 
13     while(scanf("%d",&day),day!=random_number,count<3)
14     {
15         printf("\n");
16         if(day>random_number) printf("你猜的日期晚了,你的lucky day还在前面哦\n");
17         else printf("你猜的日期早了,你的lucky day还没到呢\n") ;
18         printf("\n");
19         printf("再猜(1~30):");
20         count+=1;
21     }
22     printf("\n");
23     if(day==random_number) printf("哇,猜中了:)");
24     
25     else if(day>random_number){
26     printf("你的日期猜晚了,你的lucky day还在前面哦\n");
27     printf("\n");
28     printf("次数用光了。偷偷告诉你,11月你的lucky day是%d号",random_number);}
29     
30     else {
31     printf("你猜的日期早了,你的lucky day还没到呢\n");
32     printf("\n");
33     printf("次数用光了。偷偷告诉你,11月你的lucky day是%d号",random_number);}
34 
35     return 0;
36 }

 

运行结果:

 

标签:%.,int,else,实验,printf,include,day
From: https://www.cnblogs.com/zzh-247061/p/18455042

相关文章

  • 实验2
    任务一:代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand......
  • 数据结构实验第六周
    6-1在一个数组中实现两个堆栈原理就是共享栈,不会的可以看我的数据结构博客StackCreateStack(intMaxSize){StackS=(Stack)malloc(sizeof(structSNode));//这个初始化记得写S->Top1=-1,S->Top2=MaxSize;//栈满的条件S->MaxSize=MaxSize;S->Data=(int......
  • Vchitect 2.0:上海 AI 实验室推出的视频生成利器
    目录引言一、Vchitect2.0模型概述二、Vchitect2.0的技术特点1.超分辨率功能2.帧插入功能3.基于文本和图像的生成能力三、Vchitect2.0的应用场景四、Vchitect2.0测评报告五、生成视频样例六、本地部署推理1.环境准备2.安装依赖3.模型推理结语引言随......
  • 实验2
    任务1源代码1#include<time.h>2#defineN53#defineN13974#defineN24765#defineN3216intmain(){7intcnt;8intrandom_major,random_no;910srand(time(NULL));1112cnt=0;13while(cnt<N){14random_major=rand()%......
  • 实验1 Linux操作系统的安装
    目录1.实验目的和要求:2.主要仪器设备:3.实验内容和原理:(1)实验内容:(2)实验原理:4.操作方法和实验步骤(绘图):5.实验结果与分析(1)将登陆后的效果截图证明系统安装成功(2)回答问题:安装Linux系统一般需要哪些分区?(3)文件和目录类命令的使用(写下你所使用的命令)(4)系统信息类命令......
  • 实验2
    1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45intmain(){6intluckyday,x,y=3;78printf("猜猜2024年11月哪一天会是你的luckyday\n");910srand(time(NULL));11luckyday=rand()......
  • 实验2
    实验2实验任务1:源代码:#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));//以当前系统时间作为随机种子cnt......
  • 20222415 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容本周学习了缓冲区溢出的相关知识,介绍了缓冲区和缓冲区溢出,并介绍了几种缓冲区溢出的实现方式。2.实验过程1.直接修改程序机器指令,改变程序执行流程1.1将目标文件pwn20222415通过共享文件夹放到kali里,运行pwn20222415.1.2输入objdump-dpwn1|more对pwn20222415......
  • 20222319zzs 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1知识回顾1.1.1什么是缓冲区溢出?计算机中,如果程序试图向一个缓冲区填充超出它能够容纳的数据,溢出的数据可能会覆盖其他重要的内存区域,导致程序运行失败甚至崩溃,如果这些溢出数据是精心设计的.则攻击者就可以利用它们指向预先设计的攻击代码(shellcode)(Shellcode是核......
  • 实验1 现代C++编程初体验
    实验任务1代码1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9......