cdrsir said:
2. Like in command line I just enter:
it will return 5.
how to write this "myDivide"?
If the program was written in C++ it might look like this. (In reality
the program would need to be much more complicated as it would need to
thoroughly check the arguments and so on, and maybe let you know if it
couldnt make sense of them).
regards
Andy Little
--------------------
// include common libraries
#include <iostream>
#include <sstream>
// declare to program a function to convert a character array
// to a number
double double_from_string( char const s[]);
int main(int number_of_args , char* prog_arg_values[])
{
// basic check that there are a valid number of arguments
// to the program
if (number_of_args != 3){
std::cout << "useage mydivide <numerator> <denominator>\n";
return 0;
}
// convert the program arguments from text to numbers
double numerator = double_from_string(prog_arg_values[1]);
double denominator = double_from_string(prog_arg_values[2]);
// do the math
double result = numerator / denominator;
// display the result
std::cout << result << '\n';
return 0;
}
// A function to convert a character array to a number
double double_from_string( char const s[])
{
std::stringstream ss;
ss << s;
double result;
ss >> result;
return result;
}