首页 > 其他分享 >实验文档2

实验文档2

时间:2024-10-10 23:35:25浏览次数:1  
标签:rand %. int random 实验 printf 文档 include

关于第二次实践课作业

实验结论

task1.c 

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

回答问题

question1:解释random_no=rand()%(N2-N1+1)+N1;的功能

answer1:N2-N1+1是该专业的总人数,利用随机数取余可以得到【0,N2-N1)内任一整数,并加上该专业的初始学号N1,可得该专业内任意学生的学号

question2:解释random_no=rand()%N3+1;的功能;

answer2:N3是该专业的总人数,利用随机数取余可以得到【0,N3-1)内任意整数,并加上该专业的初始学号N2,可得该专业内任意学生的学号

question3:解释这个程序的功能

answer3:生成5个随机学号,并且这些学号分为两类,每类学号有不同的前缀和范围(其代表着不同的专业),通过随机选择专业(通过random_major的结果为0或为1决定if的走向从而选择专业)并在该专业范围内生成随机学号并按照指定的格式打印出来

 

task2.c

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<math.h>
 4 int main()
 5 {
 6     double a, b, c;
 7     double delta, p1, p2;
 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         delta = b * b - 4 * a * c;
14         p1 = -b / 2 /a;
15         p2 = sqrt(fabs(delta)) / 2 / a;
16 
17         if (delta == 0) {
18             printf("x1 = x2 = %.2g\n", p1);
19         }
20         else if (delta > 0) {
21             printf("x1 = %.2g , x2 = %.2g\n", p1 + p2, p1 - p2);
22         }
23         else {
24             printf("x1 = %.2g + %.2gi,", p1, p2);
25             printf("x2 = %.2g - %.2gi\n", p1, p2);
26         }
27     }
28     return 0;
29 }

 

task3.c

 1 #include<stdio.h>
 2 int main()
 3 {
 4     char ch;
 5 
 6     printf("请从键盘输入字符用来表示交通信号灯颜色:");
 7     printf("(输入r表示red,输入g表示green, 输入y表示yellow)\n");
 8 
 9     while ((ch = getchar()) != EOF) {
10         getchar();
11 
12         if (ch == 'r') {
13             printf("stop!\n");
14         }
15         else if (ch == 'g') {
16             printf("go go go\n");
17         }
18         else if (ch == 'y') {
19             printf("wait a minute\n");
20         }
21         else {
22             printf("something must be wrong...\n");
23         }
24 
25         }
26         
27     return 0;
28 }

 

 

task4.c

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 int main()
 5 {
 6     double x;
 7     double max = 0;
 8     double min = 20000.0;
 9     double sum = 0.0;
10 
11     printf("输入今日开销,直到输入-1终止:\n");
12     while (1) {
13         scanf("%lf", &x);
14 
15         if (x == -1) {
16             break;
17         }
18         sum += x;
19         if (x > max) {
20             max = x;
21         }
22         if (x < min) {
23             min = x;
24         }
25 
26     }
27     printf("今日累计消费总额:%.1lf\n", sum);
28     printf("今日最高一笔开销:%.1lf\n", max);
29     printf("今日最低一笔开销;%.1lf\n", min);
30     system("pause");
31     return 0;
32 }

 

task5.c

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

 

task6.c

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

 实验总结

1.可以通过设置变量并赋予其一定的计算法则,实现单一出口

2.格式控制符%g,其用法是:如果数据值是整数,会自动按整数输出, 如果数据值是小数,会自动按小数并按指定精度输出

3.%0,其用法是输出数值时指定左面不使用的空位置自动填0;%d,其用法是输出一定数据长度d的数据,如果数据长度<d,左补空格

其中task1.c中%04d即实现了左补0,且数据长度为4的数据作为符合格式的学号

4.利用rand()生成随机数时注意引入头文件#include<stdlib.h>

此外为了确保每次运行程序时生成的随机数序列不同,需引入头文件#include<time.h>

引入时间库,用于获取当前时间,以当前系统时间作为随机种子,作为随机数生成的种子

并且要在主函数中加入srand(time(NULL))

<stdlib.h>用于调用rand()和srand()函数,以及<time.h>用于获取当前时间

5.引用:

Tips: 与伪随机数生成相关的两个标准库函数: rand(), srand()

int rand(void) 返回一个0~RAND_MAX之间的伪随机数。 头文件(也有的在) rand()函数原型及使用示例参考: https://en.cppreference.com/w/cpp/numeric/random/ra nd

void srand(unsigned int seed)

为rand()设置随机种子。通常以时间作为随机种子,即srand(time(NULL)) 头文件(也有的在), srand()函数运行及使用示例参考: https://en.cppreference.com/w/cpp/numeric/random/sr and   

6.单个字符需要用’ch‘引好,eg ch==’c‘

7.试图实现字符型变量的多组输入时

while()中(ch=getchar())!=EOF是正确写法(既读取了字符又检查了是否到达文件末尾)

而不能舍弃()写成 ch=getchar()!=EOF的形式

在这种错误的形式下,使用了赋值运算符 = 而不是比较运算符 ==。此外,getchar() != EOF 的结果应该被用来控制循环,而不是赋值给 ch,故要添加()

标签:rand,%.,int,random,实验,printf,文档,include
From: https://www.cnblogs.com/Samoyed0721/p/18456354

相关文章

  • 实验2
    任务11#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#defineN55#defineN13976#defineN24767#defineN3218intmain()9{10intcnt;11intrandom_major,random_no;12srand(time(NULL));13......
  • 实验2
     实验任务1:源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN3219//随机抽取学生的学号10intmain(){11intcnt;12intrandom_major,random_no;1......
  • 实验2
    任务1 #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cn......
  • 实验1 现代c++编程初体验
    实验1:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>9#include......
  • 20222403 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容本周学习内容1.熟悉基本的汇编语言指令及其功能。2.掌握了栈与堆的概念及其在进程内存管理中的应用以及用户态与内核态的区别。3.熟练运用了Linux系统下的基本操作命令。2.实验过程任务一直接修改程序机器指令,改变程序执行流程下载并解压目标文件pwn1,然后拖入虚......
  • 实验2
    试验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(cnt&l......
  • 实验1 现代C++编程初体验
    1.实验任务1task1源代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>//*****表明这是一个模板参数,可以接受任意类型的参数......
  • 20222428 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1本周学习内容1.1.1安全漏洞简介定义:系统的缺陷或不足。作用:网络攻击和防御的关键点。网络攻击:利用系统漏洞进行攻击。防御:提前发现漏洞并修复。exploit:完整的代码攻击。shellcode:不完整的代码攻击。PoC:验证是否存在漏洞。1.1.2缓冲区溢出的定义和原因......
  • 实验1
    实验任务1:task1.cpp:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 基于SpringBoot+Vue+uniapp的在线远程考试系统的详细设计和实现(源码+lw+部署文档+讲
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......