473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

stdin to stdout

I'm a complete novice at c programming. I was wondering what would be the best way to read in roman numerals and convert them into decimal values. I tried the switch statement but it told me that the case label does not reduce to an integer constant when the case was more than two characters.
Sep 12 '06 #1
3 2918
D_C
293 100+
Do them one character at a time.

Expand|Select|Wrap|Line Numbers
  1. // Suppose the string that holds MC...VI is named RN.
  2. while RN is not empty
  3. {
  4.   switch the first character of the string RN
  5.   {
  6.     convert it to it's integer form (case 'M': value = 500; break; // etc.)
  7.   }
  8.   if the previous character is less than current character
  9.   {
  10.     subtract previous character instead of add (total -= (2*prevVal);)
  11.   }
  12.   add the value to the total (total += value;)
  13.   assign value to prevVal;
  14.   remove the first character from string RN
  15. }
Sep 13 '06 #2
thankyou, it was fantastic. Would it be the same in the reverse order (ie decimal to roman numeral) or would the code be different (ie. finding the place value of the digits)
Sep 13 '06 #3
D_C
293 100+
After reviewing the rules, yeah, take as big of a chunk as you can. The rules aren't exactly clear, but there should be some scheme for uniqueness.

Rules I would use:
- Use as few numerals as possible
- Only subtract (put a smaller number before a larger one) if it's less than some percentage of the bigger number's value. Some say 10% is the cutoff. That is exactly two roman numerals before the current one. That would mean 40 = XXXX instead of XL, and 400 = CCCC instead of CD.
- For numbers considerably larger than M (1000), notations can be used to multiply each number by 1000, or even 1,000,000. That exists for all numbers except I because I * 1000 = M.

For example, 947= (1000 - 100) + (50 - 5) + (2) = CMVLII
Sep 13 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
0
by: lickspittle | last post by:
Hi, I have Python embedded with my other code, and when my other code opens a console and redirects stdout, stdin and stderr to it, then calls PyRun_InteractiveLoop, it immediately returns with...
3
by: Harayasu | last post by:
Hi, Using fgets() I can read from stdin and with fputs() I can write to stdout. Now I have two programs, one writing to stdin and the other one reading from stdin. And I would like the second...
23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
6
by: ccdrbrg | last post by:
What is the best way to protect stdin within a library? I am writing a terminal based program that provides plugin capability using the dlopen() API. Sequencing program commands (typed) and...
2
by: velle | last post by:
My headache is growing while playing arround with unicode in Python, please help this novice. I have chosen to divide my problem into a few questions. Python 2.3.4 (#1, Feb 2 2005, 12:11:53) ...
1
by: asdsd sir | last post by:
Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat "file.txt"|python somefile.py #somefile.py import sys
1
by: BenjaMinster | last post by:
I want to read and write unicode on stdin and stdout. I can't seem to find any way to force sys.stdin.encoding and sys.stdout.encoding to be utf-8, so I've got the following workaround: import...
2
by: kimonp | last post by:
I am running on windows XP with a fresh install of wamp5 (1.7.2) and mediawiki. I am trying to call a perl function from within php using proc_open. The perl script is being executed and...
0
by: Gabriel Genellina | last post by:
En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <almar.klein@gmail.com> escribió: Use subprocess.PIPE Usually the tricky part is to figure out exactly whether there is more input or not. With...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.