Use of Pointers
I have to write a program that converts Numbers to Roman Numerals. Now I have already done it but I want to know if there is an easier way to program it. I would like not to have to use pointers in my program could some please show me how this would be done. And yes this is for school
#include
#include
#include
void main () {
int decimal;
char romanDigits[8] = "mdclxvi";
char* currentDigit = romanDigits;
int romanValues[8] = {1000, 500, 100, 50, 10, 5, 1};
int* currentValue = romanValues;
/* longest Roman numeral < 5000 has 19 digits. */
char roman[20] = "-------------------";
/* hyphens are ddebugging */
char* currentRoman = roman;
printf ("Please type a positive integer less than 5000: ");
scanf ("%d", &decimal);
if (decimal <= 0) {
exit(1);
}
while (decimal > 0) {
if (decimal >= *currentValue) {
*(currentRoman++) = *currentDigit;
decimal = decimal - *currentValue;
} else {
currentDigit++;
currentValue++;
}
}
*currentRoman = 0;
printf("Roman numeral equivalent = %s\n", roman);
}
These notations are equivalent:
Instead of using pointer arithmetic (incrementing a pointer to go to the next character) you can use an index, like x in the above code, and increment that instead. You then access the string through pSomeString[x].
You still need char* pointers for the strings of course, but if you find code full of pointer arithmetic hard to read then the array subscript notation might be easier.
Harry.
|
Instead of using pointer arithmetic (incrementing a pointer to go to the next character) you can use an index, like x in the above code, and increment that instead. You then access the string through pSomeString[x].
You still need char* pointers for the strings of course, but if you find code full of pointer arithmetic hard to read then the array subscript notation might be easier.
Harry.
Harry.
Two things:
First, rather than having CurrentValue being a pointer, i would use it as an index.
ie:
int CurrentValue = 0
if(decimal>=romanValues[CurrentValue])
{
*(currentRoman++)=currentDigit[CurrentValue];
decimal = decimal - romanValues[CurrentValue];
} else { currentValue++;
}
notice that you can use this as in index into both the currentvalue and the current digit ?![](smile.gif)
The other thing is that your implementation is wrong:
it would come up with xxviiii for 19, when it should be xix.
and iiii for 4, when it should be iv.
ie, you can only have 3 of one character in a row (except for m)
so you could add a check to see if it''s more than 3 times the current Romanvalue
hope that helps
Wyzfen
First, rather than having CurrentValue being a pointer, i would use it as an index.
ie:
int CurrentValue = 0
if(decimal>=romanValues[CurrentValue])
{
*(currentRoman++)=currentDigit[CurrentValue];
decimal = decimal - romanValues[CurrentValue];
} else { currentValue++;
}
notice that you can use this as in index into both the currentvalue and the current digit ?
![](smile.gif)
The other thing is that your implementation is wrong:
it would come up with xxviiii for 19, when it should be xix.
and iiii for 4, when it should be iv.
ie, you can only have 3 of one character in a row (except for m)
so you could add a check to see if it''s more than 3 times the current Romanvalue
hope that helps
Wyzfen
Not really related to the subject, but just nit-picking that may come up useful to you as time goes on on this board, typing (for example)
#include <iostream.h> /* If you typed just as shown in the message textbox...*/
...the "<" and ">" wouldn't show up, because they're interpreted as HTML tags, so type < and > instead of < and > =o)
![](http://www.crosswinds.net/~druidgames/resist.jpg)
Edited by - cliffhawkens on March 15, 2001 8:31:01 PM
#include <iostream.h> /* If you typed just as shown in the message textbox...*/
...the "<" and ">" wouldn't show up, because they're interpreted as HTML tags, so type < and > instead of < and > =o)
![](http://www.crosswinds.net/~druidgames/resist.jpg)
Edited by - cliffhawkens on March 15, 2001 8:31:01 PM
[email=erydo@gdnmail.net" style="color: #ff0000; text-decoration:none; cursor:help;](o= erydo =o)[/email]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement