473,466 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ANSI C Challenge on readint

/* What do you say ? */
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <errno.h>

#define P printf
#define R return
#define W while
#define F for
#define is_digit(x) ('0'<=(x) && (x)<='9')

#define IERR_EOF 8u
#define IERR_UNGET 4u
#define IERR_RANGE 2u
#define IERR_NONUM 1u

unsigned ierrno = 0;

int read_int(FILE* fp);
int main( void )
{
int i, c;
char a[81]={0};

do {
P("Enter an int[1919 or EOF for end]>\n");
ierrno = 0;
i = read_int( stdin );
P( "%d==NUMBER\n", i );
if(ierrno & IERR_RANGE)
puts( "Out of range for int." );
if(ierrno & IERR_EOF)
puts( "Find EOF." );
if(ierrno & IERR_NONUM)
puts( "Nonum" );
if(ierrno & IERR_UNGET)
puts( "Unget" );
if( !feof(stdin) )
{P("Scan..."); fflush(stdout);
if(scanf("%80[^\n]", a)>0)
/*## in stdin there is '\n' ##*/
P("\"%s\"\n", a);
else P("NO out from scanf\n");
}
else P("Press eof\n");
}while(i!=1919 && !feof(stdin));
R 0;
}
/* #### int skip_spaces(FILE* fp) ####
* skip the spaces = ' ' and '\t' and '\n'
* Return EERR_EOF if it meets the eof of fp
* Return EERR_UNGET on error in the preserve the stream
* otherwise return 0.
* (ierrno & IERR_EOF ) !=0 if EOF is meet
* (ierrno & IERR_UNGET) !=0 if stream error (no ungetc)
*/
int skip_spaces(FILE* fp)
{int c;
if( feof(fp) )
R ( ierrno |= IERR_EOF );
W( (c = fgetc(fp))==' ' || c=='\t' || c=='\n') ;
R (c == EOF ? (ierrno |= IERR_EOF ):
ungetc(c, fp)==EOF ? (ierrno |= IERR_UNGET): 0
);
}
/* #### int readint(FILE* fp) #####
* It gets an integer from fp; it is allowed this input
* ({+}||{-}||{}) && ({.}||{}) && [0123456789] &&
* ( ({.}&&[0123456789])||{} )
* range=[ INT_MIN, INT_MAX ]
* on error it returns
* INT_MAX (number too big ;
* or not good input)
* or INT_MIN (a negative number too big)
*
* (ierrno & IERR_RANGE) !=0 if a range error
* (ierrno & IERR_NONUM) !=0 if number is not gets
* (ierrno & IERR_EOF ) !=0 if EOF is meet
* (ierrno & IERR_UNGET) !=0 if stream error in ungetc
* Examples:
* fp="+a+bbb" -> readint(fp)=INT_MAX, fp="+a+bbb"
* fp="++aabbb" -> readint(fp)=INT_MAX, fp="++aabbb"
* fp="+999999999999999999999+9" -> readint(fp)= INT_MAX, fp="+9"
* fp="-999999999999999999999+9" -> readint(fp)= INT_MIN, fp="+9"
* fp="+EOF" -> readint(fp)=INT_MAX, fp="+EOF"
* fp="add" -> readint(fp)=INT_MAX, fp="add"
* fp="+8989s"-> readint(fp)=8989 , fp="s"
* fp=".1231M" -> readint(fp)=0, fp="M"
* fp="-.8989s" -> readint(fp)=-1 , fp="s"
* fp="23.1231M" -> readint(fp)=23, fp="M"
* fp="-23.8989s" -> readint(fp)=-24,fp="s"
*/
int readint(FILE* fp)
{int c, sign = +1;
unsigned n, u = 'a', led=0;

assert(fp != NULL);
if( feof(fp) )
{ierrno |= (IERR_EOF|IERR_NONUM) ; R INT_MAX;}

if((c = fgetc(fp))=='+') u = '+';
else if(c == '-') {u = '-'; sign = -1;}
else if(c == '.')
{
thePoint: /*### case of ".else" exit ###*/
if( sign = fgetc(fp), is_digit(sign) )
{ /*## case .number return 0 or -1 ##*/
if(sign != '0') led = 1;
W( sign = fgetc(fp), is_digit(sign))
if(sign != '0') led = 1;
if(sign != EOF)
{if(ungetc(sign, fp) == EOF)
ierrno |= IERR_UNGET;
}
else ierrno |= IERR_EOF;
R (u=='-' && led) ? -1: 0;
}
else { /*## case ".no_num" exit ##*/
if(sign == EOF) clearerr(fp);
else if(ungetc( sign, fp) == EOF)
goto label1;
if( ungetc('.', fp) == EOF )
{ /*## A waste stream ##*/
label1:
ierrno |= IERR_UNGET|IERR_NONUM;
R INT_MAX;
}
if( u!='a' && ungetc(u, fp)==EOF )
goto label1;
ierrno |= IERR_NONUM;
R INT_MAX;
}
}
else if(!is_digit(c) )
{ /* "no_num" --> exit */
if(c != EOF)
{ if( ungetc(c, fp)==EOF ) goto label1; }
else ierrno |= IERR_EOF;
ierrno |= IERR_NONUM;
R INT_MAX;
}

if( !is_digit(c) && (c = fgetc(fp), !is_digit(c)) )
{ /*## case fp="+else" or fp="-else" ##*/
if(c == '.')
goto thePoint;
/*## fp="+no_num"||"-no_num" ##*/
if(c == EOF) clearerr( fp );
else if( ungetc(c, fp) == EOF )
goto label1;
if( ungetc(u, fp) == EOF ) /*## u=='+' || u=='-'##*/
goto label1;
ierrno |= IERR_NONUM;
R INT_MAX;
}

n = 0; /*## here c would be digit ##*/
if(sign>0) u = INT_MAX;
else {u = -(INT_MIN + 1); u += 1;}

W( 1 )
{
if( n > ( u - ( c - '0' ))/10 )
{
W( c = fgetc(fp), is_digit(c) );
n = u; ierrno |= IERR_RANGE;
break;
}
n = 10*n + (c - '0');
if(c = fgetc(fp), !is_digit(c))
break;
}
if(c == '.') /*# number.else#*/
{ /*#I'm here ^ #*/
if( c = fgetc(fp), !is_digit(c) )
{
if(c == EOF) clearerr(fp);
else if(ungetc( c, fp) == EOF)
ierrno |= IERR_UNGET;
if( !(ierrno & IERR_UNGET) &&
ungetc('.', fp) == EOF )
ierrno |= IERR_UNGET;
R sign>=0 ? n:
n==u ? INT_MIN: (-1)*( (int)n ) ;
}
}
if(is_digit(c)) /*## it jumps .numbers ##*/
{
if(c != '0') led = 1;
W( c = fgetc(fp), is_digit(c))
if(c != '0') led = 1;
}
if(c != EOF)
{
if(ungetc(c, fp) == EOF)
ierrno |= IERR_UNGET;
}
else ierrno |= IERR_EOF;
R sign>=0 ? n:
n==u && led ? (ierrno|=IERR_RANGE, INT_MIN):
n==u ? INT_MIN:
led ? (-1)*( (int)n ) - 1: (-1) * ( (int)n );
}
/* #### int read_int(FILE* fp) #####
* It gets an integer from fp; it is allowed this input
* ([' \t\n']||{}) && ({+}||{-}||{}) && ({.}||{}) && [0123456789] &&
* ( ({.}&&[0123456789])||{} )
* range=[ INT_MIN, INT_MAX ]
* on error it returns
* INT_MAX (number too big;
* or not good input)
* or INT_MIN (a negative number too big)
*
* (ierrno & IERR_RANGE) !=0 if a range error
* (ierrno & IERR_NONUM) !=0 if number is not gets
* (ierrno & IERR_EOF ) !=0 if EOF is meet
* (ierrno & IERR_UNGET) !=0 if stream error in ungetc
*/
int read_int(FILE* fp)
{
skip_spaces(fp);
if(ierrno & IERR_UNGET) /* stream problem */
{ierrno |= IERR_NONUM; R INT_MAX;}
R readint(fp);
}

