Question
THIS IS FOR C++ ONLY!!! PSUEDOCODE OF PROGRAMMING LAB module Lab08 function main() int call kalk_init() call kalk_interpret() return 0 end function main end module
THIS IS FOR C++ ONLY!!!
PSUEDOCODE OF PROGRAMMING LAB
module Lab08
function main() int
call kalk_init()
call kalk_interpret()
return 0
end function main
end module Lab08
module Kalk
-- In C++, a variable is a global variable if it is defined outside the functions of the file. The scope of a local variable is
-- from the location where it is defined to the end of the source code file, so these three global variables are accessible
-- from every function in this file.
define global double variable g_reg_a -- this is the a register
define global double variable g_reg_b -- this is the b register
define global double variable g_reg_c -- this is the c register
function kalk_add() -- implements the add operator
g_reg_a g_reg_a + g_reg_b
end function kalk_add
function kalk_atob() -- implements the atob operator
g_reg_b g_reg_a
end function kalk_atob
function kalk_atoc() -- implements the atoc operator
g_reg_c g_reg_a
end function kalk_atoc
function kalk_btoa() -- implements the btoa operator
g_reg_a g_reg_b
end function kalk_btoa
function kalk_btoc() -- implements the btoc operator
g_reg_c g_reg_b
end function kalk_btoc
function kalk_ctoa() -- implements the ctoa operator
g_reg_a g_reg_c
end function kalk_ctoa
function kalk_ctob() -- implements the ctob operator
g_reg_b g_reg_c
end function kalk_ctob
function kalk_display() -- implements the d operator
define char variable reg
reg read char from keybd -- read the register name to display from the user
if reg is 'a' then
send g_reg_a, endl to cout
else if reg is 'b' then
send g_reg_b, endl to cout
else if reg is 'c' then
send g_reg_c, endl to cout
else
send "Invalid register operand: ", reg, endl to cout
end if
end function kalk_display
function kalk_div() -- implements the / operator
g_reg_a g_reg_a / g_reg_b
end function kalk_div
function kalk_init() -- initializes the global variables for the registers to 0's
g_reg_a 0
g_reg_b 0
g_reg_c 0
end function kalk_init
function kalk_interpret()
define a string object named oper
oper kalk_read_operator()
while oper "q" do
if oper is "+" then
kalk_add()
else if oper is "atob" then
kalk_atob()
else if oper is "atoc" then
kalk_atoc()
else if oper is "btoa" then
kalk_btoa()
else if oper is "btoc" then
kalk_btoc()
else if oper is "ctoa" then
kalk_ctoa()
else if oper is "ctob" then
kalk_ctob()
else if oper is "d" then
kalk_display()
else if oper is "/" then
kalk_div()
else if oper is "l" then -- this is an ell not a one
kalk_load()
else if oper is "*" then
kalk_mul()
else if oper is "^" then
kalk_power()
else if oper is "&" then
kalk_sqrt()
else if oper is "-" then
kalk_sub()
else
send "Invalid operator: ", oper, endl to cout
end if
oper kalk_read_operator()
end while
end function kalk_interpret
function kalk_load()
define char variable reg
reg read char from keybd
if reg is 'a' then
g_reg_a read real number from keybd
else if reg is 'b' then
g_reg_b read real number from keybd
else if reg is 'c' then
g_reg_c read real number from keybd
else send "Invalid register operand: ", reg, endl to cout
end if
end function kalk_load
function kalk_mul()
g_reg_a g_reg_a * g_reg_b
end function kalk_mul
function kalk_power()
g_reg_a g_reg_a g_reg_b
end function kalk_power
function kalk_read_operator(prompt : string)
send prompt to cout
define string object named oper
oper read string from keybd
return oper
end function kalk_read_operator
function kalk_sqrt()
g_reg_a g_reg_a
end function kalk_sqrt
function kalk_sub()
g_reg_a g_reg_a - g_reg_b
end function kalk_sub
end module Kalk
Start of Programming Help. ??? indicates areas of coding that need actual codes.
Kalk.hpp
// These #ifndef, #define, and #endif (at the end of the file) preprocessor directives form what // is sometimes called an "include guard" or a "preprocessor guard". Every C++ header file should // contain this guard, which ensures that the header file cannot be included in a .cpp file // multiple times when building. In general, if we are writing a header file named foo.hpp, then // the preprocessor guard in foo.hpp should be, // // #ifndef FOO_HPP // #define FOO_HPP // // ... write the contents of the header file here // // #endif -- matches the #ifndef FOO_HPP directive above // // Reference: https://en.wikipedia.org/wiki/Include_guard //-------------------------------------------------------------------------------------------------- #ifndef KALK_HPP #define KALK_HPP
//================================================================================================== // PUBLIC FUNCTION PROTOTYPES //==================================================================================================
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_init() // // DESCRIPTION // Called to initialize the Kalk interpreter. //--------------------------------------------------------------------------------------------------
??? Write the function prototype for kalk_init().
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_interpret() // // DESCRIPTION: // Called to run the Kalk interpreter. //--------------------------------------------------------------------------------------------------
??? Write the function prototype for kalk_interpret().
#endif
Lab.cpp
// Kalk.hpp contains prototypes for the kalk_init() and kalk_interpret() functions. This #include // directive places those prototypes into the source code file at this location so that when the // compiler reaches the function calls in main(), it will have already seen the prototypes for the // two functions and will be able to generate the machine language code that calls the functions // at run-time. // // Note that when including header files that are part of the C++SL, we surround the header filename // with angle brackets <>. However, when including our own header files, we surround the filename // with a pair of double quotation marks. #include "Kalk.hpp"
//-------------------------------------------------------------------------------------------------- // FUNCTION: main() // // DESCRIPTION // The program starts executing here. Call kalk_init() to initialize the Kalk interpreter and then // call kalk_interpret() to run it. kalk_interpret() returns to main when the user enters the Q // (quit) command. // // PSEUDOCODE // Function main() // Call kalk_init() // Call kalk_interpret() // Return 0 // End Function //-------------------------------------------------------------------------------------------------- ???
Kalk.cpp
??? // For sqrt(), pow() ??? // For cin, cout ??? // For string class declaration ??? // For Kalk public function prototypes
using namespace std;
//================================================================================================== // GLOBAL VARIABLE DEFINITIONS //==================================================================================================
// Define three global double variables named g_reg_a, g_reg_b, and g_reg_c. Global variables are // variables that are defined outside the functions of a file. The scope of a global variable is // from the location where it is defined to the end of the source code file in which they are de- // fined, so g_reg_a, g_reg_b, and g_reg_c are accessible in all of the functions of this file. // ??? ???
//================================================================================================== // PRIVATE FUNCTION PROTOTYPES // // A private function is one that is only called by other functions of the same source code file, // i.e., a private function is not called by functions in other source code files. Prototypes for // public functions must be placed in a header file, so the public function of Kalk.cpp are declared // in Kalk.hpp. // // I order the functions in a source code file in alphabetical order, and although not all of these // functions technically requires a prototype, I have provided all of them anyway. //==================================================================================================
void kalk_add(); void kalk_atob(); void kalk_atoc(); void kalk_btoa(); void kalk_btoc(); void kalk_ctoa(); void kalk_ctob(); void kalk_display(); void kalk_div(); void kalk_load(); void kalk_mul(); void kalk_power(); string kalk_read_operator(string sPrompt); void kalk_sqrt(); void kalk_sub();
//================================================================================================== // FUNCTION DEFINITIONS //==================================================================================================
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_add() // // OPERATOR: + // // DESCRIPTION // Adds registers a + b and writes the result to register a. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_atob() // // OPERATOR: atob // // DESCRIPTION // Copies the value of register a to register b. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_atoc() // // OPERATOR: atoc // // DESCRIPTION // Copies the value of register a to register c. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_btoa() // // OPERATOR: btoa // // DESCRIPTION // Copies the value of register b to register a. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_btoc() // // OPERATOR: btoc // // DESCRIPTION // Copies the value of register b to register c. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_ctoa() // // OPERATOR: ctoa // // DESCRIPTION // Copies the value of register c to register a. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_ctob() // // OPERATOR: ctob // // DESCRIPTION // Copies the value of register c to register b. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_display() // // OPERATOR: d reg // // DESCRIPTION // Displays the contents of the specified register that the user inputs. //-------------------------------------------------------------------------------------------------- void kalk_display() { char reg; cin >> reg; ??? }
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_div() // // OPERATOR: / // // DESCRIPTION // Divides registers a / b and writes the result to register a. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_init() // // DESCRIPTION // Called to initialize the Kalk interpreter. Initializes registers a, b, and c to 0. //-------------------------------------------------------------------------------------------------- ??? //-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_interpret() // // DESCRIPTION // The main interpreter loop. Reads the operator string and calls the appropriate function to per- // form the requested operation. Stop looping and return when "q" is entered. //-------------------------------------------------------------------------------------------------- void kalk_interpret() { // Define string object named oper. Call kalk_read_operator() and assign the return value to // oper. ???
// This is a while loop, which we will discuss in Chapter 5 later. For now, just know that the // statements inside the pair of braces of the while loop will be executed over and over until // the user enters the q command to quite the program. while (oper != "q") {
// If oper is "+" then call kalk_add() if (oper == "+") { kalk_add();
// Else if oper is "atob" then call kalk_atob } else if (oper == "atob") { kalk_atob();
// Else if oper is "atoc" then call kalk_atoc // ... and so on
??? } else if (oper == "-") { kalk_sub(); } else { cout << "Invalid operator: " << oper << endl; } oper = kalk_read_operator("? "); } }
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_load() // // OPERATOR: l reg value // // DESCRIPTION // Loads a register with a double value that is read from the keyboard. //-------------------------------------------------------------------------------------------------- void kalk_load() { char reg; cin >> reg; ??? }
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_mul() // // OPERATOR: * // // DESCRIPTION // Multiplies registers a * b and writes the result to register a. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_power() // // OPERATOR: ^ // // DESCRIPTION // Writes a^b to a. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_read_operator() // // DESCRIPTION // Display the prompt string 'prompt', read a string from the keyboard representing the operator to // be performed, and return the string. //-------------------------------------------------------------------------------------------------- string kalk_read_operator(string prompt) { cout << prompt; string oper; cin >> oper; return oper; }
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_sqrt() // // OPERATOR: & // // DESCRIPTION // Writes the sqrt of register a to a. //-------------------------------------------------------------------------------------------------- ???
//-------------------------------------------------------------------------------------------------- // FUNCTION: kalk_sub() // // OPERATOR: - // // DESCRIPTION // Subtracts registers a - b and writes the result to register a. //-------------------------------------------------------------------------------------------------- ???
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started