BasicMath Program

#include <iostream>
#include <cstdlib>

using namespace std;

int ChkNum (char* _chr){
int i;
i = 0;
int neg_count;
neg_count = 0;
int dec_count;
dec_count = 0;
for (i = 0; _chr[i] != ”; i++){
if (_chr[i] == ‘-‘ && neg_count == 0)
neg_count += 1;

else {
if (_chr[i] == ‘-‘ && dec_count == 1)
return 1;
if (_chr[i] == ‘.’ && dec_count == 0)
dec_count ++;
else if (_chr[i] == ‘.’ && dec_count != 0)
return 1;
else if (_chr[i] > 57 || _chr[i] < 48)
return 1;
}
}
return 0;
}

int main(int argc, char* argv[])
{
if(argc != 4)
cout << “bm <number> <+-x/> <number><ENTER>”;
if(ChkNum(argv[1]) == 0 && strlen(argv[2]) == 1 && ChkNum(argv[3]) == 0) {
double one = atof (argv[1]);
double two = atof (argv[3]);
switch( *argv[2])
{
case ‘+’:
cout << one+two << endl;
break;
case ‘-‘:
cout << one-two << endl;
break;
case ‘x’:
cout << one*two << endl;
break;
case ‘/’:
cout << one/two << endl;
break;
default:
cout << “bm <number> <+-x/> <number><ENTER>” << endl;
break;
}
}
else
cout << “bm <number> <+-x/> <number><ENTER>”;

return 0;
}

Advertisements