Question: Can someone help me modify the code to fit the details? I have provided the partially written code and sample input file. Code: #include #include
Can someone help me modify the code to fit the details? I have provided the partially written code and sample input file.




![{ char roman[16]; char arabic[5]; }; // function prototypes typedef Number* NumberPtr;](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66efa4c2d8c29_31466efa4c2734ec.jpg)
Code:
#include #include
using namespace std;
struct Node { int data; struct Node *next; }*head;
// Number struct struct Number { char roman[16]; char arabic[5]; };
// function prototypes typedef Number* NumberPtr; int convertRomanToArabic(string); string convertArabicToRoman(int); void sort(); void linearSearch(struct Number, int); void insertFirstElement(NumberPtr&, char, char); void insert(NumberPtr&, char, char);
// main function int main() { // open the file to read ifstream inFile; ofstream outFile; inFile.open("numbers1.txt", ios::in);
// exit from the program if the input file does not exist if (inFile.fail()) { cout
//Declared variables list
inFile.seekg(0); //Go to first line
while (!inFile.eof())//While not end of file { num.roman[15] = '\0'; num.arabic[4] = '\0';
inFile.read((char *)&num, sizeof(Number)); //Read characters from input file if (num.roman[0] == ' ') { str = num.arabic; si = str.find(' '); str = str.substr(0, si);
string rom = convertArabicToRoman(atoi(str.c_str())); //Call to arabic-to-roman conversion function
strcpy(num.roman, rom.c_str()); }
else { str = num.roman; si = str.find(' '); str = str.substr(0, si);
for(int i=0;i int arab = convertRomanToArabic(str);//Call to roman-to-arabic conversion function stringstream ss; ss strcpy(num.arabic, s.c_str()); cout lst.push_back(num); count++; } //Close file inFile.close(); //Open file to write to outFile.open("output.txt", ios::out); //Write data to file list if(itr != lst.end()) outFile //Close file outFile.close(); //Output options menu cout> choice;//Get selection from user if (choice == 1) { linearSearch(num,count);//Call to search function } else if (choice == 2) { sort();//Call to sort function } else if (choice == 3) { return 0; //Return 0 } else { cout //char rom; //char arab; NumberPtr head = new Number; //Write numbers to linked list while (inFile >> num.roman >> num.arabic) { cout return 0; } //function that converts roman numerals to arabic numbers int convertRomanToArabic(string str) { int arabic = 0; char ch; //Current character char nch; //Next character if (str.length() == 0) { return 0; } for (unsigned int i = 0; i //Arabic conversions 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 ch = str[str.length() - 1]; //Arabic conversions 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 return arabic; } //function that coverts arabic numbers to roman numerals string convertArabicToRoman(int arabic) { string roman; int curr; //Roman conversoins if (arabic >= 5000) { curr = arabic / 1000; for (int i = 0; i arabic = arabic % 1000; } //Roman conversions if (arabic >= 100) { curr = arabic / 100; if (curr == 9) { roman += "CM"; } else if (curr >= 5) { roman += 'D'; for (int i = 0; i = 1) { for (int i = 0; i arabic = arabic % 100; } //Roman conversions if (arabic >= 10) { curr = arabic / 10; if (curr == 9) { roman += "XC"; } else if (curr >= 5) { roman += 'L'; for (int i = 0; i = 1) { for (int i = 0; i arabic = arabic % 10; } if (arabic >= 1) { curr = arabic; if (curr == 9) { roman += "IX"; } else if (curr >= 5) { roman += 'V'; for (int i = 0; i = 1) { for (int i = 0; i return roman; } //Function to insert first element void insertFirstElement(int number, struct Node **head) { struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); newNode->data = number; newNode->next = NULL; if(*head == NULL) { *head = newNode; } else { struct Node *current = *head; while(current->next != NULL) { current = current->next; } current->next = newNode; } } //Fucntion to insert nodes void insert(Node **head) { if(head == NULL) //If head is null { return; } Node* temp = *head; *head = temp->next; delete temp; //Delete temp } //Sort function void sort() { struct Node *ptr, *s, *prev; if (head == NULL) { cout while ( ptr && ptr->next ) //Check preset and next { if ( ptr->data > ptr->next->data ) //Check if 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 ) //If 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 previous } 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; } } } //Function for linear search void linearSearch(Number num, int size) { //Declared and initialized variables int value, position = 0; cout > value; //Value entered by user struct Node *s; s = head; while (s != NULL) //While head is not null { position++;//Increment position if (s->data == value) { coutnext; } cout ------------------------------------------------------------- Sample File: XXVII 123 MCLXV 1975
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
