chriscct7

GCD C++ program

May 11th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. void testints(int *x, int *y);
  7.  
  8.  
  9. int GCD(int x, int y) //take in the two variables
  10. {
  11.     while( 1 ) //while true
  12.     {
  13.         if( y == 0 ){ //if y =0 then according to the assignment, the GCD is x
  14.             return x;
  15.         }
  16.         else{
  17.             x = x % y;
  18.             if( x == 0 )
  19.                 return y;
  20.             y = y % x;
  21.         }
  22.  
  23.     }
  24. }
  25. int inputargs(int argumentnum){
  26.     int x;
  27.     char a[256];
  28.     cout << "Please enter positive integers for" << endl;
  29.     cout << "Value "<< argumentnum<<": ";
  30.     cin >> a;
  31.     x=atoi ( a );
  32.     return x; // Return User input
  33. }
  34. void testints(int& x, int& y) {
  35.     while ( x<=0 || y<0 || (x<=0 && y<0)){
  36.         cin.clear();
  37.         cin.ignore(100, '\n');
  38.         char a[256];
  39.         char b[256];
  40.         cout << "Invalid input" << endl;
  41.         cout << "Please enter positive integers for" << endl;
  42.         cout << "Value 1: ";
  43.         cin >> a;
  44.         x=atoi (a);
  45.         cout << "Value 2: ";
  46.         cin >> b;
  47.         y=atoi (b);
  48.     }
  49. }
  50. void printresults(int x, int y){
  51.     cout << "\nThe Greatest Common Divisor of "<< x << " and " << y << " is " << GCD(x, y) << endl;  
  52.     system("PAUSE"); //stops the dialog window from closing, though this issue didn't happen before. Will research this later.
  53. }
  54. int main(int argc, char ** argv){
  55.     if (argc <=0){ //something weird has happened, terminate
  56.         cout << "A Critical Error has occured, system is terminating" << endl;
  57.         exit (1);
  58.     }
  59.     if (argc == 1) //Zero Prompts
  60.     {
  61.         int argnum1=1;
  62.         int x = inputargs(argnum1);
  63.         int argnum2=2;
  64.         int y = inputargs(argnum2);
  65.         testints(x,y);
  66.         printresults(x,y);
  67.     }
  68.     if (argc == 2) // One Command Line
  69.     {
  70.         int x;
  71.         x=atoi(argv[1]);
  72.         int argnum2=2;
  73.         int y = inputargs(argnum2);
  74.         testints(x,y);
  75.         printresults(x,y);
  76.     }
  77.     if (argc == 3) // Two Command Line
  78.     {
  79.         int x,y;
  80.         x=atoi(argv[1]);
  81.         y=atoi(argv[2]);
  82.         testints(x,y);
  83.         printresults(x,y);
  84.     }
  85.     if (argc > 3) // More then Two Command Line
  86.     {
  87.         cout << "You have entered more than three inputs" << endl;
  88.         cout << "We will only use the first two inputs" << endl;
  89.         cout << "Hit enter to acknowledge and continue..." << endl;
  90.         int x,y;
  91.         x=atoi(argv[1]);
  92.         y=atoi(argv[2]);
  93.         testints(x,y);
  94.         printresults(x,y);
  95.     }
  96.     else{
  97.         return 0;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment