嵌入式第九天学习

第九天学习

课外知识

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;

}

相关推荐
轻微的风格艾丝凡1 小时前
光伏 MPPT 算法介绍
人工智能·算法·光伏
程序员与背包客_CoderZ1 小时前
C/C++版LLM推理框架Llama.cpp——入门与编码实战
c语言·开发语言·网络·c++·人工智能·语言模型·llama
CS_浮鱼2 小时前
【Linux】UDP
linux·udp
专注于大数据技术栈2 小时前
java学习--main方法
java·开发语言·学习
大侠课堂2 小时前
ARM Linux内核异常排查指南
linux·arm开发
DeeplyMind2 小时前
Linux Virtio 子系统核心数据结构解析
linux·驱动开发·virtio-gpu
无敌最俊朗@2 小时前
力扣hot100-环形链表(2)142
算法·leetcode·链表
Creeper.exe3 小时前
【C语言】函数
c语言·开发语言
贝塔实验室3 小时前
Altium Designer 6.0 初学教程-如何生成一个集成库并且实现对库的管理
linux·服务器·前端·fpga开发·硬件架构·基带工程·pcb工艺