473,698 Members | 2,022 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 1681

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

Similar topics

8
1704
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
3288
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 still leaves me with the question of how to write those bitsets to an image file as big-endian bytes...
100
6977
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 standard which is defined by an international committee. People who write compilers refer to this in...
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
1208
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 start of the challenge. REGISTRATION IS NOT YET OPEN --
0
1231
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 start of the challenge. REGISTRATION IS NOW OPEN --
47
5943
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. C++, but the intellectual challenge of implementing these concepts with pure ANSI C is relevant to...
6
16793
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
1467
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 its relevance.
1
13229
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
8603
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8893
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7723
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3045
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
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.