Is it good the use of va_arg?
____________________
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
unsigned
gcd_my(unsigned a, unsigned b)
{unsigned t;
if(a==0 || b==0) return 0;
while(a!=0)
{if( a<b )
{t = a; a = b; b = t;}
a = a%b;
}
return b;
}
int domanda(char* a, char* fmt, ... );
int main(void)
{
int first, second, nread;
char input[9];
printf
("This program will figure out the greatest common\n"
"divisor in two numbers you give.\n");
nread= domanda("Please enter the numbers> ",
"%d%d", &first, &second) ;
if(!nread)
{
printf("You don't want to play? Ok.\n");
exit(0);
}
printf( "first=%d, second=%d\n", first, second );
first = first >=0 ? first : -first ;
second = second>=0 ? second: -second;
printf("The GCD is %d\n", (int) gcd_my(first, second));
exit(0);
}
size_t fgetm(char* s, size_t n, FILE* iop)
{
int c;
char *cs;
if(!s) return 0;
else if( n==0 || iop==0 )
{*s = 0; return 0;}
cs=s;
while(--n>0 && (c = getc(iop))!=EOF)
if((*cs++ = c)=='\n') break;
*cs = '\0';
return (cs - s);
}
unsigned conta(char* a)
{
unsigned num=0;
if(!a) return 0;
while(*a)
{if(*a=='%')
if(a[1]=='*' || a[1]=='%') ;
else ++num;
++a;
}
return num;
}
int domanda(char* a, char* fmt, ... )
{va_list ap;
int h;
size_t j;
char buffer[258];
ricomincia:
printf("%s", a); fflush(stdout);
if((j = fgetm(buffer, 256, stdin))==0)
return 0; /* entra solo EOF*/
if(buffer[j-1]!='\n')
{fprintf(stderr, "Hai sforato la linea di 256 caratteri\n"
"PS: Si esce se tutte le conversioni sono riuscite\n"
"Oppure se EOF (^z o ^d) e' premuto\n");
while( j = fgetm(buffer, 256, stdin), j!=0 && buffer[j-1]!='\n');
if(feof(stdin)) return 0;
goto ricomincia;
}
j=conta(fmt);
va_start(ap, fmt);
errno = 0;
h=(vsscanf( buffer, fmt, ap)==j);
va_end(ap);
if(h && errno==0) return 1;
if(!errno)
fprintf(stderr, "Errore: numero di argomenti insufficienti\n");
else perror("Errore");
goto ricomincia;
} 2 1336
/* escuse me */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
unsigned
gcd_my(unsigned a, unsigned b)
{unsigned t;
if(a==0 || b==0) return 0;
while(a!=0)
{if(a < b)
{t = a; a = b; b = t;}
a = a%b;
}
return b;
}
int domanda(char* a, char* fmt, ... );
int main(void)
{
int first, second, nread;
char input[9];
printf
("This program will figure out the greatest common\n"
"divisor in two numbers you give.\n");
nread= domanda("Please enter the numbers> ",
"%d%d", &first, &second) ;
if(!nread)
{
printf("You don't want to play? Ok.\n");
exit(0);
}
printf( "first=%d, second=%d\n", first, second );
first = first>=0 ? first: -first;
second = second>=0 ? second: -second;
printf("The GCD is %d\n", (int) gcd_my(first, second));
exit(0);
}
size_t fgetm(char* s, size_t n, FILE* iop)
{
int c;
char *cs;
if(!s) return 0;
else if( n==0 || iop==0 )
{*s = 0; return 0;}
cs = s;
while(--n>0 && (c = getc(iop))!=EOF)
if((*cs++ = c)=='\n') break;
*cs = '\0';
return (cs - s);
}
unsigned conta(char* a)
{
unsigned num = 0;
if(!a) return 0;
while(*a)
{if(*a=='%')
if(a[1]=='*' || a[1]=='%') ;
else ++num;
++a;
}
return num;
}
int domanda(char* a, char* fmt, ... )
{va_list ap;
int h;
size_t j;
char buffer[258];
ricomincia:
printf("%s", a); fflush(stdout);
if((j = fgetm(buffer, 256, stdin))==0)
return 0; /* entra solo EOF*/
if(buffer[j-1]!='\n' && !feof(stdin) )
{fprintf(stderr, "Hai sforato la linea di 256 caratteri\n"
"PS: Si esce se tutte le conversioni sono riuscite\n"
"Oppure se EOF (^z o ^d) e' premuto\n");
while( j = fgetm(buffer, 256, stdin), j!=0 && buffer[j-1]!='\n');
if(feof(stdin)) return 0;
goto ricomincia;
}
j=conta(fmt);
va_start(ap, fmt);
errno = 0;
h=(vsscanf( buffer, fmt, ap)==j);
va_end(ap);
if(h && errno==0) return 1;
if(!errno)
fprintf(stderr, "Errore: numero di argomenti insufficienti\n");
else perror("Errore");
if(feof(stdin)) return 0;
goto ricomincia;
}
/* Is it legible? */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
unsigned
gcd_my(unsigned a, unsigned b)
{
unsigned t;
if(a==0 || b==0)
return 0;
while(a!=0)
{if(a < b)
{t = a; a = b; b = t;}
a = a%b;
}
return b;
}
int domanda(char* a, char* fmt, ... );
int main(void)
{
int first, second, nread;
char input[9];
printf
("This program will figure out the greatest common\n"
"divisor in two numbers you give.\n");
nread = domanda("Please enter the numbers> ",
"%d%d", &first, &second) ;
if(!nread)
{
printf("You don't want to play? Ok.\n");
exit(0);
}
printf( "first=%d, second=%d\n", first, second );
first = first>=0 ? first: -first;
second = second>=0 ? second: -second;
printf("The GCD is %d\n", (int) gcd_my(first, second));
exit(0);
}
size_t fgetm(char* s, size_t n, FILE* iop)
{
int c;
char *cs;
if(!s) return 0;
else if( n==0 || iop==0 || feof(iop) )
{*s = 0; return 0;}
cs = s;
while(--n>0 && (c = getc(iop))!=EOF)
if((*cs++ = c)=='\n') break;
*cs = '\0';
return (cs - s);
}
unsigned conta(char* a)
{
unsigned num = 0;
if(!a) return 0;
while(*a)
{
if(*a=='%')
{
if(a[1]=='*' || a[1]=='%') ;
else ++num;
}
++a;
}
return num;
}
int domanda(char* a, char* fmt, ... )
{
va_list ap;
int h;
size_t j;
char buffer[258];
ricomincia:
if(feof(stdin))
return 0;
printf("%s", a); fflush(stdout);
if((j = fgetm(buffer, 256, stdin))==0)
return 0; /* entra solo EOF*/
if(buffer[j-1]!='\n' && !feof(stdin) )
{
fprintf(stderr, "Hai sforato la linea di 256 caratteri\n"
"PS: Si esce se tutte le conversioni sono riuscite\n"
"Oppure se EOF (^z o ^d) e' premuto\n");
while( j = fgetm(buffer, 256, stdin), j!=0 && buffer[j-1]!='\n');
if(feof(stdin)) return 0;
goto ricomincia;
}
j=conta(fmt);
va_start(ap, fmt);
errno = 0;
h=(vsscanf( buffer, fmt, ap)==j);
va_end(ap);
if(h && errno==0) return 1;
if(!errno)
fprintf(stderr, "Errore: numero di argomenti insufficienti\n");
else perror("Errore");
if(feof(stdin)) return 0;
goto ricomincia;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ka |
last post by:
I install a mysql server in default installation with latin charset, but
I want to use GBK(a chinese charset), so that I can store and
search chinese words directly.
so, I download, unpack and...
|
by: RoSsIaCrIiLoIA |
last post by:
On Wed, 05 May 2004, CBFalconer <cbfalconer@yahoo.com> wrote:
>Even if sscanf succeeds in inputting various fields, there are
>probably range and other validity checks to be applied. The...
|
by: Ray Mitchell |
last post by:
Hello,
I realize that the source code for vsscanf is available from several
sources, such as GNU. However, all such source code I've found so far is
filled with cryptic (to me) #ifdefs, system...
|
by: Ray Mitchell |
last post by:
Hello,
I realize that the source code for vsscanf is available from several
sources, such as GNU. However, all such source code I've found so far is
filled with cryptic (to me) #ifdefs, system...
|
by: smshahriar |
last post by:
Hi,
I want to scan from the following string all the hex numbers and
populate an array of integers:
0x27 0x00
0x30 0x00
0x33 0x00
0x36 0x00
|
by: Christopher Benson-Manica |
last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.)
strchr() is to strrchr() as strstr() is to strrstr(), but strrstr()
isn't part of the standard. Why not?
--...
|
by: Urs Beeli |
last post by:
I have a question regarding errno. If I understand it correctly,
including <errno.h> allows me to check "errno" for error values
that some standard library functions may set.
This brings up some...
|
by: Gaijinco |
last post by:
I have always used istringstream for inputting data from a string.
But I heard someone talking about vsscanf()
However from what I can find in the internet is not quite clear if is
standard or...
|
by: alex |
last post by:
I've converted a latin1 database I have to utf8. The process has been:
# mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore
--skip-set-charset mydb mydb.sql
# iconv -f...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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,...
|
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...
| |