C - How to check if the number is integer or float? [closed]

家住魔仙堡 提交于 2019-12-17 16:53:34

问题


Exercise 30
Write a program which reads float value developed as decimal extension and

  • If it's integer, it says that it's integer
  • on the other hand it rounds it to integer and writes the result.

Remember about data control

Here's the new one without this message about integer type.

#include <stdio.h>
#include <math.h>

int main(){
    double x;              //the argument of f(x)
    printf("Program demands x");
    printf("\nand writes the rounded value\n");
    printf("Author: xXx\n\n");
                          //loading data
    printf("Write x in float type in decimal extension  "); // after many tries, program is not rounding the value
    if (scanf("%lf",&x)!=1 || getchar()!='\n'){
        printf("Wrong data.\n");
        printf("\nEnd of program.\n");
        return 0;     
    }
    double round( double x );
    printf( "Rounded value is = %lf\n", x);
    printf("\nEnd of program.\n");
    return 0;   
}

回答1:


I would suggest the following:

  1. Read the number into a floating point variable, val, say.
  2. Put the integer part of val into an int variable, truncated, say.
  3. Check whether or not val and truncated are equal.

The function might look like this:

bool isInteger(double val)
{
    int truncated = (int)val;
    return (val == truncated);
}

You will likely want to add some sanity checking in case val is outside the range of values that can be stored in an int.

Note that I am assuming that you want to use a mathematician's definition for an integer. For example, this code would regard "0.0" as specifying an integer.




回答2:


Keep it simple:

  1. Read input as a string to a buffer fgets(buffer, BUFFER_SIZE, stdin);

  2. Use sscanf to try reading integer:

    int i, r, n;
    r = sscanf(buffer, "%d%n", &i, &n);
    if(r == 1 && n == strlen(buffer)) {
        // is integer
    }
    

    Extra length check here is to make sure that all characters are evaluated, and number like 12.3 won't be accepted as 12.

  3. If previous step failed, try reading floating point:

    double dbl;
    r = sscanf(buffer, "%lf", &dbl);
    if(r == 1) {
            // is double
    }
    



回答3:


#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
    char buffer[128], *p = buffer; 
    int   isint;
    fgets(buffer, sizeof buffer, stdin);
    if (p[0] == '+' || p[0] == '-')
      p++;
    isint = strlen(p) - 1;
    for (; *p && *p != '\n' && isint; p++) {
        isint = isdigit(*p);
    }   
    if (isint)
        printf("Woaaa\n");
    return 0;
}

And slightly cleaner version based on comparing the input string and the string created using the scanned integer:

#include <stdio.h>
#include <string.h>
int main() {
    char buffer[128], tostr[128];
    int d;
    fgets(buffer, sizeof buffer, stdin);
    buffer[strlen(buffer) - 1 ] = 0;
    sscanf(buffer, "%d", &d);
    sprintf(tostr, "%d", d); 
    if (!strcmp(buffer, tostr)) {
        printf("Woa\n");
    }
    return 0;
}



回答4:


I would suggest that you can get the input by string and check whether it is float or integer.

for example:

#define IS_FLOAT = 1;
#define IS_INT = 2;
int main()
{
    char tempString[20];

    scanf("%s", tempString);

    if(checkType(tempString)==IS_INT)
        printf("This is integer\n");    
    else if(checkType(tempString)==IS_FLOAT)
        printf("This is Float");
    else
        printf("undifined");
}

int checkType(char *input)
{
    short int isInt=0;//we use int as a boolean value;
    short int isFloat=0;
    short int isUndifined=0;
    int count;

    for(count = 0 ; input[count ]!='\0'; count++)
    {
        if(isdigit(input[count]))//you should include ctype.h at the beginning of this program
            isInt=1;
        else if(input[count] == '.')
            isFloat=1;
        else
            return -1;//some character that neither int nor '.' char.
    }
    if(isInt == 1 && isFloat ==1)
        return IS_FLOAT;
    else if(isInt == 1 && isFloat ==0)
        return IS_INT;
    else
        return -1;//illegal format
}


来源:https://stackoverflow.com/questions/20068234/c-how-to-check-if-the-number-is-integer-or-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!