第五次作业

本文介绍了使用C语言实现的几个实用程序与小游戏,包括猜数字游戏、数组搜索、密码验证及字符转换程序。通过这些实例,展示了C语言的基础应用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.完成猜数字游戏。

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
void menu()  //定义菜单
{
    printf("********************************\n");
    printf("************ 1,play*************\n");
    printf("************ 0,exit**************\n");
    printf("********************************\n");
}

void game() //定义游戏
{
    int random_n = rand() % 100 + 1;
    int input = 0;
    while (1)
    {
        printf("请输入猜的数字:");
        scanf("%d", &input);
        if (input > random_n)
        {
            printf("猜大了\n");
        }
        else if (input < random_n)
        {
            printf("猜小了\n");
        }
        else
        {
            printf("恭喜你,猜对了\n");
            break;
        }
    }
}
int main()
{
    int input = 0;  //出随机数
    srand((unsigned)time(NULL));
    do
    {
        menu();
        printf("请选择->:");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default :
            printf("选择错误,请重新输入!\n");
            break;
        }
    } while (input);
        return 0;
}

终于学会用宏改正scanf的问题了

2.写代码可以在整型有序数组中查找想要的数字,
找到了返回下标,找不到返回-1.(折半查找)

代码还有点问题

3.编写代码模拟三次密码输入的场景。
最多能输入三次密码,密码正确,提示“登录成功”,密码错误,
可以重新输入,最多输入三次。三次均错,则提示退出程序。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int pwdJudge(char pwd[ ])
{
    char input[21] = { 0 };
    int i;

    for (i = 0; i < 3; i++)
    {
        scanf_s("%s", input);

        if (0 == strcmp(input, pwd))
        {
            return 1;
        }
    }
    return 0;
}

4.编写一个程序,可以一直接收键盘字符,
如果是小写字符就输出对应的大写字符,
如果接收的是大写字符,就输出对应的小写字符,
如果是数字不输出。

#include <stdio.h>
#include <ctype.h>
#define isBigLetter(ch) (ch <= 'Z' && ch >= 'A')

int isSmallLetter(char ch)
{
    return ch <= 'z' && ch >= 'a';
}

int main()
{
    char ch;

    while (1)
    {
        ch = getchar();
        if (ch == '@')
        {
            break;
        }

        if (isSmallLetter(ch))
        {
            putchar(ch - 32);
        }
        else if (isBigLetter(ch))
        {
            putchar(ch + 32);
        }
        else if (isalnum(ch))
        {

        }
        else
        {
            putchar(ch);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值