罗马数字转换阿拉伯数字

//罗马数字转换阿拉伯数字
#include <stdio.h>
#include <string.h>

int judge(char* roma);
int Match(char* temp);
int main(int argc, const char * argv[]) {
    char roma[50];
    scanf("%s",roma);
    printf("%d",judge(roma));
    return 0;
}

int judge(char* roma)
{
    int num=0,i,j,k=0,l=0;
    char temp[4]={0};
    int res[4]={0};
    for(i=0;i<strlen(roma);i=i+k)
    {
        //将字符串中的字符抠出,并进行判断,如果判断找不到,则缩小范围继续抠出,直至符合
        //使用两个for循环
        for(j=3;j>=0;j--)//最多抠出四位
        {
            
            if(roma[i+j])//判断最后一位存在
            {
                memset(temp, '\0', sizeof(temp));
                //将抠出的字符放入temp
                for(k=0;k<=j;k++)
                {
                    temp[k]=roma[i+k];
                }
                //判断temp
                //Match(temp);
                if(Match(temp)!=0)
                {
                    res[l++]=Match(temp);
                    break;
                }
            }
        }
    }
    for(l=0;l<4;l++)
    {
        num+=res[l];
    }
    return num;
}

int Match(char* temp)//如果匹配到,则返回相应数字;未匹配到,返回0
{
    char* r[50]={"M","MM","MMM",
        "C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
        "X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
        "I","II","III","IV","V","VI","VII","VIII","IX"};
    for(int i=0;i<30;i++)
    {
        //printf("%d",strlen(r[0]));
        //printf("%d",sizeof(r)/sizeof(r[0]));
        if(strcmp(r[i], temp)==0)
        {
            switch (i) {
                case 0:return 1000;
                case 1:return 2000;
                case 2:return 3000;
                case 3:return 100;
                case 4:return 200;
                case 5:return 300;
                case 6:return 400;
                case 7:return 500;
                case 8:return 600;
                case 9:return 700;
                case 10:return 800;
                case 11:return 900;
                case 12:return 10;
                case 13:return 20;
                case 14:return 30;
                case 15:return 40;
                case 16:return 50;
                case 17:return 60;
                case 18:return 70;
                case 19:return 80;
                case 20:return 90;
                case 21:return 1;
                case 22:return 2;
                case 23:return 3;
                case 24:return 4;
                case 25:return 5;
                case 26:return 6;
                case 27:return 7;
                case 28:return 8;
                case 29:return 9;
                    break;
                    
                default:
                    break;
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值