本文共 3291 字,大约阅读时间需要 10 分钟。
c++中将字符串转化为数字
Here you will get program to convert decimal number to roman numeral in C and C++.
在这里,您将获得将C和C ++中的十进制数字转换为罗马数字的程序。
How it Works?
这个怎么运作?
Example:
例:
Decimal Number: 250
小数:250
The final roman numeral is CCL.
最终的罗马数字是CCL 。
Below program implements above algorithm.
下面的程序实现上面的算法。
#includevoid decimal2roman(int num){ int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; //base values char *symbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; //roman symbols int i = 0; while(num){ //repeat process until num is not 0 while(num/decimal[i]){ //first base value that divides num is largest base value printf("%s",symbol[i]); //print roman symbol equivalent to largest base value num -= decimal[i]; //subtract largest base value from num } i++; //move to next base value to divide num }} int main(){ printf("250 -> "); decimal2roman(250); printf("\n1550 -> "); decimal2roman(1550); printf("\n670 -> "); decimal2roman(670); return 0;}
Output
输出量
250 -> CCL 1550 -> MDL 670 -> DCLXX
250-> CCL 1550-> MDL 670-> DCLXX
#includeusing namespace std; void decimal2roman(int num){ int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; //base values char *symbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; //roman symbols int i = 0; while(num){ //repeat process until num is not 0 while(num/decimal[i]){ //first base value that divides num is largest base value cout< "; decimal2roman(250); cout<<"\n1550 -> "; decimal2roman(1550); cout<<"\n670 -> "; decimal2roman(670); return 0;}
Comment below if you any queries related to above program.
如果您对以上程序有任何疑问,请在下面评论。
翻译自:
c++中将字符串转化为数字
转载地址:http://hmggb.baihongyu.com/