罗马数字的年份转换 [英] Conversion of Year in Roman Numerals

查看:642
本文介绍了罗马数字的年份转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个程序,将给定的年份(十进制形式)转换为罗马数字表示形式.这些是罗马数字:

I have to write a program to convert given year (in decimal form) into Roman numeral representation. These are the Roman numeral digits:

Decimal 1  5  10  50  100  500  1000
Roman   i  v  x   l   c    d    m



转换示例如下:



Some example conversions:

Roman equivalent of 1988 is mdccclxxxviii.
Roman equivalent of 1525 is mdxxv.



到目前为止,我的程序是:



My program so far:

#include<stdio.h>
#include<conio.h>


int main(){
    clrscr();
    int yr,i,j=0;
    char roman[20];

    printf("Enter Year : ");
    scanf("%d",&yr);

    if(yr/1000>=0)
    {
        for(i=0;i<(yr/1000);i++)
        {
            roman[j]='m';
            j++;
        }
    yr = yr - (i+1)*1000;
    }
    if(yr/500>=0)
    {
        for(i=0;i<(yr/500);i++)
        {
            roman[j]='d';
            j++;
        }
    yr = yr - (i+1)*500;
    }
    if(yr/100>=0)
    {
        for(i=0;i<(yr/100);i++)
        {
            roman[j]='c';
            j++;
        }
    yr = yr - (i+1)*100;
    }
    if(yr/50>=0)
    {
        for(i=0;i<(yr/50);i++)
        {
            roman[j]='l';
            j++;
        }
     yr = yr - (i+1)*50;
    }
    if(yr/10>=0)
    {
        for(i=0;i<(yr/10);i++)
        {
            roman[j]='x';
            j++;
        }
    yr = yr - (i+1)*10;
    }
    if(yr/5>=0)
    {
        for(i=0;i<(yr/5);i++)
        {
            roman[j]='v';
            j++;
        }
    yr = yr - (i+1)*5;
    }
    if(yr/1>=0)
    {
        for(i=0;i<(yr/1);i++)
        {
            roman[j]='i';
            j++;
        }
    }
    printf("year in roman : ");
    for(i=0;i<=j;i++)
        printf("%c",roman[i]);
getch();
  }



但是它仅显示"m".



But it only prints "m". Can anyone help me to sort out this problem?

推荐答案

Hmmm ..您知道如何使用调试器吗?

yr = yr-(i + 1)* 1000;

这是您的问题,因为您使用i + 1,yr变为负数,因此其余测试无效.您可以在IDE中设置断点并逐步执行代码来解决类似的问题,这就是我为您解决的问题(尽管我从一开始就怀疑这是错误)
Hmmm.. do you know how to use the debugger ?

yr = yr - (i+1)*1000;

This is your problem, because you use i+1, yr becomes negative and so the rest of the tests do not work. You can set breakpoints in the IDE and step through your code to work out problems like this, that''s how I solved it for you ( although I was suspicious from the start that this was the error )


由于这种情况经常发生,因此我已发布了完整的解决方案作为技巧/窍门 ^ ]
Since this comes up quite often, I have posted a complete solution as a Tip/Trick here[^]


糟糕!

整理演示文稿.


还...


这是一件很重要的事情...

你知道约会有多难吗?
Yuck!

Sort out the presentation.


Also...


And this is such an important thing...

Do you understand how hard dates are?


这篇关于罗马数字的年份转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