Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Another C string conversion question

You have provided me with the answer to my last question->
How to convert integers to roman numerals (ie input: 25 output: 25 = XXV), but how do I the reverse? (ie input: XXV output: XXV = 25)

please help
0
trying
Asked:
trying
1 Solution
 
Jackie071497Commented:
It looks like your homework. eh?

0
 
meessenCommented:
50 points ? You skinflint.
0
 
tryingAuthor Commented:
If you can't answer the question, then just say so. But I will increase the number of points to 100, as an added incentive.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
RONSLOWCommented:
char ch;
int thousands = 0;
int fivehundreds = 0;
int hundreds = 0;
int fities = 0;
int tens = 0;
int fives = 0;
int ones = 0;
int total = 0;
while ((ch = getc(stdin)) != EOF) {
      ch = toupper(ch);
      switch (ch) {
      case 'M': thousands+=1000; break;
      case 'D': if (hundreds>0) hundreds = -hundreds; fivehundreds+=500; break;
      case 'C': hundreds+=100; break;
      case 'L': if (tens>0) tens = -tens; fifties+=50; break;
      case 'X': tens+=10; break;
      case 'V': if (ones>0) ones = -ones; fives+=5; break;
      case 'I': ones+=1; break;
      }
}
total = thousands+fivehundreds+hundreds+fifties+tens+fives+ones;

0
 
ozoCommented:
MCMXCIX
0
 
RONSLOWCommented:
Oops

0
 
RONSLOWCommented:
Try this then...

switch (ch) {
case 'M': if (hundreds>0) hundreds = -hundreds; thousands+=1000; break;
case 'D': if (hundreds>0) hundreds = -hundreds; fivehundreds+=500; break;
case 'C': if (tens>0) tens = -tens; hundreds+=100; break;
case 'L': if (tens>0) tens = -tens; fifties+=50; break;
case 'X': if (ones>0) ones = -ones; tens+=10; break;
case 'V': if (ones>0) ones = -ones; fives+=5; break;
case 'I': ones+=1; break;
}

0
 
ozoCommented:
I've been trying to find a definitive ruling on whether
IC or VL or CMCM are legal roman numerals,
but I have been unable to find anything either forbiding or permitting them.
Does anyone know for sure, or can anyone cite a reference?
Some sources, while not saying whether thise forms are legal,
do give explicit rules for evaluating roman numerals,
which if applied to those forms give different results from the above code.
0
 
RONSLOWCommented:
Try this then...

switch (ch) {
case 'M': if (fivehundreds>0) fivehundreds = -fivehundreds;
case 'D': if (hundreds>0) hundreds = -hundreds;
case 'C': if (fifties>0) fifties = -fifties;
case 'L': if (tens>0) tens = -tens;
case 'X': if (fives>0) fives = -fives;
case 'V': if (ones>0) ones = -ones;
}
switch (ch) {
case 'M': thousands+=1000; break;
case 'D': fivehundreds+=500; break;
case 'C': hundreds+=100; break;
case 'L': fifties+=50; break;
case 'X': tens+=10; break;
case 'V': fives+=5; break;
case 'I': ones+=1; break;
}

0
 
ozoCommented:
That last comment wan't meant to criticize the code,
(which should work to reverse anything a reasonable interger to roman conversion would produce)
so much as to ask about exactly what stings are valid as roman numerals.
Is IXC a roman numeral?
BTW, IVIX, (if it's a roman numeral) would be 13 by every source I can find.
But I can't find a source the tells me whether or not it is a roman numeral.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now