Skip to content

Commit

Permalink
Exerc 7_4.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsm-lisper committed Jul 24, 2024
1 parent 5b0bcf5 commit 23a1842
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chapter_7.input_output/7_4.minscanf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BINARY=minscanf

include ../../Exercise.mk
29 changes: 29 additions & 0 deletions chapter_7.input_output/7_4.minscanf/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# include <stdio.h>
# include "minscanf.h"

int main ()
{
int r;
char type;

if ((r = minscanf("type: %c", &type)) == EOF || r != 1) {
printf("Error reading type. Format:\ntype t v0 v1 ... vn\nt - d: decimal int, f: float\n");
return 0;
}
if (type == 'f') {
float v;
double sum = 0;
while ((r = minscanf("%f", &v)) != EOF && r == 1)
printf("\t%.2f\n", sum += v);
}
else if (type == 'd') {
int v;
long int sum = 0;
while ((r = minscanf("%d", &v)) != EOF && r == 1)
printf("\t%ld\n", sum += v);
}
else
printf("ERROR! Unrecognized type %c.\n", type);

return 0;
}
56 changes: 56 additions & 0 deletions chapter_7.input_output/7_4.minscanf/minscanf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# include <stdio.h>
# include <stdarg.h>

/* minscanf: minimal scanf with variable argument list */
int minscanf (char *fmt, ...)
{
va_list ap; /* points to each annamed arg in turn */
char *p;
int c, sr, r = 0;

va_start(ap, fmt); /* make ap point to 1st unnamed arg */
for (p = fmt; *p; p++) {
if (*p != '%') {
if ((c = getchar()) == EOF) {
va_end(ap);
return EOF;
}
else if (c != *p) {
va_end(ap);
return r;
}
}
else {
switch (*++p) {
case 'o': sr = scanf("%o", va_arg(ap, int*)); break;
case 'd': sr = scanf("%d", va_arg(ap, int*)); break;
case 'u': sr = scanf("%u", va_arg(ap, unsigned int*)); break;
case 'e':
case 'f':
case 'g': sr = scanf("%f", va_arg(ap, float*)); break;
case 'c': sr = scanf("%c", va_arg(ap, char*)); break;
case '%':
if (c == EOF) {
va_end(ap);
return EOF;
}
else if (c != '%') {
va_end(ap);
return r;
}
break;
default:
printf("ERROR! Unsupported conversion %%%c.\n", *p);
va_end(ap);
return EOF;
}
if (sr == EOF) {
va_end(ap);
return EOF;
}
r++;
}
}
va_end(ap); /* clean up when done */
return r;
}
7 changes: 7 additions & 0 deletions chapter_7.input_output/7_4.minscanf/minscanf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ifndef MINSCANF_H
# define MINSCANF_H

/* minscanf: minimal scanf with variable argument list */
int minscanf (char *fmt, ...);

# endif
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 3
3 changes: 3 additions & 0 deletions chapter_7.input_output/7_4.minscanf/tests/00_err_no_type.tout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Error reading type. Format:
type t v0 v1 ... vn
t - d: decimal int, f: float
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type X 1 2 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Error reading type. Format:
type t v0 v1 ... vn
t - d: decimal int, f: float
1 change: 1 addition & 0 deletions chapter_7.input_output/7_4.minscanf/tests/1_d_2.tin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: d -100 101
2 changes: 2 additions & 0 deletions chapter_7.input_output/7_4.minscanf/tests/1_d_2.tout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-100
1
1 change: 1 addition & 0 deletions chapter_7.input_output/7_4.minscanf/tests/2_d_6.tin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: d -100 -10 1 12 123 1234567890
6 changes: 6 additions & 0 deletions chapter_7.input_output/7_4.minscanf/tests/2_d_6.tout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-100
-110
-109
-97
26
1234567916
1 change: 1 addition & 0 deletions chapter_7.input_output/7_4.minscanf/tests/3_f_2.tin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: f -100.5 101.05
2 changes: 2 additions & 0 deletions chapter_7.input_output/7_4.minscanf/tests/3_f_2.tout
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-100.50
0.55
1 change: 1 addition & 0 deletions chapter_7.input_output/7_4.minscanf/tests/4_f_6.tin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: f -.100 -.10 .1 .12 1.23 12.34567890
6 changes: 6 additions & 0 deletions chapter_7.input_output/7_4.minscanf/tests/4_f_6.tout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-0.10
-0.20
-0.10
0.02
1.25
13.60
2 changes: 2 additions & 0 deletions chapter_7.input_output/7_4.minscanf/xx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1.00
3.00

0 comments on commit 23a1842

Please sign in to comment.