Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.38 KB | None | 0 0
  1. #include "myLibraryBMR.h"
  2. using namespace std;
  3. //Blake Robinson COSC 1336
  4.  
  5.  
  6.  
  7. //****************************readInt*********************************
  8. //description: reads and validates an interger in the range between min and max
  9. // errorMsg: describes the type of data to reenter if error encountered
  10. // min: the minimum accetable value
  11. // max: the maximum accetable value
  12. // precondition: user has been prompted for input
  13. // errorMsg may be assigned a value,
  14. // otherwise default argument is empty string
  15. // min and max may be assigned values,
  16. // otherwise default arguments are INT_MIN and INT_MAX
  17. //uses header files: iostream, string, limits, cctype
  18. // postcondition: a valid doubleerger in the range min to max is returned
  19.  
  20. int readInt(string errorMsg, int min, int max)
  21. {
  22.     int someInt; //stores each integer entered
  23.     bool valid = false; //loop control flag indicating validity of entry
  24.  
  25.     do {
  26.         //read an integer delimited by a whitespace character
  27.         cin >> someInt;
  28.  
  29.         //valid if within min to max range; && is logical AND operator
  30.         valid = someInt >= min && someInt <= max;
  31.  
  32.         //if input has failed or is invalid or if next char is invalid clear
  33.         //fail state and remove invalid input; || is logical OR operator
  34.         if (cin.fail() || !valid || !isspace(cin.peek()))
  35.         {
  36.             cout << "\nError! Invalid input.\n" << errorMsg << "must be a "
  37.                 << "whole number between " << min << " and " << max
  38.                 << ".\nTry Again: ";
  39.  
  40.             cin.clear();     //clear the fail state
  41.  
  42.             //remove up to max number of chars in input stream or up to '\n'
  43.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  44.             valid = false;         //no valid entry has been read
  45.         }
  46.     } while (!valid); //loop again, prompting user to re-enter a valid integer
  47.  
  48.     //when valid input received, remove from input stream up to 100 remaining
  49.     //characters or until reaches the '\n' <ENTER> character
  50.     cin.ignore(100, '\n');
  51.  
  52.     return someInt; // return a valid integer to calling enviroment
  53. }
  54.  
  55. //****************************readDouble*********************************
  56. //description: reads and validates an double in the range between min and max
  57. // errorMsg: describes the type of data to reenter if error encountered
  58. // min: the minimum accetable value
  59. // max: the maximum accetable value
  60. // precondition: user has been prompted for input
  61. // errorMsg may be assigned a value,
  62. // otherwise default argument is empty string
  63. // min and max may be assigned values,
  64. // otherwise default arguments are INT_MIN and INT_MAX
  65. //uses header files: iostream, string, limits, cctype
  66. // postcondition: a valid double in the range min to max is returned
  67.  
  68. double readDouble(string errorMsg, double min, double max)
  69. {
  70.     double someDouble; //stores each double entered
  71.     bool valid = false; //loop control flag indicating validity of entry
  72.  
  73.     do {
  74.         //read an double delimited by a whitespace character
  75.         cin >> someDouble;
  76.  
  77.         //valid if within min to max range; && is logical AND operator
  78.         valid = someDouble >= min && someDouble <= max;
  79.  
  80.         //if input has failed or is invalid or if next char is invalid clear
  81.         //fail state and remove invalid input; || is logical OR operator
  82.         if (cin.fail() || !valid || !isspace(cin.peek()))
  83.         {
  84.             cout << "\nError! Invalid input.\n" << errorMsg << " must be a "
  85.                 << " double between " << setprecision(6)
  86.                 << (min >= -9e6 && min <= -1e-6 || min <= 9e6 && min >= 1e-6 || min == 0 ? fixed : scientific)
  87.                 << min << " and "
  88.                 << (max >= -9e6 && max <= -1e-6 || max <= 9e6 && max >= 1e-6 || max ==0 ? fixed : scientific)
  89.                 << max
  90.                 << ".\nTry Again: ";
  91.  
  92.             cin.clear();     //clear the fail state
  93.  
  94.                              //remove up to max number of chars in input stream or up to '\n'
  95.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  96.             valid = false;         //no valid entry has been read
  97.         }
  98.     } while (!valid); //loop again, prompting user to re-enter a valid Doubleeger
  99.  
  100.                       //when valid input received, remove from input stream up to 100 remaining
  101.                       //characters or until reaches the '\n' <ENTER> character
  102.     cin.ignore(100, '\n');
  103.  
  104.     return someDouble; // return a valid double to calling enviroment
  105. }
  106.  
  107. //takes in loan amount, number of payments, apr rate, and length of loan to calculate total of each payment
  108. double payment(double rate, double terms, int perYear, double loan)
  109. {
  110.     double nper;
  111.     nper = perYear * terms;
  112.     double owed;
  113.     if (rate == 0.0)                    //if statement to deal with 0% apr
  114.     {
  115.        
  116.         owed = loan / nper;
  117.     }
  118.     else {
  119.  
  120.         rate /= 100;                    //convert rate to decimal. example: 6.25=>.0625
  121.         rate /= nper;                   // divide rate by nper to to convert rate again
  122.                                       //convert nper by multiply by terms to get exact amout of total payments that will be made
  123.         double n = pow(1 + rate, nper); //saving exponent as variable n
  124.         owed = ((rate*n) / (n - 1)) *loan; //loan formula
  125.     }
  126.  
  127.     return owed;
  128. }
  129.  
  130. //takes in length and width and returns the area
  131. double getArea(double length, double width)
  132. {
  133.     return length * width;
  134. }
  135.  
  136. //****************************readChar*********************************
  137. //description: reads and validates if one of the two char parameters have been entered
  138. // errorMsg: describes the type of data to reenter if error encountered
  139. // char1 is passed one of the char entered and validates it
  140. // char2 is passed the other char entered and validates it
  141. // precondition: user has been prompted for input
  142. // errorMsg may be assigned any string,
  143. // otherwise default argument is "Yes or No"
  144. // char1 or char2 can be any char otherwise defaults are 'Y' or 'N'
  145. //uses header files: iostream, string, cctype
  146. // postcondition: a valid char that is equal to char1, or char2
  147.  
  148. char readChar(string errorMsg, char char1, char char2)
  149. {
  150.     char someChar; //stores each char entered
  151.     bool valid = false; //loop control flag indicating validity of entry
  152.  
  153.     do {
  154.         //read an char delimited by a whitespace character
  155.         cin >> someChar;
  156.         someChar = toupper(someChar);
  157.         //valid if within min to max range; && is logical AND operator
  158.         valid = someChar == char1 || someChar == char2;
  159.  
  160.         //if input has failed or is invalid or if next char is invalid clear
  161.         //fail state and remove invalid input; || is logical OR operator
  162.         if (cin.fail() || !valid || !isspace(cin.peek()))
  163.         {
  164.             cout << "\nError! Invalid input.\n" << errorMsg << " must be a "
  165.                 << "letter " << char1 << " or " << char2
  166.                 << ".\nTry Again: ";
  167.  
  168.             cin.clear();     //clear the fail state
  169.  
  170.                              //remove up to max number of chars in input stream or up to '\n'
  171.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  172.             valid = false;         //no valid entry has been read
  173.         }
  174.     } while (!valid); //loop again, prompting user to re-enter a valid integer
  175.  
  176.                       //when valid input received, remove from input stream up to 100 remaining
  177.                       //characters or until reaches the '\n' <ENTER> character
  178.     cin.ignore(100, '\n');
  179.  
  180.     return someChar; // return a valid char to calling enviroment
  181. }
  182.  
  183.  
  184. //prompts user to enter a name then returns name. Parameter type is what type of name you want to enter
  185. string getName(string type)
  186. {
  187.     string name;
  188.     cout << "Enter " << type << " Name: ";
  189.     getline(cin,name);
  190.     return name;
  191. }
  192.  
  193. //takes in a sum and divides it by the counter total to return an average of the sum
  194. double getAverage(double sum, int count)
  195. {
  196.     if (count == 0)
  197.     {
  198.         return 0.0;
  199.     }
  200.     else{
  201.         return sum / count;
  202.     }
  203. }
  204.  
  205. //takes in string as parameter and caps every first alpha char in a string and
  206. //lowers every other char in that string until reaching a whitespace where it repeats the process
  207. string formatString(string theString)
  208. {
  209.     int capital = true;
  210.     int length = theString.length();
  211.  
  212.     for (int i = 0; i < length; i++)
  213.     {
  214.         char c = theString[i];
  215.         if (!capital)
  216.         {
  217.             if (isalpha(c))
  218.                 theString[i] = tolower(theString[i]);
  219.         }
  220.         else
  221.         {
  222.             capital = false;
  223.             if (isalpha(c))
  224.                 theString[i] = toupper(theString[i]);
  225.         }
  226.         if (isspace(theString.at(i)))
  227.         {
  228.             capital = true;
  229.         }
  230.     }
  231.     return theString;
  232. }
  233.  
  234. //CE Static Arrays displayArray function
  235. void displayArray(const int array[], const int size)
  236. {
  237.     for (int i = 0; i < size;)
  238.     {
  239.         if (i % 5 == 0)
  240.         {
  241.             cout << endl;
  242.         }
  243.         cout << setw(5)  << right << array[i++];
  244.     }
  245.     cout << endl;
  246. }
  247.  
  248. //CE Static Arrays copyArray function
  249. void copyArray(int toArray[], const int fromArray[], const int size)
  250. {
  251.     for (int i = 0; i < size; i++)
  252.     {
  253.         toArray[i] = fromArray[i];
  254.     }
  255. }
  256.  
  257. //opens i/o file
  258. void openIOFile(fstream&file, string fileName, ios::openmode mode)
  259. {
  260.     file.open(fileName, mode);
  261.    
  262.     if (file.fail()) //exit program if file open fails
  263.     {
  264.         cerr << "\n\nERROR OPENING " << fileName << " FILE FOR "
  265.             << (mode == 1 ? "input" : "output") << ". \n\n"
  266.             << "PROGRAM TERMINATED. \n\n";
  267.         system("pause");
  268.         exit(EXIT_FAILURE);
  269.     }
  270. }
  271.  
  272. //parameter 1 is passed address of array parameter 2 is passed size of array
  273. //the function saves first value in array as smallest and then compares each
  274. //one with the rest of the array if any value is smaller it is saved as smallest
  275. int min(const int theArray[], const int size)
  276. {
  277.     int smallest = theArray[0];
  278.  
  279.     for (int i = 1; i < size; i++)
  280.     {
  281.         if (smallest > theArray[i])
  282.         {
  283.             smallest = theArray[i];
  284.         }
  285.     }
  286.  
  287.     return smallest;
  288. }
  289.  
  290. //parameter 1 is passed address of array parameter 2 is passed size of array
  291. //the function saves first value in array as largest and then compares each
  292. //one with the rest of the array if any value is larger it is saved as largest
  293. int max(const int theArray[], const int size)
  294. {
  295.     int largest = theArray[0];
  296.  
  297.     for (int i = 1; i < size; i++)
  298.     {
  299.         if (largest < theArray[i])
  300.         {
  301.             largest = theArray[i];
  302.         }
  303.     }
  304.  
  305.     return largest;
  306. }
  307.  
  308. //takes in a grade rounds it to nearest 0th place
  309. //divides average grade by 10 to get a single digit
  310. //uses switch case to compare that digit if the digit matches it
  311. //returns the corresponding letter grade
  312. char grade(double averageGrade)
  313. {
  314.    
  315.    
  316.     if(averageGrade <= 100 && averageGrade >= 0)
  317.     {
  318.         int rounded = static_cast<int>(round(averageGrade, 0));
  319.  
  320.         switch (rounded/10)
  321.         {
  322.         case 10:
  323.         case 9:
  324.             return 'A';
  325.         case 8:
  326.             return 'B';
  327.         case 7:
  328.             return 'C';
  329.         case 6:
  330.             return 'D';
  331.         default:
  332.             return 'F';
  333.         }
  334.     }
  335.     else return '*';
  336. }
  337.  
  338. //overloaded function that takes in an array address as parameter 1 and size
  339. //as parameter 2 adds up each value in an array then divides by the size of array
  340. double getAverage(const int theArray[], const int size)
  341. {
  342.     double total = 0;
  343.  
  344.     for (int i = 0; i < size; i++)
  345.     {
  346.         total += theArray[i];
  347.        
  348.     }
  349.     if (total == 0 || size == 0)
  350.     {
  351.         return 0;
  352.     }
  353.     else
  354.     {
  355.         return total / size;
  356.     }
  357. }
  358.  
  359. //takes string of name in format last, first middle
  360. //returns as first middle last
  361. string reverseName(string name)
  362. {
  363.     string first;
  364.     string last;
  365.     string::size_type pos = name.find(',');
  366.     if (pos != string::npos)
  367.     {
  368.         first = name.substr(pos + 2); //extracts until end of string
  369.         last = name.substr(0, pos); //extracts from start of string to right before comma
  370.  
  371.         return first + " " + last;
  372.  
  373.     }
  374.     else
  375.     {
  376.         return name.insert(0, "Invalid: ");
  377.     }
  378.  
  379.    
  380.    
  381. }
  382.  
  383. //function which takes a format string as an argument
  384. string getDate(string fmtStr)
  385. {
  386.     const int SIZE = 50; //size of char arrray to store date
  387.     char strDate[SIZE]; //c-style string - array of chars to store date
  388.  
  389.     time_t today; //represent time as a time_t integer which represents the
  390.                   //number of seconds that have elapsed since 1/1/1970 UTC
  391.                   //time_t good until 2038, then will need 64 bit _times64_t
  392.  
  393.     time(&today); //get time from system storing in the address of today
  394.  
  395.     tm thisTime; //a tm struct object used to store, retrieve
  396.                  //and format various time and date components
  397.  
  398.     //localtime_s function converts a time stored as a time_t value in today and stored the
  399.     // result in the thisTime structure of type tm
  400.     localtime_s(&thisTime, &today);
  401.  
  402.     //strftime function extracts time components form the tm structure thisTime
  403.     //and returns a time formatted according to the fmtStr. The string function
  404.     //c_str() converts a c-style string to a string class object.
  405.     //the formatted results are stored in strDate.
  406.     strftime(strDate, sizeof(strDate) - 1, fmtStr.c_str(), &thisTime);
  407.  
  408.     return strDate;
  409. }
  410.  
  411. //takes in value of number as parameter 1 and parameter 2 is the number of digits you'd like to round to
  412. //uses cmath libary fabs and floor functions to round a number to the desired decimal place
  413. double round (double num, int place)
  414. {
  415.     auto factor = pow(10.0, place);
  416.  
  417.     double rounded = floor(fabs(num) * factor + 0.5) / factor;
  418.  
  419.     return rounded;
  420. }
  421.  
  422. GradeType getGrade(double gradeAvg)
  423. {
  424.    
  425.     if (gradeAvg <= 100 && gradeAvg >= 0)
  426.     {
  427.         int rounded = static_cast<int>(round(gradeAvg, 0));
  428.  
  429.         switch (rounded / 10)
  430.         {
  431.         case 10:
  432.         case 9:
  433.             return A;
  434.         case 8:
  435.             return B;
  436.         case 7:
  437.             return C;
  438.         case 6:
  439.             return D;
  440.         default:
  441.             return F;
  442.         }
  443.     }
  444.     else return X;
  445. }
  446.  
  447. char gradeTypeToChar(GradeType grade)
  448. {
  449.     switch (grade)
  450.     {
  451.     case A:
  452.         return 'A';
  453.     case B:
  454.         return 'B';
  455.     case C:
  456.         return 'C';
  457.     case D:
  458.         return 'D';
  459.     case F:
  460.         return 'F';
  461.     case X:
  462.         return '*';
  463.    
  464.     }
  465. }
  466.  
  467. void displayArray(const double array[], const int size)
  468. {
  469.     for (int i = 0; i < size;)
  470.     {
  471.         if (i % 5 == 0 && i >= 10)
  472.         {
  473.             cout << endl;
  474.         }
  475.         cout << setw(8) << right << array[i++];
  476.     }
  477.     cout << endl;
  478. }
  479.  
  480.  
  481. //Blake Robinson COSC 1336
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement