C language implementation of a calculator

Source: Internet
Author: User

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: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Y2FsLmM6PC9wPgo8cD48cHJlIGNsYXNzPQ = "brush: java;" >#include # Include 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:

 
   -> 
  
    {  
   
     } -> + | -
    
      -> 
     
       { 
       
       
         }
        
          -> * | /
         
           -> ( 
          
            ) | Number
          
         
        
       
      
     
    
   
  
 

We will not go into details about EBNF here.

Related Article

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.