An exercise on week2 of Oop344 of all operations using commandline.
Add Command Line
// OOP344 Week2 Exercise Peter Huang CMDL Add
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[], char* env[]) {
int sum = 0;
if(argc != 3)
cout << "Not valid add command. Command format is : add num1 num2" <<endl;
else{
for(int i = 1; i < argc; i++)
sum = sum + atoi(argv[i]);
cout << "Sum : " << sum <<endl;
}
return 0;
}
Subtract Command Line
// OOP344 Week2 Exercise Peter Huang CMDL Subtract
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[], char* env[]) {
int dif = 0;
if(argc != 3)
cout << "Not valid subtract command. Command format is : sub num1(to) num2(from)" <<endl;
else {
dif = atoi(argv[1]) - atoi(argv[2]);
cout << "Difference : " << dif <<endl;
}
return 0;
}
Multiplication Command Line
// OOP344 Week2 Exercise Peter Huang CMDL Multiply
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[], char* env[]) {
int ans;
if(argc != 3)
cout << "Not valid multiplication command. Command format is : mul num1 num2" <<endl;
else {
ans = atoi(argv[1]) * atoi(argv[2]);
cout << "Answer(Multiplication) : " << ans <<endl;
}
return 0;
}
Division Command Line
// OOP344 Week2 Exercise Peter Huang CMDL Divide
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[], char* env[]) {
double ans;
if(argc != 3)
cout << "Not valid division command. Command format is : div num1(numerator) num2(denominator)" <<endl;
else {
ans = double(atoi(argv[1])) / atoi(argv[2]);
cout.precision(2);
cout << "Answer(Division) : " << ans <<endl;
}
return 0;
}
Tuesday, January 15, 2013
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment