Thursday, September 13, 2012

calculator code in c++ without using + , - , x , / and % operator

#include<iostream.h>
int main ()
{
    char ch;
    do
    {
int a,b,result=0,count=0,num,c=0,d=0;
char op;
do
{
cout<<"\t      \"Enter an operator from following options\" "<<endl;
cout<<"\t\t    Enter '!' to Invert sign"<<endl;
cout<<"\t\t    Enter '+' for Addition"<<endl;
cout<<"\t\t    Enter '-' for Subtraction"<<endl;
cout<<"\t\t    Enter '*' for Multiplication"<<endl;
cout<<"\t\t    Enter '/' for Division"<<endl;
cout<<"\t\t    Enter '%' for Remainder"<<endl;
cout<<endl<<"Please enter your option from list given above : ";
cin>>op;
if(op!='!' && op!='+' && op!='-' && op!='*' && op!='/' && op!='%')
cout<<endl<<endl<<"\a\"Please enter a valid character\" "<<endl<<endl;
}
while(op!='!' && op!='+' && op!='-' && op!='*' && op!='/' && op!='%');
switch (op)
{
case '!':
    cout<<endl<<"Enter a number to invert the sign :";
    cin>>a;
    if(a>0)
    {
        while(a)
        {
            result--;
            a--;
        }
        cout<<"The inverted number is "<<result<<endl;
    }
    else if (a<0)
    {
        while(a)
        {
            result++;
            a++;
        }
        cout<<"The inverted number is "<<result<<endl;
    }
    break ;
case '+':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"The sum is "<<a<<endl;
    /*else if (a==0)
        cout<<endl<<"The sum is "<<b<<endl;*/
    if(b>0)
    {
        while (b)
        {
            a++;
            b--;
        }
        cout<<endl<<"The sum is "<<a<<endl;
    }
    else if( b<0)
    {
        while (b)
        {
            a--;
            b++;
        }
        cout<<endl<<"The sum is "<<a<<endl;
    }
    break;
case '-':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"The difference is "<<a<<endl;
    if( b>0)
    {
        while (b)
        {
            a--;
            b--;
        }
        cout<<endl<<"The difference is "<<a<<endl;
    }
    else if( b<0)
    {
        while (b)
        {
            a++;
            b++;
        }
        cout<<endl<<"The difference is "<<a<<endl;
    }
    break;
case '*':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(a>=0 && b>=0)
    {
        for(a;a>0;a--)
        {
            num=b;
            for(;num>0;num--)
            {
                result++;
            }
        }
        cout<<endl<<"The product is "<<result<<endl;
    }
    else if (a<0 && b<0)
    {
        while(a)
        {
            c++;
            a++;
        }
        while(b)
        {
            d++;
            b++;
        }
        for(c;c>0;c--)
        {
            num=d;
            for(;num>0;num--)
            {
                result++;
            }
        }
        cout<<endl<<"The product is "<<result<<endl;
    }
    else if (a>=0 && b<0)
    {
        while(b)
        {
            d++;
            b++;
        }
        for(a;a>0;a--)
        {
            num=d;
            for(;num>0;num--)
            {
                result++;
            }
        }
        while(result)
        {
            result--;
            c--;
        }
        cout<<endl<<"The product is "<<c<<endl;
    }
    else if (a<0 && b>=0)
    {
        while(a)
        {
            c++;
            a++;
        }
        for(c;c>0;c--)
        {
            num=b;
            for(;num>0;num--)
            {
                result++;
            }
        }
        while(result)
        {
            result--;
            d--;
        }
        cout<<endl<<"The product is "<<d<<endl;
    }
    break;
case '/':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"Division is not possible "<<endl;
    else if(a==0)
        cout<<endl<<"The quotient is 0 "<<endl;
    if(a>0 && b>0)
    {
    while (a)
    {
        num=b;
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        count++;
    }
    cout<<endl<<"The quotient is "<<count<<endl;
    }
    else if (a<0 && b>0)
    {
       
        while(a)   
        {       
            c++;       
            a++;       
        }
        while(c)
        {
        num=b;   
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        count++;
        }
        while(count)
        {
            count--;
            d--;
        }
    cout<<endl<<"The quotient is "<<d<<endl;
    }
    else if (a>0 && b<0)
    {
       
        while(b)   
        {       
            d++;       
            b++;       
        }
        while(a)
        {
        num=d;   
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        count++;
        }
        while(count)
        {
            count--;
            c--;
        }
    cout<<endl<<"The quotient is "<<c<<endl;
    }
    else if (a<0 && b<0)
    {
       
        while(b)   
        {       
            d++;       
            b++;       
        }
        while(a)   
        {       
            c++;       
            a++;       
        }
        while(c)
        {
        num=d;   
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        count++;
        }
       
       
    cout<<endl<<"The quotient is "<<count<<endl;
    }
    break;
case '%':
    cout<<endl<<"Enter first number :";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    if(b==0)
        cout<<endl<<"Division is not possible "<<endl;
    else if(a==0)
        cout<<endl<<"The remainder is "<<b<<endl;
    if(a>0 && b>0)
    {
    while (a)
    {
        num=b;
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        if(a<b)
            break;
    }
        cout<<endl<<"The remainder is "<<a<<endl;
    }
    if(a<0 && b>0)
    {
        while(a)
        {
            c++;
            a++;
        }
    while (c)
    {
        num=b;
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        if(c<b)
            break;
    }
    while(c)
        {
            d++;
            c++;
        }
        cout<<endl<<"The remainder is "<<d<<endl;
    }
    if(a>0 && b<0)
    {
        while(b)
        {
            c++;
            b++;
        }
    while (a)
    {
        num=c;
        while(num>0 && a>=num)
        {
            num--;
            a--;
        }
        if (a>0 && a<num)
        {
            a=0;
            count--;
        }
        if(a<c)
            break;
    }
        cout<<endl<<"The remainder is "<<a<<endl;
    }
        if(a<0 && b<0)
    {
        while(b)
        {
            d++;
            b++;
        }
        while(a)
        {
            c++;
            a++;
        }
    while (c)
    {
        num=d;
        while(num>0 && c>=num)
        {
            num--;
            c--;
        }
        if (c>0 && c<num)
        {
            c=0;
            count--;
        }
        if(c<d)
            break;
    }    while(c)
        {
            result--;
            c--;
        }
        cout<<endl<<"The remainder is "<<result<<endl;
    }
    break;
}
cout<<endl<<"Do you want to calculate again [y/n] ";
cin>>ch;
}
while(ch=='y' || ch=='Y');
cout<<endl<<endl<<"Take Care ... Goodbye :)"<<endl;
return 0;
}

No comments:

Post a Comment