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.