fork download
  1. /****************************************************************************
  2.  * cs50.c
  3.  *
  4.  * version 1.1.6
  5.  *
  6.  * Computer Science 50
  7.  * Glenn Holloway
  8.  * David J. Malan
  9.  *
  10.  * Definitions for the CS50 Library.
  11.  * Based on Eric Roberts' genlib.c and simpio.c.
  12.  *
  13.  * The latest version of this file can be found at
  14.  * http://w...content-available-to-author-only...0.net/pub/releases/cs50/cs50.c.
  15.  *
  16.  * To compile as a static library on your own system:
  17.  * % gcc -c -ggdb -std=c99 cs50.c -o cs50.o
  18.  * % ar rcs libcs50.a cs50.o
  19.  * % rm -f cs50.o
  20.  * % cp cs50.h /usr/local/include
  21.  * % cp libcs50.a /usr/local/lib
  22.  ***************************************************************************/
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #include "cs50.h"
  29.  
  30.  
  31. /*
  32.  * Default capacity of buffer for standard input.
  33.  */
  34.  
  35. #define CAPACITY 128
  36.  
  37.  
  38. /*
  39.  * Reads a line of text from standard input and returns the equivalent
  40.  * char; if text does not represent a char, user is prompted to retry.
  41.  * Leading and trailing whitespace is ignored. If line can't be read,
  42.  * returns CHAR_MAX.
  43.  */
  44.  
  45. char
  46. GetChar(void)
  47. {
  48. // try to get a char from user
  49. while (true)
  50. {
  51. // get line of text, returning CHAR_MAX on failure
  52. string line = GetString();
  53. if (line == NULL)
  54. return CHAR_MAX;
  55.  
  56. // return a char if only a char (possibly with
  57. // leading and/or trailing whitespace) was provided
  58. char c1, c2;
  59. if (sscanf(line, " %c %c", &c1, &c2) == 1)
  60. {
  61. free(line);
  62. return c1;
  63. }
  64. else
  65. {
  66. free(line);
  67. printf("Retry: ");
  68. }
  69. }
  70. }
  71.  
  72.  
  73. /*
  74.  * Reads a line of text from standard input and returns the equivalent
  75.  * double as precisely as possible; if text does not represent a
  76.  * double, user is prompted to retry. Leading and trailing whitespace
  77.  * is ignored. For simplicity, overflow and underflow are not detected.
  78.  * If line can't be read, returns DBL_MAX.
  79.  */
  80.  
  81. double
  82. GetDouble(void)
  83. {
  84. // try to get a double from user
  85. while (true)
  86. {
  87. // get line of text, returning DBL_MAX on failure
  88. string line = GetString();
  89. if (line == NULL)
  90. return DBL_MAX;
  91.  
  92. // return a double if only a double (possibly with
  93. // leading and/or trailing whitespace) was provided
  94. double d; char c;
  95. if (sscanf(line, " %lf %c", &d, &c) == 1)
  96. {
  97. free(line);
  98. return d;
  99. }
  100. else
  101. {
  102. free(line);
  103. printf("Retry: ");
  104. }
  105. }
  106. }
  107.  
  108.  
  109. /*
  110.  * Reads a line of text from standard input and returns the equivalent
  111.  * float as precisely as possible; if text does not represent a float,
  112.  * user is prompted to retry. Leading and trailing whitespace is ignored.
  113.  * For simplicity, overflow and underflow are not detected. If line can't
  114.  * be read, returns FLT_MAX.
  115.  */
  116.  
  117. float
  118. GetFloat(void)
  119. {
  120. // try to get a float from user
  121. while (true)
  122. {
  123. // get line of text, returning FLT_MAX on failure
  124. string line = GetString();
  125. if (line == NULL)
  126. return FLT_MAX;
  127.  
  128. // return a float if only a float (possibly with
  129. // leading and/or trailing whitespace) was provided
  130. char c; float f;
  131. if (sscanf(line, " %f %c", &f, &c) == 1)
  132. {
  133. free(line);
  134. return f;
  135. }
  136. else
  137. {
  138. free(line);
  139. printf("Retry: ");
  140. }
  141. }
  142. }
  143.  
  144.  
  145. /*
  146.  * Reads a line of text from standard input and returns it as an
  147.  * int in the range of [-2^31 + 1, 2^31 - 2], if possible; if text
  148.  * does not represent such an int, user is prompted to retry. Leading
  149.  * and trailing whitespace is ignored. For simplicity, overflow is not
  150.  * detected. If line can't be read, returns INT_MAX.
  151.  */
  152.  
  153. int
  154. GetInt(void)
  155. {
  156. // try to get an int from user
  157. while (true)
  158. {
  159. // get line of text, returning INT_MAX on failure
  160. string line = GetString();
  161. if (line == NULL)
  162. return INT_MAX;
  163.  
  164. // return an int if only an int (possibly with
  165. // leading and/or trailing whitespace) was provided
  166. int n; char c;
  167. if (sscanf(line, " %d %c", &n, &c) == 1)
  168. {
  169. free(line);
  170. return n;
  171. }
  172. else
  173. {
  174. free(line);
  175. printf("Retry: ");
  176. }
  177. }
  178. }
  179.  
  180.  
  181. /*
  182.  * Reads a line of text from standard input and returns an equivalent
  183.  * long long in the range [-2^63 + 1, 2^63 - 2], if possible; if text
  184.  * does not represent such a long long, user is prompted to retry.
  185.  * Leading and trailing whitespace is ignored. For simplicity, overflow
  186.  * is not detected. If line can't be read, returns LLONG_MAX.
  187.  */
  188.  
  189. long long
  190. GetLongLong(void)
  191. {
  192. // try to get a long long from user
  193. while (true)
  194. {
  195. // get line of text, returning LLONG_MAX on failure
  196. string line = GetString();
  197. if (line == NULL)
  198. return LLONG_MAX;
  199.  
  200. // return a long long if only a long long (possibly with
  201. // leading and/or trailing whitespace) was provided
  202. long long n; char c;
  203. if (sscanf(line, " %lld %c", &n, &c) == 1)
  204. {
  205. free(line);
  206. return n;
  207. }
  208. else
  209. {
  210. free(line);
  211. printf("Retry: ");
  212. }
  213. }
  214. }
  215.  
  216.  
  217. /*
  218.  * Reads a line of text from standard input and returns it as a string,
  219.  * sans trailing newline character. (Ergo, if user inputs only "\n",
  220.  * returns "" not NULL.) Leading and trailing whitespace is not ignored.
  221.  * Returns NULL upon error or no input whatsoever (i.e., just EOF).
  222.  */
  223.  
  224. string
  225. GetString(void)
  226. {
  227. // growable buffer for chars
  228. string buffer = NULL;
  229.  
  230. // capacity of buffer
  231. unsigned int capacity = 0;
  232.  
  233. // number of chars actually in buffer
  234. unsigned int n = 0;
  235.  
  236. // character read or EOF
  237. int c;
  238.  
  239. // iteratively get chars from standard input
  240. while ((c = fgetc(stdin)) != '\n' && c != EOF)
  241. {
  242. // grow buffer if necessary
  243. if (n + 1 > capacity)
  244. {
  245. // determine new capacity: start at CAPACITY then double
  246. if (capacity == 0)
  247. capacity = CAPACITY;
  248. else if (capacity <= (UINT_MAX / 2))
  249. capacity *= 2;
  250. else
  251. {
  252. free(buffer);
  253. return NULL;
  254. }
  255.  
  256. // extend buffer's capacity
  257. string temp = realloc(buffer, capacity * sizeof(char));
  258. if (temp == NULL)
  259. {
  260. free(buffer);
  261. return NULL;
  262. }
  263. buffer = temp;
  264. }
  265.  
  266. // append current character to buffer
  267. buffer[n++] = c;
  268. }
  269.  
  270. // return NULL if user provided no input
  271. if (n == 0 && c == EOF)
  272. return NULL;
  273.  
  274. // minimize buffer
  275. string minimal = malloc((n + 1) * sizeof(char));
  276. strncpy(minimal, buffer, n);
  277. free(buffer);
  278.  
  279. // terminate string
  280. minimal[n] = '\0';
  281.  
  282. // return string
  283. return minimal;
  284. }
  285.  
  286. #include <stdio.h>
  287.  
  288.  
  289. int main(void) {
  290. // your code goes here
  291. return 0;
  292. }
  293.  
Success #stdin #stdout 0.01s 5396KB
stdin
W
stdout
Standard output is empty