This simple program enable LPC11U24 MBED board to act as a simple integer calculator. Just type the operation you want to execute from the serial terminal. Admitted operations: +, -, * /, ^ (only positive exponent). Multiple operations in raw are supported as well as negative integer.

Dependencies:   mbed

Fork of Serial_HelloWorld_Mbed by mbed official

main.cpp

Committer:
andrelongo85
Date:
2014-09-22
Revision:
1:247161a5c5d6
Parent:
0:879aa9d0247b
Child:
2:0baf535ed8f8

File content as of revision 1:247161a5c5d6:

#include "mbed.h"

#define FALSE 0
#define TRUE 1

Serial pc(USBTX, USBRX); // tx, rx

// function to convert a char buffer to decimal inetger
int char_to_int (const char *char_p, const int size)
{
    int tmp = 0;
    int pot_ten=1;
    for ( int i= (size -1); i >= 0 ; i-- ) {
        tmp = int( tmp + ( ( int(char_p[i]) - 48 ) * pot_ten ));
        pot_ten*=10;
        //pc.printf("\n");
        //print_num(tmp);
        //pc.printf("\n");
        //print_num(pot_ten);
    }
    return tmp;
}


// check if the input is valid
bool si_check_valid(const char & input)
{
    if ( ( ( int(input) > 57) || ( int(input) < 48) ) && (input != '+') && (input != '-') && (input != '*') && (input != '/') && (input != '=') ) {
        return FALSE;
    } else {
        return TRUE;
    }
}

//check if the input is a number
bool si_check_number(const char & input)
{
    if ( (int(input) >= 48) && ( int(input) <= 57) ) {
        return TRUE;
    } else {
        return FALSE;
    }
}

 

int main()
{
    char c;
    char op = '=';
    char operand [100];
    char buf[10];
    int result =0;
    int index = 0;
    bool first = TRUE;

    pc.printf("LPC Integer Calculator!\n");
    
    while(1) {
 
        c=pc.getc();  //get serial input

        if (!si_check_valid(c)) { // Invalid Input
            pc.putc(c);
            printf ("Invalid Input!\n");
        } else if (si_check_number(c)) { // Operand has been entered
            pc.putc(c);
            operand[index] = c;
            index++;
        } else { //Operator has been entered
        
            pc.putc('\n');
            pc.putc(c);
            pc.putc('\n');

            if (first) {  //First Operator
                result = char_to_int(operand,index);
                first = FALSE;
            } else { //Do Calculation

                if(op == '+') {
                    result = int (result + char_to_int(operand,index));
                } else if (op == '-') {
                    result = int (result - char_to_int(operand,index));
                } else if (op  == '/') {
                    result = int (result / char_to_int(operand,index));
                } else if (op == '*') {
                    result = int (result * char_to_int(operand,index));
                }

                if (c == '=') { // Operator = has been entered--> Print result and reset calculator
                    for (int j =9; j>=0; j--){buf[j]='\0';}
                    sprintf(buf, "%d", result);
                    pc.printf("%s", buf);
                    pc.printf("\n\n");
                    pc.printf("Please enter a new operation\n");
                    first = TRUE;
                }
            }
            op = c;  // assign the Operator
            index=0; // reset the operand
        }
    }
}