Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /******************************************************************************\
  2. * Project: Rectangle Display *
  3. * Authors: Ronald Swedlow *
  4. * Version: 2019.09.27 *
  5. * License: GNU General Public License (GPL) v3 *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify it *
  8. * under the terms of the GNU General Public License as published by the Free *
  9. * Software Foundation, either version 3 of the License, or (at your option) *
  10. * any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT *
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU GPL for more details. *
  15. \******************************************************************************/
  16.  
  17. #ifdef __cplusplus
  18. // Automatically inferred from .CPP file extension, unless gcc -x c is used.
  19.  
  20. // We are not allowed to submit code with C functions in this course.
  21. // This is so we can compile using C++-specific features to pass the class.
  22.  
  23. #include <iostream>
  24. using std::cin;
  25. using std::cout;
  26.  
  27. #else
  28. /*
  29. * This is what I run at home to code things the right way in C. :)
  30. * No C library calls ever will be used in Computer Science I assignments.
  31. */
  32.  
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35.  
  36. #endif
  37.  
  38. /*
  39. * These are code cleanliness functions built on top of cin and cout.
  40. * When gcc -x c or #undef __cplusplus is used, they use C functions.
  41. */
  42. extern long int userin(const char* promptmsg); /* cout, then cin */
  43. extern void userout(const char* displaymsg); /* just cout */
  44.  
  45. int
  46. main(void)
  47. {
  48. long int cols, rows;
  49. register long int col, row;
  50.  
  51. cols = userin("Rectangle how many units wide? ");
  52. while (cols <= 0 || cols > 75) {
  53. cols = userin("Please specify in the domain (0, 75]: ");
  54. }
  55. rows = userin("Rectangle how many units tall? ");
  56. while (rows <= 0 || rows > 75) {
  57. rows = userin("Please specify in the domain (0, 75]: ");
  58. }
  59.  
  60. for (row = 0; row < rows; row++) {
  61. for (col = 0; col < cols; col++) {
  62. userout("X");
  63. }
  64. userout("\n");
  65. }
  66. return 0;
  67. }
  68. /*
  69. * Note that when I print "please try again with (0 < x <= 75), I am merely
  70. * giving the user an algebraic rule expression. (0 < x <= 75) does not
  71. * bear any purposeful C expression, just an algebraic expression the user
  72. * can understand.
  73. *
  74. * In C++, (0 < x <= 75) has the identical evaluation:
  75. * (0 < x) ? ((int)true <= 75) : ((int)false <= 75)
  76. * Perfectly valid but highly unlikely to be of practical use.
  77. */
  78.  
  79. void
  80. userout(const char* displaymsg)
  81. {
  82. #ifdef __cplusplus
  83. cout << displaymsg;
  84. #else
  85. fputs(displaymsg, stdout);
  86. #endif
  87. }
  88.  
  89. long int
  90. userin(const char* promptmsg)
  91. {
  92. #ifndef __cplusplus
  93. static char txt_buf[512];
  94. #endif
  95. register long int high_precision_integer;
  96. int outofbounds;
  97.  
  98. userout(promptmsg);
  99. #ifdef __cplusplus
  100. cin >> high_precision_integer;
  101. outofbounds = cin.fail() ? 1 : 0;
  102. #else
  103. fgets(&txt_buf[0], sizeof(txt_buf), stdin);
  104. high_precision_integer = strtol(txt_buf, NULL, 10);
  105. outofbounds = (errno != 0) ? 1 : 0;
  106. #endif
  107.  
  108. if (outofbounds) {
  109. userout("WARNING: Input outside LONG_MAX and LONG_MIN.\n");
  110. } /* We are not really worrying about this yet. */
  111. return (high_precision_integer);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement