Prog1  v0.1
Libreria didattica per il corso di Programmazione 1, Corso di Laurea in Matematica
 Tutto File Funzioni Ridefinizioni di tipo (typedef)
IO.h
Vai alla documentazione di questo file.
1 
7 //
8 // IO.h
9 // Prog1
10 //
11 // Created by Vincenzo Marra on 16/03/14.
12 // Copyright (c) 2014 Vincenzo Marra. All rights reserved.
13 //
14 
15 #ifndef Prog1_IO_h
16 #define Prog1_IO_h
17 
18 #include "Stringhe.h"
19 #include <stdlib.h>
20 #include <string.h>
21 
32 int leggi_int(String msg)
33 {
34  String buf;
35  char *p;
36  int i=0;
37  short valid=1;
38 
39  printf("%s", msg);
40 
41  do
42  {
43  if (fgets(buf, sizeof(buf), stdin) != NULL)
44  {
45  i = (int) strtol(buf, &p, 10);
46 
47  if (!(buf[0] != '\n' && (*p == '\n' || *p == '\0')))
48  {
49  printf ("Il dato inserito non e' un intero. Reinserire.\n");
50  valid=0;
51  }
52  else
53  valid=1;
54  }
55  }
56  while (!valid);
57  return i;
58 }
59 
70 double leggi_double(String msg)
71 {
72  String buf;
73  double d=0;
74  short valid=1;
75 
76  printf("%s", msg);
77 
78  do
79  {
80  if (fgets(buf, sizeof(buf), stdin) != NULL)
81  {
82  d = strtod(buf, NULL);
83 
84  if ( strtod(buf, NULL) == 0 && (buf[0] != '0' && (buf[0] != '\0' && buf[0] != '-' && buf[1] != '0')) )
85 
86  {
87  printf ("Il dato inserito non e' un double. Reinserire.\n");
88  valid=0;
89  }
90  else
91  valid=1;
92  }
93  }
94  while (!valid);
95  return d;
96 }
97 
106 char leggi_car(void)
107 {
108  String s;
109 
110  if ( (fgets(s, BUFSIZ, stdin) != NULL) && (s != NULL) )
111  {
112  return *s;
113  }
114  return 0;
115 }
116 
128 int leggi_str(String msg, String letta)
129 {
130  char *p;
131 
132  printf("%s", msg);
133 
134  if (fgets(letta, BUFSIZ, stdin) != NULL)
135  {
136  if ((p = strchr(letta, '\n')) != NULL)
137  *p = '\0';
138  return 1;
139  }
140  return 0;
141 }
142 
143 #endif
144 
int leggi_str(String msg, String letta)
Legge una riga da terminale.
Definition: IO.h:128
double leggi_double(String msg)
Legge un reale da terminale.
Definition: IO.h:70
char leggi_car(void)
Legge un carattere da terminale.
Definition: IO.h:106
int leggi_int(String msg)
Legge un intero da terminale.
Definition: IO.h:32
Contiene funzioni di utilità per elaborare stringhe.
char String[BUFSIZ]
Definisce il String.
Definition: Stringhe.h:23