From a2731b9caee24f46169134854b7ebc2897a99bc3 Mon Sep 17 00:00:00 2001 From: Menna Date: Mon, 14 Dec 2015 23:09:49 +0200 Subject: [PATCH] ~ --- .gitignore | 4 ++ README.md | 23 +++++++++ main.cpp | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++ operations.h | 130 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 292 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.cpp create mode 100644 operations.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f210bb3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.exe +*.o +*.gch +a.out diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f5f499 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Training Project: calculator +This is a calculator +It is supposed to take commands on the form of `awesomecalculator add 3 5`
+and the output would be `8`
+so `argv[1]` is the operation. ex. add, subtract, multiply, divide, absolute, negate, power, root, log, ln, factorial, sin, cos and tan.
+`argv[2]` and `argv[3]` are the operands.
+everyone can implement one operation function.
+and someone will implement the main function body.
+if all functions were already implemented, come up with a new one and make it :D + +## People who have contributed to the project so far: +
    +
  1. Eslam Romario
  2. +
  3. Mohamed Medhat
  4. +
  5. Omar Alsawaf
  6. +
  7. Abdo Alaa
  8. +
  9. Yasmin Ghazy
  10. +
  11. Yara Yasser
  12. +
  13. Saieed Osama
  14. +
  15. Omneya Yahya
  16. +
  17. Sara Rabie
  18. +
  19. Omar M. Ali
  20. +
diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..3214627 --- /dev/null +++ b/main.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include "operations.h" +#include +#define PI 3.14159265 + + +using namespace std; + +float str_to_float(string str) +{ + istringstream iss(str); + float value; + if (iss >> value) + return value; + else + return NULL; +} + +int main(int argc, char* argv[]) +{ + if (argc <= 2) + { + cerr << "too few arguments" << endl; + return 1; + } + else if (argc == 3) + { + string operation = argv[1]; + float num1 = str_to_float(argv[2]); + + if (num1 == NULL) + { + cerr << "invalid input" << endl; + return 1; + } + + if (operation == "absolute") + { + cout << absolute(num1) << endl; + } + else if (operation == "negate") + { + cout << negate(num1) << endl; + } + else if (operation == "root") + { + // cout << root(num1) << endl; + } + else if (operation == "log") + { + cout << _log(num1) << endl; + } + else if (operation == "ln") + { + cout << ln(num1) << endl; + } + else if (operation == "factorial") + { + cout << factorial(num1) << endl; + } + else if (operation == "sin") + { + cout << _sin(num1) << endl; + } + else if (operation == "cos") + { + cout << cos ( num1 * PI / 180.0 ) ; + + } + else if (operation == "tan") + { + cout << _tan (num1); + } + else + { + cerr << "invalid input" << endl; + return 1; + } + } + else if (argc == 4) + { + string operation = argv[1]; + float num1 = str_to_float(argv[2]); + float num2 = str_to_float(argv[3]); + + if (num1 == NULL || num2 == NULL) + { + cerr << "invalid input" << endl; + return 1; + } + + if (operation == "add") + { + // cout << add(num1, num2) << endl; + } + else if (operation == "subtract") + { + cout << subtract(num1, num2) << endl; + } + else if (operation == "multiply") + { + // cout << multiply(num1, num2) << endl; + } + else if (operation == "divide") + { + cout << divide(num1, num2) << endl; + } + else if (operation == "power") + { + cout << powerfunction(num1, num2) << endl; + } + else if (operation == "root") + { + // cout << root(num1, num2) << endl; + } + else if (operation == "log") + { + cout << _log(num1, num2) << endl; + } + else + { + cerr << "invalid input" << endl; + return 1; + } + } + else + { + cerr << "too many arguments" << endl; + return 1; + } + + return 0; +} diff --git a/operations.h b/operations.h new file mode 100644 index 0000000..59dddb2 --- /dev/null +++ b/operations.h @@ -0,0 +1,130 @@ +#include + +using namespace std; + +#define PI 3.14159265 + +float _tan (float num1) +{ + float result =tan(num1*PI/180); + + return result; +} + +float subtract(float num1, float num2) +{ + float result = num1-num2 ; + return result; +} + + +float _sin(float rate) +{ + float result,r; + r=rate/180; + int i=r; + if (i-r==0) + { + result=0; + } + else + { + result = sin ((rate*PI)/180); + } + return result; +} + + +double _log(double x) +{ + return (log10(x)); +} + +double _log(double base, double x) { + return (double)(log(x) / log(base)); +} + +double powerfunction(double base, double power){ + + double f = base; + for (int i = 1; i < power; i++) + f *= base; + return f; +} + + +float factorial(float n) +{ + + float result=1; + + + for (float i =1;i<=n;i++){ + result*=i;} + + return result; +} + +double _negate (double num){ + return num*(-1); +} + +double add (double n1 , double n2){ + return n1+n2; +} + +double root (double num){ + return sqrt(num); +} + +double divide(double n1 , n2){ + if(n2!=0) + return n1/n2; + else cout<<"AN error"; + return 0; +} + +double absolute (double num){ + return (num<0?-1*num:num); +} + +unsigned long long factorial(double num){ + unsigned long long fac=1; + for(int i = 1 ; i <= num ; i++) + fac*=i; + return fac; +} + +long double ln(long double x) +{ + //***********Variable Declarations************/ + long double count = 0; //Keeps track of the count in the loop + long double totalValue = 0; //The summation of each polynomial evaluated + bool reciprocal = false; //Flag to use if greater than 2 + long double precision = 9; //The the highest order of polynomial to use. + + if (x > 2.0) //Use the rule -ln(1/x) == ln(x) to keep accuracy + { + x = 1 / x; //Change to using 1/x rather than x + reciprocal = true; //Flag as true (sign change is later) + } + + while (count < precision) + { + totalValue += pow(-1, count + 1) * (pow((x-1),count)/count); + count++; + } + + if (reciprocal == true) + totalValue * -1; //If reciprocal was used multiply by -1 to change sign + + return totalValue; +} + +double divide(double num1, double num2) +{ +if(num2 == 0) +cout<<"Invalid, the second number must be > 0"<