Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************\
- * Project: Rectangle Display *
- * Authors: Ronald Swedlow *
- * Version: 2019.09.27 *
- * License: GNU General Public License (GPL) v3 *
- * *
- * This program is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU General Public License as published by the Free *
- * Software Foundation, either version 3 of the License, or (at your option) *
- * any later version. *
- * *
- * This program is distributed in the hope that it will be useful, but WITHOUT *
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU GPL for more details. *
- \******************************************************************************/
- #ifdef __cplusplus
- // Automatically inferred from .CPP file extension, unless gcc -x c is used.
- // We are not allowed to submit code with C functions in this course.
- // This is so we can compile using C++-specific features to pass the class.
- #include <iostream>
- using std::cin;
- using std::cout;
- #else
- /*
- * This is what I run at home to code things the right way in C. :)
- * No C library calls ever will be used in Computer Science I assignments.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #endif
- /*
- * These are code cleanliness functions built on top of cin and cout.
- * When gcc -x c or #undef __cplusplus is used, they use C functions.
- */
- extern long int userin(const char* promptmsg); /* cout, then cin */
- extern void userout(const char* displaymsg); /* just cout */
- int
- main(void)
- {
- long int cols, rows;
- register long int col, row;
- cols = userin("Rectangle how many units wide? ");
- while (cols <= 0 || cols > 75) {
- cols = userin("Please specify in the domain (0, 75]: ");
- }
- rows = userin("Rectangle how many units tall? ");
- while (rows <= 0 || rows > 75) {
- rows = userin("Please specify in the domain (0, 75]: ");
- }
- for (row = 0; row < rows; row++) {
- for (col = 0; col < cols; col++) {
- userout("X");
- }
- userout("\n");
- }
- return 0;
- }
- /*
- * Note that when I print "please try again with (0 < x <= 75), I am merely
- * giving the user an algebraic rule expression. (0 < x <= 75) does not
- * bear any purposeful C expression, just an algebraic expression the user
- * can understand.
- *
- * In C++, (0 < x <= 75) has the identical evaluation:
- * (0 < x) ? ((int)true <= 75) : ((int)false <= 75)
- * Perfectly valid but highly unlikely to be of practical use.
- */
- void
- userout(const char* displaymsg)
- {
- #ifdef __cplusplus
- cout << displaymsg;
- #else
- fputs(displaymsg, stdout);
- #endif
- }
- long int
- userin(const char* promptmsg)
- {
- #ifndef __cplusplus
- static char txt_buf[512];
- #endif
- register long int high_precision_integer;
- int outofbounds;
- userout(promptmsg);
- #ifdef __cplusplus
- cin >> high_precision_integer;
- outofbounds = cin.fail() ? 1 : 0;
- #else
- fgets(&txt_buf[0], sizeof(txt_buf), stdin);
- high_precision_integer = strtol(txt_buf, NULL, 10);
- outofbounds = (errno != 0) ? 1 : 0;
- #endif
- if (outofbounds) {
- userout("WARNING: Input outside LONG_MAX and LONG_MIN.\n");
- } /* We are not really worrying about this yet. */
- return (high_precision_integer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement