C++ PROGRAMMING. Finished all the code but its not quite running correctly. Will
ID: 3841082 • Letter: C
Question
C++ PROGRAMMING. Finished all the code but its not quite running correctly. Will attach all instructions and code. All code to be edited is in Text.cpp copied below. Please do not edit anything except Text.cpp. These are all functions of the class Text which isn't included or needed to write these methods. Please make sure the code compiles when I put it in my program so I can give 5 stars. Thank you <3
Instructions:
- Implement the Text ADT (40 points)
- Text(), operator=() and ~Text(), getLength(), clear(), operator[]
- Write the functions toUpper() and toLower() to test (30 points)
- Programming Exercise 3 :Write the operators ==, <, > and test (30 points)
text.cpp:
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#include "Text.h"
Text::Text ( const char *charSeq )
{
bufferSize = strlen(charSeq) + 1;
buffer = new char[bufferSize];
strcpy(buffer, charSeq);
}
Text::Text ( const Text &other )
{
bufferSize = other.bufferSize;
buffer = new char[bufferSize];
strcpy(buffer, other.buffer);
}
void Text::operator = ( const Text &other )
{
int len = other.bufferSize;
if (len > bufferSize)
{
delete[] buffer;
bufferSize = other.bufferSize;
buffer = new char[bufferSize];
}
strcpy(buffer, other.buffer);
}
Text::~Text ()
{
delete[] buffer;
}
int Text::getLength () const
{
return bufferSize - 1;
}
char Text::operator [] ( int n ) const
{
// check if n is out of range first
return 0;
}
void Text::clear ()
{
buffer[0] = '';
}
void Text::showStructure() const
// Outputs the characters in a string. This operation is intended for
// testing/debugging purposes only.
{
int j; // Loop counter
for (j = 0; j < bufferSize; j++)
cout << j << " ";
cout << endl;
for (j = 0; buffer[j] != ''; j++)
cout << buffer[j] << " ";
cout << "\0" << endl;
}
Text Text::toUpper( ) const
{
Text temp;
for (int i = 0; i < bufferSize; i++)
{
if ((buffer[i] > 96 && (buffer[i] < 123)))
{
temp.buffer[i] = char(buffer[i] - 32);
}
}
return temp;
}
Text Text::toLower( ) const
{
Text temp;
for (int i = 0; i < bufferSize; i++)
{
if ((buffer[i] > 64 && (buffer[i] < 91)))
{
temp.buffer[i] = char(buffer[i] + 32);
}
}
return temp;
}
bool Text::operator == ( const Text& other ) const
{
{
if (bufferSize == other.getLength())
{
if (bufferSize == 0)
{
return true;
}
else
{
for (int i = 0; i < bufferSize; i++)
{
if (buffer[i] != other.buffer[i])
{
return false;
}
}
return true;
}
}
else
{
return false;
}
}
}
bool Text::operator < ( const Text& other ) const
{
if (bufferSize < other.getLength())
{
return true;
}
else
{
return false;
}
}
bool Text::operator > ( const Text& other ) const
{
if (bufferSize > other.getLength())
{
return true;
}
else
{
return false;
}
}
Explanation / Answer
using namespace std;
{
int data1s;
struct list *nxt;
}
node_type;
node_type *ptr[maximum],*root[maximum],*temp[maximum];
class Dictionary
{
public:
int index;
Dictionary();
void insert(int);
void search(int);
void del_element(int);
};
Dictionary::Dictionary()
{
index=-1;
for(int k=0; k<maximum; k++)
{
root[k]=EMPTY;
ptr[k]=EMPTY;
temp[k]=EMPTY;
}
}
void Dictionary::insert(int key)
{
index=int(key%maximum);
ptr[index]=(node_type*)malloc(sizeof(node_type));
ptr[index]->data=key;
if(root[index]==EMPTY)
{
root[index]=ptr[index];
root[index]->next=EMPTY;
temp[index]=ptr[index];
}
else
{
temp[index]=root[index];
while(temp[index]->next!=EMPTY)
temp[index]=temp[index]->next;
temp[index]->next=ptr[index];
}
}
void Dictionary::search(int key)
{
int flag=0;
index=int(key%max);
temp[index]=root[index];
while(temp[index]!=EMPTY)
{
if(temp[index]->data==key)
{
cout<<" Search key is found!!";
flag=1;
break;
}
else temp[index]=temp[index]->next;
}
if (flag==0)
cout<<" search key not found.......";
}
void Dictionary::delete_ele(int key)
{
index=int(key%max);
temp[index]=root[index];
while(temp[index]->data!=key && temp[index]!=EMPTY)
{
ptr[index]=temp[index];
temp[index]=temp[index]->next;
}
ptr[index]->next=temp[index]->next;
cout<<" "<<temp[index]->data<<" has been deleted.";
temp[index]->data=-1;
temp[index]=EMPTY;
free(temp[index]);
}
main()
{
int val,ch,n,num;
char c;
Dictionary d;
do
{
cout<<" MENU: 1.Create";
cout<<" 2.Search for a value 3.Delete an value";
cout<<" Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<" Enter the number of elements to be inserted:";
cin>>n;
cout<<" Enter the elements to be inserted:";
for(int k=0; k<n; k++)
{
cin>>num;
d.insert(num);
}
break;
case 2:
cout<<" Enter the element to be searched:";
cin>>n;
d.search(n);
case 3:
cout<<" Enter the element to be deleted:";
cin>>n;
d.delete_ele(n);
break;
default:
cout<<" Invalid Choice.";
}
cout<<" Enter y to Continue:";
cin>>c;
}
while(c=='y');
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.