首页 > 其他分享 >实验2

实验2

时间:2024-10-09 23:44:40浏览次数:1  
标签:%. int float else 实验 printf include

任务1

源代码

#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;
}

运行结果

问题一 a:生成379~476间的随机数

问题二a:生成1~21间的随机数

问题三a:随机在两个班中抽取5个人

任务二

源代码

#include <stdio.h>
#include <math.h>

int main() {
    double a, b, c;
    double delta, p1, p2; // 用于保存中间计算结果

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

运行结果

任务四

源代码

#include<stdio.h>
#include<stdlib.h>
float max(float n, float m) {
    return (n > m) ? n : m;
}
float min(float n, float m) {
    return (n < m) ? n : m;
}
int main() {
    float i, input, sum = 0;
    float m = 0;
    float q = 32767.0;
    printf("输入今日开销,直到输入-1终止\n");
    for (i = 0; scanf_s("%f", &input) && input != -1; i++) {
        sum += input;
        m = max(m, input);
        q = min(q, input);

    }
    printf("今天累计消费:%.1f\n", sum);
    printf("今日最高一笔开销:%.1f\n", m);
    printf("今日最低一笔开销:%.1f\n", q);
    system("pause");
    return 0;
}

运行结果

任务五

源代码

#include<stdio.h>
int main()
{
    int n, a, b, c;

    
    while (scanf_s("%d%d%d", &a, &b, &c) != EOF) {

        if (a + b <= c || a + c <= b || b + c <= a) {
            printf("不构成三角形\n");
        }
        else if (a == b && b == c) {
            printf("正三角形\n");
        }
        else if (a == b || a == c || b == c) {
            printf("等腰三角形\n");
        }
        else if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) {
            printf("直角三角形\n");
        }
        else {
            printf("普通三角形\n");
        }
    }
    return 0;
}

运行结果

任务六

源代码

#include<stdio.h>
#include<stdlib.h>
int main() {
    printf("猜猜2024年11月哪天会是你的lucky day\n");
    int i = 0, a, n;
    printf("开始喽,你有3次机会,猜吧(1~30):");
    n = rand() % 31;
    while (i < 2) {
        ++i;
        scanf_s("%d", &a);

        if (a < n) {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
            printf("再猜:");

        }
        else if (a > n) {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
            printf("再猜:");


        }
        else if (a == n) {
            printf("哇,猜中了:)\n");
            system("pause");
            exit(0);

        }

    }
    scanf_s("%d", &a);


    if (a < n) {
        printf("你猜的日期早了,你的lucky day还没到呢\n");


    }
    else if (a > n) {
        printf("你猜的日期晚了,你的lucky day在前面哦\n");



    }
    else if (a == n) {
        printf("哇,猜中了:)");

    }
    printf("次数用光啦,偷偷告诉你,你的幸运数字是%d\n", n);


    system("pause");
    return 0;
}

运行结果

 

标签:%.,int,float,else,实验,printf,include
From: https://www.cnblogs.com/hh0417blogs/p/18455419

相关文章

  • 20222414 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    实验目的本次实践的对象是一个名为pwn1的linux可执行文件。该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串。该程序同时包含另一个代码片段,getShell,会返回一个可用Shell。正常情况下这个代码是不会被运行的。我们实践的目标就是想办法运行这个代码......
  • 实验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<N){rando......
  • #20222309 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1、直接修改程序机器指令,改变程序执行流程2、通过构造输入参数,造成BOF攻击,改变程序执行流3、注入Shellcode并执行2.实验过程1、直接修改程序机器指令,改变程序执行流程将pwn1改名为pwn20222309-1,并运行打开文件打开文件为乱码按esc键,输入:%!xxd进入十六进制......
  • # 20222323 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容1、熟悉基本的汇编指令,如管道、输入、输出重定向2、掌握了栈与堆的概念3、掌握反汇编与十六进制编程器实验任务1、手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数。2、利用foo函数的Bof漏洞,构造一个攻击输入字符串,覆盖返回地址,触发getShell函数。3、......
  • 20222426 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    学号202224262024-2025-1《网络与系统攻防技术》实验一实验报告1.实验内容1.1NOP,JNE,JE,JMP,CMP汇编指令的机器码:1.1.1NOP(NoOperation)功能:NOP指令是一条空操作指令,它不做任何事情。执行NOP指令时,处理器的状态(如寄存器值、内存内容等)不会发生变化,只是简单地消耗了一......
  • 20222401 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    1.实验内容本次实验是关于缓冲区溢出攻击的,主要的学习内容如下:1.基本Linux命令objdump:将代码段反汇编,在这次实验中主要是用来找地址的。xxd:实现十六进制与二进制的转换,在这次实验的过程中,主要是有两个地方用到了这个命令。一是在打开文件后进行转换,而是以十六进制打开文件,保证......
  • 实验1
    include<stdio.h>intmain(){printf("O\n");printf("\n");printf("II\n");return0;}intmain(){printf("O\n");printf("\n");printf("II\n");printf("O\n");pr......
  • 实验一 现代C++基础编程
    1.实验任务1task1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//3.函数模板、const引用作为形参56#include<iostream>7#include<string>8#include<vector>9#include<algorithm>......
  • 实验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......
  • 实验1 现代C++编程初体验
    实验任务1:task1.cpp:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......