//Source.cpp #include #include \"SimpleList.h\" #include \"SimpleStack.h\" using
ID: 673958 • Letter: #
Question
//Source.cpp
#include
#include "SimpleList.h"
#include "SimpleStack.h"
using namespace std;
/*
* Print the given list in reverse by using a stack
* Performance analysis:
*
*
*
*
* This function is O(?).
*/
void printListReversed(SimpleList l) {
// Your code here
}
int main() {
/*
* This code is here just as an example.
*
* Test your code well.
* You can leave that code in the main but I will not use that or grade you on it.
* I will test your classes with my own driver code.
*/
SimpleList s;
s.add("two");
s.add("three");
s.add("four");
s.addFront("one");
s.addFront("zero");
s.printList();
/*
* Prints IN ONE LINE:
*
* [ four, three, two, one, zero, ]
*
*/
printListReversed(s);
return 0;
}
#ifndef SIMPLELIST_H_
#define SIMPLELIST_H_
#include
using namespace std;
class SimpleList {
struct Node {
string val;
Node* next;
};
int size;
Node* head;
Node* tail;
public:
/*
* Default ctor
*/
SimpleList();
/*
* return the list size
*/
int getSize() const;
/*
* Add element e to the end of the list
*/
void add(string e);
/*
* Add element e to the front of the list
*/
void addFront(string e);
/*
* Get the element at position index
*
*/
string get(int index) const;
/*
* Set the element at position index to the given element
*/
void set(int index, string element);
/*
* Remove the element at the front of the list and return the element
*
*/
string removeFront();
/*
* Print all the list elements in order (in one line, separated by commas)
*/
void printList() const;
};
#endif /* SIMPLELIST_H_ */
#include "SimpleList.h"
#include
using namespace std;
SimpleList::SimpleList() {
head = NULL;
tail = NULL;
size = 0;
}
int SimpleList::getSize() const {
return size;
}
void SimpleList::add(string e) {
Node* n = new Node();
n->val = e;
n->next = NULL;
if (head == NULL) { // adding to empty list
head = n;
tail = n;
} else { // adding to the end
tail->next = n;
tail = n;
}
size++;
}
void SimpleList::addFront(string e) {
Node* n = new Node();
n -> val = e;
n -> next = head;
head = n;
}
string SimpleList::get(int index) const {
}
void SimpleList::set(int index, string element) {
}
string SimpleList::removeFront() {
if(head != NULL);
Node* old_head = head;
if(size == 1) {
head = NULL;
tail = NULL;
}
else {
head = head->next;
}
delete old_head;
size--;
}
void SimpleList::printList() const {
Node* cur = head;
while (cur != NULL) {
cout<val<<", ";
cur = cur->next;
}
cout<
}
#ifndef SIMPLESTACK_H_
#define SIMPLESTACK_H_
#include "SimpleList.h"
class SimpleStack {
SimpleList list;
public:
/*
* true if the stack is empty, false otherwise
*/
bool isEmpty() const;
/*
* Add to the stack
*/
void push(string e);
/*
* Remove the element at the top of the stack and return it
*/
string pop();
/*
* Get the element at top of the stack
*/
string peek() const;
};
#endif /* SIMPLESTACK_H_ */
Part 1:
You will implement a simplified singly linked list class. The SimpleList.h header file has been
provided to you together with an incomplete SimpleList.cpp file. Please provide the implementation for
the missing functions.
Part 2:
With the SimpleList class on hand you decide to experiment using it as the basis for
implementing a stack. The SimpleStack.h header file has been provided to you. Please provide the
implementation in SimpleStack.cpp.
Part 3:
The SimpleList class has a function that prints the list elements in order but you are wondering
about how you could print them in reverse order. You could use the get(i) function from back to front,
but you decide to be clever. You will use the stack class from Part 2 instead. The plan:
- get each of the list elements in order and push them one by one to the stack
- now that all the elements are in the stack, while the stack has elements left in it, you will print
the top of the stack and pop
Provide this implementation for the empty bodied function in Source.cpp.
Explanation / Answer
Answer :
mortgagePaymentTable.html
<head><title>Mortagage Payment Table</title></head>
<body bgcolor="cyan">
<form name="loan">
<table>
<tr><td colspan="3"><b>Enter Your Loan Information:</b></td></tr>
<tr>
<td>1)</td>
<td>Enter The Loan Amount:</td>
<td><input type="text" name="principal" size="12"
></td>
</tr>
<tr>
<td>2)</td>
<td>Enter Given Rate of Interest 1/8 i.e 0.125 or Any :</td>
<td><input type="text" name="interest" size="12"
></td>
</tr>
<tr>
<td>3)</td>
<td>Enter The Loan Period in Number of Years:</td>
<td><input type="text" name="years" size="12"
></td>
</tr>
<tr><td colspan="3">
<input type="button" value="Compute">
</td></tr>
<tr><td colspan="3">
<b>Your Payment Information of Loan:</b>
</td></tr>
<tr>
<td>4)</td>
<td>Your Monthly Payment Will Be:</td>
<td><input type="text" name="payment" size="12"></td>
</tr>
<tr>
<td>5)</td>
<td>Your Total Payment Will Be:</td>
<td><input type="text" name="total" size="12"></td>
</tr>
<tr>
<td>6)</td>
<td>Your Total Interest Payments Will Be:</td>
<td><input type="text" name="totalinterest" size="12"></td>
</tr>
</table>
</form>
<script language="JavaScript">
function calculate()
{
var principal = document.loan.principal.value;
var interest = document.loan.interest.value / 100 / 12;
var payments = document.loan.years.value * 12;
var x = Math.pow(1 + interest, payments);
var month = (principal*x*interest)/(x-1);
if (!isNaN(month) &&
(month != Number.POSITIVE_INFINITY) &&
(month != Number.NEGATIVE_INFINITY)) {
document.loan.payment.value = round(month);
document.loan.total.value = round(month * payments);
document.loan.totalinterest.value =
round((month * payments) - principal);
}
else {
document.loan.payment.value = "";
document.loan.total.value = "";
document.loan.totalinterest.value = "";
}
}
function round(x) {
return Math.round(x*100)/100;
}
</script>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.