阿拉伯数字转化为罗马数字

来源:互联网 发布:百度贴吧营销软件 编辑:程序博客网 时间:2024/04/28 16:02
<span style="font-size:18px;">#include<stdio.h>#include<stdlib.h>#include<string.h>#define ROWS 4#define COLS 4int nums[ROWS][COLS]={{1000,1000,1000,1000},{900,500,400,100},{90,50,40,10},{9,5,4,1}};char *roms[ROWS][COLS]={{"m","m","m","m"},{"cm","d","cd","c"},{"xc","l","xl","x"},{"ix","v","iv","i"}};/*二维的数组指针*/void judge(int num)//判断输入的数字是否在制定范围内{if(num<1||num>9999){printf("please input the right number!\n");exit(0);}}void trans(int num,char roman[])//将阿拉伯数字转化为罗马数字{int i,j;roman[0]='\0';/*这一步是非常必要的,因为strcat函数将会找第一个字符串的'\0',找到之后将其去掉然后复制字符串,如果不初始化,第二个字符串的内容没法复制在第一个最后*/for(i=0;i<ROWS;i++){for(j=0;j<COLS;j++){while(num>=nums[i][j])/*设置为循环*/{printf("%s\n",roman);strcat(roman,roms[i][j]);num-=nums[i][j];}}}}void main(int argc,char *argv[]){char roman[25];int low,high;if(argc<2){printf("please input number!\n");exit(0);}low=high=atoi(argv[1]);//atoi函数就是将字符串转化为整数judge(low);if(argc>2){high=atoi(argv[2]);//本程序最多只能成功接收三个字符串judge(high);if(low>high){low=high;high=atoi(argv[1]);}}else low=1;for(;low<=high;low++){trans(low,roman);printf("%4d=%s\n",low,roman);}}</span>
本程序在dos命令状态中输入文件路径和两个字符串(将会转化成整型),将会输出两个整数之间的所有阿拉伯数的罗马数字。
1 0