Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > progrm for converting integers to roman numerals using files in clanguage

Reply
Thread Tools

progrm for converting integers to roman numerals using files in clanguage

 
 
Randy Howard
Guest
Posts: n/a
 
      01-25-2008
On Fri, 25 Jan 2008 11:02:19 -0600, Bart C wrote
(in article <vComj.83078$(E-Mail Removed)> ):

>
> "Eric Sosman" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed). ..
>> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>>> please help me out the program for converting the integers to roman
>>> numerals using files in the c language from 1-5000 range send the
>>> program to my
>>> mail id ::::::: (E-Mail Removed)

>>
>> Ask here, get answers here.
>>
>> #include<stdio.h>
>> #define LO 1
>> #define HI 5000
>> #define SPAN(min,max)(1-((max)-(min)))
>> static char*r[SPAN(HI,LO)]={"I","II","III",
>> /* ... fill in remaining values here ... */
>> "MMMMCMXCVIII","MMMMCMXCIX","?"};

>
> Above 1000 you use V, X, L etc with a 'bar' over to scale by 1000.
>
> So not so easy, unless you use a convention like /V.
>
> And the 4000 above looks better as MV-Bar I think. Might be wrong though.


How about:

_ _ _
V X L

etc.



--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      01-25-2008
Bart C wrote:

> Above 1000 you use V, X, L etc with a 'bar' over to scale by 1000.
>
> So not so easy, unless you use a convention like /V.
>
> And the 4000 above looks better as MV-Bar I think. Might be wrong though.
>

