The programming language is C++ File input begins: File input ends How, would I
ID: 3732453 • Letter: T
Question
The programming language is C++
File input begins:
File input ends
How, would I put a Roman Numeral or an Arabic numeral as a node in a linked list without taking in the rest of the blank space of the line?
The file contains lines of either a Roman Numeral or an Arabic Numeral
The lines should follow this format :
Roman numeral field – 15 characters -
Arabic numeral field – 4 characters
If a line only contains a Roman Numeral it needs to be converted into an Arabic Numeral and vice versa( I have already done this part)
My question is how would I put a Roman Numeral or an Arabic numeral as a node in a linked list without taking in the rest of the blank space of the line?
I have seen the use of #include, but this doesn't work with the compiler used in class
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <list>
using namespace std;
// function prototypes
int convertRomanToArabic(string);
string convertArabicToRoman(int);
// Number struct
struct Number
{
char roman[16];
char arabic[5];
};
// main function
int main()
{
// open the file to read
fstream file;
file.open("input.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<Number> lst;
Number num;
string str;
int si; // space index
int count = 0;
// read data from the file
file.seekg(0); // go to the first line
while (!file.eof())
{
file>>num.roman;
//file.read((char *)&num, sizeof(Number));
num.roman[15] = '';
num.arabic[4] = '';
if (num.roman[0]>='0' && num.roman[0]<='9')
{
strcpy(num.arabic, num.roman);
str = num.roman;
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++;
}
// close the file
file.close();
// open the file to write
file.open("output.txt", ios::out);
// write data to the file
list<Number>::iterator itr = lst.begin();
while (1)
{
file << left << setw(15) << (*itr).roman << " " << setw(4) << (*itr).arabic;
itr++;
if (itr != lst.end())
file << endl;
else
break;
}
// close the file
file.close();
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
Input File(input.txt):
XXVII
123
MCLXV
1975
Output write into output.txt and it contents like:
XXVII 27
CXXIII 123
MCLXV 1165
MCMLXXV 1975
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.