首页 > 其他分享 >实验2

实验2

时间:2024-10-12 21:49:01浏览次数:1  
标签:11 %. int else 实验 printf include

实验1代码:

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

问题回答:

1.随机生成计类11和12班的学号

2.随机生成奇安信班的学号

3.随机生成计类11,12班和奇安信班的学号

图片:

实验2:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 int main()
 5 {
 6     double a,b,c,delta,p1,p2;
 7     while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
 8     {
 9         if(a==0)
10         {
11             printf("a=0,invalid input\n");
12             continue;
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         {
24             printf("x1 = %.2g + %.2gi,",p1,p2);
25             printf("x2 = %.2g - %.2gi\n",p1,p2);
26         }
27     }
28     system("pause");
29     return 0;
30 }

图片:

实验3:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     char a;
 6     while(scanf("%c",&a)!=EOF)
 7     {
 8         if(a=='r')
 9             printf("stop!\n");
10         else if(a=='g')
11             printf("go go go\n");
12         else if(a=='y')
13             printf("wait a minute\n");
14         else
15             printf("something must be wrong...\n");
16             getchar();
17     }
18     system("pause");
19     return 0;
20 }

图片3:

实验4:

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

图片:

实验5:

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

图片:

实验6:

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

图片:

实验结论:

1.break,continue只能用于循环结构

2.continue跳过语句过后的所有语句,并开始新一轮循环;break终止循环

标签:11,%.,int,else,实验,printf,include
From: https://www.cnblogs.com/liuseki/p/18461564

相关文章

  • 实验2
    #include<stdio.h>#include<malloc.h>#defineSIZE100#defineINCREMENT_SIZE10#defineTRUE1#defineFALSE-1#defineOK1#defineERROR-1typedefintStatus;typedefintElemType;typedefstructLNode{ElemTypedata;structLNode*next;}LNode,*LLi......
  • 实验项目3 自定义路由转换器
    实验目的了解Django处理HTTP请求的流程。掌握路由转换器的用法。掌握如何定义和使用自定义路由转换器。实验内容操作1 创建Django项目chapter02(先进入之前创建的虚拟环境(python3.7、有Django))操作2 在项目chapter02中创建应用app01(应用需要激活应用并分配根路由、创建子......
  • 实验1
    任务1:task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • 实验2
    task.1#include<stdio.h>#include<stdio.h>#include<time.h>#defineN5#defineN1397#defineN2476#defineN321intmain(){intcnt;intrandom_major,random_no;srand(time(NULL));cnt=0;while(c......
  • 实验一 现代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......
  • 《DNK210使用指南 -CanMV版 V1.0》第二十九章 音频录制实验
    第二十九章音频录制实验1)实验平台:正点原子DNK210开发板2)章节摘自【正点原子】DNK210使用指南-CanMV版V1.03)购买链接:https://detail.tmall.com/item.htm?&id=7828013987504)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/k210/ATK-DNK210.html5)正点原......
  • 实验二
    任务一:#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1397//11班第一位学生的尾号#defineN2476//12班最后一位学生的尾号#defineN321//奇安信班一共21人intmain(){intcnt;intrandom_major,random_......
  • 实验1现代c++编程初体验
    test1:源代码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>5usingnamespacestd;6template<typenameT>7voidoutput(constT&c);8voidtest1();9voidtest2();10voidtest3......