// /*--------------------------------------------------------*\
// | Main Program: Simple tester for Natural Power
// |*--------------------------------------------------------*|
// | Date: Summer Quarter 2006
// | Author: Bruce W. Weide (adapted from "anonymous")
// |
// | Brief User's Manual:
// | Allows user either to exercise the Power operation
// | in an interactive menu-driven mode or in a batch
// | file mode (using input redirection).
// |
// | Modified: 8 April 2004
// | Modifier: Wayne D. Heym
// | Modification:
// | Support for comments in test scripts
// |
// | Modified: 6 December 2006
// | Modifier: Tim Long
// | Modification:
// | Request expected outgoing values for each test case
// |
// \*--------------------------------------------------------*/
///-------------------------------------------------------------
/// Global Context ---------------------------------------------
///-------------------------------------------------------------
#include "RESOLVE_Foundation.h"
#include "CI/Natural/Number_1_C.h"
///-------------------------------------------------------------
/// Interface --------------------------------------------------
///-------------------------------------------------------------
global_procedure Power (
alters Natural_Number_1_C& n,
preserves Integer p
);
/*!
requires
p >= 0
ensures
n = #n ^ (p)
!*/
//--------------------------------------------------------------
procedure_body Power (
alters Natural_Number_1_C& n,
preserves Integer p
)
{
//-------- for students to fill in --------
if (p > 1)
{
n.Multiply(n);
Power(n, p-1);
}
else if (p == 0)
{
n.Clear();
n.Multiply_By_Radix(1);
}
}
//--------------------------------------------------------------
//--------------------------------------------------------------
program_body main ()
{
object Character_IStream input;
object Character_OStream output;
object Text response;
object Boolean interactive_mode;
object Integer p, p_exp_out;
object Natural_Number_1_C n, n_exp_out;
// Open input and output streams
input.Open_External ("");
output.Open_External ("");
// Ask user about interactive mode
output << "Run in interactive mode (y/n)? ";
input >> response;
interactive_mode = (response == "y");
output << "\n";
// Ask user for whether to test
if (interactive_mode)
{
output << "Start testing (y/n)? ";
}
input >> response;
// Execute interactive testing loop until finished
while (response != "n")
{
if (response != "y")
{
output << response << '\n';
}
else
{
// Get values for n and p for this test case
if (interactive_mode)
{
output << "n = ";
}
input >> n;
if (interactive_mode)
{
output << "p = ";
}
input >> p;
// Get expected output for n and p for this test case
if (interactive_mode)
{
output << "\nexpected output for n = ";
}
input >> n_exp_out;
if (interactive_mode)
{
output << "expected output for p = ";
}
input >> p_exp_out;
// Report results of this test case
output << "-------------------------------------------------\n"
<< " | n = " << n << '\n'
<< " | p = " << p << '\n'
<< "-------------------------------------------------\n";
Power (n, p);
output << "Power (n, p); |\n"
<< "-------------------------------------------------\n"
<< " | n = " << n << '\n'
<< " | p = " << p << '\n'
<< "-------------------------------------------------\n";
if (p != p_exp_out)
{
output << "\nERROR DETECTED:\n"
<< " expected p = "
<< p_exp_out << '\n'
<< " observed p = "
<< p << "\nEXECUTION TERMINATING\n";
return 0;
}
if (n.Compare (n_exp_out) != 0)
{
output << "ERROR DETECTED:\n"
<< " expected n = "
<< n_exp_out << '\n'
<< " observed n = "
<< n << "\nEXECUTION TERMINATING\n";
return 0;
}
}
// Determine whether to continue testing
if (interactive_mode)
{
output << "\nContinue testing (y/n)? ";
}
input >> response;
}
// Close input and output streams
input.Close_External ();
output.Close_External ();
}
I am having problem at
if (p > 1)
{
n.Multiply(n);
Power(n, p-1);
}
this multiplication. because for every value I get zero. Any idea??