Solved

Another C string conversion question

Posted on 1997-10-22
10
262 Views
Last Modified: 2012-06-27
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
Comment
Question by:trying
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 3

Expert Comment

by:Jackie071497
ID: 1255924
It looks like your homework. eh?

0
 
LVL 1

Expert Comment

by:meessen
ID: 1255925
50 points ? You skinflint.
0
 

Author Comment

by:trying
ID: 1255926
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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
ID: 1255927
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
 
LVL 84

Expert Comment

by:ozo
ID: 1255928
MCMXCIX
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255929
Oops

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255930
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
 
LVL 84

Expert Comment

by:ozo
ID: 1255931
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255932
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
 
LVL 84

Expert Comment

by:ozo
ID: 1255933
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

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question