View Poll Results: do you know

Voters
6. You may not vote on this poll
  • yes

    4 66.67%
  • no

    2 33.33%

Thread: calculator in c++

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    56

    Question calculator in c++

    i think making a calculator is a good idee
    but i need a but can i get some help here's the code

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    char answer1[200]
    char answar2[200]
    char calc[200]
    int main ()
    {
    cout ("this program allowes you to calculate like 10+10") <<endl;
    cout ("enter number 1") <<endl;
    cin.getline (answar1,ziseof(answar1);
    cout ("enter second answar" <<endl;
    cin.getline (answar2,sizeof(name))
    //here must come a code to do + with the answars does anybodu
    // knows that and puts it in varubale calc
    cout ("answar"<<calc<<"\n" <<endl;
    system ("pause");
    if x == y , y == x

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Help with what?

    EDIT: Please consider shortening your signature.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    This is my first attempt at helping somebody!

    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <stdlib>
    #include <conio>
    #include <time>
    #include <ctype>
    
    int main ()
       { 
          int x, y;
    
          cout<<"this program allowes you to calculate like 10+10\n\n";
          
          cout<<"enter number 1: ";
          cin>>x;
    
          cout<<"enter number 2: ";
          cin>>y;
    
          x=x+y;
          cout<<"\n\n\nYour total is: ";
          cout<<x;
    
          getch();
    
       return 0;
    }
    Last edited by Noobie; 02-17-2003 at 02:07 PM.
    AIM: MarderIII

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Originally posted by Noobie
    x=x+y;
    you can also do x += y

    Hmm, after looking over your code, I can't help but to wonder why you are using all those ugly global variables and including those completely redundant header files. And, though surprisingly, correct spelling can enormously improve readability This should work nicely:

    Code:
    #include <iostream>
    using namespace std;
    
    #define MAX_CHARS 20
    
    void eatWhiteSpace(char ch[])
    {
    	int i = 0, t = 0;
    
    	while((ch[i] = ch[t ++]))
    	{
    		if(ch[i] != ' ')
    			i ++;
    	}
    }
    
    int main()
    {
    	char a[MAX_CHARS], b[MAX_CHARS];
    
    	cout << "Enter an addition problem: ";
    	cin.getline(a, MAX_CHARS, '\n');
    
    	eatWhiteSpace(a);
    
    	for(int i = 0; a[i]; i ++)
    	{
    		if(a[i] == '+')
    		{
    			for(int t = 0; a[i]; t ++, i ++)
    				b[t] = a[i];
    
    			a[i + 1] = '\0';
    			break;
    		}
    	}
    
    	cout << "The sum is " << atoi(a) + atoi(b) << "\n\n";
    
    	return 0;
    }
    Last edited by abrege; 02-17-2003 at 08:10 PM.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Calculators without a GUI are kinda wierd. Do it like graphing calculators do it. Input a string, parse it and print the answer. Store variables -- stuff like that

  6. #6
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    yup..thats what i am working on..a person enter an equation in like

    ((4+5)*(6-7))/(45) etc...

    working with classes to do that...
    nextus, the samurai warrior

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    To get ya started:

    Every operator has precedence, and parenthesis "up" that precedence for that block. So lets say we only have

    3 operators:

    Code:
    Op:	Name:		Precedence Value:
    -----------------------------------------
    /	Division	3
    *	Multiply	2
    +	Addition	1
    And lets say parenthesis add 10 to that precedence value:

    Code:
    Brace:	Precedence Value:
    (	+10
    )	-10
    
    [ ] { }    -> same thing

    Now we parse the string:

    Code:
    String:   2 + (2/3 * 5/6 + 4)
    Braces:       +10       -10
    PValue:     1   13 12 13 11
    What we have to do is incorporate a stack. We push (unless they're already there) their l-values and r-values and operate. The operation leaves the answer on the

    stack. Ex: pushing 2 values and adding leaves one value on the top of the stack -> the answer.

    There are many ways to do this. Here's one way where we start with the highest value operators:

    Code:
    push 2
    push 3
    divide
    
    push 5
    push 6
    divide
    
    multiply
    
    push 4
    addition
    
    push 2
    addition
    This way uses post-fix "syntax" (make a search; I think that's how you call it). We push all the values from lowest priority to first, then operate all at once:

    EDIT: Method two doesn't work, lol, but it can if you do it right, I'm going to sleep now....

    Code:
    push 2
    push 4
    push 2
    push 3
    push 5
    push 6
    divide
    divide
    multiply
    addition
    addition
    And what's left on the stack is the answer! Hope I helped...

  8. #8
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Expanding on what speedy5 said, you should search for "building expression trees"
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    56

    Talking it workt great

    thanks noobie
    for your code here's the calcuculator
    if x == y , y == x

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    gamer, you might want to add some extra input validation
    what if I enter a letter in stead of a number?

    more information:
    http://www.augustcouncil.com/~tgibso....html#directly

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    56

    Unhappy oke but now

    if you enter 0.5
    he shows a really $$$$ answar
    PHP Code:
    #include <iomanip.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <time.h>
    #include <ctype.h>

    int main ()
       { 
          
    int xy;

          
    cout<<"this program allowes you to calculate like 10+10\n\n";
          
          
    cout<<"enter number 1: ";
          
    cin>>x;

          
    cout<<"enter number 2: ";
          
    cin>>y;

          
    x=x+y;
          
    cout<<"\n\n\nYour total is: ";
          
    cout<<x;
          
    cout <<"\n\n\n\n\n"<<endl;

          
    system ("pause");

       return 
    0;


  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640

    Re: oke but now

    Originally posted by gamer
    if you enter 0.5
    he shows a really $$$$ answar
    If i remember correct, Integers won't store anything behind the
    decimal. Use doubles or floats instead.

Popular pages Recent additions subscribe to a feed