简体   繁体   English

确定用户输入值的数据类型

[英]Determining Data Type Of User Entered Value

I was reading one of the application of "Unions" of creating mixed data types. 我正在阅读创建混合数据类型的“联合”应用程序之一。 Example

typedef union {
int x;
float y;
}mix;
mix arr[100];

Each element of array arr[100] can store either a int or a float type value. 数组arr[100]每个元素都可以存储intfloat类型的值。 But suppose I want to take a input from user and store it in arr[ ] and I won't know that either user is going to enter a float or an int value. 但是,假设我想从用户那里获取输入并将其存储在arr[ ] ,我将不知道任何一个用户都将要输入floatint值。 So I don't know which statement from the following I should choose to store that input from user. 因此,我不知道应该选择以下哪条语句来存储用户的输入。

1. scanf ("%d",&arr[0].x); 1. scanf ("%d",&arr[0].x);

OR 要么

2. scanf ("%f",&arr[0].y); 2. scanf ("%f",&arr[0].y);

And similar problem arises when I want to print that value. 当我要打印该值时,也会出现类似的问题。

I know I can solve this problem using a "tag field" . 我知道我可以使用“标签字段”解决此问题。 So I can do it as 所以我可以做到

#include <stdio.h>
typedef union {
int x;
float y;
}mix;
mix arr[100];

int main ()
{
int k; // tag field
puts("Mention 1 if you want to enter integer and 0 if you want to enter float value");
scanf ("%d",&k);
if (k)
{
scanf ("%d",&arr[0].x);
printf ("%d is the entered integer value\n",arr[0].x);
}
else
{
scanf ("%f",&arr[0].y);
printf ("%f is the entered float value\n",arr[0].y);
}

}

But here user is telling the type of data he is gonna enter(with the help of 0 or 1). 但是这里用户告诉他要输入的数据类型(借助0或1)。 I want to know: Is there any way in C language that compiler can automatically detect the type of data user is entering and according run either 1st or 2nd scanf statement without help of user? 我想知道:在C语言中,编译器是否可以通过任何方式自动检测用户输入的数据类型,并在没有用户帮助的情况下运行1st或2nd scanf语句? OR Is there any predefined function present in library for doing that? 或库中是否存在用于执行此操作的预定义函数?

Also tell if you have any another intresting way of doing this program. 还要告诉您是否还有其他有趣的方式来执行此程序。

This might help you I guess... 我猜这可能对您有帮助...

float num;
printf("Enter number\n");
scanf("%f",&num);
if((num - (int)num)== 0) //(int)num : Type casting
    printf("Entered number is of int type\n");
else
    printf("Entered number is of float type\n");

Determining Data Type Of User Entered Value 确定用户输入值的数据类型

Read as a line fgets() and the parse with strtol(), strtof() . 读取为fgets()并使用strtol(), strtof()进行解析。

Untested code, read comments. 未经测试的代码,请阅读注释。

puts("Mention 1 if you want to enter integer and 0 if you want to enter float value");
char buffer[100];
if (fgets(buffer, sizeof buffer, stdin)) {
  // OK we have an input line of text now as a string
  buffer[strcspn(buffer, "\n")] = '\0'; // lop off potential \n

  char *endptr;
  errno = 0;
  long lval = strtol(buffer, &endptr);
  // If conversion occurred and no text at the end ...
  if (endptr > buffer && *endptr == '\0') {
    // An integer type!
    if (errno == ERANGE) puts("Integer value out of range");
    printf("Integer value: %ld\n", lval);
  } else {
    errno = 0;
    float = strtof(buffer, &endptr);
    if (endptr > buffer && *endptr == '\0') {
      // A float
      if (errno == ERANGE) puts("float value out of range");
      printf("float value: %g\n", f);
    } else
      puts("Non-numeric input");
    }
  }

For a int mystrtoi(const char *str) , see ref code . 有关int mystrtoi(const char *str) ,请参见ref代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM