/C-Codes/cs50.c

https://bitbucket.org/kernelwars/code-repository · C · 296 lines · 147 code · 35 blank · 114 comment · 40 complexity · 16f397d3a742ff46233257539b831218 MD5 · raw file

  1. /****************************************************************************
  2. * CS50 Library 3.1
  3. * https://manual.cs50.net/Library
  4. *
  5. * Based on Eric Roberts' genlib.c and simpio.c.
  6. *
  7. * Copyright (c) 2011,
  8. * Glenn Holloway <holloway@eecs.harvard.edu>
  9. * David J. Malan <malan@harvard.edu>
  10. * All rights reserved.
  11. *
  12. * BSD 3-Clause License
  13. * http://www.opensource.org/licenses/BSD-3-Clause
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are
  17. * met:
  18. *
  19. * * Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * * Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. * * Neither the name of CS50 nor the names of its contributors may be used
  25. * to endorse or promote products derived from this software without
  26. * specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  30. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  31. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  34. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  37. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. ***************************************************************************/
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include "cs50.h"
  44. /*
  45. * Reads a line of text from standard input and returns the equivalent
  46. * char; if text does not represent a char, user is prompted to retry.
  47. * Leading and trailing whitespace is ignored. If line can't be read,
  48. * returns CHAR_MAX.
  49. */
  50. char
  51. GetChar(void)
  52. {
  53. // try to get a char from user
  54. while (true)
  55. {
  56. // get line of text, returning CHAR_MAX on failure
  57. string line = GetString();
  58. if (line == NULL)
  59. return CHAR_MAX;
  60. // return a char if only a char (possibly with
  61. // leading and/or trailing whitespace) was provided
  62. char c1, c2;
  63. if (sscanf(line, " %c %c", &c1, &c2) == 1)
  64. {
  65. free(line);
  66. return c1;
  67. }
  68. else
  69. {
  70. free(line);
  71. printf("Retry: ");
  72. }
  73. }
  74. }
  75. /*
  76. * Reads a line of text from standard input and returns the equivalent
  77. * double as precisely as possible; if text does not represent a
  78. * double, user is prompted to retry. Leading and trailing whitespace
  79. * is ignored. For simplicity, overflow and underflow are not detected.
  80. * If line can't be read, returns DBL_MAX.
  81. */
  82. double
  83. GetDouble(void)
  84. {
  85. // try to get a double from user
  86. while (true)
  87. {
  88. // get line of text, returning DBL_MAX on failure
  89. string line = GetString();
  90. if (line == NULL)
  91. return DBL_MAX;
  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. * Reads a line of text from standard input and returns the equivalent
  109. * float as precisely as possible; if text does not represent a float,
  110. * user is prompted to retry. Leading and trailing whitespace is ignored.
  111. * For simplicity, overflow and underflow are not detected. If line can't
  112. * be read, returns FLT_MAX.
  113. */
  114. float
  115. GetFloat(void)
  116. {
  117. // try to get a float from user
  118. while (true)
  119. {
  120. // get line of text, returning FLT_MAX on failure
  121. string line = GetString();
  122. if (line == NULL)
  123. return FLT_MAX;
  124. // return a float if only a float (possibly with
  125. // leading and/or trailing whitespace) was provided
  126. char c; float f;
  127. if (sscanf(line, " %f %c", &f, &c) == 1)
  128. {
  129. free(line);
  130. return f;
  131. }
  132. else
  133. {
  134. free(line);
  135. printf("Retry: ");
  136. }
  137. }
  138. }
  139. /*
  140. * Reads a line of text from standard input and returns it as an
  141. * int in the range of [-2^31 + 1, 2^31 - 2], if possible; if text
  142. * does not represent such an int, user is prompted to retry. Leading
  143. * and trailing whitespace is ignored. For simplicity, overflow is not
  144. * detected. If line can't be read, returns INT_MAX.
  145. */
  146. int
  147. GetInt(void)
  148. {
  149. // try to get an int from user
  150. while (true)
  151. {
  152. // get line of text, returning INT_MAX on failure
  153. string line = GetString();
  154. if (line == NULL)
  155. return INT_MAX;
  156. // return an int if only an int (possibly with
  157. // leading and/or trailing whitespace) was provided
  158. int n; char c;
  159. if (sscanf(line, " %d %c", &n, &c) == 1)
  160. {
  161. free(line);
  162. return n;
  163. }
  164. else
  165. {
  166. free(line);
  167. printf("Retry: ");
  168. }
  169. }
  170. }
  171. /*
  172. * Reads a line of text from standard input and returns an equivalent
  173. * long long in the range [-2^63 + 1, 2^63 - 2], if possible; if text
  174. * does not represent such a long long, user is prompted to retry.
  175. * Leading and trailing whitespace is ignored. For simplicity, overflow
  176. * is not detected. If line can't be read, returns LLONG_MAX.
  177. */
  178. long long
  179. GetLongLong(void)
  180. {
  181. // try to get a long long from user
  182. while (true)
  183. {
  184. // get line of text, returning LLONG_MAX on failure
  185. string line = GetString();
  186. if (line == NULL)
  187. return LLONG_MAX;
  188. // return a long long if only a long long (possibly with
  189. // leading and/or trailing whitespace) was provided
  190. long long n; char c;
  191. if (sscanf(line, " %lld %c", &n, &c) == 1)
  192. {
  193. free(line);
  194. return n;
  195. }
  196. else
  197. {
  198. free(line);
  199. printf("Retry: ");
  200. }
  201. }
  202. }
  203. /*
  204. * Reads a line of text from standard input and returns it as a
  205. * string (char *), sans trailing newline character. (Ergo, if
  206. * user inputs only "\n", returns "" not NULL.) Returns NULL
  207. * upon error or no input whatsoever (i.e., just EOF). Leading
  208. * and trailing whitespace is not ignored. Stores string on heap
  209. * (via malloc); memory must be freed by caller to avoid leak.
  210. */
  211. string
  212. GetString(void)
  213. {
  214. // growable buffer for chars
  215. string buffer = NULL;
  216. // capacity of buffer
  217. unsigned int capacity = 0;
  218. // number of chars actually in buffer
  219. unsigned int n = 0;
  220. // character read or EOF
  221. int c;
  222. // iteratively get chars from standard input
  223. while ((c = fgetc(stdin)) != '\n' && c != EOF)
  224. {
  225. // grow buffer if necessary
  226. if (n + 1 > capacity)
  227. {
  228. // determine new capacity: start at 32 then double
  229. if (capacity == 0)
  230. capacity = 32;
  231. else if (capacity <= (UINT_MAX / 2))
  232. capacity *= 2;
  233. else
  234. {
  235. free(buffer);
  236. return NULL;
  237. }
  238. // extend buffer's capacity
  239. string temp = realloc(buffer, capacity * sizeof(char));
  240. if (temp == NULL)
  241. {
  242. free(buffer);
  243. return NULL;
  244. }
  245. buffer = temp;
  246. }
  247. // append current character to buffer
  248. buffer[n++] = c;
  249. }
  250. // return NULL if user provided no input
  251. if (n == 0 && c == EOF)
  252. return NULL;
  253. // minimize buffer
  254. string minimal = malloc((n + 1) * sizeof(char));
  255. strncpy(minimal, buffer, n);
  256. free(buffer);
  257. // terminate string
  258. minimal[n] = '\0';
  259. // return string
  260. return minimal;
  261. }