Thread: Need help
View Single Post
 
Reply
Posted 2003-11-24, 09:32 PM in reply to Hades-Knight's post "Need help"
Don't know what all you want done... How's this?


#include<stdio.h>
#include <iostream.h>
int main() //Main function
{
int i; //Define variables
int previous = 0;
int num;
int total = 0;
int flag = 0;
int key = 0;
int repeat = 1;
char roman[50]; //defines roman char for input
char roman2[50]; //defines roman char for output
char pchar = 'z';

cout << "Enter the Roman numeral(in caps!!): "; //prompt for user input
cin >>roman;

for (i = 0; i < 50; i++) //count what the input value range is
{

if (roman[i] == 'M') //assigns 1000 to input M
num = 1000;
else if (roman[i] == 'D') //assigns 500 to input D
num = 500;
else if (roman[i] == 'C') //assigns 100 to input C
num = 100;
else if (roman[i] == 'L') //assigns 50 to input L
num = 50;
else if (roman[i] == 'X') //assigns 10 to input X
num = 10;
else if (roman[i] == 'V') //assigns 5 to input V
num = 5;
else if (roman[i] == 'I') //assigns 1 to input I
num = 1;
else if (roman[i] == '\0') //this is the end of input

/****************************Start Calculation for order of input******************/
/* If input has a value next to it, it checks to see if its more or less than first */
/* If it has less value than the one to the right, substract, if it has higher value add */
if (num == previous)
repeat = repeat + 1;
else
repeat = 1;

if (repeat == 4 && num == 1)
{

flag = 1;
}
if (repeat == 2 && num == 5)
{

flag = 1;
}
if (repeat == 4 && num == 10)
{

flag = 1;
}
if (repeat == 2 && num == 50)
{

flag = 1;
}
if (repeat == 4 && num == 100)
{

flag = 1;
}
if (repeat == 2 && num == 500)
{

flag = 1;
}

if (flag == 0)
{


if (num > previous)
{
if (key == 1)
{

i = 50;
}
total = (total - (2 * previous)) + num;
key = 1;
}
else
total += num;

previous = num;
num = 0;
}
else
{

i = 50;
}

}


cout<<total << endl; //Output the calculated number given that roman characters are calculated right
/*******************************START DOUBLING****************************************/
total = total * 2; //doubles the number you got from convertion
repeat = 1;

for (i = 0; i < 50; i++)
{ //Given then total in int , assign character to each int value

if (total >= 1)
roman2[i] = 'I';
if (total >= 5)
roman2[i] = 'V';
if (total >= 10)
roman2[i] = 'X';
if (total >= 50)
roman2[i] = 'L';
if (total >= 100)
roman2[i] = 'C';
if (total >= 500)
roman2[i] = 'D';
if (total >= 1000)
roman2[i] = 'M';
if (total == 0)
roman2[i] = '\0';

if (roman2[i] == pchar)
repeat = repeat + 1;
else
repeat = 1;

if (repeat == 4 && roman2[i] == 'I') //I has been repeated too many times
{
roman2[i - 2] = 'V';
roman2[i - 1] = '\0';
}

if (roman2[i] == 'M') // After getting the range value calculate exact value by substracting the value character to the left
total -= 1000; //and assign character to integer value , convertion.
else if (roman2[i] == 'D')
total -= 500;
else if (roman2[i] == 'C')
total -= 100;
else if (roman2[i] == 'L')
total -= 50;
else if (roman2[i] == 'X')
total -= 10;
else if (roman2[i] == 'V')
total -= 5;
else if (roman2[i] == 'I')
total -= 1;
else if (roman2[i] == '\0')
i = 50;

pchar = roman2[i];
}

cout<<roman2; //outputs the double value in roman characters

return(0);
}
D3V said:
This message is hidden because D3V is on your ignore list.
What is it they say about silence being golden?
Old
Profile PM WWW Search
Medieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHzMedieval Bob enjoys the static noises of ten television sets simultaneously tuned to 412.84 MHz
 
 
Medieval Bob