|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - downloadIndex: ┃ T n ┃
Length: 3064 (0xbf8) Types: TextFile Names: »number.c«
└─⟦87ddcff64⟧ Bits:30001253 CPHDIST85 Tape, 1985 Autumn Conference Copenhagen └─ ⟦this⟧ »cph85dist/stat/src/number.c«
/* Copyright (c) 1982 Gary Perlman (see Copyright file) */ /* number: report if a string is a UNIX formatted number usage: number (string) char *string; notes: a number in UNIX is one that can be converted from a string to an integer or real with no loss of information due to bad format all numbers can be surrounded by whitespace an integer has an optional minus sign, followed by digits (a decimal point optinally followed by zeros is forbidden) a real number has an optional minus sign followed by didits parameters: char *string; string is tested to see if it is a number side effects: none value: 1 is string is an integer [-] 0-9+ 2 is string is a real number 0 for non-numbers compilation flags: -DSTANDALONE includes test main program trace flags: none deficiencies: does not allow optional plus sign because atof(3) does not does not check to see if significant digits will be ignored author: Gary Perlman date: Wed May 22 13:30:40 EDT 1985 */ #include <ctype.h> static char sccsfid[] = "@(#) number.c 5.1 (unix|stat) 6/15/85"; #define IS_NOT 0 #define IS_INT 1 #define IS_REAL 2 /*LINTLIBRARY*/ number (string) char *string; /* the string to be tested */ { int answer = IS_INT; /* start by assuming it is an integer */ int before = 0; /* anything before the decimal? */ int after = 0; /* anything after the decimal? */ while (isspace (*string)) /* skip over blank space */ string++; if (!*string) /* empty string not allowed */ return (IS_NOT); if (*string == '-') /* optional plus not allowed by atof */ { string++; if (!isdigit (*string) && *string != '.') return (IS_NOT); } if (isdigit (*string)) /* note that there was a digit before . */ { before = 1; while (isdigit (*string)) string++; } if (*string == '.') /* found a decimal point, parse for real */ { answer = IS_REAL; string++; if (isdigit (*string)) /* note that there was a digit after . */ { after = 1; while (isdigit (*string)) string++; } } if (!before && !after) /* must be digit before or after . */ return (IS_NOT); if (*string == 'E' || *string == 'e') /* exponent */ { answer = IS_REAL; string++; if (*string == '+' || *string == '-') /* optional sign */ string++; if (!isdigit (*string)) /* missing exponent */ return (IS_NOT); while (isdigit (*string)) string++; } while (isspace (*string)) /* skip optional spaces */ string++; /* should now have exhausted the input string */ return (*string == '\0' ? answer : IS_NOT); } #ifdef STANDALONE #include <stdio.h> main () { char line[BUFSIZ]; while (gets (line)) puts ("Type in strings and I'll tell you if they are numbers"); { switch (number (line)) { case IS_NOT: puts (" is not a number"); break; case IS_INT: puts (" is an integer"); break; case IS_REAL: puts (" is a real number"); break; } } } #endif