C++ linklist supercard
SuperCard - GivetoStudents/Prog 5 Soln/.vs/Prog5/v15/.suo
SuperCard - GivetoStudents/Prog 5 Soln/.vs/Prog5/v15/Browse.VC.db
SuperCard - GivetoStudents/Prog 5 Soln/.vs/Prog5/v15/ipch/AutoPCH/2363d5fe954176e7/BIGNUMBERS.ipch
SuperCard - GivetoStudents/Prog 5 Soln/.vs/Prog5/v15/ipch/AutoPCH/36a89ee00242d97e/SUPERCARD.ipch
SuperCard - GivetoStudents/Prog 5 Soln/.vs/Prog5/v15/ipch/AutoPCH/72c5e135eca22cf0/TESTSUPERCARD.ipch
SuperCard - GivetoStudents/Prog 5 Soln/.vs/Prog5/v15/ipch/AutoPCH/b0c0e6fe679aa55e/SUPERCARD.ipch
SuperCard - GivetoStudents/Prog 5 Soln/.vs/Prog5/v15/ipch/AutoPCH/b7442e9ad243ece0/TESTSUPERCARD.ipch
SuperCard - GivetoStudents/Prog 5 Soln/.vs/Prog5/v15/ipch/AutoPCH/edd4e81fb454d4ee/SUPERCARD.ipch
SuperCard - GivetoStudents/Prog 5 Soln/Prog5.ncb
SuperCard - GivetoStudents/Prog 5 Soln/Prog5.sln
Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26730.15 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Prog5", "Prog5\Prog5.vcxproj", "{4AF4D858-7B07-4FD1-B36C-BDE56A9E328B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 Release|Win32 = Release|Win32 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {4AF4D858-7B07-4FD1-B36C-BDE56A9E328B}.Debug|Win32.ActiveCfg = Debug|Win32 {4AF4D858-7B07-4FD1-B36C-BDE56A9E328B}.Debug|Win32.Build.0 = Debug|Win32 {4AF4D858-7B07-4FD1-B36C-BDE56A9E328B}.Release|Win32.ActiveCfg = Release|Win32 {4AF4D858-7B07-4FD1-B36C-BDE56A9E328B}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B8A4C050-898E-4E30-9174-7E8C7EE20958} EndGlobalSection EndGlobal
SuperCard - GivetoStudents/Prog 5 Soln/Prog5.suo
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/Prog5.vcproj
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/Prog5.vcproj.ADMIN.catherine.stringfell.user
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/Prog5.vcproj.admin-PC.admin.user
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/Prog5.vcproj.LABS.Bo119LabStudent.user
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/Prog5.vcproj.LABS.CSCLabStudent.user
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/Prog5.vcxproj
Debug Win32 Release Win32 {4AF4D858-7B07-4FD1-B36C-BDE56A9E328B} Prog5 10.0.15063.0 Application v141 MultiByte true Application v141 MultiByte 15.0.26730.12 $(SolutionDir)$(Configuration)\ $(Configuration)\ $(SolutionDir)$(Configuration)\ $(Configuration)\ Disabled true EnableFastChecks MultiThreadedDebugDLL Level3 EditAndContinue true MachineX86 MaxSpeed true MultiThreadedDLL true Level3 ProgramDatabase true true true MachineX86
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/Prog5.vcxproj.filters
{4FC737F1-C7A5-4376-A066-2A32D752A2FF} cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx {93995380-89BD-4b04-88EB-625FBE52EBFB} h;hpp;hxx;hm;inl;inc;xsd {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav Source Files Source Files Header Files
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/SCOut.txt
Catherine Stringfellow Program 5 3 April 2018 Program will process big numbers. Initialize S1 The length of S1 is 14 S1 = 00000987654321 Swap S1 and s2 S2 = 00000987654321 Reset S2 to 987654321 S2 = 987654321 Let S1 be the 10's complement of S2 S1 = 123456789 Sum of S1 and S2 is 111111110 Getting ready to destruct S1 and S2. Thank you for using the SuperCard Program. If your program does not crash - destructor worked. Goodbye.
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/Supercard.cpp
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/Supercard.cpp
//Catherine Stringfellow and ???
//
#include
<
iostream
>
#include
"Supercard.h"
using
namespace
std
;
Supercard
::
Supercard
(
)
{
//insert a new node with 0 in it
//initialize other 3 member data
}
void
Supercard
::
clear
(
)
{
//declare a temp Node pointer
//reset cursor
//while more nodes get rid of them
//insert a node with 0 in it
//update other member data
}
Supercard
::~
Supercard
()
{
//clear the list of ALL nodes
}
//Swap supercards
void
Supercard
::
swap
(
Supercard
&
other
)
{
//declare temp variables
//swap lengths
//swap index
//swap heads
//swap cursors
}
int
Supercard
::
getLength
(
)
const
{
}
void
Supercard
::
moveIndex
(
int
pos
)
{
//write error message if pos illegal
//else pos legal
//while not in correct position, move index forward
//while not in correct position, move index backwards
}
void
Supercard
::
setDigit
(
int
pos
,
int
d
)
{
//declare temp pointer
//write error message if position illegal
//else if position legal
//check if inserting a new digit at end of list
//move index to pos right before digit
//get a new node and update pointers
//update any other index and cursor
//else update an old digit
//move index to pos
//update digit
}
int
Supercard
::
getDigit
(
int
pos
)
{
int
d
=
-
1
;
// in case pos not legal
//write error message if pos illegal
//else pos legal
//move index and cursor to correct position
//get digit in current node
//return digit
}
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/Supercard.h
//Catherine Stringfellow //File : Supercard.h //This file describes the class Supercard - an unordered sequence of integer //digits used to store big numbers //Catherine Stringfellow #pragma once #include <iostream> using namespace std; class Supercard { private: //Node is a helper class to store data in a node class Node { friend Supercard; //Needed so Supercard can access int item; Node * prev; Node *next; //constructor for a node, if last 2 args are missing //they are set to NULL Node (int item1, Node *prev1 = NULL, Node *next1 = NULL) { item = item1; prev = prev1; next = next1; } }; //end class Node //Data for a Supercard int length; int currentIndex; Node *cursor; Node *head; public: //create a supercard with the value of 0 Supercard ( ); //Purpose: reset the supercard to just the digit 0 void clear ( ); //Destruct the supercard and return all allocated data to memory ~Supercard(); //Swap this Supercard with other void swap(Supercard & other); //Returns: the number of digits in the Supercard int getLength ( ) const; //Purpose: moves the current index to the digit at position pos, //if it exists //Requires/Checks: 1 <= i <= length void moveIndex (int pos); //Returns: returns the digit at the position pos //Requires/Checks: 1 <= pos <= length int getDigit (int pos); //Purpose: Inserts the digit d in the supercard in the proper position, // if it is 1 more than the length it inserts the digit //Requires: 1 <= pos <= length+1 void setDigit (int pos, int d); };
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/TestSupercard.cpp
SuperCard - GivetoStudents/Prog 5 Soln/Prog5/TestSupercard.cpp
//Catherine Stringfellow and ????
//
//
#include
<
iostream
>
#include
<
fstream
>
#include
<
string
>
#include
"Supercard.h"
using
namespace
std
;
//Purpose: Introduces the program.
//Requires: Outfile.
void
printHeading
(
ofstream
&
outfile
);
//Purpose: Prints message (string) as label and the digits of supercard.
//Requires: A Supercard and outfile.
void
printSuperCard
(
ofstream
&
outfile
,
string message
,
Supercard
&
S
);
//Purpose: Set S1 to the 10s complement of S2
//Requires: Two Supercards S1 and S2
void
tensComplement
(
Supercard
&
S1
,
Supercard
&
S2
);
//Purpose: Sets result to sum of S1 and S2
//Requires: Three supercards S1, S2, and S3
void
addSupercards
(
Supercard
&
S1
,
Supercard
&
S2
,
Supercard
&
result
);
//Purpose: Opens file to be written to.
void
openFiles
(
ofstream
&
outfile
);
void
printEndMessage
(
ofstream
&
outfile
);
//Purpose: Thanks the user for running SuperCard Program.
//Requires: outfile.
int
main
()
{
Supercard
S1
,
S2
,
S3
;
ofstream outfile
;
openFiles
(
outfile
);
printHeading
(
outfile
);
//test SetDigit and initialize S1
outfile
<<
"Initialize S1\n"
;
for
(
int
i
=
1
;
i
<=
9
;
i
++
)
S1
.
setDigit
(
i
,
i
);
for
(
int
i
=
10
;
i
<=
14
;
i
++
)
S1
.
setDigit
(
i
,
0
);
//test length
outfile
<<
"The length of S1 is "
<<
S1
.
getLength
()
<<
endl
;
//output value of S1
printSuperCard
(
outfile
,
"S1 = "
,
S1
);
//swap S1 and S2 and print S2
outfile
<<
"Swap S1 and s2"
<<
endl
;
S1
.
swap
(
S2
);
printSuperCard
(
outfile
,
"S2 = "
,
S2
);
//clear S2 and reset it to 987654321 and ouput value of S2
outfile
<<
"Reset S2 to 987654321\n"
;
S2
.
clear
();
for
(
int
i
=
1
;
i
<=
9
;
i
++
)
S2
.
setDigit
(
i
,
i
);
printSuperCard
(
outfile
,
"S2 = "
,
S2
);
//test get digit and compute 10's complement of S2
outfile
<<
"Let S1 be the 10's complement of S2\n"
;
tensComplement
(
S1
,
S2
);
//output the new value of S1
printSuperCard
(
outfile
,
"S1 = "
,
S1
);
//add S1 and S2, store in S3 and print it
addSupercards
(
S1
,
S2
,
S3
);
printSuperCard
(
outfile
,
"Sum of S1 and S2 is "
,
S3
);
//test destructor
outfile
<<
"Getting ready to destruct S1 and S2.\n"
;
printEndMessage
(
outfile
);
return
0
;
}
void
openFiles
(
ofstream
&
outfile
)
{
//opens file to be written to.
outfile
.
open
(
"SCOut.txt"
);
}
void
printHeading
(
ofstream
&
outfile
)
{
outfile
<<
"Catherine Stringfellow"
<<
endl
<<
"Program 5"
<<
endl
<<
"3 April 2018"
<<
endl
<<
"Program will process big numbers."
<<
endl
<<
endl
;
}
void
printEndMessage
(
ofstream
&
outfile
)
{
outfile
<<
"Thank you for using the SuperCard Program. \n\n"
;
outfile
<<
"If your program does not crash - destructor worked.\n\n"
;
outfile
<<
"Goodbye."
<<
endl
;
}
void
printSuperCard
(
ofstream
&
outfile
,
string message
,
Supercard
&
S
)
{
}
void
tensComplement
(
Supercard
&
S1
,
Supercard
&
S2
)
{
//clear S1
//set each digit of S1 to 10 - the corresponding digit in S2
}
void
addSupercards
(
Supercard
&
S1
,
Supercard
&
S2
,
Supercard
&
result
)
{
//clear result
//declare variables needed
//while more digits in either S1 or S2, keep adding digits
//get digit from S1, if none set digit to 0
//get digit from S2, if none set digit to 0
//sum digits and compute carry
//set sum to just the one's digit
//set the proper digit in result
//update pos
//take care of last carry if 1
}
SuperCard - GivetoStudents/prog5SuperCard.pdf
CMPS 1063 – Data Structures & ADTs Spring 2018 Program Assignment 5 100 points DUE: Wednesday, April 18, 2018
Purpose: To learn how to use dynamic memory allocation and a doubly-linked list.
Problem: In this assignment, you are to process super cardinals, that is big numbers. You will test methods
of the Supercard class, and then will find the 10’s complement of a number, sum two super cardinal
numbers, and print them out.
Method: Very large integer numbers cannot be stored in variables of type in or even long int. Certainly one
cannot perform arithmetic, e.g. addition, multiplication, etc., on very large numbers. For example, one
cannot compute 500! For this assignment, you will implement a class and methods to access the digits
in a super cardinal. Suppose the Supercard S = 81234. In your representation of S, the digits of S will
be stored in a doubly-linked list with the least significant digit first; that is, in reverse order. This is
pictured as follows:
Note the digits are stored backwards to make it easier to perform addition.
In addition to representing the digits of S, the representation will also keep track of four other pieces of
data.
head, a pointer to the front of the list (the least significant digit of the number; remember the
digits are stored in reverse in the list)
cursor, a pointer to some digit in the list
length, which represents the number of digits in S.
currentIndex, which is described as follows:
Let S = dndn-1..d2d1 where di is the ith digit of S. For example if S = 81234, then d1 = 4, d2 =3,
d3= 2, d4 =1, and d5 = 8. In the conceptual view of the double-linked list S could be
represented as 4 3 ↓ 2 1 8 with the cursor currently indicating 2, which is d3. In this case the
current index is 3. Current index is used to improve the efficiency of the implementation of the
getDigit and setDigit methods in the Supercard class. The need for this information arises from
the fact that getDigit and setDigit are conceptually “random access” operations for Supercard,
but the implementation of supercard, a double-linked list, is not a random access structure like
an array.
The type declaration for the representation of Supercard shown above are:
class Supercard
{ private:
int length; int currentIndex;
Node * head; Node * cursor; }
where Node is declared as a class inside Supercard as:
class Node
{ friend Supercard; int item; Node * prev;
4 3 2 1 8
length 5
index 3
head
cursor
Node *next; };
You will find a zipped project on D2L for this assignment. Unzip it. In the project, there are 3 files:
Supercard.h, Supercard.cpp, and TestSupercard.cpp (the main file). Print out the Supercard.h file and
study it – it specifies a list of methods for Supercard. There are also comments and some code in the
other files. You are to complete the implementation of Supercard and complete the implementation of
three functions in main: printSupercard, complement and addSupercards.
Keep the following ideas in mind when your implement the Supercard methods:
The digits of Supercard are stored in a double-linked list, which means you can go forward and
backwards in the list. Use the cursor and current index to figure out which way to move
current index to get to the desired position. For example, if you need the 95th digit of a 100
digit number, you do not want to start with the 1st digit if the cursor is at the 85th digit. Likewise
you can retreat from the 100th digit to get to the 95th digit. You will be gaded on efficiency.
A Supercard always has at least one node in it – it may be just 0.
Comment out parts of the main program until you can the constructor, getLength and getDigits
and setDigits working.
Then get printSuperCard to work.
Get the methods swap and the destructor to work.
Finally, you must implement two other NON-MEMBER functions: tensComplement and
addSupercards.
The 10’s complement is easy, replace each digit with 10 – digit, so the 10’s complement of 84321 is
26789.
Adding two Supercards is done by adding corresponding digits starting with the least significant digits
with proper carry from previous digits. Think about how you added big numbers in the 3rd grade using
the usual paper-and-pencil method. The result of the addition is stored in another Supercard. The
function must accept as parameters two Supercards and it returns a Supercard with the digits for the
sum. Upon returning from the function, the program should print the result.
Input: None, you are running a test driver.
Output: Output is to a file.
Extra Credit (10 points)
Write the non-member function in the main program to print or addSupercards using recursion. (5
points each.)
Turn in: Printouts of all .h and .cpp files (3 of them)
Printout of the output file – turn this in EVEN if your output is incorrect!
Storage media containing all .h, .cpp files AND project files.
Everything in a big envelope
Academic misconduct statement.