找出C ++中用户输入的数据类型的程序
在本教程中,我们将讨论一个程序来找出用户输入的数据类型。
为此,我们将获得一些随机输入值。我们的任务是处理该输入并打印回给定输入的数据类型。
示例
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
# define MAX_INPUT 100
int main() {
//阅读输入
char value[MAX_INPUT] = "";
double temp;
int n;
char str[MAX_INPUT] = "";
//检查整数值
double val = 1e-12;
fgets(value, 100, stdin);
if (sscanf(value, "%lf", &temp) == 1) {
n = (int)temp; //typecasting
if (fabs(temp - n) / temp > val)
printf("The input is a floating point\n");
else
printf("The input is an integer\n");
}
//检查字符串值
else if (sscanf(value, "%s", str) == 1)
printf("The input is a string\n");
else
printf("Input not recognized\n");
}输出结果
Input not recognized
热门推荐
10 生日祝福语祝长辈简短
11 他舅生日祝福语简短
12 中秋祝福语送朋友简短
13 三八简短祝福语给婆婆
14 给师父春节祝福语简短
15 幼儿祝福语大全简短卡片
16 双节祝福语简短唯美
17 形容新年的祝福语简短
18 结婚十三年祝福语简短