0% found this document useful (0 votes)
62 views5 pages

T14 Calculator

Uploaded by

Usama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views5 pages

T14 Calculator

Uploaded by

Usama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Complete Example Calculator 1

Consider the simple calculator program on the following slides. The program reads
simple integer expressions from an input file and evaluates them:

Input: 17 + 43 Output: 17 + 43 = 60
67 - 14 67 - 14 = 53
89 * 12 89 * 12 = 1068
43 / 7 43 / 7 = 6
No more expressions.

This program also illustrates using the placement of function prototypes to restrict access
so that only functions that need to make calls can do so.

CS@VT June 2009 Intro Programming in C++ ©1995-2009 Barnette ND, McQuain WD & Kennan MA
Modular Decomposition Calculator 2

An analysis of the formal specification leads to the following design:

main

open I/O files While there are expressions to process close I/O files

NextExpression Print

GetOps If expression is OK If sentinel value returned

. DoCalc Stop
.
.
.
.
.

CS@VT June 2009 Intro Programming in C++ ©1995-2009 Barnette ND, McQuain WD & Kennan MA
Simple Calculator Calculator 3
. . .
int main( ) {
int NextExpression(ifstream& In, int& Op1, char& Op, int& Op2);
void Print(int Op1, char Op, int Op2, int Val);
int Operand1, Operand2, Value = 0;
char Operator;
ifstream iFile;

iFile.open("Calculator.data");
while (iFile && Value != INT_MIN) {
Value = NextExpression(iFile, Operand1, Operator, Operand2);
if (Value == INT_MIN) {
cout << “No more expressions.” << endl;
iFile.close();
return 0;
}
Print(Operand1, Operator, Operand2, Value);
}
iFile.close(); // probably never executed, included for safety
return 0;
}

CS@VT June 2009 Intro Programming in C++ ©1995-2009 Barnette ND, McQuain WD & Kennan MA
Simple Calculator Calculator 4
int NextExpression(ifstream& In, int& Op1, char& Op, int& Op2) {
int DoCalc(int Op1, char Op, int Op2);
void GetOps(ifstream& In, int& Op1, char& Op, int& Op2);

GetOps(In, Op1, Op, Op2); // get expression

if (In)
return DoCalc(Op1, Op, Op2); // evaluate if OK
else
return INT_MIN; // return flag if not
}

void GetOps(ifstream& In, int& Op1, char& Op, int& Op2) {

In >> Op1 >> Op >> Op2; // read expression


In.ignore(80, '\n'); // skip to beginning of next line
return;
}

CS@VT June 2009 Intro Programming in C++ ©1995-2009 Barnette ND, McQuain WD & Kennan MA
Simple Calculator Calculator 5

void Print(int Op1, char Op, int Op2, int Val) {


cout << setw(5) << Op1 << ' ' << Op << ' '
<< setw(5) << Op2 << " = "
<< setw(5) << Val << endl;
}

int DoCalc(int Op1, char Op, int Op2) {


int Result;

switch ( Op ) { // consider operation


case '+': Result = Op1 + Op2;
break;
case '-': Result = Op1 - Op2;
break;
case '*': Result = Op1 * Op2;
break;
case '/': Result = Op1 / Op2; // no protection against Op2 == 0
break;
default: Result = INT_MIN; // return flag if operation
} // is invalid
return Result;
}

CS@VT June 2009 Intro Programming in C++ ©1995-2009 Barnette ND, McQuain WD & Kennan MA

You might also like