Nov 14 '05 #1
0 1664

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Frank Buss | last post by:
A new challenge: http://www.frank-buss.de/marsrescue/index.html Have fun! Now you can win real prices. -- Frank Buß, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
100
by: Roose | last post by:
Just to make a tangential point here, in case anyone new to C doesn't understand what all these flame wars are about. Shorthand title: "My boss would fire me if I wrote 100% ANSI C code" We...
20
by: RoSsIaCrIiLoIA | last post by:
I a poor beginner defy this NG to write an ANSI-C portable int readint(FILE* fp); better of my in the treatment of a stream ______________ #include <stdio.h> #include <assert.h> #include ...
0
by: Richard Jones | last post by:
The date for the second PyWeek challenge has been set: Sunday 26th March to Sunday 2nd April (00:00UTC to 00:00UTC). The PyWeek challenge invites entrants to write a game in one week from...
0
by: richard | last post by:
The date for the second PyWeek challenge has been set: Sunday 26th March to Sunday 2nd April (00:00UTC to 00:00UTC). The PyWeek challenge invites entrants to write a game in one week from...
47
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web...
6
kaleeswaran
by: kaleeswaran | last post by:
hi! i tried to get the input through keyboard using keyboard.readInt() method but after compilation error shows "variable keyboard undefined" i don't know how to solve the pbm..plzz tell...
3
by: Thierry | last post by:
For those interested in <b>programming riddles</b>, I would like to announce a new programming challenge I'm just launching at http://software.challenge.googlepages.com This challenge is in its...
1
by: Techno3000 | last post by:
import java.io.*; import java.awt.*; import hsa.Console; public class Calculator { static Console c; public static void main (String args) throws IOException
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.