罗马数字到整数 [英] Roman numerals to ints

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

问题描述

受到clc ++上的一个帖子的启发,我决定尝试一下......


#include< stdio.h>

#include< ; stdlib.h>


int main(int argc,char * argv [])

{

int i;

int result = 0;

int this;

int last = 0;


if( argc!= 2){

printf(" No string specified\\\
");

return(EXIT_FAILURE);

}

for(i = 0; argv [1] [i]!=''\'''; i ++){

switch(argv [1] [i] ){

case''M'':

this = 1000;

break;

case'' D'':

这= 500;

休息;

案例''C'':

这个= 100;

休息;

案例''L'':

这= 50;

休息;

案例''X'':

这= 10;

休息;

案例''V' ':

这= 5;

休息;

案例''我':

th是= 1;

休息;

默认值:

printf(" Bad character%c\ n",argv [1] [ i]);

返回(EXIT_FAILURE);

}

结果+ =这个;

if(this> ;最后){

result- = 2 * last;

}

last = this;

}

printf(" Result is%d \ n",result);

return(EXIT_SUCCESS);

}


这似乎有效(这次我没有忘记我的头文件:P)。

问题:


A )它是100%合法的,ANSI C吗?

B)是否有更有效的方法来实现它?


-

Christopher Benson-Manica | Jumonji giri,荣誉。

ataru(at)cyberspace.org |

Inspired by a thread on clc++, I decided to try it out...

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[] )
{
int i;
int result=0;
int this;
int last=0;

if( argc != 2 ) {
printf( "No string specified\n" );
return( EXIT_FAILURE );
}
for( i=0 ; argv[1][i] != ''\0'' ; i++ ) {
switch( argv[1][i] ) {
case ''M'':
this=1000;
break;
case ''D'':
this=500;
break;
case ''C'':
this=100;
break;
case ''L'':
this=50;
break;
case ''X'':
this=10;
break;
case ''V'':
this=5;
break;
case ''I'':
this=1;
break;
default:
printf( "Bad character %c\n", argv[1][i] );
return( EXIT_FAILURE );
}
result+=this;
if( this > last ) {
result-=2*last;
}
last=this;
}
printf( "Result is %d\n", result );
return( EXIT_SUCCESS );
}

This seems to work (and this time I didn''t forget my header files :P).
Questions:

A) Is it 100% legal, ANSI C?
B) Is there a more efficient way to implement it?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |

推荐答案

Christopher Benson-Manica写道:


整体看起来不错。一些缩短的方法:
Christopher Benson-Manica wrote:

Looks pretty good overall. A few ways to make it shorter:
for(i = 0; argv [1] [i]!=''\'''; i ++){
开关(argv [1] [i]){


char * p;


for(p = argv [1]; * p ; p ++){

开关(* p){


指针是你的朋友,在C.

case''M '':
这= 1000;
休息;
案例''D'':
这= 500;
休息;
for( i=0 ; argv[1][i] != ''\0'' ; i++ ) {
switch( argv[1][i] ) {
char *p;

for (p = argv[1]; *p; p++) {
switch (*p) {

Pointers are your friend, in C.
case ''M'':
this=1000;
break;
case ''D'':
this=500;
break;




这种方式使用1/3的行并且更容易阅读:

case''M'':this = 1000;休息;

案例''D'':这= 500;打破;


您还可以使用表格进行转换,例如:


char letters [] =" MDCLXVI";

char values [] = {1000,500,100,50,10,5,1};


并使用strchr(),但你'' d必须减去指针并且稍微混乱

。我认为开关声明更清晰,真的。


现在(在这里变得愚蠢),如果你必须将* lot * of roman

数字转换成阿拉伯语*非常快*,你可以建立一个256

值的表,其中表[''M''] = 1000等,无效字符是

零。 .. :)


-

Tom Zych

这个电子邮件地址将在某个时候到期以阻止垃圾邮件发送者。

永久地址:echo''g******@cbobk.pbz''| rot13



This way uses 1/3 as many lines and is easier to read besides:
case ''M'': this = 1000; break;
case ''D'': this = 500; break;

You could also use a table for the conversion, something like:

char letters[] = "MDCLXVI";
char values[] = {1000, 500, 100, 50, 10, 5, 1};

And use strchr(), but you''d have to subtract pointers and mess
around a bit. I think the switch statement is cleaner, really.

Now (getting silly here), if you had to convert *lots* of roman
numerals to arabic *really fast*, you could build a table of 256
values, where table[''M''] = 1000, etc, and invalid characters were
zero... :)

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo ''g******@cbobk.pbz'' | rot13


在文章< bj ********** @ chessie.cirr.com>中,Christopher Benson-Manica写道:
In article <bj**********@chessie.cirr.com>, Christopher Benson-Manica wrote:
受到clc ++上的一个帖子的启发,我决定试一试......
#include< stdio.h>
#include< stdlib.h>

int main(int argc,char * argv [])
{
int i;
int result = 0;
int this;
int last = 0;

if(argc!= 2){
printf(" No string specified\\\
");


这里的使用说明很好,例如罗马:用法罗马

[罗马数字]。它会使错误信息更加有用。

返回(EXIT_FAILURE);
}
for(i = 0; argv [1] [i]!=''\'''; i ++){
开关(argv [1] [i]){
案例''M'':
这= 1000;
休息;
案例''D'':
这= 500;
休息;
案例''C'':
这= 100;
休息;
案例''L'':
这= 50;
休息;
案例''X'':
这= 10;
休息;
案例''V'':
这= 5;
休息;
案例''我':
这= 1;
break;
默认:
printf(Bad character%c\ n,argv [1] [i]);
return(EXIT_FAILURE);
}
结果+ =这个;
if(this> last){
result- = 2 * last;
}
last = thi s;
}
printf(" Result is%d \ n",result);
return(EXIT_SUCCESS);
}

这似乎工作(这次我没有忘记我的标题
文件:P)。问题:

A)100%合法,ANSI C?


如果你的意思是:它是100%按照语言中所描述的那样,每个人都在讨论C标准中描述的
吗?,然后

是的。


我不知道如果写上面这样的程序实际上是

合法。请咨询律师。 ;-)

B)是否有更有效的方法来实现它?
Inspired by a thread on clc++, I decided to try it out...

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[] )
{
int i;
int result=0;
int this;
int last=0;

if( argc != 2 ) {
printf( "No string specified\n" );
A usage note is good here, e.g. roman: usage roman
[roman numeral]. It makes the error message a little more
helpful.
return( EXIT_FAILURE );
}
for( i=0 ; argv[1][i] != ''\0'' ; i++ ) {
switch( argv[1][i] ) {
case ''M'':
this=1000;
break;
case ''D'':
this=500;
break;
case ''C'':
this=100;
break;
case ''L'':
this=50;
break;
case ''X'':
this=10;
break;
case ''V'':
this=5;
break;
case ''I'':
this=1;
break;
default:
printf( "Bad character %c\n", argv[1][i] );
return( EXIT_FAILURE );
}
result+=this;
if( this > last ) {
result-=2*last;
}
last=this;
}
printf( "Result is %d\n", result );
return( EXIT_SUCCESS );
}

This seems to work (and this time I didn''t forget my header
files :P). Questions:

A) Is it 100% legal, ANSI C?
If you mean: "Is it 100% in accordance with the langauge
described in the C standard everybody is talking about?", then
yes.

I have no idea if writing such a program as the above is actually
legal. Consult a lawyer for that. ;-)
B) Is there a more efficient way to implement it?




我认为它足够有效。


C)有效吗?


好​​像。测试此程序的最佳方法是使用另一个

程序将基数10转换为罗马数字,或者获得巨大的b
转换表。然后你可以写一个快速的脚本来

在那里转换几百万个数字然后再回来,这样就可以对你的程序更有信心了。

如果它也接受小写罗马数字会很好。


最后,它可能适用于有效的罗马数字,但它没有

为罗马

数字的无意义组合提供有用的诊断,例如VIM等。或者IXI。


-

Neil Cerutti



I think it''s efficient enough.

C) Does it work?

It seems to. The best way to test this program is with another
program that converts base 10 to roman numerals, or obtain a huge
table of conversions. You can then write a quick script to
convert a few million numbers there and back again, and thus have
more confidence in your program.

It would be nice if it also accepted lower case roman digits.

Finally, it may work for valid roman numerals, but it doesn''t
provide useful diagnostics for nonsense combinations of roman
digits like "VIM" or "IXI".

--
Neil Cerutti


9月12日星期五2003 19:17:23 UTC,Christopher Benson-Manica

< at *** @ nospam.cyberspace.org>写道:
On Fri, 12 Sep 2003 19:17:23 UTC, Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
灵感来自clc ++上的一个主题,我决定尝试一下......

#include< stdio.h>
#include< stdlib.h>

{
int i;
int result = 0;
int this;
int last = 0;

if(argc!= 2){
printf(" No string specified\\\
");
return(EXIT_FAILURE);
}
for(i = 0; argv [1] [i]!=''\'''; i ++){
switch(argv [ 1] [i]){
案例''M'':
这= 1000;
休息;
案例''D'':
这= 500 ;
休息;
案例''C'':
这= 100;
休息;
案例''L'':
这= 50 ;
休息;
案例''X'':
这= 10;
休息;
案例''V'':
这= 5 ;
休息;
案例''我':
这= 1;
休息;
默认:
printf(" Bad character%c\ n",argv [1] [i ]);
返回(EXIT_FAILURE);
}
结果+ = this;
if(this>最后){
result- = 2 * last;
}
last = this;
}
printf(" Result is%d \ n",result );
返回(EXIT_SUCCESS);
}
这似乎有效(这次我没有忘记我的头文件:P)。
问题:

A)100%合法,ANSI C?


是的。

B)是否有更有效的方式来实施它?
Inspired by a thread on clc++, I decided to try it out...

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[] )
{
int i;
int result=0;
int this;
int last=0;

if( argc != 2 ) {
printf( "No string specified\n" );
return( EXIT_FAILURE );
}
for( i=0 ; argv[1][i] != ''\0'' ; i++ ) {
switch( argv[1][i] ) {
case ''M'':
this=1000;
break;
case ''D'':
this=500;
break;
case ''C'':
this=100;
break;
case ''L'':
this=50;
break;
case ''X'':
this=10;
break;
case ''V'':
this=5;
break;
case ''I'':
this=1;
break;
default:
printf( "Bad character %c\n", argv[1][i] );
return( EXIT_FAILURE );
}
result+=this;
if( this > last ) {
result-=2*last;
}
last=this;
}
printf( "Result is %d\n", result );
return( EXIT_SUCCESS );
}

This seems to work (and this time I didn''t forget my header files :P).
Questions:

A) Is it 100% legal, ANSI C?
Yes.
B) Is there a more efficient way to implement it?




/ *您可以使用此表将dec转换为罗马* *


struct {

char rom;

long dec ;

} rom_dec [] = {

{''我',1},

{''V'',5; },

{''X'',10},

{''L'',50},

{''C '',100},

{''D'',500},

{''M'',1000},

{0,0} / *清单结尾,纾困,未知信* /

};


char * r = argv [1];

char * d = rom_dec;

long result = 0;

long last = 0;


代表(; * r,r ++){

代表(d = rom_dec; d-> rom&& * r!= d-> rom; d ++); / *为身体是

空! * /

if(!d-> rom){

printf(无法转换%c\ n,* r);

返回EXIT_FAILTURE;

}

结果+ = d-> dec;

if(d-> dec>最后一个)

结果 - = 2 *最后一个;

last = this;

}


尚未测试,但应该工作


-

Tschau / Bye

Herbert


eComStation 1.1 Deutsch Betaistverügbar



/* you may use this table to convert dec to roman too */

struct {
char rom;
long dec;
} rom_dec[] = {
{ ''I'', 1 },
{ ''V'', 5; },
{ ''X'', 10 },
{ ''L'', 50 },
{ ''C'', 100 },
{ ''D'', 500 },
{ ''M'', 1000},
{ 0, 0 } /* end of list, bail out, unknown letter */
};

char *r = argv[1];
char *d = rom_dec;
long result = 0;
long last = 0;

for (; *r, r++) {
for (d = rom_dec; d->rom && *r != d->rom; d++) ; /* for body is
empty! */
if (!d->rom) {
printf("unable to convert %c\n", *r);
return EXIT_FAILTURE;
}
result += d->dec;
if (d->dec > last)
result -= 2*last;
last = this;
}

Not tested yet, but should work

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch Beta ist verügbar


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

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