Saturday 26 January 2013

Week 3: console.display



void Console::display(const char* str, int row, int col, int fieldLen){
        unsigned int i;
        unsigned int size;
        setPos(row, col);
        fieldLen > 0 ? size=fieldLen : size=strlen(str);
        for(i=0;i<size;i<strlen(str) ? putChar(str[i]) : putChar(' '),i++);

  }



Does this count as one line program?

Week 3 challenges: Xmover




#include "console.h"

using namespace cio;

int main(){
  bool done = false;
  int row = console.getRows()/2;
  int col = console.getCols()/2;
  int key;
  int end_flag=0;
  while(!done){
if ((row == console.getRows()-1 && col == console.getCols()-2) || (row == console.getRows()-2 && col == console.getCols()-1)) {
end_flag=1;
} else {
end_flag=0;
}
    console.setPos(row, col);
    console.putChar('A');
    key = console.getKey();
    console.setPos(row, col);
    console.putChar('.');
    switch(key){
    case UP:
      if(row > 0 ){
        row--;
      }
      else{
        console.alarm();
      }
      break;
    case DOWN:
      if(row < console.getRows()-1 && end_flag==0){
        row++;
      }
      else{
        console.alarm();
      }
      break;
    case LEFT:
      if(col > 0){
        col--;
      }
      else{
        console.alarm();
      }
      break;
    case RIGHT:
      if(col < console.getCols()-1 && end_flag==0){
        col++;
      }
      else{
        console.alarm();
      }
      break;
    case ESCAPE:
      done = true;
      break;
    }
  }

  return 0;
}

Wednesday 16 January 2013


Add with checking for valid input


#include <iostream>
#include <stdlib.h>
#include <ctype.h>

using namespace std;


int main(int argc, char* argv[]) {

int sum;
int i;
int j;
int check = 0;
if (argc == 3) { // can only handle 3 parameters
for (j=1; j<=2 && check==0; j++) {
if (!isdigit(argv[j][0]) && argv[j][0]!='-')
// special check for the first char, which may be '-'
check=1;
for (i=1; argv[j][i]!='\0' && check==0; i++) {
if (!isdigit(argv[j][i]))
check=1;
}
}
}
if (argc==3 && check==0) {
sum = atoi(argv[1]) + atoi(argv[2]);
cout << sum << endl;
} else {
cout << "Enter 2 valid parameters after the program name" << endl;
}
return 0;
}

I don't think I'll post the the other three math programs with validation, it's the same code as the blue section.


Tuesday 15 January 2013


OOP 344 Week 2

Environment Variable Content Search Program

 V2



#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char* argv[], char* env[]) {
int i;
int pos;
int check=0;
char content[50];

if (argc != 2) {
cout << "Only input one parameter after program name" << endl;
} else {
for (i=0; env[i]!=0 && check==0; i++) {
pos = strcspn(env[i], "=");
if (strncmp(argv[1], env[i], pos) == 0) {
check = 1;
strcpy(content, env[i]+pos+1);
}
}
if (check==1)
cout << argv[1] << " = " << content << endl;
else
cout << argv[1] << " not found" << endl;
        }
    return 0;
}


Better now.




$ prnenv path<ENTER>


/* case sensitive */

#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char* argv[], char* env[]){
  int i;
  int j;
  int k;
  int check=0;
  char p1[30];
  char p2[30];
  if (argc == 2) {
  for (i=0; env[i]!=0 && check==0; i++) {
 for (j=0; env[i][j] != '='; j++) {
 p1[j]=env[i][j];
 }
 p1[j]='\0';
 if (strcmp(argv[1], p1)==0) {
 check=1;
 for (k=0; env[i][k+j+1]!='\0'; k++) {
 p2[k]=env[i][k+j+1];
 }
 p2[k]='\0';
 }
  }
  if (check==1)
 cout << argv[1] << " = " << p2 << endl;
  else
 cout << argv[1] << " not found" << endl;
  }
  return 0;
}

This works but its pretty hideous looking



WEEK 2: Command Line Programs


$ add num num<ENTER>

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[]) {

  int sum;
if (argc==3) {    //only accept three parameters
sum = atoi(argv[1]) + atoi(argv[2]);
cout << "The sum of the two numbers is: " << sum << endl;
} else {
cout << "Enter 2 valid parameters after the program name" << endl;
}
return 0;
}



$ sub num num<ENTER>

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[]) {

  int diff;
if (argc==3) {    //only accept three parameters
diff = atoi(argv[1]) - atoi(argv[2]);
cout << "The difference between of the two numbers is: " << diff << endl;
} else {
cout << "Enter 2 valid parameters after the program name" << endl;
}
return 0;
}




$ mul num num<ENTER>

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[]) {

  int total;
if (argc==3) {    //only accept three parameters
total = atoi(argv[1]) * atoi(argv[2]);
cout << argv[1] << " * " << argv[2] << " is: " << total << endl;
} else {
cout << "Enter 2 valid parameters after the program name" << endl;
}
return 0;
}




$ div num num<ENTER>


#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;

int main(int argc, char* argv[]) {
double total;
if (argc==3) { //only accept three parameters
total = (double) atoi(argv[1]) / (double) atoi(argv[2]);
cout << argv[1] << " / " << argv[2] << " is: " << fixed << setprecision(2) << total << endl;
} else {
cout << "Enter 2 valid parameters after the program name" << endl;
}
return 0;
}

This gets the job done but doesn't really check the parameters to see if they are just integers. I tried to do the checking but that somehow screwed up the negative integers. Anyways, maybe I'll try again over next days.