首页 > 其他分享 >实验二

实验二

时间:2024-10-11 16:59:47浏览次数:1  
标签:%. int expense else 实验 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回答

生成一个在N1到N2范围内的随机数

问题2回答

生成一个在1到N3范围内的随机数

第三个问题回答

随机产生一个数字,且确保他在第一个范围或第二个范围里

任务二

源代码

#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 = %.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 ch;
    
    while(scanf("%c", &ch) != EOF){
        if(ch == 'y')
    {
        printf("wait a minute\n");
        getchar();
}
        else if(ch == 'g'){
        printf("go go go\n");
        getchar();
}
        else if(ch == 'r')
{
        printf("stop!\n");
        getchar();
}
        else{
            printf("something must be wrong...\n");
            getchar();
    
        }
        
    }
    
    return 0;
}

 

运行结果

 实验四

源代码

#include<stdio.h>
int main(){
    double expense;
    double totalexpense ;
    double maxexpense = 0.0;
    double minexpense = 100000000000.0;
    printf("请输入今天开销:");
    scanf("%lf",&expense) ;
    while(expense != -1){
        totalexpense = expense + expense;
    if(expense < minexpense){
        minexpense = expense;
}
    if(expense > maxexpense){
       maxexpense = expense;
}
    printf("请输入今天开销:");
    scanf("%lf",&expense) ;
}
    printf("今日累计消费总额:%.2lf\n",totalexpense);
    printf("今日最高一笔开销:%.2lf\n",maxexpense);
    printf("今日最低一笔开销:%.2lf\n",minexpense);
    return 0;
    
}

 

 运行结果

 实验五

源代码

#include<stdio.h>
int main(){
int a, b, c;
printf("输入三角形三边边长(为整数):");
while(scanf("%d%d%d",&a, &b, &c)!= EOF){
if(a + b < c || b + c < a ||a + c < b)
printf("三角形不存在\n");
else if(a == b && b == c)
printf("三角形为等边三角形\n");
else if(a == b || b == c|| a == c)
printf(" 三角形为等腰三角形\n");
else if(a*a + b*b == c*c|| b*b + c*c == a*a||a*a + c*c == b*b)
printf("三角形为直角三角形\n");
else
printf("三角形为普通三角形\n") ;
printf("输入三角形三边边长(为整数):\n");
}
return 0;

}

 

运行结果

实验六

源代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    int lurkday = rand() % 30 +1;
    int guess;
    int attempts = 3;
    srand(time(NULL));
    printf("猜猜2024年11月的哪一天是你的lurk day。\n");
    printf("开始咯,你有三次机会,猜吧(1~30):");

    while(attempts > 0){
        printf("你还有%d次机会。请输入你的猜测;",attempts);
        scanf("%d",&guess);
        
       
        
        if(guess == lurkday){
    
           printf("哇,猜中了!\n");
           return 0;}
        else if (guess < lurkday){
        
            printf("你猜的日期早了,你的lurk day还没到呢,再猜。\n");}
        else{
        
            printf("你猜的日期晚了,你的lurk day还在前面哦,再猜。\n");}
        attempts = attempts - 1;
    }
        printf("次数用光了。偷偷告诉你,11月你的lurk day是%d.\n",lurkday);
            
    return 0;    
    
}

 

运行结果

 

标签:%.,int,expense,else,实验,printf,include
From: https://www.cnblogs.com/xl14/p/18454678

相关文章

  • 实验2
    任务1:源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain(){11intcnt;12intrandom_major,random_no;1314srand(......
  • 实验2
    任务1源代码:编译 任务2源代码: 编译 任务3源代码:编译任务4源代码; 编译 任务5源代码: 编译任务6源代码:编译 ......
  • 20222302 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容本周学习内容1.熟练掌握了栈和堆的概念。2.掌握了Linux的基本操作,如shell命令和编译器gcc、调试器gdb的使用。3.掌握了缓冲区溢出的原理。实验任务本次实验的对象是一个名为pwn1的linux可执行文件。该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何......
  • 20222425 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容本周学习了缓冲区溢出的相关知识点,介绍了包括汇编语言、进程内存管理、函数调用过程等相关知识点。1.1实践目标1.手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数。2.利用foo函数的Bof漏洞,构造一个攻击输入字符串,覆盖返回地址,触发getShell函数。3.注入......
  • 20222324 石国力《网络与系统攻防技术》实验一
    1.1实践目标本次实践的对象是一个名为pwn1的linux可执行文件。该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串。该程序同时包含另一个代码片段,getShell,会返回一个可用Shell。正常情况下这个代码是不会被运行的。我们实践的目标就是想办法运行这个......
  • 实验二
    实验一:源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#defineN55#defineN13976#defineN24767#defineN3218intmain(){9intcnt;10intrandom_major,random_no;11srand(time(NULL));12......
  • 实验二
    task11.line21代码作用:随机生成在N1和N2之间的数字2.line25代码作用:随机生成在1和N3之间的数字3.该程序功能:随机生成五个两个班学生学号task2 task3task4 task5task6 ......
  • 实验2
    实验任务1:task1.c源代码:1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13977#defineN24768#defineN321910intmain()11{12intcnt;13intrandom_major,random_no;14......
  • 实验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......