Monday 21 January 2013

Two Simple Programs from OOP344

OOP344 class.
Two problems to solve.:

1. write 4 command line programs to do
$ add num num<ENTER>
$ sub num num<ENTER>
$ mul num num<ENTER>
$ div num num<ENTER>

2. write a program to show the content of an Operating system environment variable
$ prnenv path<ENTER>
The output will be either the "content" of path variable or "not found" if the environment variable does not exist.

Features added by myself.
to 1. for first program, Error checking in case of wrong inputs such as with characters(add 123 38a)
to 2. for second program, lists all environments' name.

Code (First program):
// Homework Question 1
// Write a program to do math values got from command line

// File:        q1.cpp
// Subject:	OOP344
// Author:	Hanho Ko
// Section:	A

#include <iostream>
#include <cstring>
using namespace std;

// Dispaly help message
void displayHelp(char* prg) {
	cout<<"To use this program..."<<endl;
	cout<<"   "<<prg<<" <Argument 1> <Argument 2> <Argument 3>"<<endl;
	cout<<"    *** <Argument 1> must be one of 'add', 'sub', 'mul' and 'div'."<<endl;
	cout<<"    *** <Argument 2> and <Argument 3> must be an integer(NOT double)."<<endl;
	cout<<"   ex."<<endl;
	cout<<"      $> "<<prg<<" add 325 942"<<endl;
	cout<<endl;
}

// check the arguments are integer or not
int checkInt(char* str) {
	int i;
	for(i=0; str[i]!='\0'; i++) {
		if(str[i] < 48 || str[i] > 57)
			return 0;
	}
	return 1;
}

int main(int argc, char* argv[]) {
	// val1 and val2 are valuables to store integer from command line
	// flag stores a value to do math or check wrong input
	int val1, val2, i, flag=-1;
	// math contains arithmetic operations
	char math[4][4]={"add", "sub", "mul", "div"};

	// check number of arguments taken from command line
	if(argc<3) {
		displayHelp(argv[0]); // display help message
		return 0; // terminate the program
	}

	// check the arguments that are integer or not
	if(checkInt(argv[2])==1 && checkInt(argv[3])==1) {
		val1=atoi(argv[2]); // convert characters to integer
		val2=atoi(argv[3]); // convert characters to integer

		// check the argument is one of arithmetic operations
		for(i=0; i<4; i++) {
			if(strcmp(math[i], argv[1])==0)
				flag=i; // if it is, then store index number
		}

		cout<<endl;

		// by index number, process arithmetic operation
		switch(flag) {
		case 0: // add
			cout<<val1+val2<<endl;
			break;
		case 1: // sub
			cout<<val1-val2<<endl;
			break;
		case 2: // mul
			cout<<val1*val2<<endl;
			break;
		case 3: // div
			// type-casting to get an accurate value
			cout<<(double)val1/(double)val2<<endl;
			break;
		default: // wrong input
			displayHelp(argv[0]);
			break;
		}
	}
	// if the arguments are not integer
	else {
		displayHelp(argv[0]); // display help message
		return 0;
	}

	return 0;
}

Code (Second program):
// Homework Question 2
// Write a program to print the value of an environment variable

// File:        q2.cpp
// Subject:	OOP344
// Author:	Hanho Ko
// Section:	A

#include <iostream>
#include <cstring>
using namespace std;

// get the name of environment
void getName(char* str, char* tmp) {
	int i;
	for(i=0; str[i]!='='; i++) // store characters before '='
		tmp[i]=str[i];
	tmp[i]=0; // add null byte
}

// get the value of environment
void getValue(char* str, char* tmp) {
	int i, j;
	for(i=0; str[i]!='='; i++); // skip until reach '='
	for(j=0, i++; str[i]!=0; i++, j++) // store characters after '='
		tmp[j]=str[i];
	tmp[j]=0; // add null byte
}

int main(int argc, char* argv[], char* env[]) {
	int i, flag=-1; // flag is for index of environment
	char envName[50], tmp[1024];
	
	for(i=0; env[i]!=0; i++) { // list the names of an environment
		getName(env[i], tmp);
		printf(" %-25s", tmp); // left align and assign 25 columns for each name
		if((i+1)%3==0) // new line after list 3 names in one line
			cout<<endl;
	}

	cout<<endl<<endl<<"Enter the name of an environment[Less than 50 characters, Case sensetive]"<<endl<<" Environment name: ";
	cin>>envName;

	for(i=0; env[i]!=0; i++) {
		getName(env[i], tmp); // get the name from the environment split by '='
		if(strcmp(envName, tmp)==0) { // compare two names
			flag=i; // if found the name, put index number into flag
		}
	}
	if(flag>=0) { // check flag that found the same name or not
		cout<<endl<<"Found! The value of '"<<envName<<"' is: "<<endl;
		getValue(env[flag], tmp); // get the value from the environment split by '='
		cout<<tmp<<endl;
	}
	else // print message if not found
		cout<<endl<<"Not found..."<<endl;

	return 0;
}

Source Files:
Download q1.cpp
Download q2.cpp

2 comments: