Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Whoa

Status
Not open for further replies.

airdevil

Programmer
Oct 31, 1999
10
0
0
US
Visit site
/* Programmer: Bill Korn<br>
Login:A17<br>
Computer#:26<br>
Period:1<br>
Program Title:Check.CPP<br>
Date:11/3/99<br>
<br>
Purpose:This program will allow the user to enter a starting<br>
deposit. It will then process a series of deposits,withdrawals<br>
. Then it will print the # of withdrawals, deposits, transactions<br>
and the total of the withdrawals and deposits.\n&quot;;<br>
*/<br>
<br>
<br>
#include &quot;trinity.h&quot;<br>
<br>
void neo();<br>
void morpheus();<br>
double get_deposit();<br>
char get_choice();<br>
double get_amount();<br>
void setup();<br>
void print_results(double num_w, double num_d, double total,<br>
double total_w, double total_d, double balance,<br>
double initial);<br>
<br>
int main()<br>
{<br>
neo();<br>
morpheus();<br>
<br>
// Declaring Variables<br>
double initial, change, total_d, total_w, num_d, num_w, balance, count,total;<br>
char choice, cheryl;<br>
<br>
// Rerun Loop<br>
do<br>
{<br>
<br>
// Initializing Variables<br>
total_d = total_w = num_d = num_w = count = 0;<br>
<br>
// Get initial balance<br>
initial = get_deposit();<br>
balance = initial;<br>
<br>
// Setting up the screen<br>
setup();<br>
<br>
// Do while loop regulating transactions<br>
do<br>
{<br>
// Getting the choice<br>
choice = get_choice();<br>
<br>
// getting amount<br>
if(choice != 'Q' && choice != 'q')<br>
change = get_amount();<br>
<br>
<br>
// Switch regulating options<br>
<br>
switch (choice)<br>
{<br>
case 'D', 'd': total_d = total_d + change;<br>
num_d++;<br>
balance = balance + change;<br>
gotoxy(29, (4 + count));<br>
cout &lt;&lt; setw(12) &lt;&lt; change;<br>
gotoxy(48, (4 + count));<br>
cout &lt;&lt; setw(11) &lt;&lt; balance;<br>
count++;<br>
break;<br>
<br>
case 'W', 'w': if(change &gt; balance)<br>
{<br>
gotoxy(1, 23);<br>
cout &lt;&lt; &quot; \t\tYou do not have enough. Press Enter&quot;;<br>
getch();<br>
cin.ignore();<br>
gotoxy(1, 23);<br>
cout &lt;&lt; &quot; &quot;;<br>
cout &lt;&lt; &quot; &quot;;<br>
break;<br>
}<br>
<br>
total_w = total_w + change;<br>
num_w++;<br>
balance = balance - change;<br>
gotoxy(9, (4 + count));<br>
cout &lt;&lt; setw(15) &lt;&lt; change;<br>
gotoxy(48, (4 + count));<br>
cout &lt;&lt; setw(11) &lt;&lt; balance;<br>
count++;<br>
break;<br>
case 'Q', 'q': break;<br>
}<br>
}<br>
while(choice != 'Q' && choice != 'q');<br>
<br>
// Calculating Total<br>
total = num_d + num_w;<br>
<br>
// Printing the final screen<br>
print_results(num_w, num_d, total, total_w, total_d, balance, initial);<br>
<br>
// Rerun Ask<br>
do<br>
{<br>
cout &lt;&lt; &quot;\n\n&quot;;<br>
cout &lt;&lt; &quot;\t\t Do you want to run this program again(y/n): &quot;;<br>
cin &gt;&gt; cheryl;<br>
}<br>
while(cheryl != 'Y' && cheryl != 'y' && cheryl != 'N' && cheryl!= 'n');<br>
<br>
// End of Rerun Loop<br>
<br>
}<br>
while(cheryl != 'N' && cheryl != 'n');<br>
<br>
<br>
<br>
return 0;<br>
}<br>
<br>
double get_deposit()<br>
{<br>
double hoody_hoo;<br>
do<br>
{<br>
clrscr();<br>
matrix(11);<br>
cout &lt;&lt; &quot;\t\t Enter the initial balance: &quot;;<br>
cin &gt;&gt; hoody_hoo;<br>
}<br>
while(hoody_hoo &lt;= 0);<br>
return hoody_hoo;<br>
}<br>
<br>
char get_choice()<br>
{<br>
char blingbling;<br>
do<br>
{<br>
gotoxy(61, 23);<br>
cout &lt;&lt; &quot; &quot;;<br>
gotoxy(1, 23);<br>
cout &lt;&lt; &quot;\t\t Deposit (D), Withdrawal (W), or Quit (Q): &quot;;<br>
cin &gt;&gt; blingbling;<br>
}<br>
while(blingbling != 'W' && blingbling != 'w' && blingbling != 'D' &&<br>
blingbling != 'd' && blingbling != 'Q' && blingbling != 'q');<br>
gotoxy(1, 23);<br>
cout &lt;&lt; &quot; &quot;;<br>
cout &lt;&lt; &quot; &quot;;<br>
gotoxy(1, 24);<br>
cout &lt;&lt; &quot; &quot;;<br>
cout &lt;&lt; &quot; &quot;;<br>
return blingbling;<br>
}<br>
<br>
double get_amount()<br>
{<br>
double btau;<br>
do<br>
{<br>
gotoxy(55, 23);<br>
cout &lt;&lt; &quot; &quot;;<br>
gotoxy(1, 23);<br>
cout &lt;&lt; &quot;\t\t Enter the amount for the transaction: &quot;;<br>
cin &gt;&gt; btau;<br>
gotoxy(1, 23);<br>
cout &lt;&lt; &quot; &quot;;<br>
cout &lt;&lt; &quot; &quot;;<br>
gotoxy(1, 24);<br>
cout &lt;&lt; &quot; &quot;;<br>
cout &lt;&lt; &quot; &quot;;<br>
}<br>
while(btau &lt; 0);<br>
return btau;<br>
}<br>
<br>
void setup()<br>
{<br>
clrscr();<br>
matrix(1);<br>
cout &lt;&lt; &quot; Withdrawals Deposits Balance\n&quot;;<br>
cout &lt;&lt; &quot; ----------- -------- -------\n&quot;;<br>
cout.setf(ios::showpoint ¦ ios::fixed);<br>
cout &lt;&lt; setprecision(2);<br>
}<br>
<br>
void print_results(double num_w, double num_d, double total,<br>
double total_w, double total_d, double balance,<br>
double initial)<br>
{<br>
clrscr();<br>
matrix(9);<br>
cout &lt;&lt; &quot; \t\t Initial Balance:&quot; &lt;&lt; setw(18) &lt;&lt; initial &lt;&lt; '\n';<br>
cout &lt;&lt; &quot; \t\t Number of Withdrawals:&quot; &lt;&lt; setw(12) &lt;&lt; int(num_w) &lt;&lt; '\n';<br>
cout &lt;&lt; &quot; \t\t Number of Deposits:&quot; &lt;&lt; setw(15) &lt;&lt; int(num_d) &lt;&lt; '\n';<br>
cout &lt;&lt; &quot; \t\t Number of Transactions:&quot; &lt;&lt; setw(11) &lt;&lt; int(total) &lt;&lt; '\n';<br>
cout &lt;&lt; &quot; \t\t Total of Withdrawals:&quot; &lt;&lt; setw(13) &lt;&lt; total_w &lt;&lt; '\n';<br>
cout &lt;&lt; &quot; \t\t Total of Deposits:&quot; &lt;&lt; setw(16) &lt;&lt; total_d &lt;&lt; '\n';<br>
cout &lt;&lt; &quot; \t\t Final Balance:&quot; &lt;&lt; setw(20) &lt;&lt; balance &lt;&lt; '\n';<br>
matrix(8);<br>
cout &lt;&lt; &quot;\t\t\t Press Enter To Continue: &quot;;<br>
getch();<br>
cin.ignore();<br>
} <br>
<p>Korn<br><a href=mailto:airdevil23@hotmail.com>airdevil23@hotmail.com</a><br><a href= > </a><br>"procrastination is the key to happiness"
 
// Trinity.h<br>
<br>
#ifndef trinity<br>
<br>
<br>
// Function : neo<br>
// Purpose: to display the name, date, and program title<br>
// Input: a date and a program name<br>
// Value(s) Returned: None<br>
<br>
void neo(apstring date, apstring program);<br>
<br>
// Function : morpheus<br>
// Purpose: to display the purpose of the program<br>
// Input: none<br>
// Value(s) Returned: None<br>
<br>
void morpheus();<br>
<br>
// Function : tank_ten<br>
// Purpose: to round a double to the tenth place<br>
// Input: a double<br>
// Value(s) Returned: a double<br>
<br>
double tank_ten(double tank);<br>
<br>
// Function : tank_hund<br>
// Purpose: to round a double to the hundredths place<br>
// Input: a double<br>
// Value(s) Returned: a double<br>
<br>
double tank_hund(double tank);<br>
<br>
// Function : tank_thou<br>
// Purpose: to round a double to the thousandths place<br>
// Input: a double<br>
// Value(s) Returned: a double<br>
<br>
double tank_thou(double tank);<br>
<br>
// Function : matrix<br>
// Purpose: skip a number of lines<br>
// Input: an integer<br>
// Value(s) Returned: none<br>
<br>
void matrix(int mat);<br>
<br>
// Function : space<br>
// Purpose: to produce a number fo spaces<br>
// Input: an integer<br>
// Value(s) Returned: none<br>
<br>
void space(int spam);<br>
<br>
<br>
<br>
#define trinity<br>
<br>
#include &quot;trinity.cpp&quot;<br>
<br>
#endif<br>
<br>
// trinity.cpp<br>
<br>
<br>
#include &lt;iostream.h&gt;<br>
#include &lt;conio.h&gt;<br>
#include &lt;iomanip.h&gt;<br>
#include &lt;string.h&gt;<br>
#include &lt;math.h&gt;<br>
<br>
void neo(apstring date, apstring program)<br>
{<br>
<br>
// First Screen<br>
<br>
clrscr();<br>
cout &lt;&lt; &quot;\n\n\n\n\n\n\n\n\n\n&quot;;<br>
cout &lt;&lt; &quot;\t\t\t\t Bill Korn\n\n&quot;;<br>
cout &lt;&lt; &quot;\t\t\t\t &quot; &lt;&lt; date &lt;&lt; &quot;\n\n&quot;;<br>
cout &lt;&lt; &quot;\t\t\t\t &quot; &lt;&lt; program &lt;&lt; &quot;.CPP\n&quot;;<br>
cout &lt;&lt; &quot;\n\n\n\n\n\n\n\n\n\n&quot;;<br>
cout &lt;&lt; &quot;\t\t\t Press Enter To Continue:&quot;;<br>
getch();<br>
cin.ignore();<br>
}<br>
<br>
void morpheus()<br>
{<br>
<br>
// Second Screen<br>
<br>
clrscr();<br>
cout &lt;&lt; &quot;\n\n\n\n\n\n\n\n\n\n&quot;;<br>
cout &lt;&lt; &quot; This program will ask the user for an initial inventory.\n&quot;;<br>
cout &lt;&lt; &quot; Then it will get orders from the user and and tell wether\n&quot;;<br>
cout &lt;&lt; &quot; they are filled or not. Then it will print the final inven\n&quot;;<br>
cout &lt;&lt; &quot; -tory, widgets shipped and new widgets needed.\n&quot;;<br>
cout &lt;&lt; &quot;\n\n\n\n\n\n\n\n\n\n&quot;;<br>
cout &lt;&lt; &quot;\t\t\t Press Enter To Continue:&quot;;<br>
getch();<br>
cin.ignore();<br>
}<br>
<br>
double tank_ten(double tank)<br>
{<br>
tank = floor(tank * 10 + .5001) / 10;<br>
return tank;<br>
}<br>
<br>
double tank_hund(double tank)<br>
{<br>
tank = floor(tank * 100 + .5001) / 100;<br>
return tank;<br>
}<br>
<br>
double tank_thou(double tank)<br>
{<br>
tank = floor(tank * 1000 + .5001) / 1000;<br>
return tank;<br>
}<br>
<br>
void matrix(int mat)<br>
{<br>
int i;<br>
for(i = 1; i &lt;= mat; i++)<br>
cout &lt;&lt; '\n';<br>
}<br>
<br>
void upload(int spam)<br>
{<br>
int line;<br>
int i;<br>
if(spam &lt; 10000)<br>
line = 2;<br>
if(spam &lt; 1000)<br>
line = 3;<br>
if(spam &lt; 100)<br>
line = 4;<br>
if(spam &lt; 10)<br>
line = 5;<br>
for(i = 1;i &lt;= line; i++)<br>
cout &lt;&lt; &quot; &quot;;<br>
}<br>
<br>
<br>
// apstring---------------------<br>
<br>
#ifndef _APSTRING_H<br>
#define _APSTRING_H<br>
<br>
#include &lt;iostream.h&gt;<br>
// uncomment line below if bool not built-in type<br>
#if __BORLANDC__ &lt; 0x500<br>
#include &quot;bool.h&quot;<br>
#endif<br>
<br>
// *******************************************************************<br>
// Last Revised: January 13,1998<br>
//<br>
// APCS string class<br>
//<br>
// string class consistent with a subset of the standard C++ string class<br>
// as defined in the draft ANSI standard<br>
// *******************************************************************<br>
<br>
extern const int npos; // used to indicate not a position in the string<br>
<br>
class apstring<br>
{<br>
public:<br>
<br>
// constructors/destructor<br>
<br>
apstring( ); // construct empty string &quot;&quot;<br>
apstring( const char * s ); // construct from string literal<br>
apstring( const apstring & str ); // copy constructor<br>
~apstring( ); // destructor<br>
<br>
// assignment<br>
<br>
const apstring & operator = ( const apstring & str ); // assign str<br>
const apstring & operator = ( const char * s ); // assign s<br>
const apstring & operator = ( char ch ); // assign ch<br>
<br>
// accessors<br>
<br>
int length( ) const; // number of chars<br>
int find( const apstring & str ) const; // index of first occurrence of str<br>
int find( char ch ) const; // index of first occurrence of ch<br>
apstring substr( int pos, int len ) const; // substring of len chars<br>
// starting at pos<br>
const char * c_str( ) const; // explicit conversion to char *<br>
<br>
// indexing<br>
<br>
char operator[ ]( int k ) const; // range-checked indexing<br>
char & operator[ ]( int k ); // range-checked indexing<br>
<br>
// modifiers<br>
<br>
const apstring & operator += ( const apstring & str );// append str<br>
const apstring & operator += ( char ch ); // append char<br>
<br>
<br>
private:<br>
int myLength; // length of string (# of characters)<br>
int myCapacity; // capacity of string<br>
char * myCstring; // storage for characters<br>
};<br>
<br>
// The following free (non-member) functions operate on strings<br>
//<br>
// I/O functions<br>
<br>
ostream & operator &lt;&lt; ( ostream & os, const apstring & str );<br>
istream & operator &gt;&gt; ( istream & is, apstring & str );<br>
istream & getline( istream & is, apstring & str );<br>
<br>
// comparison operators:<br>
<br>
bool operator == ( const apstring & lhs, const apstring & rhs );<br>
bool operator != ( const apstring & lhs, const apstring & rhs );<br>
bool operator &lt; ( const apstring & lhs, const apstring & rhs );<br>
bool operator &lt;= ( const apstring & lhs, const apstring & rhs );<br>
bool operator &gt; ( const apstring & lhs, const apstring & rhs );<br>
bool operator &gt;= ( const apstring & lhs, const apstring & rhs );<br>
<br>
// concatenation operator +<br>
<br>
apstring operator + ( const apstring & lhs, const apstring & rhs );<br>
apstring operator + ( char ch, const apstring & str );<br>
apstring operator + ( const apstring & str, char ch );<br>
<br>
// *******************************************************************<br>
// Specifications for string functions<br>
//<br>
// Any violation of a function's precondition will result in an error<br>
// message followed by a call to abort.<br>
//<br>
// The apstring class assumes that '\0' is not a valid<br>
// character in an apstring. Any attempts to place '\0'<br>
// in an apstring will result in undefined behavior. Generally<br>
// this means that characters that follow the '\0' will not<br>
// be considered part of the apstring for purposes of<br>
// comparison, output, and subsequent copying.<br>
//<br>
// constructors / destructor<br>
//<br>
// string( )<br>
// postcondition: string is empty<br>
//<br>
// string( const char * s )<br>
// description: constructs a string object from a literal string<br>
// such as &quot;abcd&quot;<br>
// precondition: s is '\0'-terminated string as used in C<br>
// postcondition: copy of s has been constructed<br>
//<br>
// string( const string & str )<br>
// description: copy constructor<br>
// postcondition: copy of str has been constructed<br>
//<br>
// ~string( );<br>
// description: destructor<br>
// postcondition: string is destroyed<br>
//<br>
// assignment<br>
//<br>
// string & operator = ( const string & rhs )<br>
// postcondition: normal assignment via copying has been performed<br>
//<br>
// string & operator = ( const char * s )<br>
// description: assignment from literal string such as &quot;abcd&quot;<br>
// precondition: s is '\0'-terminated string as used in C<br>
// postcondition: assignment via copying of s has been performed<br>
//<br>
// string & operator = ( char ch )<br>
// description: assignment from character as though single char string<br>
// postcondition: assignment of one-character string has been performed<br>
//<br>
// accessors<br>
//<br>
// int length( ) const;<br>
// postcondition: returns # of chars in string<br>
//<br>
// int find( const string & str) const;<br>
// description: find the first occurrence of the string str within this<br>
// string and return the index of the first character. If<br>
// str does not occur in this string, then return npos.<br>
// precondition: this string represents c0, c1, ..., c(n-1)<br>
// str represents s0, s1, ...,s(m-1)<br>
// postcondition: if s0 == ck0, s1 == ck1, ..., s(m-1) == ck(m-1) and<br>
// there is no j &lt; k0 such that s0 = cj, ...., sm == c(j+m-1),<br>
// then returns k0;<br>
// otherwise returns npos<br>
//<br>
// int find( char ch ) const;<br>
// description: finds the first occurrence of the character ch within this<br>
// string and returns the index. If ch does not occur in this<br>
// string, then returns npos.<br>
// precondition: this string represents c0, c1, ..., c(n-1)<br>
// postcondition: if ch == ck, and there is no j &lt; k such that ch == cj<br>
// then returns k;<br>
// otherwise returns npos<br>
//<br>
// string substr( int pos, int len ) const;<br>
// description: extract and return the substring of length len starting<br>
// at index pos<br>
// precondition: this string represents c0, c1, ..., c(n-1)<br>
// 0 &lt;= pos &lt;= pos + len - 1 &lt; n.<br>
// postcondition: returns the string that represents<br>
// c(pos), c(pos+1), ..., c(pos+len-1)<br>
//<br>
// const char * c_str( ) const;<br>
// description: convert string into a '\0'-terminated string as<br>
// used in C for use with functions<br>
// that have '\0'-terminated string parameters.<br>
// postcondition: returns the equivalent '\0'-terminated string<br>
//<br>
// indexing<br>
//<br>
// char operator [ ]( int k ) const;<br>
// precondition: 0 &lt;= k &lt; length()<br>
// postcondition: returns copy of the kth character<br>
//<br>
// char & operator [ ]( int k )<br>
// precondition: 0 &lt;= k &lt; length()<br>
// postcondition: returns reference to the kth character<br>
// note: if this reference is used to write a '\0'<br>
// subsequent results are undefined<br>
//<br>
// modifiers<br>
//<br>
// const string & operator += ( const string & str )<br>
// postcondition: concatenates a copy of str onto this string<br>
//<br>
// const string & operator += ( char ch )<br>
// postcondition: concatenates a copy of ch onto this string<br>
//<br>
//<br>
// non-member functions<br>
//<br>
// ostream & operator &lt;&lt; ( ostream & os, const string & str)<br>
// postcondition: str is written to output stream os<br>
//<br>
// istream & operator &gt;&gt; ( istream & is, string & str )<br>
// precondition: input stream is open for reading<br>
// postcondition: the next string from input stream is has been read<br>
// and stored in str<br>
//<br>
// istream & getline( istream & is, string & str )<br>
// description: reads a line from input stream is into the string str<br>
// precondition: input stream is open for reading<br>
// postcondition: chars from input stream is up to '\n' have been read<br>
// and stored in str; the '\n' has been read but not stored<br>
//<br>
// string operator + ( const string & lhs, const string & rhs )<br>
// postcondition: returns concatenation of lhs with rhs<br>
//<br>
// string operator + ( char ch, const string & str )<br>
// postcondition: returns concatenation of ch with str<br>
//<br>
// string operator + ( const string & str, char ch )<br>
// postcondition: returns concatenation of str with ch<br>
//<br>
//***************************************************************<br>
<br>
#include &quot;apstring.cpp&quot;<br>
<br>
#endif<br>
<br>
// *******************************************************************<br>
// Last Revised: January 13, 1998, &lt;= and &gt;= redefined using ! and &lt;<br>
// operator += now takes constant<br>
// amortized time for adding one char<br>
//<br>
// APCS string class IMPLEMENTATION<br>
//<br>
// see apstring.h for complete documentation of functions<br>
//<br>
// string class consistent with a subset of the standard C++ string class<br>
// as defined in the draft ANSI standard<br>
// *******************************************************************<br>
<br>
#include &lt;string.h&gt;<br>
#include &lt;assert.h&gt;<br>
// #include &quot;apstring.h&quot;<br>
<br>
const int npos = -1;<br>
const int MAX_LENGTH = 1024; // largest size string for input<br>
<br>
apstring::apstring()<br>
// postcondition: string is empty<br>
{<br>
myLength = 0;<br>
myCapacity = 1;<br>
myCstring = new char[myCapacity];<br>
myCstring[0] = '\0'; // make c-style string zero length<br>
}<br>
<br>
<br>
apstring::apstring(const char * s)<br>
//description: constructs a string object from a literal string<br>
// such as &quot;abcd&quot;<br>
//precondition: s is '\0'-terminated string as used in C<br>
//postcondition: copy of s has been constructed<br>
{<br>
assert (s != 0); // C-string not NULL?<br>
<br>
myLength = strlen(s);<br>
myCapacity = myLength + 1; // make room for '\0'<br>
myCstring = new char[myCapacity];<br>
strcpy(myCstring,s);<br>
}<br>
<br>
apstring::apstring(const apstring & str)<br>
//description: copy constructor<br>
//postcondition: copy of str has been constructed<br>
{<br>
myLength = str.length();<br>
myCapacity = myLength + 1;<br>
myCstring = new char[myCapacity];<br>
strcpy(myCstring,str.myCstring);<br>
}<br>
<br>
apstring::~apstring()<br>
//description: destructor<br>
//postcondition: string is destroyed<br>
{<br>
delete[] myCstring; // free memory<br>
}<br>
<br>
const apstring& apstring::eek:perator =(const apstring & rhs)<br>
//postcondition: normal assignment via copying has been performed<br>
{<br>
if (this != &rhs) // check aliasing<br>
{<br>
if (myCapacity &lt; rhs.length() + 1) // more memory needed?<br>
{<br>
delete[] myCstring; // delete old string<br>
myCapacity = rhs.length() + 1; // add 1 for '\0'<br>
myCstring = new char[myCapacity];<br>
}<br>
myLength = rhs.length();<br>
strcpy(myCstring,rhs.myCstring);<br>
}<br>
return *this;<br>
}<br>
<br>
<br>
const apstring& apstring::eek:perator = (const char * s)<br>
//description: assignment from literal string such as &quot;abcd&quot;<br>
//precondition: s is '\0'-terminated string as used in C<br>
//postcondition: assignment via copying of s has been performed<br>
{<br>
<br>
int len = 0; // length of newly constructed string<br>
assert(s != 0); // make sure s non-NULL<br>
len = strlen(s); // # of characters in string<br>
<br>
// free old string if necessary<br>
<br>
if (myCapacity &lt; len + 1)<br>
{<br>
delete[] myCstring; // delete old string<br>
myCapacity = len + 1; // add 1 for '\0'<br>
myCstring = new char[myCapacity];<br>
}<br>
myLength = len;<br>
strcpy(myCstring,s);<br>
return *this;<br>
}<br>
<br>
const apstring& apstring::eek:perator = (char ch)<br>
//description: assignment from character as though single char string<br>
//postcondition: assignment of one-character string has been performed<br>
{<br>
if (myCapacity &lt; 2)<br>
{<br>
delete [] myCstring;<br>
myCapacity = 2;<br>
myCstring = new char[myCapacity];<br>
}<br>
myLength = 1;<br>
myCstring[0] = ch; // make string one character long<br>
myCstring[1] = '\0';<br>
return *this;<br>
}<br>
<br>
int apstring::length( ) const<br>
//postcondition: returns # of chars in string<br>
{<br>
return myLength;<br>
}<br>
<br>
const char * apstring::c_str() const<br>
//description: convert string into a '\0'-terminated string as<br>
// used in C for use with functions<br>
// that have '\0'-terminated string parameters.<br>
//postcondition: returns the equivalent '\0'-terminated string<br>
{<br>
return myCstring;<br>
}<br>
<br>
char& apstring::eek:perator[](int k)<br>
// precondition: 0 &lt;= k &lt; length()<br>
// postcondition: returns copy of the kth character<br>
// note: if this reference is used to write a '\0'<br>
// subsequent results are undefined<br>
{<br>
if (k &lt; 0 ¦¦ myLength &lt;= k)<br>
{<br>
cerr &lt;&lt; &quot;index out of range: &quot; &lt;&lt; k &lt;&lt; &quot; string: &quot; &lt;&lt; myCstring<br>
&lt;&lt; endl;<br>
assert(0 &lt;= k && k &lt; myLength);<br>
}<br>
return myCstring[k];<br>
}<br>
<br>
char apstring::eek:perator[](int k) const<br>
// precondition: 0 &lt;= k &lt; length()<br>
// postcondition: returns copy of the kth character<br>
{<br>
if (k &lt; 0 ¦¦ myLength &lt;= k)<br>
{<br>
cerr &lt;&lt; &quot;index out of range: &quot; &lt;&lt; k &lt;&lt; &quot; string: &quot; &lt;&lt; myCstring<br>
&lt;&lt; endl;<br>
assert(0 &lt;= k && k &lt; myLength);<br>
}<br>
return myCstring[k];<br>
}<br>
<br>
ostream& operator &lt;&lt;(ostream & os, const apstring & str)<br>
//postcondition: str is written to output stream os<br>
{<br>
return os &lt;&lt; str.c_str();<br>
}<br>
<br>
istream& operator &gt;&gt;(istream & is, apstring & str)<br>
//precondition: input stream is open for reading<br>
//postcondition: the next string from input stream is has been read<br>
// and stored in str<br>
{<br>
char buf[MAX_LENGTH];<br>
is &gt;&gt; buf;<br>
str = buf;<br>
return is;<br>
}<br>
<br>
<br>
istream & getline(istream & is, apstring & str)<br>
//description: reads a line from input stream is into the string str<br>
//precondition: input stream is open for reading<br>
//postcondition: chars from input stream is up to '\n' have been read<br>
{<br>
char buf[MAX_LENGTH];<br>
is.getline(buf,MAX_LENGTH);<br>
str = buf;<br>
return is;<br>
}<br>
<br>
const apstring& apstring::eek:perator +=(const apstring & str)<br>
//postcondition: concatenates a copy of str onto this string<br>
{<br>
<br>
apstring copystring(str); // copy to avoid aliasing problems<br>
<br>
int newLength = length() + str.length(); // self + added string<br>
int lastLocation = length(); // index of '\0'<br>
<br>
// check to see if local buffer not big enough<br>
if (newLength &gt;= myCapacity)<br>
{<br>
myCapacity = newLength + 1;<br>
if (str.length() == 1) // special case for catenating one char<br>
{ // make room for future catenations<br>
myCapacity *= 2;<br>
}<br>
char * newBuffer = new char[myCapacity];<br>
strcpy(newBuffer,myCstring); // copy into new buffer<br>
delete [] myCstring; // delete old string<br>
myCstring = newBuffer;<br>
}<br>
<br>
// now catenate str (copystring) to end of myCstring<br>
strcpy(myCstring+lastLocation,copystring.c_str() );<br>
myLength = newLength; // update information<br>
<br>
return *this;<br>
}<br>
<br>
const apstring & apstring::eek:perator += ( char ch )<br>
// postcondition: concatenates a copy of ch onto this string<br>
{<br>
apstring temp; // make string equivalent of ch<br>
temp = ch;<br>
*this += temp;<br>
return *this;<br>
}<br>
<br>
apstring operator +(const apstring & lhs, const apstring & rhs)<br>
// postcondition: returns concatenation of lhs with rhs<br>
{<br>
apstring result(lhs); // copies lhs to result<br>
result += rhs; // catenate rhs<br>
return result; // returns a copy of result<br>
}<br>
<br>
apstring operator + ( char ch, const apstring & str )<br>
// postcondition: returns concatenation of ch with str<br>
{<br>
apstring result; // make string equivalent of ch<br>
result = ch;<br>
result += str;<br>
return result;<br>
}<br>
<br>
apstring operator + ( const apstring & str, char ch )<br>
// postcondition: returns concatenation of str with ch<br>
{<br>
apstring result(str);<br>
result += ch;<br>
return result;<br>
}<br>
<br>
<br>
apstring apstring::substr(int pos, int len) const<br>
//description: extract and return the substring of length len starting<br>
// at index pos<br>
//precondition: this string represents c0, c1, ..., c(n-1)<br>
// 0 &lt;= pos &lt;= pos + len - 1 &lt; n.<br>
//postcondition: returns the string that represents<br>
// c(pos), c(pos+1), ..., c(pos+len-1)<br>
//<br>
{<br>
if (pos &lt; 0) // start at front when pos &lt; 0<br>
{<br>
pos = 0;<br>
}<br>
<br>
if (pos &gt;= myLength) return &quot;&quot;; // empty string<br>
<br>
int lastIndex = pos + len - 1; // last char's index (to copy)<br>
if (lastIndex &gt;= myLength) // off end of string?<br>
{<br>
lastIndex = myLength-1;<br>
}<br>
<br>
apstring result(*this); // make sure enough space allocated<br>
<br>
int j,k;<br>
for(j=0,k=pos; k &lt;= lastIndex; j++,k++)<br>
{<br>
result.myCstring[j] = myCstring[k];<br>
}<br>
result.myCstring[j] = '\0'; // properly terminate C-string<br>
result.myLength = j; // record length properly<br>
return result;<br>
}<br>
<br>
int apstring::find(const apstring & str) const<br>
//description: find the first occurrence of the string str within this<br>
// string and return the index of the first character. If<br>
// str does not occur in this string, then return npos.<br>
//precondition: this string represents c0, c1, ..., c(n-1)<br>
// str represents s0, s1, ...,s(m-1)<br>
//postcondition: if s0 == ck0, s1 == ck1, ..., s(m-1) == ck(m-1) and<br>
// there is no j &lt; k0 such that s0 = cj, ...., sm == c(j+m-1),<br>
// then returns k0;<br>
// otherwise returns npos<br>
{<br>
int len = str.length();<br>
int lastIndex = length() - len;<br>
int k;<br>
for(k=0; k &lt;= lastIndex; k++)<br>
{<br>
if (strncmp(myCstring + k,str.c_str(),len) == 0) return k;<br>
}<br>
return npos;<br>
}<br>
<br>
int apstring::find( char ch ) const<br>
// description: finds the first occurrence of the character ch within this<br>
// string and returns the index. If ch does not occur in this<br>
// string, then returns npos.<br>
// precondition: this string represents c0, c1, ..., c(n-1)<br>
// postcondition: if ch == ck, and there is no j &lt; k such that ch == cj<br>
// then returns k;<br>
// otherwise returns npos<br>
{<br>
int k;<br>
for(k=0; k &lt; myLength; k++)<br>
{<br>
if (myCstring[k] == ch)<br>
{<br>
return k;<br>
}<br>
}<br>
return npos;<br>
}<br>
<br>
<br>
bool operator == ( const apstring & lhs, const apstring & rhs )<br>
{<br>
return strcmp(lhs.c_str(), rhs.c_str()) == 0;<br>
}<br>
<br>
bool operator != ( const apstring & lhs, const apstring & rhs )<br>
{<br>
return ! (lhs == rhs);<br>
}<br>
<br>
bool operator &lt; ( const apstring & lhs, const apstring & rhs )<br>
{<br>
return strcmp(lhs.c_str(), rhs.c_str()) &lt; 0;<br>
}<br>
<br>
bool operator &lt;= ( const apstring & lhs, const apstring & rhs )<br>
{<br>
return !( rhs &lt; lhs );<br>
}<br>
bool operator &gt; ( const apstring & lhs, const apstring & rhs )<br>
{<br>
return rhs &lt; lhs;<br>
}<br>
<br>
bool operator &gt;= ( const apstring & lhs, const apstring & rhs )<br>
{<br>
return ! ( lhs &lt; rhs );<br>
} <p>Korn<br><a href=mailto:airdevil23@hotmail.com>airdevil23@hotmail.com</a><br><a href= > </a><br>"procrastination is the key to happiness"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top