Monday 23 September 2013

Following is my solution for basicmath problem.
I am still not satisfied with the efficiency of the code but happy that I  tried by myself and could do it.


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



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

if (argc < 4){
cout<<"<Number> <+-x/> <Number> <Enter>" << endl;
}
else if(argc == 4)
{
int i = 1;
int j = 0;
int count=0;
int firstArg;
int thirdArg;
while(argv[i])
{
if(i != 2)//check if first and third commandline arguements have digits only.
{
while(!isdigit(argv[i][j]) && argv[i][j])
{
count++;//to count characters except digits
j++;//count digits
}

}
i++;//loop for frist and second arguements
}
if(count > 0)//if there are other characters other than digits
{
cout<<"<Number> <+-x/> <Number> <Enter>" << endl;
}
else if(*argv[2] == '+')
{
firstArg = atof(argv[1]);
thirdArg = atof(argv[3]);
cout<< firstArg + thirdArg << endl;
}
else if(*argv[2] == '-')
{
firstArg = atof(argv[1]);
thirdArg = atof(argv[3]);
cout<< firstArg - thirdArg << endl;
}
else if(*argv[2] == 'x')
{
firstArg = atof(argv[1]);
thirdArg = atof(argv[3]);
cout<< firstArg * thirdArg << endl;
}
else if(*argv[2] == '/')
{
firstArg = atof(argv[1]);
thirdArg = atof(argv[3]);
cout<< firstArg / thirdArg << endl;
}
}



}
////steps
///*
//-first determine how many arguments are there?
//if there are less than four args then print right format
//if there are 4 args in total than
//check first and third arguments contain numbers only
//if not print right format
//if yes then check second argument contain one of these(+-x/)
//if not print right format
//if yes then convert first and third arguements to integer
//at last do the calculations