Sunday, April 15, 2012

Test 2 Q3 walkthrough with comments!:
//Q3
#include <iostream>
#include "tqueue.h"
using namespace std;
int main(int argc, char* argv[]){
  bool badArgs = false;
  bool Asc = false;
  Tqueue<int> Q;//changing all "type" in tqueue class as int
  if(argc < 3){//if there are less than 3 argument in argc
    badArgs = true;
  }
  else{
    if(argv[1][0] != '-'){//checks the first character
      badArgs = true;
    }
    else{
      if(argv[1][1] == 'a'){//checks the second character
        Asc = true;
      }
      else if(argv[1][1] != 'd'){
        badArgs = true;
      }
      int arg;
      for(int i =2; !badArgs && i<argc;i++){//i=2; if !badArg is false and i < argc; i++
        if(sscanf(argv[i],"%d", &arg) == 1){//look at argv[i], read it as an integer, store it into &arg, sscanf return
                                            //sscanf returns the number of variable filled (in this case 1)
          Q.append(arg);//append the value of arg at the end of the Qlist
        }
        else{
          badArgs = true;
        }
      }
    }
  }
  if(badArgs){
    cout<<"Incorrect class use as follows:"<<endl<<"tq -[a|d] 99 [99 99 ...]"<<endl;
  }
  else{
    Q.sort(Asc);//see if the bool(Ascending) is true or not in the function sort (bool sort(bool Ascending = true);)
                //if true will sort the Qlist by ascending order or if false will go descending
    while(!Q.isEmpty()){
      cout<<Q.remove()<<" ";
    }
    cout<<endl;
  }
  return 0;
}

No comments:

Post a Comment