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

Committer:
andrelongo85
Date:
Mon Sep 22 03:12:09 2014 +0000
Revision:
1:247161a5c5d6
Parent:
0:879aa9d0247b
Child:
2:0baf535ed8f8
Serial Integer Calculator First Draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:879aa9d0247b 1 #include "mbed.h"
andrelongo85 1:247161a5c5d6 2
andrelongo85 1:247161a5c5d6 3 #define FALSE 0
andrelongo85 1:247161a5c5d6 4 #define TRUE 1
andrelongo85 1:247161a5c5d6 5
mbed_official 0:879aa9d0247b 6 Serial pc(USBTX, USBRX); // tx, rx
andrelongo85 1:247161a5c5d6 7
andrelongo85 1:247161a5c5d6 8 // function to convert a char buffer to decimal inetger
andrelongo85 1:247161a5c5d6 9 int char_to_int (const char *char_p, const int size)
andrelongo85 1:247161a5c5d6 10 {
andrelongo85 1:247161a5c5d6 11 int tmp = 0;
andrelongo85 1:247161a5c5d6 12 int pot_ten=1;
andrelongo85 1:247161a5c5d6 13 for ( int i= (size -1); i >= 0 ; i-- ) {
andrelongo85 1:247161a5c5d6 14 tmp = int( tmp + ( ( int(char_p[i]) - 48 ) * pot_ten ));
andrelongo85 1:247161a5c5d6 15 pot_ten*=10;
andrelongo85 1:247161a5c5d6 16 //pc.printf("\n");
andrelongo85 1:247161a5c5d6 17 //print_num(tmp);
andrelongo85 1:247161a5c5d6 18 //pc.printf("\n");
andrelongo85 1:247161a5c5d6 19 //print_num(pot_ten);
andrelongo85 1:247161a5c5d6 20 }
andrelongo85 1:247161a5c5d6 21 return tmp;
andrelongo85 1:247161a5c5d6 22 }
andrelongo85 1:247161a5c5d6 23
andrelongo85 1:247161a5c5d6 24
andrelongo85 1:247161a5c5d6 25 // check if the input is valid
andrelongo85 1:247161a5c5d6 26 bool si_check_valid(const char & input)
andrelongo85 1:247161a5c5d6 27 {
andrelongo85 1:247161a5c5d6 28 if ( ( ( int(input) > 57) || ( int(input) < 48) ) && (input != '+') && (input != '-') && (input != '*') && (input != '/') && (input != '=') ) {
andrelongo85 1:247161a5c5d6 29 return FALSE;
andrelongo85 1:247161a5c5d6 30 } else {
andrelongo85 1:247161a5c5d6 31 return TRUE;
andrelongo85 1:247161a5c5d6 32 }
andrelongo85 1:247161a5c5d6 33 }
andrelongo85 1:247161a5c5d6 34
andrelongo85 1:247161a5c5d6 35 //check if the input is a number
andrelongo85 1:247161a5c5d6 36 bool si_check_number(const char & input)
andrelongo85 1:247161a5c5d6 37 {
andrelongo85 1:247161a5c5d6 38 if ( (int(input) >= 48) && ( int(input) <= 57) ) {
andrelongo85 1:247161a5c5d6 39 return TRUE;
andrelongo85 1:247161a5c5d6 40 } else {
andrelongo85 1:247161a5c5d6 41 return FALSE;
andrelongo85 1:247161a5c5d6 42 }
andrelongo85 1:247161a5c5d6 43 }
andrelongo85 1:247161a5c5d6 44
mbed_official 0:879aa9d0247b 45
andrelongo85 1:247161a5c5d6 46
andrelongo85 1:247161a5c5d6 47 int main()
andrelongo85 1:247161a5c5d6 48 {
andrelongo85 1:247161a5c5d6 49 char c;
andrelongo85 1:247161a5c5d6 50 char op = '=';
andrelongo85 1:247161a5c5d6 51 char operand [100];
andrelongo85 1:247161a5c5d6 52 char buf[10];
andrelongo85 1:247161a5c5d6 53 int result =0;
andrelongo85 1:247161a5c5d6 54 int index = 0;
andrelongo85 1:247161a5c5d6 55 bool first = TRUE;
andrelongo85 1:247161a5c5d6 56
andrelongo85 1:247161a5c5d6 57 pc.printf("LPC Integer Calculator!\n");
andrelongo85 1:247161a5c5d6 58
mbed_official 0:879aa9d0247b 59 while(1) {
andrelongo85 1:247161a5c5d6 60
andrelongo85 1:247161a5c5d6 61 c=pc.getc(); //get serial input
andrelongo85 1:247161a5c5d6 62
andrelongo85 1:247161a5c5d6 63 if (!si_check_valid(c)) { // Invalid Input
andrelongo85 1:247161a5c5d6 64 pc.putc(c);
andrelongo85 1:247161a5c5d6 65 printf ("Invalid Input!\n");
andrelongo85 1:247161a5c5d6 66 } else if (si_check_number(c)) { // Operand has been entered
andrelongo85 1:247161a5c5d6 67 pc.putc(c);
andrelongo85 1:247161a5c5d6 68 operand[index] = c;
andrelongo85 1:247161a5c5d6 69 index++;
andrelongo85 1:247161a5c5d6 70 } else { //Operator has been entered
andrelongo85 1:247161a5c5d6 71
andrelongo85 1:247161a5c5d6 72 pc.putc('\n');
andrelongo85 1:247161a5c5d6 73 pc.putc(c);
andrelongo85 1:247161a5c5d6 74 pc.putc('\n');
andrelongo85 1:247161a5c5d6 75
andrelongo85 1:247161a5c5d6 76 if (first) { //First Operator
andrelongo85 1:247161a5c5d6 77 result = char_to_int(operand,index);
andrelongo85 1:247161a5c5d6 78 first = FALSE;
andrelongo85 1:247161a5c5d6 79 } else { //Do Calculation
andrelongo85 1:247161a5c5d6 80
andrelongo85 1:247161a5c5d6 81 if(op == '+') {
andrelongo85 1:247161a5c5d6 82 result = int (result + char_to_int(operand,index));
andrelongo85 1:247161a5c5d6 83 } else if (op == '-') {
andrelongo85 1:247161a5c5d6 84 result = int (result - char_to_int(operand,index));
andrelongo85 1:247161a5c5d6 85 } else if (op == '/') {
andrelongo85 1:247161a5c5d6 86 result = int (result / char_to_int(operand,index));
andrelongo85 1:247161a5c5d6 87 } else if (op == '*') {
andrelongo85 1:247161a5c5d6 88 result = int (result * char_to_int(operand,index));
andrelongo85 1:247161a5c5d6 89 }
andrelongo85 1:247161a5c5d6 90
andrelongo85 1:247161a5c5d6 91 if (c == '=') { // Operator = has been entered--> Print result and reset calculator
andrelongo85 1:247161a5c5d6 92 for (int j =9; j>=0; j--){buf[j]='\0';}
andrelongo85 1:247161a5c5d6 93 sprintf(buf, "%d", result);
andrelongo85 1:247161a5c5d6 94 pc.printf("%s", buf);
andrelongo85 1:247161a5c5d6 95 pc.printf("\n\n");
andrelongo85 1:247161a5c5d6 96 pc.printf("Please enter a new operation\n");
andrelongo85 1:247161a5c5d6 97 first = TRUE;
andrelongo85 1:247161a5c5d6 98 }
andrelongo85 1:247161a5c5d6 99 }
andrelongo85 1:247161a5c5d6 100 op = c; // assign the Operator
andrelongo85 1:247161a5c5d6 101 index=0; // reset the operand
andrelongo85 1:247161a5c5d6 102 }
mbed_official 0:879aa9d0247b 103 }
andrelongo85 1:247161a5c5d6 104 }
andrelongo85 1:247161a5c5d6 105