__
4000 is IV, though MMMM is also used (but becomes awkward for larger
numbers. _
Numbers from one million onwards are written as |X|, the "box" multiplying
by 10**5.

--
Army1987 (Replace "NOSPAM" with "email")
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      01-25-2008
Keith Thompson wrote:
> (E-Mail Removed) writes:
>
>> please help me out the program for converting the integers to
>> roman numerals using files in the c language from 1-5000 range
>> send the program to my mail id :: (E-Mail Removed)

>
> Give us your instructor's e-mail address, and we'll be glad to
> send the solution directly to him. For a nominal fee, we'll
> mention your name.


It's actually quite simple. Simply list the 5000 Roman values in
order, i.e.:

I
II
III
IV
...
IMMMMM
MMMMM

and put those strings into the array ROMANS. i.e.:

char *romans[] = ( /* here the above comma separated set */ );
/* Don't forget to quote each string. */

Now the conversion and printing is simply:

/* write i in roman numerals */
int iwrite(i) {
return puts(roman[i - 1]);
}

which returns EOF for any i/o error, else a positive int.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
John J. Smith
Guest
Posts: n/a
 
      01-27-2008
(E-Mail Removed) wrote:

> please help me out


Here you go...

> the program for converting the integers to roman
> numerals using files in the c language from 1-5000 range


Find below the portable C source code for a program that will print
all roman numerals from 0 to 5000. Changing the code so it starts
with 1 is left as an exercise.

[snips]

> thank you all


You're welcome.

Here comes the code...

/*
* runum.c
*
* Copyright (c) 2008, John J. Smith
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose except in homework assignments
* is hereby granted without fee, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
*
*/

/*
* Requirements:
*
* the program for converting the integers to roman
* numerals using file(s) in the c language from 1-5000 range
*
*/

#include <stdio.h>
#include <string.h>

/*
* the biggest foobarfoobar that foofoobarbar will accept
*/
#define MAX_FOOBARFOOBAR 5000

#define M 1000
#define D 500
#define C 100
#define L 50
#define X 10
#define V 5
#define I 1
#define N 0

/*
* struct: foofoofoofoo
*
* purpose: map foofoofoobar values to their corresponding
* foofoobarfoo values
*
*/
#define M1(a) { #a , (a) }
#define M2(a,b) { #a #b , (b)-(a) }

static struct {
char *foofoofoobar;
int foofoobarfoo;
} foofoofoofoo[] = {
M1(M),M2(C,M),M1(D),M2(C,D),M1(C),M2(X,C),M1(L),
M2(X,L),M1(X),M2(I,X),M1(V),M2(I,V),M1(I),M1(N),
};
#define N_FOOFOOFOOFOO \
((sizeof foofoofoofoo) / (sizeof foofoofoofoo[0]))

#undef M1
#undef M2

static int ConstraintFailed(void) { return -1; }

/*
* function: foofoobarbar
*
* purpose: convert a foobarfoobar value to foobarbarfoo
*
* parameters:
* foobarfoobar: value to be converted
* foobarbarfoo: buffer that receives the converted foobarfoobar
*
* notes:
* it is the callers responsiblility that foobarbarfoo is
* sufficiently large
*/
static int foofoobarbar(int foobarfoobar, char *foobarbarfoo)
{
int barfoobarfoo = 0;
size_t barbarfoofoo;
char *foobarbarbar;

if(foobarfoobar < 0 || foobarfoobar > MAX_FOOBARFOOBAR) {
barfoobarfoo = ConstraintFailed();
strcpy(foobarbarfoo, "ERROR!"); goto barfoobarbar;
}
else if(foobarfoobar == 0) {
strcpy(foobarbarfoo, "N"); goto barfoobarbar;
}

for(foobarbarbar = foobarbarfoo, *foobarbarbar = 0, barbarfoofoo = 0;
barbarfoofoo < N_FOOFOOFOOFOO-1; barbarfoofoo++) {
while(foobarfoobar >= foofoofoofoo[barbarfoofoo].foofoobarfoo) {
char *barfoofoofoo = foofoofoofoo[barbarfoofoo].foofoofoobar;
size_t barfoofoobar = strlen(barfoofoofoo);
memcpy(foobarbarbar, barfoofoofoo, barfoofoobar);
foobarbarbar += barfoofoobar;
foobarfoobar -= foofoofoofoo[barbarfoofoo].foofoobarfoo;
}
}
*foobarbarbar = '\0';

barfoobarbar:
return barfoobarfoo;
}

/*
* function: foobarfoofoo
*
* purpose: as per spec, use file foobarfoobar
*
* parameters: (none)
*/
static int foobarfoofoo(void)
{
FILE *foobarfoobar;
printf("Using file... ");
if((foobarfoobar = tmpfile()) != NULL) {
printf("success!!!\n");
fclose(foobarfoobar);
return 0;
} else {
printf("failed.\n");
return 1;
}
}


/*
* function: main
*
* purpose: program's entry point
* invokes foofoobarbar() MAX_FOOBARFOOBAR times
*/
int main(void)
{
int foobarfoobar;
char foobarbarfoo[64];

printf("John's Table of Roman Numerals\n"
"Copyright (c) MMVIII, John J. Smith\n\n");
foobarfoofoo();
printf("\n"
" arabic | roman\n"
"--------+----------------------\n");
for(foobarfoobar = 0;
foobarfoobar <= MAX_FOOBARFOOBAR;
foobarfoobar++) {
foofoobarbar(foobarfoobar, foobarbarfoo);
printf(" %5d | %s\n", foobarfoobar, foobarbarfoo);
}
return 0;
}

/* end ronum.c */

--
John J. Smith
Homework Expert

 
Reply With Quote
 
Hans Schneider
Guest
Posts: n/a
 
      01-27-2008
On Sun, 27 Jan 2008 03:10:28 +0100 (CET), John J. Smith
<(E-Mail Removed)> wrote:

>(E-Mail Removed) wrote:
>
>> please help me out

>
>Here you go...
>
>> the program for converting the integers to roman
>> numerals using files in the c language from 1-5000 range

>
>Find below the portable C source code for a program that will print
>all roman numerals from 0 to 5000. Changing the code so it starts
>with 1 is left as an exercise.
>
>[snips]
>
>> thank you all

>
>You're welcome.
>
>Here comes the code...


Does not compile with LCC-WIN32...

|
|Error ronum.c: 62 redefinition of 'ConstraintFailed'
|Error c:\lcc\include\safelib.h: 3 Previous definition of 'ConstraintFailed' here
|Warning ronum.c: 62 inconsistent linkage for 'ConstraintFailed' previously declared at c:\lcc\include\safelib.h 3
|2 errors, 1 warning
|1 error
|

(and I don't even know what "safelib.h" is. Program or compiler problem?)


>/*
> * runum.c
> *
> * Copyright (c) 2008, John J. Smith
> *
> * Permission to use, copy, modify, distribute, and sell this software
> * and its documentation for any purpose except in homework assignments
> * is hereby granted without fee, provided that the above copyright
> * notice appear in all copies and that both that copyright notice and
> * this permission notice appear in supporting documentation.
> *
> */
>
>/*
> * Requirements:
> *
> * the program for converting the integers to roman
> * numerals using file(s) in the c language from 1-5000 range
> *
> */
>
>#include <stdio.h>
>#include <string.h>
>
>/*
> * the biggest foobarfoobar that foofoobarbar will accept
> */
>#define MAX_FOOBARFOOBAR 5000
>
>#define M 1000
>#define D 500
>#define C 100
>#define L 50
>#define X 10
>#define V 5
>#define I 1
>#define N 0
>
>/*
> * struct: foofoofoofoo
> *
> * purpose: map foofoofoobar values to their corresponding
> * foofoobarfoo values
> *
> */
>#define M1(a) { #a , (a) }
>#define M2(a,b) { #a #b , (b)-(a) }
>
>static struct {
> char *foofoofoobar;
> int foofoobarfoo;
>} foofoofoofoo[] = {
> M1(M),M2(C,M),M1(D),M2(C,D),M1(C),M2(X,C),M1(L),
> M2(X,L),M1(X),M2(I,X),M1(V),M2(I,V),M1(I),M1(N),
>};
>#define N_FOOFOOFOOFOO \
> ((sizeof foofoofoofoo) / (sizeof foofoofoofoo[0]))
>
>#undef M1
>#undef M2
>
>static int ConstraintFailed(void) { return -1; }
>
>/*
> * function: foofoobarbar
> *
> * purpose: convert a foobarfoobar value to foobarbarfoo
> *
> * parameters:
> * foobarfoobar: value to be converted
> * foobarbarfoo: buffer that receives the converted foobarfoobar
> *
> * notes:
> * it is the callers responsiblility that foobarbarfoo is
> * sufficiently large
> */
>static int foofoobarbar(int foobarfoobar, char *foobarbarfoo)
>{
> int barfoobarfoo = 0;
> size_t barbarfoofoo;
> char *foobarbarbar;
>
> if(foobarfoobar < 0 || foobarfoobar > MAX_FOOBARFOOBAR) {
> barfoobarfoo = ConstraintFailed();
> strcpy(foobarbarfoo, "ERROR!"); goto barfoobarbar;
> }
> else if(foobarfoobar == 0) {
> strcpy(foobarbarfoo, "N"); goto barfoobarbar;
> }
>
> for(foobarbarbar = foobarbarfoo, *foobarbarbar = 0, barbarfoofoo = 0;
> barbarfoofoo < N_FOOFOOFOOFOO-1; barbarfoofoo++) {
> while(foobarfoobar >= foofoofoofoo[barbarfoofoo].foofoobarfoo) {
> char *barfoofoofoo = foofoofoofoo[barbarfoofoo].foofoofoobar;
> size_t barfoofoobar = strlen(barfoofoofoo);
> memcpy(foobarbarbar, barfoofoofoo, barfoofoobar);
> foobarbarbar += barfoofoobar;
> foobarfoobar -= foofoofoofoo[barbarfoofoo].foofoobarfoo;
> }
> }
> *foobarbarbar = '\0';
>
>barfoobarbar:
> return barfoobarfoo;
>}
>
>/*
> * function: foobarfoofoo
> *
> * purpose: as per spec, use file foobarfoobar
> *
> * parameters: (none)
> */
>static int foobarfoofoo(void)
>{
> FILE *foobarfoobar;
> printf("Using file... ");
> if((foobarfoobar = tmpfile()) != NULL) {
> printf("success!!!\n");
> fclose(foobarfoobar);
> return 0;
> } else {
> printf("failed.\n");
> return 1;
> }
>}
>
>
>/*
> * function: main
> *
> * purpose: program's entry point
> * invokes foofoobarbar() MAX_FOOBARFOOBAR times
> */
>int main(void)
>{
> int foobarfoobar;
> char foobarbarfoo[64];
>
> printf("John's Table of Roman Numerals\n"
> "Copyright (c) MMVIII, John J. Smith\n\n");
> foobarfoofoo();
> printf("\n"
> " arabic | roman\n"
> "--------+----------------------\n");
> for(foobarfoobar = 0;
> foobarfoobar <= MAX_FOOBARFOOBAR;
> foobarfoobar++) {
> foofoobarbar(foobarfoobar, foobarbarfoo);
> printf(" %5d | %s\n", foobarfoobar, foobarbarfoo);
> }
> return 0;
>}
>
>/* end ronum.c */


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      01-27-2008
Hans Schneider wrote, On 27/01/08 03:15:
> On Sun, 27 Jan 2008 03:10:28 +0100 (CET), John J. Smith
> <(E-Mail Removed)> wrote:
>
>> (E-Mail Removed) wrote:
>>
>>> please help me out

>> Here you go...
>>
>>> the program for converting the integers to roman
>>> numerals using files in the c language from 1-5000 range

>> Find below the portable C source code for a program that will print
>> all roman numerals from 0 to 5000. Changing the code so it starts
>> with 1 is left as an exercise.
>>
>> [snips]
>>
>>> thank you all

>> You're welcome.
>>
>> Here comes the code...

>
> Does not compile with LCC-WIN32...
>
> |
> |Error ronum.c: 62 redefinition of 'ConstraintFailed'
> |Error c:\lcc\include\safelib.h: 3 Previous definition of 'ConstraintFailed' here
> |Warning ronum.c: 62 inconsistent linkage for 'ConstraintFailed' previously declared at c:\lcc\include\safelib.h 3
> |2 errors, 1 warning
> |1 error
> |
>
> (and I don't even know what "safelib.h" is. Program or compiler problem?)


<snip>

If you put the compiler in to conforming mode (-ansic is I think the
switch) then it is a compiler problem. If not then I would still say it
is a compiler problem, but it could be a deliberate feature.

C code is allowed to use the identifier ConstraintFailed for anything it
wants.
--
Flash Gordon
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      01-27-2008
Hans Schneider wrote:
>
> Does not compile with LCC-WIN32...
>
> |
> |Error ronum.c: 62 redefinition of 'ConstraintFailed'
> |Error c:\lcc\include\safelib.h: 3 Previous definition of 'ConstraintFailed' here


Bug in lcc-win32, or you're not invoking it in conforming mode.

ConstraintFailed is a valid identifier which lcc-win32 happens to use
in some implementation-specific library. You need to disable compiler
extensions.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      01-27-2008
Hans Schneider wrote:

> On Sun, 27 Jan 2008 03:10:28 +0100 (CET), John J. Smith
> <(E-Mail Removed)> wrote:
>
>>(E-Mail Removed) wrote:
>>
>>> please help me out

>>
>>Here you go...
>>
>>> the program for converting the integers to roman
>>> numerals using files in the c language from 1-5000 range

>>
>>Find below the portable C source code for a program that will print
>>all roman numerals from 0 to 5000. Changing the code so it starts
>>with 1 is left as an exercise.
>>
>>[snips]
>>
>>> thank you all

>>
>>You're welcome.
>>
>>Here comes the code...

>
> Does not compile with LCC-WIN32...
>
> |
> |Error ronum.c: 62 redefinition of 'ConstraintFailed'
> |Error c:\lcc\include\safelib.h: 3 Previous definition of
> |'ConstraintFailed' here
> |Warning ronum.c: 62 inconsistent linkage for 'ConstraintFailed'
> |previously declared at c:\lcc\include\safelib.h 3 2 errors, 1 warning
> |1 error
> |
>
> (and I don't even know what "safelib.h" is. Program or compiler
> problem?)


Please post problems with lcc-win32 to <news:comp.compilers.lcc> where
it's author will be able to address them.

PS. I'm surprised that jacob is using an user-space identifier in his
implementation, even in non-conforming mode. Unfortunately I have not
got Windows, so I cannot check this myself at the moment.

<snip code>

 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      01-27-2008
Hans Schneider wrote:
> On Sun, 27 Jan 2008 03:10:28 +0100 (CET), John J. Smith
> <(E-Mail Removed)> wrote:
>
>> (E-Mail Removed) wrote:


>> Find below the portable C source code for a program that will print
>> all roman numerals from 0 to 5000. Changing the code so it starts
>> with 1 is left as an exercise.


> Does not compile with LCC-WIN32...


> (and I don't even know what "safelib.h" is. Program or compiler
> problem?)


This worked fine for me. Although had to rename the ConstraintFailed
function.

safelib.h seems to be used in the string.h header.

And the output seems fine apart from slight quibble of using MMMMM for 5000.

--
Bart


 
Reply With Quote
 
Coos Haak
Guest
Posts: n/a
 
      01-27-2008
Op Sun, 27 Jan 2008 19:14:38 GMT schreef Bartc:

> Hans Schneider wrote:
>> On Sun, 27 Jan 2008 03:10:28 +0100 (CET), John J. Smith
>> <(E-Mail Removed)> wrote:
>>
>>> (E-Mail Removed) wrote:

>
>>> Find below the portable C source code for a program that will print
>>> all roman numerals from 0 to 5000. Changing the code so it starts
>>> with 1 is left as an exercise.

>
>> Does not compile with LCC-WIN32...

>
>> (and I don't even know what "safelib.h" is. Program or compiler
>> problem?)

>
> This worked fine for me. Although had to rename the ConstraintFailed
> function.
>
> safelib.h seems to be used in the string.h header.
>
> And the output seems fine apart from slight quibble of using MMMMM for 5000.


Slight? Everything from 4000 onwards had at least four M's in a row
Perhaps the Romans used a wide (high) character set, the A in ASCII surely
wasn't known then.
--
Coos
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ And TDD/ C++ programming on Android/ String Calculator Kata inC++/ C++and OpenGL on Android with the NDK/c++ Convert Integers to Roman Numerals sterta bole C++ 0 08-27-2012 09:09 PM
Roman Numerals dragonsfall21@gmail.com Java 10 08-18-2007 10:12 PM
Decimal to Roman Numerals ARMAS C Programming 31 02-06-2007 09:54 AM
[QUIZ] Roman Numerals (#22) Ruby Quiz Ruby 25 03-09-2005 08:44 PM
Roman numerals to ints Christopher Benson-Manica C Programming 13 09-17-2003 03:46 PM



Advertisments