Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you convert the given C code into c++ that follows the given class as well as create the reverse function and operation < <

Can you convert the given C code into c++ that follows the given class as well as create the reverse function and operation << overload.

"Capsulize" your database by defining the following class, llist, which contains the database with necessary methods.

Create llist.h, containing this class definition. *note: you cannot change the class definition without consent or instruction.

class llist { private: record * start; char filename[16]; int readfile(); int writefile(); record * reverse(record * ); void cleanup(); public: llist(); llist(char[]); ~llist(); int addRecord (int, char [ ],char [ ]); int printRecord (int); int modifyRecord ( int, char [ ]); // replace printAll() with the << operator int deleteRecord(int); void reverse(); };

Create llist.cpp, containing the function definitions for all the class member functions, including the constructor and destructor. The constructor will initialize the class variables. The destructor will deallocate the entire linked list. The features are the same as those in Project1 unless specified as follows.

Use cin.get to get the name field, which may contain white spaces, and the addresses field, which could have multiple lines.

Implement the reverse method using recursion and add an option to the menu This will reverse the list without printing it.

Overload the << operator to print the contents of the entire database so that it replaces the calling of the printAll method in the user-interface.

Extra Credit (2 pts for each): Implement a copy constructor and/or the overloaded assignment operator for the llist class.

User-Interface

Write C++ user-interface code with the following features. Others are the same as Project1.

While you must define the two constructor functions in the class, you may use either in your main function. For example, in the main function, you will have

llist mylist;

or

llist mylist( "proj2.txt" );

You may create any other functions you like or add to the menu in the interface.

You cannot use any of the C I/O functions. Use the C++ versions instead.

Other Requirements for Coding

Implement debug mode using preprocessor directives, instead of command line arguments.

No global variables may be used.

Use ifndef directives for the header files

A makefile must be used.

===========================================================

C code:

/*****************************************************************

//

// NAME: Nicolas Lum

//

// HOMEWORK: 1

//

// CLASS: ICS 212

//

// INSTRUCTOR: Ravi Narayan

//

// DATE: July 10, 2017

//

// FILE: iofunctions.c

//

// DESCRIPTION: This file contains the database functions

// for Project1 the bank database application

//

******************************************************************/

#include

#include

#include

#include "record.h"

#include "prototype.h"

#include

/*****************************************************************

*

* Function name: getAddress

*

* DESCRIPTION: Obtains address from the user, output in a single line

*

*

* Parameters: char address[ ], int sizeOfAddress

*

* Return Value: void: function does not return anything

*

*****************************************************************/

void getAddress (char address[ ], int sizeOfAddress)

{

char c;

int boolean = 1;

int i = 0;

while(boolean == 1){

c = fgetc(stdin);

if(c == '!') {

break;

}

address[i] = (char) c;

i++;

}

address[i] = '\0';

if(DEBUG_MODE == 1)

{

printf(" DEBUG: scanned address lines : %s ", address);

}

}

/*****************************************************************

*

* Function name: addRecord

*

* DESCRIPTION: Adds a new record to the beginning of the database

*

* Parameters: struct record **: pointer to next null entry for new record

* int accnum: account numer for the new record

* char name[]: name for the new record

* char addr[]: address for the new record

*

* Return values: 1: success

* 0: failure

*

*****************************************************************/

int addRecord (struct record **start, int accno, char name[ ],char address[ ])

{

struct record *previous,*current,*newRecord;

int success;

newRecord = (struct record*) malloc (sizeof(struct record));

success = 0;

newRecord -> accountno = accno;

strcpy(newRecord -> name,name);

strcpy(newRecord -> address,address);

current = *start;

previous = NULL;

if (DEBUG_MODE == 1)

{

printf(" ********************************************* ");

printf("FUNCTION CALLED: addRecord(); ");

printf("PARAMETERS PASSED: ");

printf("Account number: %d Name: %s Address: %s", accno, name, address);

printf("********************************************* ");

}

/* add to an empty list */

if (*start == NULL)

{

newRecord -> next = NULL;

*start = newRecord;

success = 1;

}

else

{

while (current != NULL && success == 0)

if (newRecord -> accountno <= current -> accountno)

{

newRecord -> next = current;

/*add to beggining of list */

if (previous == NULL)

{

*start = newRecord;

success = 1;

}

/*add in front of list */

else

{

previous ->next = newRecord;

success = 1;

}

}

else if(current -> next == NULL)

{

newRecord -> next = current -> next;

current -> next = newRecord;

success = 1;

}

else

{

previous = current;

current = previous -> next;

}

}

return success;

}

/*****************************************************************

*

* Function name: printRecord

*

* DESCRIPTION: Prints out a record based on the account number given and starting data base pointer

*

* Parameters: struct record *: header pointer

* int accnum: account numer to search for

*

* Return values: 1: success

* 0: failure

*

*****************************************************************/

int printRecord (struct record * start, int uaccountnum)

{

struct record * current;

int matchFound;

current = start;

matchFound = 0;

if (DEBUG_MODE == 1)

{

printf(" ********************************************* ");

printf("FUNCTION CALLED: printRecord(); ");

printf("PARAMETERS PASSED: ");

printf("Account number: %d ", uaccountnum);

printf("********************************************* ");

}

if(current == NULL)

{

printf(" ERROR: List is empty ");

}

while(current != NULL)

{

if(current->accountno == uaccountnum)

{

printf(" %d %s %s ", current->accountno, current->name, current->address);

matchFound = 1;

}

current = current->next;

}

if(matchFound == 0)

{

printf(" ERROR: No Matching Account Number Found! ");

}

return 0;

}

/*****************************************************************

*

* Function name: printAllRecord

*

* DESCRIPTION: print all records of the given address book

*

* Parameters: struct record *: pointer to the address book header

*

* Return values: void : does not return any values

*

*****************************************************************/

void printAllRecords(struct record * start)

{

struct record * current;

current = start;

if (DEBUG_MODE == 1)

{

printf(" ********************************************* ");

printf("FUNCTION CALLED: printAllRecords(); ");

printf("PARAMETERS PASSED: ");

printf("********************************************* ");

}

if(current == NULL)

{

printf(" ERROR: List is empty! ");

}

else

{

printf(" PRINTING ALL RECORDS: ");

while(current != NULL)

{

printf(" %d %s %s ", current->accountno, current->name, current->address);

current = current->next;

}

printf(" END OF THE RECORD ");

}

}

/*****************************************************************

*

* Function name: deleteRecord

*

* DESCRIPTION: delete a record from the address book for the account number given

*

* Parameters: struct record **: pointer to address book header

* int accnum: account numer to searc for

*

* Return values: 1: success

* 0: failure

*

*****************************************************************/

int deleteRecord(struct record ** ptrStart, int uaccountnum)

{

struct record * previous;

struct record * current;

struct record * dump;

int returnValue;

int matchFound;

previous = * ptrStart;

current = * ptrStart;

returnValue = -1;

matchFound = 0;

if (DEBUG_MODE == 1)

{

printf(" ********************************************* ");

printf("FUNCTION CALLED: deleteRecord(); ");

printf("PARAMETERS PASSED: ");

printf("Account number: %d ", uaccountnum);

printf("********************************************* ");

}

while(current != NULL)

{

if(current == *ptrStart)

{

if(uaccountnum == current->accountno)

{

dump = current;

*ptrStart = current->next;

current = current->next;

previous = previous->next;

free(dump);

printf(" RECORD DELETED! ");

matchFound = 1;

returnValue = 0;

}

else

{

current = current->next;

}

}

else if(uaccountnum == current->accountno)

{

dump = current;

previous->next = current->next;

current = current->next;

free(dump);

printf(" RECORD DELETED! ");

matchFound = 1;

returnValue = 0;

}

else

{

previous = previous->next;

current = current->next;

}

}

if(matchFound == 0)

{

printf(" ERROR: No Matching Account Number Found! ");

}

return returnValue;

}

/*

// Function name: modifyRecord

//

// DESCRIPTION: A database function.

// This function obtains the account number that the user wants to modify

// address information of, and the new address. Then the program searches

// for matching account number, if found, the address is modified. When

// there are multiple records with same account number, all of them are

// modified.

//

// Parameters: ptrStart (struct record *) : pointer to the start of the record

// uaccountnum (int): user input account number to modify record of

// uaddress (char[]): an array of char containing the new user input address

//

// Return values: 0 : success

// -1 : failed

//

******************************************************************/

int modifyRecord (struct record * start, int uaccountnum, char uaddress[ ])

{

struct record * current;

int matchFound;

current = start;

matchFound = 0;

if (DEBUG_MODE == 1)

{

printf(" ********************************************* ");

printf("FUNCTION CALLED: modifyRecord(); ");

printf("PARAMETERS PASSED: ");

printf("Account number: %d Address: %s ", uaccountnum, uaddress);

printf("********************************************* ");

}

if(current == NULL)

{

printf(" ERROR: List is empty ");

}

while(current != NULL)

{

if(current->accountno == uaccountnum)

{

strcpy(current->address, uaddress);

printf(" RECORD MODIFIED! ");

matchFound = 1;

}

current = current->next;

}

if(matchFound == 0)

{

printf(" ERROR: No Matching Account Number Found! ");

}

return 0;

}

int readFile(struct record ** ptrStart, char filename[ ])

{

FILE * inFile = fopen(filename, "r");

int uaccountnum;

char uname[25];

char uaddress[80];

char lines[256];

int lineNum;

lineNum = 1;

inFile = fopen(filename,"r");

if (DEBUG_MODE == 1)

{

printf(" ********************************************* ");

printf("FUNCTION CALLED: readfile(); ");

printf("PARAMETERS PASSED: ");

printf("File name: %s ", filename);

printf("********************************************* ");

printf(" ---Contents of the input file being added--- ");

}

if(inFile)

{

while(!feof(inFile))

{

if(lineNum == 1)

{

fgets(lines, sizeof(lines), inFile);

uaccountnum = atoi(lines);

lineNum++;

}

else if (lineNum == 2)

{

fgets(lines, sizeof(lines), inFile);

strcpy(uname, lines);

lineNum++;

}

else if(lineNum == 3)

{

fgets(lines, sizeof(lines), inFile);

if(lines[strlen(lines) - 2] != '!')

{

strcpy(uaddress, lines);

lineNum++;

}

else

{

strcpy(uaddress, lines);

lineNum++;

}

}

else

{

uname[strlen(uname) - 1] = '\0';

uaddress[strlen(uaddress) - 2] = uaddress[strlen(uaddress) - 1];

uaddress[strlen(uaddress) - 2] = '\0';

lineNum = 1;

addRecord(ptrStart,uaccountnum,uname,uaddress);

}

}fclose(inFile);

}

else

{

printf("No files found");

}

return 0;

}

/*****************************************************************

//

// Function name: writeFile

//

// DESCRIPTION: A database function.

// This function obtains the file name from the user to write all the records

// in the database to at the end of the program.

//

// Parameters: start (struct record *) : pointer to the start of the record

// filename (char[]): an array of char containing the file name

//

// Return values: void : Function does not return anything;

//

******************************************************************/

void writeFile(struct record * start, char filename[ ])

{

struct record * temp;

FILE *outFile;

temp = start;

outFile = fopen(filename, "w");

if (DEBUG_MODE == 1)

{

printf(" ********************************************* ");

printf("FUNCTION CALLED: writefile(); ");

printf("PARAMETERS PASSED: ");

printf("File name: %s ", filename);

printf("********************************************* ");

}

while (temp != NULL)

{

fprintf(outFile, "%d %s %s! ", temp->accountno, temp->name, temp->address);

temp = temp->next;

}

fclose(outFile);

}

void concatenate(char str1[], char str2[])

{

int c, d;

c = 0;

while (str1[c] != '\0') {

c++;

}

d = 0;

while (str2[d] != '\0') {

str1[c] = str2[d];

d++;

c++;

}

str1[c] = '\0';

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions