cmpslib
cmpslib19.h
Go to the documentation of this file.
1 /*
2 @cmpslib19.h
3 @general functions for use by students for homework and labs
4 */
5 #pragma once
6 
7 #include <cxxabi.h>
8 #include <bitset>
9 #include <time.h>
10 #include <string> // string class
11 #include <string.h> // c-string library
12 #include <sstream> // ostringstream
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <iostream>
16 #include <fstream> // c file output
17 #include <stdarg.h>
18 #include <typeinfo> // find names of ADTs at runtime
19 #include <limits.h> // min and max values for dataytypes
20 #include <limits>
21 #include <ios>
22 #include <float.h>
23 #include <math.h>
24 #include <iomanip>
25 #include <stdexcept> // exception handling ..
26 #include <signal.h> // for catching the segfaults
27 #include <random>
28 #include <algorithm>
29 
30 using std::string;
31 using std::cout;
32 using std::cin;
33 using std::ostringstream;
34 using std::setfill;
35 using std::setw;
36 using std::endl;
37 using std::boolalpha;
38 
39 
40 
41 #ifndef DONT_DOCUMENT
42 template <typename CharT>
43 std::streamsize ignore_line (std::basic_istream<CharT>& in, bool always_discard = false );
44 #endif
45 
46 
47 
48 /**************************************
49  user input
50  ***************************************/
51 
52 
71 inline void Prompt (string prompt, string & val)
72 {
73  std::cout << prompt;
74  ignore_line( std::cin );// clear the buffer if needed
75  getline(std::cin, val); // this
76 }
77 
97  template <class T>
98 inline void Prompt (string prompt, T & val)
99 {
100 
101  std::cout << prompt;
102  std::cin >> val;
103  while(std::cin.fail()) // if it fails... ie user input does not match the datatype
104  {
105  std::cin.clear();
106  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
107  std::cout << prompt;
108  std::cin >> val;
109  }
110 }
111 
112 
131  template <class T>
132 inline void Prompt( string prompt,T& val, T minval, T maxval)
133 {
134  Prompt(prompt,val);
135  while ( val < minval || val > maxval)
136  {
137  std::cout << "The entered value must be with the range " << minval << " to " << maxval << std::endl;
138  Prompt(prompt,val);
139  }
140 }
141 
142 
143 
144 
145 
157  inline bool PromptYN(string prompt)
158  {
159  while (true)
160  {
161  std::cout << prompt << " (Y/N)"<< std::endl;
162  ignore_line( std::cin ); // clear the buffer if needed
163  string user_input;
164  std::cin >> user_input;
165  if ( user_input == "Y" || user_input =="y")
166  {
167  return true;
168  }
169  if (user_input == "N" || user_input =="n")
170  {
171  return false;
172  }
173  }
174  }
175 
176 
177 
178 
179 
180 
181  /*********************************************
182  system stuff
183  *********************************************/
184 
191  inline void WaitHundredth(int time)
192  {
193  struct timespec tim, tim2;
194  tim.tv_sec = 0;
195  tim.tv_nsec = time*10000000;
196  nanosleep(&tim , &tim2);
197  }
198 
199 
203 inline void ClearScreen() {system("clear");}
204 
205 
214  inline char * GetTime()
215  {
216  time_t rawtime = time(0);
217  struct tm * timeinfo;
218 
219  timeinfo = localtime ( &rawtime );
220  return asctime (timeinfo);
221  }
229  inline string GetDateTime()
230  {
231  time_t t = time(0); // get time now
232  struct tm * now = localtime( & t );
233  ostringstream tm;
234  tm << (now->tm_year + 1900) << '-'
235  << setfill('0') << setw(2) <<(now->tm_mon + 1) << '-'
236  << setfill('0') << setw(2) << now->tm_mday << "-"
237  << setfill('0') << setw(2) << now->tm_hour << ":"
238  << setfill('0') << setw(2) << now->tm_min << ":"
239  << setfill('0') << setw(2) << now->tm_sec ;
240  return tm.str();
241  }
242 
243 
244 
245  /****************************************
246  Numberic fuctions
247 
248  *****************************************/
249 
255  inline int StringToInteger( string input){return stoi(input);}
256 
262  inline double StringToDouble( string input ){return stod(input);}
263 
268  inline string ToLowerCase(string input)
269  {
270  std::transform(input.begin(), input.end(), input.begin(),
271  [](unsigned char c){ return std::tolower(c); });
272  return input;
273  }
274 
279  inline string ToUpperCase(string input)
280  {
281  std::transform(input.begin(), input.end(), input.begin(),
282  [](unsigned char c){ return std::toupper(c); });
283  return input;
284  }
285 
286 
292  inline bool StringToBool(string input)
293  {
294  input = ToLowerCase(input);
295  if(input=="true" || input =="1"){return true;}
296  if(input=="false" || input =="0"){return false;}
297  return false;
298  }
299 
300 
301 
311  inline bool VeryClose(float A, float B, float epsilon = 0.0005f)
312  {
313  return (fabs(A - B) < epsilon);
314  }
315 
316 
317 
318 
324  template <typename T>
325  inline string NumberToString ( T Number )
326  {
327  ostringstream ss;
328  ss << Number;
329  return ss.str();
330  }
331 
332 
333 
340  inline int CreateRandomNumber(int min, int max)
341  {
342  std::random_device rd;
343  std::mt19937 gen(rd());
344  std::uniform_int_distribution<> dis(min,max);
345  return (dis(gen));
346  }
347 
348 
349 
356  inline bool IsNumber(string str)
357  {
358  for (unsigned int i = 0; i < str.length(); i++)
359  if (! isdigit(str[i])){return false;}
360  return true;
361  }
362 
363 
364 
371  inline bool IsInteger(string input)
372  {
373  try
374  {
375  int temp = stoi(input);
376  temp++; // to avoid unused var warning
377  }
378  catch(...){return false;}
379  return true;
380  }
381 
382 
383 
390  inline bool IsDouble(string input)
391  {
392 
393  try
394  {
395  double temp = stod(input);
396  temp++; // to avoid unused var warning
397  }
398  catch(...){return false;}
399  return true;
400  }
401 
402 
415  inline string PassFail(int in)
416  {
417  if ( in > 1)
418  {
419  std::cout << "The PassFail function is designed to take an expresson as its parameter\n something that evaluates to true or false\n you are not using the fuction properly stopping execution of your program. line number " <<__LINE__ << endl;
420  exit(9);
421  }
422  return (in) ? "Pass":"Fail";
423 
424  }
425 
426 
430  inline string PF(int in) {return PassFail(in);}
431 
432 
433 
449 template<class T>
450 inline bool IsSortedAscending(T* data,unsigned int size)
451  {
452  for(unsigned int loop=0;loop<size-1;loop++)
453  if(data[loop]>data[loop+1])
454  return false;
455  return true;
456  }
457 
458 
459 #ifndef DONT_DOCUMENT
460 /* this is a kinda cool way to do it, pass in a reference to the array
461  unfortunatly it isnt very compatable with most exisitng progams as
462  it implememts some new features to c++
463  but you dont have to pass in the size as a 2nd parameter */
464 template<class T,size_t N>
465 inline bool IsSortedAscending(T(&data )[N])
466  {
467  // data is a reference to the array and N is the size of the array
468  for(unsigned int loop=0;loop<N -1;loop++)
469  if(data[loop]>data[loop+1])
470  return false;
471  return true;
472  }
473 #endif
474 
475 
491 template<class T>
492 inline bool IsSortedDescending(T* data,unsigned int size)
493  {
494  for(unsigned int loop=0;loop<size-1;loop++)
495  if(data[loop]<data[loop+1])
496  return false;
497  return true;
498  }
499 
500 
501 
502 #ifndef DONT_DOCUMENT
503 /* this is a kinda cool way to do it, pass in a reference to the array
504  unfortunatly it isnt very compatable with most exisitng progams as
505  it implememts some new features to c++
506  but you dont have to pass in the size as a 2nd parameter */
507 template<class T,size_t N>
508 inline bool IsSortedDescending(T(&data )[N])
509  {
510  // data is a reference to the array and N is the size of the array
511  for(unsigned int loop=0;loop<N -1;loop++)
512  if(data[loop]<data[loop+1])
513  return false;
514  return true;
515  }
516 #endif
517 
518 
519  /***************************************
520  console functions
521  ****************************************/
522 
523 
527  inline string RED(string input){return string("\033[1;31m"+input+"\033[0m");}
531  inline string GREEN(string input){return string("\033[1;32m"+input+"\033[0m");}
535  inline string YELLOW(string input){return string("\033[1;33m"+input+"\033[0m");}
539  inline string BLUE(string input){return string("\033[1;34m"+input+"\033[0m");}
543  inline string MAGENTA(string input){return string("\033[1;35m"+input+"\033[0m");}
547  inline string CYAN(string input){return string("\033[1;36m"+input+"\033[0m");}
548 
553  inline string ChangeToRed(){return string("\033[1;31m");}
558  inline string ChangeToGreen(){return string("\033[1;32m");}
563  inline string ChangeToYellow(){return string("\033[1;33m");}
568  inline string ChangeToBlue(){return string("\033[1;34m");}
573  inline string ChangeToMagenta(){return string("\033[1;35m");}
578  inline string ChangeToCyan(){return string("\033[1;36m");}
583  inline string ChangeToWhite(){return string("\033[0m");}
584 
585 
586 
594 inline void PositionCursor(int row,int col){printf("\033[%i;%iH",row+1,col+1);}
595 
596 
597 
602 template <typename T>
603 inline std::string GetClassName(const T & a)
604 {
605  int itemp=0;
606  char * ptemp = abi::__cxa_demangle( typeid(T).name(),0,0,&itemp);
607  std::string temp( ptemp);
608  free (ptemp);
609  return temp;
610 }
611 
612 
613 
614 
615  /*********************************************************************************************************************
616  **********************************************************************************************************************
617  below are fucnctions that you wont be calling they are
618  for use by other functions
619  **********************************************************************************************************************
620  **********************************************************************************************************************/
621 #ifndef DONT_DOCUMENT
622  template <typename CharT>
623  std::streamsize ignore_line (
624  std::basic_istream<CharT>& in, bool always_discard )
625  {
626  std::streamsize nread = 0;
627  if ( always_discard
628  || ( in.rdbuf()->sungetc() != std::char_traits<CharT>::eof()
629  && in.get() != in.widen ( '\n' ) ) )
630  {
631  // The stream is good, and we haven't
632  // read a full line yet, so clear it out
633  in.ignore ( std::numeric_limits<std::streamsize>::max(), in.widen ( '\n' ) );
634  nread = in.gcount();
635  }
636  return nread;
637  }
638 
639 #endif
void ClearScreen()
clear the screen
Definition: cmpslib19.h:203
char * GetTime()
get the time
Definition: cmpslib19.h:214
string GREEN(string input)
return a string formatted to display in color
Definition: cmpslib19.h:531
string BLUE(string input)
return a string formatted to display in color
Definition: cmpslib19.h:539
bool IsInteger(string input)
can this string be converted to an integer numeric value
Definition: cmpslib19.h:371
string ChangeToGreen()
returns a string formated to display in a color and does not reset to white
Definition: cmpslib19.h:558
string CYAN(string input)
return a string formatted to display in color
Definition: cmpslib19.h:547
string RED(string input)
return a string formatted to display in color
Definition: cmpslib19.h:527
bool PromptYN(string prompt)
for simple yes or no questions
Definition: cmpslib19.h:157
string ChangeToYellow()
returns a string formated to display in a color and does not reset to white
Definition: cmpslib19.h:563
string ChangeToWhite()
returns a string formated to display in a color and does not reset to white
Definition: cmpslib19.h:583
bool StringToBool(string input)
convert string to bool
Definition: cmpslib19.h:292
bool VeryClose(float A, float B, float epsilon=0.0005f)
compare two floats
Definition: cmpslib19.h:311
double StringToDouble(string input)
convert string to double
Definition: cmpslib19.h:262
void PositionCursor(int row, int col)
move the cursor to the position indcated by the row and column
Definition: cmpslib19.h:594
string ChangeToCyan()
returns a string formated to display in a color and does not reset to white
Definition: cmpslib19.h:578
std::string GetClassName(const T &a)
get the class name as a string for the object
Definition: cmpslib19.h:603
bool IsSortedDescending(T *data, unsigned int size)
is an array sorted a function that will tell you if an array is sorted
Definition: cmpslib19.h:492
string PassFail(int in)
convert bool value into "Pass" or "Fail"
Definition: cmpslib19.h:415
int StringToInteger(string input)
convert string to int
Definition: cmpslib19.h:255
string NumberToString(T Number)
convert a numveric value to string
Definition: cmpslib19.h:325
string ToUpperCase(string input)
convert string to uppercase
Definition: cmpslib19.h:279
string PF(int in)
same as PassFail same as PassFail
Definition: cmpslib19.h:430
string ToLowerCase(string input)
convert string to lowercase
Definition: cmpslib19.h:268
string ChangeToMagenta()
returns a string formated to display in a color and does not reset to white
Definition: cmpslib19.h:573
bool IsSortedAscending(T *data, unsigned int size)
is an array sorted a function that will tell you if an array is sorted
Definition: cmpslib19.h:450
string GetDateTime()
The DateTime as a string.
Definition: cmpslib19.h:229
void Prompt(string prompt, string &val)
use to request input from the user
Definition: cmpslib19.h:71
bool IsDouble(string input)
can this string be converted to an double value
Definition: cmpslib19.h:390
bool IsNumber(string str)
can this string be converted to a numeric value
Definition: cmpslib19.h:356
string YELLOW(string input)
return a string formatted to display in color
Definition: cmpslib19.h:535
void WaitHundredth(int time)
Do nothing for a period of time.
Definition: cmpslib19.h:191
string MAGENTA(string input)
return a string formatted to display in color
Definition: cmpslib19.h:543
int CreateRandomNumber(int min, int max)
create a random number
Definition: cmpslib19.h:340
string ChangeToRed()
returns a string formated to display in a color and does not reset to white
Definition: cmpslib19.h:553
string ChangeToBlue()
returns a string formated to display in a color and does not reset to white
Definition: cmpslib19.h:568