Nhận đầu vào từ người dùng và xác định kiểu dữ liệu: số nguyên, số thực, hoặc chuỗi.
Ví dụ
Input: 34 → Output: It is an integer
Input: tutorialspoint → Output: It is a string
Phương pháp
- Đọc input vào chuỗi.
- Thử parse thành double (sscanf). Nếu thành công:
- Ép sang int, so sánh với giá trị double.
- Nếu khác nhau (có phần thập phân) → float.
- Ngược lại → integer.
- Nếu parse double thất bại → thử parse string.
Ví dụ minh họa (C++)
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
char input[50] = "";
double temp;
int intVal;
char stringVal[50] = "";
double val = 1e-12;
fgets(input, 100, stdin);
if (sscanf(input, "%lf", &temp) == 1) {
intVal = (int)temp;
if (fabs(temp - intVal) / temp > val)
printf("The input is a floating point\n");
else
printf("The input is an integer\n");
} else if (sscanf(input, "%s", stringVal) == 1)
printf("The input is a string\n");
else
printf("input not recognized\n");
return 0;
}
Đầu vào
452
Đầu ra
The input is an integer