473,566 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ 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(FIL E* 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(FIL E* 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="+9999999999 99999999999+9" -> readint(fp)= INT_MAX, fp="+9"
* fp="-999999999999999 999999+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)=898 9 , 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_R ANGE, 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 1672

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

Similar topics

8
1701
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
3256
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 'sizeof(char)'. It seems that I could use bitset<8> to represent a byte in my code --- if you have a better suggestion, I welcome it --- but that...
100
6846
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 are discussing whether this newsgroup should focus on 100% ANSI C or simply topics related to the C language in the real world. There is a C...
20
404
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 <limits.h> #define P printf #define R return
0
1204
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 scratch either as an individual or in a team. Entries must be developed in Python, during the challenge, and must incorporate some theme chosen at the...
0
1228
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 scratch either as an individual or in a team. Entries must be developed in Python, during the challenge, and must incorporate some theme chosen at the...
47
5902
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 site at http://ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html. The approach is very instructive. I know that I could do much of this stuff with e.g....
6
16695
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 me...and this is my pgm: import java.io.*; import keyboard.*; public class test { public static void main(String args)
3
1460
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 early stage and thus set to be continuously improved. I would be especially interested in your comments and feedbacks about this initiative and...
1
13216
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
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8109
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.