BasicIO.c,RUN
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//整数小数的输入
void BasicIO()
{
int nValue;
long long llValue;
float fValue;
double dValue;
//%d %lld %f %lf分别对应int,long long, float, double 类型的输入
//1. 如果类型不匹配,将出现运行错误
//2. 只能空格隔开,不能加上','等其它分割符
int count = scanf("%d %lld %f %lf", &nValue, &llValue, &fValue, &dValue);
//scanf_s("%d %lld %f %lf", &nValue, &llValue, &fValue, &dValue); //或者
}
//菜单的实现方法
void Menu()
{
int count = 0;
int select;
count = scanf("%d", &select);
//实现菜单
switch (select)
{
case 0:
break;
case 1:
break;
default:
break;
}
//char类型和char字符串类型的scanf_s必须指定输入字符的个数
char c;
count = scanf("%c", &c);
c = getchar();
scanf_s("%c", &c, 1); //1个字符
//实现菜单
switch (c)
{
case 'A':
case 'a':
break;
case 'B':
break;
default:
break;
}
//char类型和char字符串类型的scanf_s必须指定输入字符的个数
char msg[128];
count = scanf("%s", msg);
scanf_s("%s", msg, 128);
if (strcmp(msg, "Here") == 0)
{
}
else if (strcmp(msg, "There") == 0)
{
}
}
void Advanced()
{
//循环输入,直到满足条件退出
//scanf返回值来判断(返回获取数据的个数,如果输入正常,返回值应等于等待输入的变量的个数)
//https://en.cppreference.com/w/c/io/fscanf
char c0;
while (scanf("%c", &c0) == 1)//scanf返回1个变量的值
{
if (c0 == '#')break; //退出循环
}
char c1, c2;
while (scanf("%c %c", &c1, &c2) == 2)//scanf返回2个变量的值
{
if (c2 == '#')break; //退出循环
}
//getchar返回单个字符
while ((c0 = getchar()) != '#')
{
}
//检测连续的输入"ei",用多个变量记录连续的输入
char prev = '\0', cur;
while ((cur = getchar()) != '#')
{
if (prev == 'e' && cur == 'i')
{
//检测到"ei"
}
//...
prev = cur;
}
}
//带空格的字符串输入
void IO_String()
{
char buffer[128];
fgets(buffer, 128, stdin);
//gets(buffer); //或者
//gets_s(buffer, 128); //或者
puts(buffer);
fputs(buffer, stdout); //或者
}
int main()
{
Menu();
IO_String();
return 0;
}