嵌入式第九天学习

第九天学习

课外知识

ai的简洁循环

c 复制代码
const char *prompt[3] = {"请输入你的名字:", "请输入你的学号:", "请输入你的成绩:"};

for (int i = 0; i < 3; ++i) {
    printf("%s", prompt[i]);
    scanf("%31s", massage[time1][i]);   // %31s 防止写越界
}
c 复制代码
#include <stdio.h>

int main(void) {
    int n;
    printf("请输入元素个数:");
    if (scanf("%d", &n) != 1 || n <= 0 || n > 1000) {
        fprintf(stderr, "非法个数!\n");
        return 1;
    }

    int a[n];           // C99 可变长数组(VLA)
    printf("请输入 %d 个整数:", n);
    for (int i = 0; i < n; ++i) {
        if (scanf("%d", &a[i]) != 1) {
            fprintf(stderr, "输入错误!\n");
            return 1;
        }
    }

    /* 打印验证 */
    for (int i = 0; i < n; ++i)
        printf("%d ", a[i]);
    puts("");
    return 0;
}

一句话总结:

给 %c/%[ ] 等不自动跳过空白的格式加前导空格 是最简单、最通用的"吃掉换行符"技巧。

c 复制代码
scanf(" %c", &ch);   // %c 前面有空格

查询系统

#include <stdio.h>

#include <string.h>

#include <windows.h>

char name1[10][32];

char password1[10][32];

char name[] = {0};

char password[] = {0};

int num = 0;

char massage[10][3][32];

void menu()

{

printf("*********\n");
printf("***** 1.创建账户 \n");
printf("
2.登录账户 \n");
printf("
3.查询信息 \n");
printf("
4.退出 \n");
printf("
\n");

}

void creat()

{

printf("正在创建账户,请稍等。 \n");

Sleep(1000);

while (1)

{

printf("请输入账号:");

scanf(" %s", name1[num]);

printf("%s", name1[num]);

printf("请输入密码:");

scanf(" %s", password1[num]);

printf("账号为:%s ,密码为:%s \n", name1[num], password1[num]);

num++;

break;

}

}

void getin()

{

int time1 = num;

int time2 = num;

int flag = 0;

while (1)

{

err:

printf("请输入账号:\n");

scanf(" %s", name);

// while (getchar() != '\n')

// {}

while (time1 + 1)

{

int true1 = strcmp(name, name1[time1]);

if (true1 == 0)

{

break;

}

time1--;

if (time1 == -1)

{

printf("账号错误请重新输入! \n");

goto err;

}

}

break;

}

while (1)

{

err1:

printf("请输入密码: \n");

scanf(" %s", password);

while (getchar() != '\n')

{

}

while (time2 + 1)

{

int true2 = strcmp(password, password1[time2]);

if (true2 == 0)

{

break;

}

time2--;

}

if (time2 == -1)

{

printf("密码错误请重新输入! \n");

goto err1;

}

break;

}

for (int i = 0; i < 3; i++)

{

if (i == 0)

{

printf("请输入你的名字:");

scanf(" %s", massage[time1][i]);

continue;

}

if (i == 1)

{

printf("请输入你的学号:");

scanf(" %s", massage[time1][i]);

continue;

}

if (i == 2)

{

printf("请输入你的成绩:");

scanf(" %s", massage[time1][i]);

continue;

}

}

printf("您查询的序号为:%d \n", time1);

}

void found()

{

printf("请输入你的查询序号:");

int a = 0;

scanf(" %d", &a);

for (int i = 0; i < 3; i++)

{

switch (i)

{

case 0:

printf("您的名字为:%s \n", massage[a][i]);

break;

case 1:

printf("您的学号为:%s \n", massage[a][i]);

break;

case 2:

printf("您的成绩为:%s \n", massage[a][i]);

break;

default:

break;

}

}

}

int main(int argc, char const *argv[])

{

复制代码
menu();
int chose = 0;
printf("请输入你的选择:");
while (1)
{
    scanf("%d", &chose);
    if (chose == 1)
    {
        creat();
        Sleep(1000);
        menu();
        continue;
    }

    if (chose == 2)
    {
        getin();
        Sleep(1000);
        menu();
        continue;
    }
    if (chose == 3)
    {
        found();
        Sleep(1000);
        menu();
        continue;
    }
    if (chose == 4)
    {
        printf("下次再见!");
        break;
    }
    else
    {
        printf("选择错误,请重新输入:");
        continue;
    }
}
return 0;

}

相关推荐
June`1 天前
SSH连接原理与守护进程实战
linux·运维·服务器
萌>__<新1 天前
力扣打卡每日一题——缺失的第一个正数
数据结构·算法·leetcode
DuHz1 天前
车对车对向交汇场景的毫米波路径损耗建模论文精读
论文阅读·算法·汽车·信息与通信·信号处理
水天需0101 天前
Grep 例程大全
linux
杼蛘1 天前
XXL-Job工具使用操作记录
linux·windows·python·jdk·kettle·xxl-job
lxh01131 天前
二叉树中的最大路径和
前端·算法·js
萌>__<新1 天前
力扣打卡每日一题————零钱兑换
算法·leetcode·职场和发展
重生之后端学习1 天前
238. 除自身以外数组的乘积
java·数据结构·算法·leetcode·职场和发展·哈希算法
Gavin在路上1 天前
AI学习之Anthropic的访谈者工具
人工智能·学习
CQ_YM1 天前
Linux进程基础
linux·服务器·进程