Click to See Complete Forum and Search --> : program that simulates a combination lock


loaie
November 14th, 2005, 02:33 AM
Objective:
To practice writing the specification, designing, implementing and testing a class of some data structure. What to do: You have been given a complete C++ example showing how to specify, design, implement and test a class for a list data structure
(see the URL: http://engg.kau.edu.sa/~aqasimi/courses/ee367/Programs/list1.zip).



You are asked in this assignment to specify, design, implement and test a class that can be used in a program that simulates a combination lock. The lock has a circular knob, with the numbers 0 through 39 marked on the edge, and it has a three-number combination, which we will call x, y, z. To open the lock, you must turn the knob clockwise at least one entire revolution, stopping with x at the top; then turn the knob counterclockwise, stopping the second time that y appears at the top; finally turn the knob clockwise again, stopping the next time that z appears at the top. At this point you may open the lock. Your lock class should have a constructor that initializes the three-number combination (use 0, 0, 0 for default arguments).



Also provide member functions to:

1. Alter the lock’s combination to a new three-number combination.
2. Turn the knob in a given direction until a specified number appears at the top.
3. Close the lock.
4. Attempt to open the lock.
5. Inquire the status of the lock (open or shut).
6. Tell you what number is currently at the top.



Write the class and the test program and test your lock to verify that it does the job right. What to turn in:
-Your specification for the lock class, and your design.
-Your lock class implementation.
-Your test program.
-All code must be fully documented and commented.
-All code must be submitted in disk files and in printed form.
-Your test program must be a virus free executable.



please can any body here reply the solution of this project or can tell me where I can find it ?!

until 16-novmber-2005

Alsvha
November 14th, 2005, 04:32 AM
please can any body here reply the solution of this project or can tell me where I can find it ?!

So you want others to do your homework for you?

Rich2189
November 16th, 2005, 08:27 AM
Is there anything in the rules that says NO requesting homework assignments to be finished. The people on here will be willing to help if you have some fairly completed code to offer. Help with finding bugs but not write the whole thing.

Rich

loaie
November 21st, 2005, 02:45 AM
// FILE: list_test.cpp
// An interactive test program for the new list class
#include <cctype> // Provides toupper
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "list1.h" // With Item defined as double
using namespace std;
using namespace ch3;

// PROTOTYPES for functions used by this test program:

void print_menu();
// Postcondition: A menu of choices for this program has been written to cout.

char get_user_command();
// Postcondition: The user has been prompted to enter a one character command.
// The next character has been read (skipping blanks and newline characters),
// and this character has been returned.

void show_list(list<double> display);
// Postcondition: The items on display have been printed to cout (one per line).

double get_number();
// Postcondition: The user has been prompted to enter a real number. The
// number has been read, echoed to the screen, and returned by the function.

size_t get_index();
// Postcondition: The user has been prompted to enter a index. The
// index has been read, echoed to the screen, and returned by the function.


int main()
{
list<double> test; // A list that we'll perform tests on
char choice; // A command character entered by the user
size_t i; // An index value entered by the user

cout << "I have initialized an empty list of real numbers." << endl;

do
{
print_menu();
choice = toupper(get_user_command());
switch (choice)
{
case '!': test.first();
break;

case '*': test.last();
break;

case '+': if (test.is_item())
test.next();
else
cout << "There is no current item." << endl;
break;

case '-': if (test.is_item())
test.prior();
else
cout << "There is no current item." << endl;
break;

case '^': test.seek(get_index());
break;

case '#': if (test.is_item())
{
cout << "The current index value is: ";
cout << test.get_current_index() << endl;
}
else
cout << "There is no current index." << endl;

break;

case 'S': test.search(get_number());
break;

case 'B': test.bsearch(get_number(), 0, test.size());
break;

case 'O': test.sort();
break;

case '?': if (test.is_item())
cout << "There is an item." << endl;
else
cout << "There is no current item." << endl;
break;

case 'E': if (test.is_empty())
cout << "The list is empty." << endl;
else
cout << "The list is not empty." << endl;
break;

case 'F': if (test.is_full())
cout << "The list is full." << endl;
else
cout << "The list is not full." << endl;
break;

case 'C': if (test.is_item())
cout << "Current item is: " << test.current( ) << endl;
else
cout << "There is no current item." << endl;
break;

case 'G': i = get_index();
if (i <= test.size())
{
cout << "The item at index " << i << " in the list is: ";
cout << test[i] << endl;
}
else
cout << "The index " << i << " is not vaild." << endl;
break;

case 'P': show_list(test);
break;

case 'N': cout << "Number of items is " << test.size() << '.' << endl;
break;

case 'U': if (test.is_item())
test.change_current(get_number());
else
cout << "There is no current item to update" << endl;
break;

case 'I': if (!test.is_full())
test.insert(get_number());
else
cout << "The list is full" << endl;
break;

case 'A': if (!test.is_full())
test.attach(get_number());
else
cout << "The list is full" << endl;
break;

case 'R': if (!test.is_empty())
{
test.remove_current();
cout << "The current item has been removed." << endl;
}
else
cout << "The list is empty" << endl;
break;

case 'L': test.clear();
cout << "The list is cleared, size = " << test.size() << endl;
break;

case 'Q': cout << "Ridicule is the best test of truth." << endl;
break;

default: cout << choice << " is invalid." << endl;
}
}
while ((choice != 'Q'));

return EXIT_SUCCESS;
}

void print_menu( )
// Library facilities used: iostream
{
cout << endl; // Print blank line before the menu
// cout << "The following choices are available: " << endl;
cout << " ! Activate the first() function" << endl;
cout << " * Activate the last() function" << endl;
cout << " + Activate the next() function" << endl;
cout << " - Activate the prior() function" << endl;
cout << " ^ Activate the seek() function" << endl;
cout << " # Activate the get_current_index() function" << endl;
cout << " S Use sequential search to find an item" << endl;
cout << " B Use binary search to find an item" << endl;
cout << " O Oreder the list ascending" << endl;
cout << " ? Print the result from the is_item() function" << endl;
cout << " E Print the result from the empty() function" << endl;
cout << " F Print the result from the full() function" << endl;
cout << " C Print the result from the current() function" << endl;
cout << " G Print the item at the index entered by user" << endl;
cout << " P Print a copy of the entire list" << endl;
cout << " N Print the result from the size() function" << endl;
cout << " U Update current item using the change_current(...) function" << endl;
cout << " I Insert a new number with the insert(...) function" << endl;
cout << " A Attach a new number with the attach(...) function" << endl;
cout << " R Activate the remove_current() function" << endl;
cout << " L Activate the clear() function to clear the list" << endl;
cout << " Q Quit this test program" << endl;
}

char get_user_command()
// Library facilities used: iostream
{
char command;

cout << "Enter choice: ";
cin >> command; // Input of characters skips blanks and newline character

return command;
}

void show_list(list<double> display)
// Library facilities used: iostream
{
for (display.first(); display.is_item(); display.next())
cout << display.current() << " , ";
cout << endl;
}

double get_number()
// Library facilities used: iostream
{
double result;

cout << "Please enter a real number for the list: ";
cin >> result;
cout << result << " has been read." << endl;
return result;
}

size_t get_index()
// Library facilities used: iostream
{
size_t result;

cout << "Please enter the desired index: ";
cin >> result;
cout << result << " has been read." << endl;
return result;
}

loaie
November 21st, 2005, 02:47 AM
// FILE: list1.h
// See chapter 3 of Data Structures and Other Objects (Second Edition)
//
// TEMPLATE CLASS PROVIDED: list<Item> (a container template class for a list
// of items, where each List may have a designated item called the current
// item.)
//
// TEMPLATE PARAMETER, and MEMBER CONSTANTS for the list<Item> class:
// The template parameter, Item, is the data type of the items in the list.
// It may be any of the C++ built-in types (int, char, etc.), or a class with
// a default constructor, a copy constructor, and an assignment operator.
// Type size_t is the data type of any variable that keeps track of how many
// items are in a list. The static const CAPACITY is the maximum capacity of
// a list for this first list implementation.
//
// CLASS PROVIDED: list
//
// MEMBER CONSTANTS for the list class:
// static const size_t CAPACITY = _____
// list::CAPACITY is the maximum number of items that a list can hold.
//
// CONSTRUCTOR for the list class:
// list()
// Postcondition: The list has been initialized as an empty list.
//
// MODIFICATION MEMBER FUNCTIONS for the list class:
// void first()
// Postcondition: The first item on the list becomes the current item
// (but if the list is empty, then there is no current item).
//
// void last()
// Postcondition: The last item on the list becomes the current item
// (but if the list is empty, then there is no current item).
//
// void next()
// Precondition: is_item() returns true.
// Postcondition: If the current item was already the last item in the
// list, then there is no longer any current item. Otherwise, the new
// current item is the item immediately after the original current item.
//
// void prior()
// Precondition: is_item() returns true.
// Postcondition: If the current item was already the first item in the
// list, then there is no longer any current item. Otherwise, the new
// current item is the item immediately before the original current item.
//
// void seek(size_t index)
// Postcondition: If index is within the list, the item at index becomes
// the current item of the list, otherwise, there is no longer any current
// item.
//
// void search(const Item& target)
// Postcondition: The list has been searched for the target. If the target
// was present, then the found target is now the current item.
// Otherwise, there is no current item.
//
// void sort()
// Postcondition: The items of the list have been sorted in an ascending
// order of their key values, and there is no current item.
//
// void bsearch(const Item& target, size_t first, size_t size)
// Precondition: The array segment starting at data[first] and containing
// size elements is sorted from smallest to largest.
// Postcondition: The array segment starting at data[first] and containing
// size elements has been searched for the target.
// If the target was present, then the found target is now the current item.
// Otherwise, there is no current item.
//
// void change_current(const Item& entry)
// Precondition: is_item() returns true.
// Postcondition: The value of the current item has changed to entry.
//
// void insert(const Item& entry)
// Precondition: size() < CAPACITY.
// Postcondition: A new copy of entry has been inserted in the list
// before the current item. If there was no current item, then the new entry
// has been inserted at the front of the list. In either case, the newly
// inserted item is now the current item of the list.
//
// void attach(const Item& entry)
// Precondition: size() < CAPACITY.
// Postcondition: A new copy of entry has been inserted in the list after
// the current item. If there was no current item, then the new entry has
// been attached to the end of the list. In either case, the newly
// inserted item is now the current item of the list.
//
// void remove_current()
// Precondition: is_item() returns true.
// Postcondition: The current item has been removed from the list, and the
// item after this (if there is one) is now the new current item.
//
// void clear();
// Postcondition: The list is now empty.
//
// CONSTANT MEMBER FUNCTIONS for the list class:
// size_t size() const
// Postcondition: The return value is the number of items in the list.
//
// bool is_empty() const
// Postcondition: The return value is true if the list has no items,
// otherwise, it is false.
//
// bool is_full() const
// Postcondition: The return value is true if the list has a number
// of items equal to its capacity, otherwise it is false.
//
// bool is_item() const
// Postcondition: A true return value indicates that there is a valid
// "current" item that may be retrieved by activating the current
// member function (listed below). A false return value indicates that
// there is no valid current item.
//
// size_t get_current_index() const
// Postcondition: The returned value is the index of the current item in
// the list.
//
// Item current() const
// Precondition: is_item() returns true.
// Postcondition: The item returned is the current item in the list.
//
// Item operator[](size_t index) const
// Precondition: index < used.
// Postcondition: The item returned is at the given index in the list
// the current item is not changed by this function.
//
// VALUE SEMANTICS for the list class:
// Assignments and the copy constructor may be used with list objects.

#ifndef LIST1_H
#define LIST1_H
#include <cstdlib> // Provides size_t
#include <cassert> // Provides assert function

namespace ch3
{
template <class Item>
class list
{
public:
// TYPEDEFS and MEMBER CONSTANTS
// * For VC++ 6.0 we are using size_t instead of std::size_t. And we
// * have defined CAPACITY using an enum instead of a static member
// * constant.

enum { CAPACITY = 30 };

// CONSTRUCTOR
list() { used = 0; current_index = 0; }

// MODIFICATION MEMBER FUNCTIONS
void first() { current_index = 0; }
void last();
void next();
void prior();
void seek(size_t index);
void search(const Item& target);
void sort();
void bsearch(const Item& target, size_t first, size_t size);
void change_current(const Item& entry);
void insert(const Item& entry);
void attach(const Item& entry);
void remove_current();
void clear() { used = current_index = 0; }

// CONSTANT MEMBER FUNCTIONS
size_t size() const { return used; }
bool is_empty() const { return used == 0; }
bool is_full() const { return used == CAPACITY; }
bool is_item() const { return (current_index < used); }
size_t get_current_index() const { return current_index; }
Item current() const;
Item operator[](size_t index) const;
private:
Item data[CAPACITY];
size_t used;
size_t current_index;
};

// TEMPLATE CLASS IMPLEMENTED: list<Item>
// INVARIANT for the list class:
// 1. The number of items in the list is stored in the member
// variable used;
// 2. For an empty list, we do not care what is stored in any of data;
// for a non-empty list the items in the list are stored in their
// sequence order from data[0] to data[used-1], and we don't care
// what's in the rest of data.
// 3. If there is a current item, then it lies in data[current_index]; if
// there is no current item, then current_index equals used.

template <class Item>
void list<Item>::last()
{
if (used > 0)
current_index = used-1;
else
current_index = 0;
}

template <class Item>
void list<Item>::next()
// Library facilities used: cassert
{
assert(is_item());
++current_index;
}

template <class Item>
void list<Item>::prior()
// Library facilities used: cassert
{
assert(is_item());
if (current_index > 0)
--current_index;
else
current_index = used;
}

template <class Item>
void list<Item>::seek(size_t index)
{
if (index < used)
current_index = index;
else
current_index = used;
}

template <class Item>
void list<Item>::search(const Item& target)
{
size_t i=0;
bool found = false;

while ((i < used) && !found)
{
if (data[i] == target)
found = true;
else
i++;
}
current_index = i;
}

template <class Item>
void list<Item>::sort()
{
// Selection Sort Algorithm:
//
size_t small, j, k;

for (k = 0; k < used-1; k++)
{
small = k;
for (j = k+1; j < used; j++)
if (data[j] < data[small]) small = j;
Item temp;
temp = data[small];
data[small] = data[k];
data[k] = temp;
}
//
// Insertion Sort Algorithm:
//
// Item entry;
// int i, j;
//
// for (i = 1; i < used; i++)
// {
// entry = data[i];
// for (j = i; (j > 0) && (data[j-1] > entry); j--)
// data[j] = data[j-1];
// data[j] = entry;
// }
//
// Bubble Sort Algorithm:
//
// bool sorted = false;
// for ( int k = used-1; (k > 0) && !sorted; k--)
// {
// sorted = true;
// for (int j = 0; j < k; j++)
// if (data[j] > data[j+1])
// {
// sorted = false;
// // Swap the two items: data[k] and data[j].
// Item temp;
// temp = data[j+1];
// data[j+1] = data[j];
// data[j] = temp;
// }
// }
current_index = used;
}

template <class Item>
void list<Item>::bsearch(const Item& target, size_t first, size_t size)
// Library facilities used: cstdlib
{
size_t middle;

if (size == 0)
current_index = used;
else
{
middle = first + size/2;

if (target == data[middle])
current_index = middle;
else
if (target < data[middle])
// The target is less than middle item, search before it
bsearch(target, first, size/2);
else
// The target is greater than middle item, search after it
bsearch(target, middle+1, (size-1)/2);
}
}

template <class Item>
void list<Item>::change_current(const Item& entry)
// Library facilities used: cassert
{
assert(is_item());
data[current_index] = entry;
}

template <class Item>
void list<Item>::insert(const Item& entry)
// Library facilities used: cassert
{
size_t i;

assert(size() < CAPACITY);
if (!is_item())
current_index = 0;
for (i = used; i > current_index; i--)
data[i] = data[i-1];
data[current_index] = entry;
used++;
}

template <class Item>
void list<Item>::attach(const Item& entry)
// Library facilities used: cassert
{
size_t i;

assert(size() < CAPACITY);
if (is_item())
{
++current_index;
for (i = used; i > current_index; i--)
data[i] = data[i-1];
}
data[current_index] = entry;
used++;
}

template <class Item>
void list<Item>::remove_current( )
// Library facilities used: cassert
{
size_t i;

assert(is_item());
for (i = current_index+1; i < used; i++)
data[i-1] = data[i];
used--;
}

template <class Item>
Item list<Item>::current() const
// Library facilities used: cassert
{
assert(is_item());
return data[current_index];
}

template <class Item>
Item list<Item>::operator[](size_t index) const
// Library facilities used: cassert
{
assert(index < used);
return data[index];
}
}
#endif

GuOddian
November 21st, 2005, 05:18 PM
I don't understand. That's an aweful lot of code and no indication that there is a problem with it. And no code tags so it's extremely hard to read. Firstly I suggest you edit your posts and put in the code tags if you want someone to read it. Secondly I suggest you post what is actually wrong with the code, people are going to download it and compile it and check it for you unless they know something is worng. Lastly it won't get a response here, you'll need to post it in the C++ forum.