C language implementation of a calculator and C language implementation of a calculator

Source: Internet
Author: User

C language implementation of a calculator and C language implementation of a calculator

Today, I read compilation principles and practices and saw the implementation of a simple Integer Calculator.

Based on the ideas in the book, I made some extensions:

1. Extend From Integer Calculator to decimal calculator.

2. Division is supported.

3. Empty characters are supported.

The running effect is as follows:


The Code is as follows:

Cal. c:

#include <stdio.h>#include <stdlib.h>char token;double exp(void);double term(void);double factor(void);char getPrintableChar(void);void match(char expectedToken);void error(void);int main(void){double result;for (;;){token = getPrintableChar();if (token == 'q')break;result = exp();if (token == '\n')printf("Result is: %g\n", result);elseerror();}return 0;}double exp(void){double temp = term();while (token == '+' || token == '-')switch(token){case '+': match('+');  temp += term();  break;case '-': match('-');  temp -= term();  break;}return temp;}double term(void){double temp = factor();while (token == '*' || token == '/')switch(token){case '*': match('*');  temp *= factor();  break;case '/': match('/');  temp /= factor();  break;}return temp;}double factor(void){double temp;if (token == '('){match('(');temp = exp();match(')');} else if (isdigit(token)){ungetc(token, stdin);scanf("%lf", &temp);token = getPrintableChar();} elseerror();return temp;}void error(void){fprintf(stderr, "Error!\n");exit(EXIT_FAILURE);}void match(char expectedToken){if (expectedToken == token)token = getPrintableChar();elseerror();}char getPrintableChar(void){char temp;dotemp = getchar();while (isblank(temp));return temp;}

The program implementation logic is implemented according to the EBNF rules, namely:

<exp> -> <term> { <addop> <term> }<addop> -> + | -<term> -> <factor> { <mulop> <factor> }<mulop> -> * | /<factor> -> ( <exp> ) | Number

We will not go into details about EBNF here.


How to use C language to implement a simple calculator

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Int main ()
{
Char s [101], x [101], y [101], * t, op;
Gets (s );
Int I, j, n = strlen (s );
Double a, B;
For (I = j = 0; s [I]! = ''; I ++)
X [j ++] = s [I];
X [j] = 0;
A = strtodd (x, & t );
I ++;
For (j = 0; I <n; I ++)
{
If (j = 0)
{Op = s [I ++];
I ++;
}
Y [j ++] = s [I];
If (s [I] = '')
{
Y [j] = 0;
B = strtodd (y, & t );
J = 0;

}
If (j = 0)
Switch (op)
{
Case '+': a + = B; break;
Case '-': a-= B; break;
Case '*': a * = B; break;
Case '/': a/= B; break;
};
}
Printf ("% f \ n", );
}

Implement a simple arithmetic calculator by using C language programming

# Include <stdio. h>

// Function, read Operations
Int getNextNum ()
{
Int ret;
Scanf ("% d", & ret );
Return ret;
}

// Function, read Operator
Char getOpt ()
{
Return getchar ();
}

// Function, computing
Int caculate (int op1, int op2, char opt)
{
If (opt = '+') return op1 + op2;
If (opt = '-') return op1-op2;
If (opt = '*') return op1 * op2;
If (opt = '/') return op1/op2;
Return 0;
}

Int main ()
{
Int op1, op2;
Char opt;
// Place the calculation result in the first operand
Op1 = getNextNum ();
While (1)
{
Opt = getOpt ();
If (opt = ') break;
Op2 = getNextNum ();
Op1 = caculate (op1, op2, opt );
}
Printf ("% d \ n", op1 );
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.