Thread: Calculator Operand

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    18

    Calculator Operand

    This is a part of the main function of my calculator:

    Code:
    	if(op == '+')
      					{
      						ans = ans + m;  						
      					}
      					else if(op == '-')
      					{
      						ans = ans - m;
      					}
      					else if(op == '*')
      					{
      						ans = ans * m;
      					}
      					else if(op == '/')
      					{
      						ans = ans / m;
      					}
      					else if(op == '%')
      					{
      						ans = ans % m;
      					}
      					else if(op == '^')
      					{
      						ans = ans ^ m;
    what I need to learn is on how to create "operands" like "abs" and "neg"

    so that when I input something like "abs -10 + 10" the result would be 20
    or " neg -38 + 2" the result would be 40...

    even if i tried
    Code:
    if(op == 'abs')
      {
      	if ( m >= 0)
               return m;
            else
               return -m;
    it wouldnt work...

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    strcmp or strncmp

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    18
    what statements should I use with that?

    anyway any tutorials on using strcmp or strncmp you could recomment?

    im having a hard time understanding strings...T_T

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    Greece,Athens
    Posts
    17
    Code:
    if( strcmp("abs",op)==0)
      {
      	if ( m >= 0)
               return m;
            else
               return -m;
        }
    use strcmp (string compare).it returns the difference of the ASCII values of each string.when op contains "abs", this function will return 0.don't forget to include string.h .

    Code:
     #include <string.h>

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    18
    well I put the code above the previous codes i posted.

    when my input is "abs -50"(with space) it just returns 0.

    should I change char op to int op?

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    Greece,Athens
    Posts
    17
    would you mind posting the part of the code where program receives input?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by xetehkah View Post
    well I put the code above the previous codes i posted.

    when my input is "abs -50"(with space) it just returns 0.

    should I change char op to int op?
    op should be a string (an array of characters) so the declaration of op should be char op[SIZE]; where SIZE is a positive integer of your choosing giving the size of the array.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Post the code for parsing the operator "abs".
    After coming across "abs" grab the operand by skipping over any whitespace.
    What is the datatype of "m"?

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    18
    heres more on the code.

    sorry if i cant put it all...

    Code:
    fgets(line, 80, fr);
       
       while(fgets(line, 80, fr) != NULL)
       {   		
    		p = strtok(line," ");
    		
      		while (p != NULL)  		
      		{  			
      			n = n++;
    
    some code here....
    
      				else
      				{
      					if(*p == 'U')
      					{
      						m = U;
      					}
      					else if(*p == 'P')
      					{
      						m = P;
      					}
      					else
      					{
      						m = atoi(p);
      					}					
      					if(op == '+')
      					{
      						ans = ans + m;  						
      					}
      					else if(op == '-')
      					{
      						ans = ans - m;
      					}
      					else if(op == '*')
      					{
      						ans = ans * m;
      					}
      					else if(op == '/')
      					{
      						ans = ans / m;
      					}
      					else if(op == '%')
      					{
      						ans = ans % m;
      					}
      					else if(op == '^')
      					{
      						ans = ans ^ m;
      					}
      				}
      			}
    i tried putting if
    Code:
    ( strcmp("abs",op)==0)
      {
      	if ( m >= 0)
               return m;
            else
               return -m;
        }
    above the addition code but it wouldnt work...

    this calculator uses fprintf and fscanf...~_~

    another thing how do i get the exponent to work?

    it doesnt seem to work...~_~
    Last edited by xetehkah; 02-10-2009 at 04:20 AM.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
      					if(op == '+')
      					{
      						ans = ans + m;  						
      					}
      					else if(op == '-')
      					{
      						ans = ans - m;
      					}
      					else if(op == '*')
      					{
      						ans = ans * m;
      					}
      					else if(op == '/')
      					{
      						ans = ans / m;
      					}
      					else if(op == '%')
      					{
      						ans = ans % m;
      					}
      					else if(op == '^')
      					{
      						ans = ans ^ m;
      					}
    Surely this should be a switch-statement.

    Red: Are you actually intending to do exclusive or - or did you mean "to the power of", in which case your code should not use ^.

    Code:
    ( strcmp("abs",op)==0)
      {
      	if ( m >= 0)
               return m;
            else
               return -m;
        }
    What is op, and how was it assigned?
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    18
    Code:
       int n = 0, m = 0, ans = 0, lcnt = 0, U = 0, P = 0;
       char line[80], op;
       char *p;
    as for the power, it should use the "^" symbol~
    like 5 ^ 2 = 25

    T_T

    should I use pow()?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by xetehkah View Post
    Code:
       int n = 0, m = 0, ans = 0, lcnt = 0, U = 0, P = 0;
       char line[80], op;
       char *p;
    as for the power, it should use the "^" symbol~
    like 5 ^ 2 = 25

    T_T

    should I use pow()?
    So, op is a single character, so it can NEVER match "abs", can it?

    And yes, you need to use pow() or something similar for the ^ operator.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    18
    so how should I inset pow() in that?

    i tried changing op to op[size] but the other operators had the problem...

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by xetehkah View Post
    so how should I inset pow() in that?
    Not sure what you mean with that... You need to use pow() to calculate power - unless you write your own code to do something similar.

    i tried changing op to op[size] but the other operators had the problem...
    You haven't really shown us the bit of code that does the "split input into operations and operands", so it's hard to know what you need to do in understanding your expression and turning "ABS" into an "absolute operator".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Just a few hints.

    • Strings in C(Or character arrays to be specific), cannot be compared to each other using the comparison operator (==)
    • Use strncmp for C-style string comparisons
    • Make sure that op is a character array, not a single character


    That's all we can tell you if you don't post all the code. Do so, and you will receive more help.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed