首页 > 其他分享 >实验二

实验二

时间:2024-10-09 17:23:10浏览次数:1  
标签:%. int else 实验 printf input 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:解释line21代码的功能

问题2:解释line25代码的功能

问题3:这个程序的功能是什么?

  1. 随机生成了一个397~476范围的数
  2. 随机生成了一个1~21范围的数
  3. 随机生成五个学号

任务二

验证性实验 根据一元二次方程求解公式,编程求解一元二次方程

源码

// 一元二次方程求解

#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>
#include <math.h>

int main()
{
    int a, b, c;
    double del, x1, x2;
    while (1)
    {
        printf("输入三个系数: ");
        scanf("%d %d %d", &a, &b, &c);
        if (a == 0)
        {
            if (b == 0 && c == 0)
            {
                break;
            }
            else
            {
                printf("不是一个有效的二次方程!\n");
                continue;
            }
        }

        del = b * b - 4 * a * c;

        if (del > 0)
        {
            x1 = (-b + sqrt(del)) / (2 * a);
            x2 = (-b - sqrt(del)) / (2 * a);
            printf("方程有两个不等的实根。\n");
            printf("x1 = %.2lf\n", x1);
            printf("x2 = %.2lf\n", x2);
            continue;
        }
        else if (del == 0)
        {
            x1 = (-b) / (2 * a);
            printf("方程有两个相等的实根。\n");
            printf("x1 = x2 = %.2lf\n", x1);
            continue;
        }
        else if (del < 0)
        {
            double real, imag;
            real = -b / (2 * a);
            imag = sqrt(-del) / (2 * a);
            printf("方程有两个复数根。\n");
            printf("x1 = %.2lf+%.2lfi\n", real, imag);
            printf("x2 = %.2lf-%.2lfi\n", real, imag);
            continue;
        }
    }
    return 0;
}

结果

任务三

编写一个模拟红绿灯信息的程序

源码

#include <stdio.h>

int main() {
    char in;
    while (scanf("%c", &in) != EOF)
    {
        if (in=='r')
        {
            getchar();
            printf("stop!\n");
        }
        else if (in=='g')
        {
            getchar();
            printf("go go go\n");
        }
        else if (in=='y')
        {
            getchar();
            printf("wait a minute\n");
        }
        else
        {
            getchar();
            printf("something must be wrong\n");
        }
    }
}

结果

任务四

编写一个模拟记账程序统计一天的开销

源码

#include <stdio.h>

int main()
{
    float input;
    float sum = 0, max = 0, min = 20000;
    printf("Enter expense (-1 to stop):\n");
    for (int i = 0; scanf("%f", &input) && input != -1; i++)
    {
        sum += input;
        if (input > max)
            max = input;
        if (input < min)
            min = input;
    }

    printf("Today's total expense:%.1f\nToday's biggest expense:%.1f\nToday's smallest expense:%.1f\n", sum, max, min);

    return 0;
}

结果

任务五

编写程序 根据输入的三角形三边边长,判断三角形类型

源码

#include <stdio.h>

int main()
{
    int a, b, c;
    while (scanf("%d%d%d", &a, &b, &c) != EOF)
    {
        if (a + b > c && a + c > b && b + c > a)
        {
            if (a == b || a == c || b == c)
            {
                if (a == b && b == c)
                {
                    printf("等边三角形\n");
                }
                else
                {
                    printf("等腰三角形\n");
                }
            }
            else if (a * a + b * b == c * c || a * a + c * c == b * b || c * c + b * b == a * a)
            {
                printf("直角三角形\n");
            }
            else
            {
                printf("普通三角形\n");
            }
        }
        else
        {
            printf("不能构成三角形\n");
        }
    }
}

结果

任务六

编写一个简单的猜日期程序

源码

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

int main()
{
    srand(time(NULL));
    int lknum = rand() % 30 + 1;
    int n = 3,input;
    printf("猜猜2024年11月哪一天会是你的lucky day\n开始咯,你有三次机会,猜吧(1~30):");
    while (n--)
    {
        scanf("%d", &input);
        if (input == lknum)
        {
            printf("哇,猜中了:)");
            break;
        }
        else if (input < lknum)
        {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
            if (n==0)
            {
                printf("你已经用完了三次机会啦,偷偷告诉你,lucky day是%d号哦\n",lknum);
                break;
            }
            else
            {
                printf("再猜(1~30):");
                continue;
            }
        }
        else
        {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
            if (n==0)
            {
                printf("你已经用完了三次机会啦,偷偷告诉你,lucky day是%d号哦\n",lknum);
                break;
            }
            else
            {
                printf("再猜(1~30):");
                continue;
            }
        }
    }
}

结果

没猜对

猜对了

标签:%.,int,else,实验,printf,input,include
From: https://www.cnblogs.com/churk/p/18454713

相关文章

  • 实验一
    实验任务1://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorithm>u......
  • 实验一 C++
    实验任务1:task1.cpp:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213......
  • OOP实验一
    任务1:源码:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>5usingnamespacestd;6//声明7//模板函数声明8template<typenameT>9voidoutput(constT&c);10//普通函数声明11voidtes......
  • 实验1 现代C++编程初体验
    任务一#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){cout<<&qu......
  • 实验1
    #include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){cout<<&qu......
  • 《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)正点原......
  • 20222407 2024-2025-1《网络与系统攻防技术》实验一实验报告
    1.实验内容1.1本周学习内容1.1.1缓冲区溢出的定义和原因定义:写入缓冲区的数据量超过该缓冲区能容纳的最大限度,造成溢出的数据改写了与该缓冲区相邻的原始数据的情形。原因:(直接)由于代码语言的设计问题、程序员的安全意识问题,程序没有严格的内存越界检查;(根本)冯诺依曼体系的安全......
  • 20222314 2024-2025-1 《网络与系统攻防技术》实验一实验报告
    网络攻防实验报告姓名:陈振烨学号:20222314实验日期:2024/09/29—2024/10/09实验名称:缓冲区溢出和shellcode指导教师:王志强实验要求: 1.掌握NOP,JNE,JE,JMP,CMP汇编指令的......
  • 实验十二 迈克耳孙干涉仪
         ......
  • OOP实验一
    ##任务一代码:1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......