Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help creating an insert node function and determining the parameters that will make the sort and linear search functions work with the main

I need help creating an insert node function and determining the parameters that will make the sort and linear search functions work with the main function.

The program is supposed to read from the input file and convert the arabic number or roman numeral into its counterpart. I have also provide an sample for the input file. Any and all help would be appreciated.

In C++ and all in one file. Thank you.

#include #include #include #include #include #include #include using namespace std;

// function prototypes int convertRomanToArabic(string); string convertArabicToRoman(int); void sort(); void Linear_search(int );

// Number struct struct Number { char roman[16]; char arabic[5]; };

struct node { int data; struct node *next; }*head;

// main function int main() { // open the file to read fstream file; file.open("numbers.txt", ios::in);

// exit from the program if the input file does not exist if (file.fail()) { cout << "The input file could not be opened!" << endl; exit(1); }

// declare the required variables list lst; Number num; string str; int si; // space index int count = 0; int choice;

// read data from the file file.seekg(0); // go to the first line file.read((char *)&num, sizeof(Number)); while (!file.eof()) { num.roman[15] = '\0'; num.arabic[4] = '\0';

if (num.roman[0] == ' ') { str = num.arabic; si = str.find(' '); str = str.substr(0, si);

string rom = convertArabicToRoman(atoi(str.c_str()));

strcpy(num.roman, rom.c_str()); }

else { str = num.roman; si = str.find(' '); str = str.substr(0, si);

int arab = convertRomanToArabic(str);

stringstream ss; ss << arab; string s = ss.str();

strcpy(num.arabic, s.c_str()); }

lst.push_back(num);

count++;

file.read((char *)&num, sizeof(Number)); }

// close the file file.close();

// open the file to write file.open("numbers.txt", ios::out);

// write data to the file list::iterator itr = lst.begin(); while (itr != lst.end()) { file << left << setw(15) << (*itr).roman << " " << setw(4) << (*itr).arabic; itr++;

if (itr != lst.end()) file << endl; }

// close the file file.close();

cout<<"1. Search"<> choice;

if (choice == 1) { // Linear_search(); } else if (choice == 2) { sort(); } else if (choice == 3) { return 0; } else { cout << "Invalid option" << endl; } return 0;

} // end of main function

// convertRomanToArabic function implementation int convertRomanToArabic(string str) { if (str.length() == 0) return 0;

int arabic = 0; char ch; // current character char nch; // next character

for (unsigned int i = 0; i < str.length() - 1; i++) { ch = str[i]; nch = str[i + 1]; if (ch == 'M') arabic += 1000; else if (ch == 'D') arabic += 500; else if (ch == 'C' && (nch == 'D' || nch == 'M')) arabic -= 100; else if (ch == 'C') arabic += 100; else if (ch == 'L') arabic += 50; else if (ch == 'X' && (nch == 'L' || nch == 'C')) arabic -= 10; else if (ch == 'X') arabic += 10; else if (ch == 'V') arabic += 5; else if (ch == 'I' && (nch == 'V' || nch == 'X')) arabic -= 1; else if (ch == 'I') arabic += 1; else { cout << "Invalid roman number! " << ch << endl; exit(1); } }

ch = str[str.length() - 1];

if (ch == 'M') arabic += 1000; else if (ch == 'D') arabic += 500; else if (ch == 'C') arabic += 100; else if (ch == 'L') arabic += 50; else if (ch == 'X') arabic += 10; else if (ch == 'V') arabic += 5; else if (ch == 'I') arabic += 1; else { cout << "Invalid roman number! " << ch << endl; exit(1); }

return arabic; } // end of convertRomanToArabic function

// convertArabicToRoman function implementation string convertArabicToRoman(int arabic) { string roman; int curr;

if (arabic >= 1000) { curr = arabic / 1000; for (int i = 0; i < curr; i++) { roman += 'M'; }

arabic = arabic % 1000; }

if (arabic >= 100) { curr = arabic / 100;

if (curr == 9) { roman += "CM"; } else if (curr >= 5) { roman += 'D'; for (int i = 0; i < curr - 5; i++) { roman += 'C'; } } else if (curr == 4) { roman += "CD"; } else if (curr >= 1) { for (int i = 0; i < curr; i++) { roman += 'C'; } }

arabic = arabic % 100; }

if (arabic >= 10) { curr = arabic / 10; if (curr == 9) { roman += "XC"; } else if (curr >= 5) { roman += 'L'; for (int i = 0; i < curr - 5; i++) { roman += 'X'; } } else if (curr == 4) { roman += "XL"; } else if (curr >= 1) { for (int i = 0; i < curr; i++) { roman += 'X'; } }

arabic = arabic % 10; }

if (arabic >= 1) { curr = arabic; if (curr == 9) { roman += "IX"; } else if (curr >= 5) { roman += 'V'; for (int i = 0; i < curr - 5; i++) { roman += 'I'; } } else if (curr == 4) { roman += "IV"; } else if (curr >= 1) { for (int i = 0; i < curr; i++) { roman += 'I'; } } }

return roman; } // end of convertArabicToRoman function

void sort() { struct node *ptr, *s, *prev; if (head == NULL) { cout << "The List is empty" << endl; return; } ptr = head;

while ( ptr && ptr->next ) // check preset and next { if ( ptr->data > ptr->next->data ) // check next element is greater than or not { if ( prev == NULL ) // for first condition, when head needs to change { s = ptr->next; // save next ptr->next = ptr->next->next; // connect next to next next s->next = ptr; // connect previous head = s; // change the head ptr = head; // reset the ptr } else if ( prev && ptr->next->next ) // when swap is not for first element { s = ptr->next; ptr->next = ptr->next->next; s->next = ptr; prev->next = s; ptr = head; prev = NULL; // reset prev } else if ( prev && ptr->next ) // condition for last elements { s = ptr->next; ptr->next = NULL; // last element connect to null s->next = ptr; prev->next = s; ptr = head; prev = NULL; } } else // executes first elements need not to be swap { prev = ptr; ptr = ptr->next; } } }

void Linear_search( ) { int value, pos = 0;

cout<<"Enter the value to be searched: "; cin>>value; struct node *s; s = head; while (s != NULL) { pos++; if (s->data == value) { cout<<"Element "<next; } cout<<"Element "<

Sample Input:

XXVII 123 MCLXV 1975 

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

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions

Question

Identify possible reasons for ineffective performance.

Answered: 1 week ago

Question

Describe the components of a needs assessment.

Answered: 1 week ago

Question

Describe the benefits of employee orientation.

Answered: 1 week ago