Skip to content

Commit

Permalink
~
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuLuNAx committed Dec 14, 2015
0 parents commit a2731b9
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.exe
*.o
*.gch
a.out
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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` <br/>
and the output would be `8` <br/>
so `argv[1]` is the operation. ex. add, subtract, multiply, divide, absolute, negate, power, root, log, ln, factorial, sin, cos and tan.<br/>
`argv[2]` and `argv[3]` are the operands.<br/>
everyone can implement one operation function.<br/>
and someone will implement the main function body.<br/>
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:
<ol>
<li>Eslam Romario</li>
<li>Mohamed Medhat</li>
<li>Omar Alsawaf</li>
<li>Abdo Alaa</li>
<li>Yasmin Ghazy</li>
<li>Yara Yasser</li>
<li>Saieed Osama</li>
<li>Omneya Yahya</li>
<li>Sara Rabie</li>
<li>Omar M. Ali</li>
</ol>
135 changes: 135 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include <iostream>
#include <sstream>
#include <string>
#include "operations.h"
#include <math.h>
#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;
}
130 changes: 130 additions & 0 deletions operations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <math.h>

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"<<endl;
double result = num1/num2;
return result;
}

0 comments on commit a2731b9

Please sign in to comment.