-
Notifications
You must be signed in to change notification settings - Fork 0
/
7-4.c
55 lines (52 loc) · 1.04 KB
/
7-4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "7-4-getch.h"
void minscanf(char* fmt, ...);
void minscanf(char* fmt, ...)
{
va_list ap;
char buf[1024];
char* p, * bufp = buf;
va_start(ap, fmt);
for (p = fmt; *p; ++p)
{
if (*p != '%')
{
if (getchar() != *p)
break;
continue;
}
switch (*++p)
{
case 'd':
while (isdigit(*bufp++ = getch()))
;
ungetch(*bufp);
*bufp = '\0';
*va_arg(ap, int*) = atoi(bufp);
bufp = buf;
break;
case 'f':
while (isdigit(*bufp++ = getchar()))
;
if (*bufp == '.')
while (isdigit(*++bufp = getchar()))
;
ungetch(*bufp);
*bufp = '\0';
*va_arg(ap, double*) = atof(bufp);
bufp = buf;
break;
case 's':
bufp = va_arg(ap, char*);
while (!isspace(*bufp++ = getchar()))
;
ungetch(*bufp);
*bufp = '\0';
bufp = buf;
break;
}
}
va_end(ap);
}