Can someone help me modify the code to fit the details? I have provided the part
ID: 3718861 • Letter: C
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 <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include<cstring>
#include <list>
#include <cstdlib>
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 << "Input file could not be opened." << endl;
exit(1);
}
//Declared variables
list<Number> lst;
Number num;
string str;
int si; //Space index
int count = 0;
int choice;
inFile.seekg(0); //Go to first line
while (!inFile.eof())//While not end of file
{
num.roman[15] = '';
num.arabic[4] = '';
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<si;i++){
num.roman[i] = str[i];
}
for(int i=si;i<15;i++){
num.roman[i] ='';
}
int arab = convertRomanToArabic(str);//Call to roman-to-arabic conversion function
stringstream ss;
ss << arab;
string s = ss.str();
strcpy(num.arabic, s.c_str());
cout<<num.arabic<<endl;
}
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<Number>::iterator itr = lst.begin();
while (itr != lst.end())
{
outFile << left << setw(15) << (*itr).roman << " " << setw(4) << (*itr).arabic;//Write to output file
itr++;
if(itr != lst.end())
outFile << endl;
}
//Close file
outFile.close();
//Output options menu
cout<<"1. Search"<<endl;
cout<<"2. Sort"<<endl;
cout<<"3. Exit"<<endl;
cin >> 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 << "Invalid option" << endl; //If invalid option is entered
}
//char rom;
//char arab;
NumberPtr head = new Number;
//Write numbers to linked list
while (inFile >> num.roman >> num.arabic)
{
cout << num.roman << endl;
cout << num.arabic << endl;
// insertFirstElement (num.roman, arab);
}
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 < str.length() - 1; i++)
{
ch = str[i];
nch = str[i + 1];
//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 << "Invalid roman numeral." << ch << endl;
exit(1);
}
}
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 << "Invalid roman numeral." << ch << endl;
exit(1);
}
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 < curr; i++)
{
roman += 'M';
}
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 < 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;
}
//Roman conversions
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;
}
//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 << "List is empty" << endl;
return;
}
ptr = head;
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 << "Enter the value to be searched: ";
cin >> 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)
{
cout<<"Element "<<value<<" is found at position "<<position<<endl; //Output found position
return;
}
s = s->next;
}
cout<<"Element "<<value<<" not found"<<endl; //If element value is not found
}
-------------------------------------------------------------
Sample File:
Problem: Doc Brown has configured the DeLorean to go back to the days of the Roman Empire. Unfortunately the Roman Empire does not use Arabic numerals which is going to make it difficult for Doc to do the proper calculations he needs to get back home. Before he heads back in time, he wants you to create a Roman numeral converter for himm Pseudocode Details: Linked list functions o Recursive print o Overloadedoperator o Overloaded operator - Prefix logic Postfix logic Main function Class Details BaseNode class o Must be abstract (-5 points) o Attributes Roman numeral Arabic numeral o Methods Default constructor Necessary for inheritance Overloaded constructor Copy constructor Accessors Mutators Overloaded . Read data from input stream and store in node DoubleLinkNode o Derived from BaseNode o Attributes Next pointerExplanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int count();
void display(struct node *r);
struct node
{
int data;
struct node *next;
}*head;
void append(int num)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
right=(struct node *)head;
while(right->next != NULL)
right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}
void add( int num )
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(head== NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addafter(int num, int loc)
{
int i;
struct node *temp,*left,*right;
right=head;
for(i=1;i<loc;i++)
{
left=right;
right=right->next;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
left->next=temp;
left=temp;
left->next=right;
return;
}
void insert(int num)
{
int c=0;
struct node *temp;
temp=head;
if(temp==NULL)
{
add(num);
}
else
{
while(temp!=NULL)
{
if(temp->data<num)
c++;
temp=temp->next;
}
if(c==0)
add(num);
else if(c<count())
addafter(num,++c);
else
append(num);
}
}
int delete_n(int num)
{
struct node *temp, *prev;
temp=head;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==head)
{
head=temp->next;
free(temp);
return 1;
}
else
{
prev->next=temp->next;
free(temp);
return 1;
}
}
else
{
prev=temp;
temp= temp->next;
}
}
return 0;
}
void display(struct node *r)
{
r=head;
if(r==NULL)
{
return;
}
while(r!=NULL)
{
printf("%d ",r->data);
r=r->next;
}
printf(" ");
}
int count()
{
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}
void sort()
{
struct node *ptr, *s;
int value;
if (head == NULL)
{
cout<<"The List is empty"<<endl;
return;
}
ptr = head;
while (ptr != NULL)
{
for (s = ptr->next;s !=NULL;s = s->next)
{
if (ptr->data < s->data)
{
value = ptr->data;
ptr->data = s->data;
s->data = value;
}
}
ptr = ptr->next;
}
}
void Linear_search()
{
int value, pos = 0;
bool flag = false;
if (head == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the value to be searched: ";
cin>>value;
struct node *s;
s = head;
while (s != NULL)
{
pos++;
if (s->data == value)
{
flag = true;
cout<<"Element "<<value<<" is found at position "<<pos<<endl;
}
s = s->next;
}
if (!flag)
cout<<"Element "<<value<<" not found in the list"<<endl;
}
int main()
{
int i,num;
int last_item;
struct node *n;
head=NULL;
while(1)
{
cout<<" Linked List Operations "<<endl;
cout<<"========================= "<<endl;
cout<<"1.Insert"<<endl;
cout<<"2.Display"<<endl;
cout<<"3.Size"<<endl;
cout<<"4.delete_n"<<endl;
cout<<"5.search Element"<<endl;
cout<<"6.Sort the Elements:"<<endl;
cout<<"7.Exit "<<endl;
cout<<"Enter your choice:"<<endl;
if(scanf("%d",&i)<=0)
{
cout<<"Enter only an Integer"<<endl;
exit(0);
}
else
{
switch(i)
{
case 1:
cout<<"Enter the number to insert"<<endl;
scanf("%d",&num);
insert(num);
break;
case 2:
if(head==NULL)
{
cout<<"List is Empty"<<endl;
}
else
{
cout<<"Element(s) in the list are:"<<endl;
}
display(n);
break;
case 3:
cout<<"Size of the list is:"<<count()<<endl;
break;
case 4:
if(head==NULL)
cout<<"List is Empty "<<endl;
else
{
cout<<"Enter the number to delete_n"<<endl;
scanf("%d",&num);
if(delete_n(num))
cout<<num<<" deleted successfully"<<endl;
else
cout<<num<<"is not found in the list"<<endl;
}
break;
case 5:
Linear_search();
break;
case 6:
sort();
break;
case 7:
return 0;
default:
cout<<"Invalid option"<<endl;
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.