Page 183 - ARM Based Microcontroller Projects Using MBED
P. 183
8.11 PROJECT 8—LEARN YOUR TIMES TABLES 169
/******************************************************************
CALCULATOR
==========
This is a calculator program. The user enters two numbers and the
requested operation. The result is displayed on the PC screen for
5 seconds and then the process is repeated
Author: Dogan Ibrahim
Date : August 2018
File : Calculator
********************************************************************/
#include "mbed.h"
Serial MyPC(USBTX, USBRX);
//
// Clear the screen
//
void clrscr()
{
char clrscr[] = {0x1B, '[', '2' , 'J',0};
MyPC.printf(clrscr);
}
//
// Home the cursor
//
void homescr()
{
char homescr[] = {0x1B, '[' , 'H' , 0};
MyPC.printf(homescr);
}
int main()
{
float first, second, result;
char operation;
while(1)
{
clrscr(); // Clear the screen
homescr(); // Home the cursor
MyPC.printf("\n\rSimple Calculator"); // Display heading
MyPC.printf("\n\r=================");
MyPC.printf("");
MyPC.printf("\n\rEnter First Number: ");
MyPC.scanf("%f", &first); // Read first no
MyPC.printf("\n\rEnter Second Number: ");
MyPC.scanf("%f", &second); // Read second no
MyPC.printf("\n\rEnter Operation (+-*/) : ");
MyPC.scanf("%c", &operation); // Read operation
FIG. 8.29 Program listing.
(Continued)