Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Roman numbers (https://forums.graalonline.com/forums/showthread.php?t=79137)

Chompy 03-22-2008 11:49 PM

Roman numbers
 
Well, gives the ability to convert numbers into roman numbers and vice versa:

PHP Code:

public function romanToNumber(roman) {
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
  
temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.0;
  for(
temp.0roman.length(); ++) {
    if (
roman.substring(i2in table_l) {
      
+= table_n[table_l.index(roman.substring(i2))];
      
++;
    }else 
+= table_n[table_l.index(roman.charat(i))];
  }
  return 
n;
}
public function 
numberToRoman(number) {
  
temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
  
temp.out ""temp.number2 number;
  for(
temp.table_n) {
    while(
number2 >= n) {
      
number2 -= n;
      
out @= table_l[table_n.index(n)];
    }
  }
  return 
out;


(Credits to Ian :))

Example:

PHP Code:

function onCreated() {
  echo(
numberToRoman(947));
  echo(
romanToNumber("CMXLVII"));
  
/*
  CMXLVII
  947
  */
  
echo(numberToRoman(2008));
  echo(
romanToNumber("MMVIII"));
  
/* 
  MMVIII
  2008
  */
  
echo(numberToRoman(romanToNumber("MMVIII")));
  
// MMVIII


anyways, enjoy?

DustyPorViva 03-23-2008 12:03 AM

Ace, this will be pretty cool for implementing a sort of RP thing with numbers. I don't really see it making a big gameplay impact as not many know how to translate roman numerals, but I guess it's still cool, and new!

cbk1994 03-23-2008 01:00 AM

PHP Code:

  temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 

PLEASE use multi-dimensional arrays :cry:

Programmer 03-23-2008 01:24 AM

Quote:

Originally Posted by cbkbud (Post 1381657)
PHP Code:

  temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 

PLEASE use multi-dimensional arrays :cry:

Aw be quiet :/ ... its good enough for what it's doing.

Chompy 03-23-2008 01:34 AM

Quote:

Originally Posted by cbkbud (Post 1381657)
PHP Code:

  temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 

PLEASE use multi-dimensional arrays :cry:

naw, I don't see a reason for it..

cbk1994 03-23-2008 01:39 AM

Quote:

Originally Posted by Chompy (Post 1381661)
Naw. Using multi-dimensional arrays in this case makes it just alot more complicated since you have to divide the index by two for find the correct number, then plus 1 to find the letter( and by using multi dimensional arrays you can't use the
for(member : array) loop because of indexes being wrong, and you to use an extra variable to count the index), so... Nope. Using multi-dimensional arrays in this case just makes it more complicated then it needs too.

What?

PHP Code:

temp.table = { { 1000"M" }, { 900"CM" } };
for ( 
temp.temp.table )
{
  
temp.num temp.n[0];
  
temp.letter temp.n[1];


I think you misunderstand the concept of multi-dimensional arrays.

Tolnaftate2004 03-23-2008 01:42 AM

Quote:

Originally Posted by Chompy (Post 1381661)
Naw. Using multi-dimensional arrays in this case makes it just alot more complicated since you have to divide the index by two for find the correct number, then plus 1 to find the letter( and by using multi dimensional arrays you can't use the
for(member : array) loop because of indexes being wrong, and you to use an extra variable to count the index), so... Nope. Using multi-dimensional arrays in this case just makes it more complicated then it needs too.

He means {{int,int, ...},{str,str,...}}.
And you can certainly use for (needle: haystack) with a multidimensional array.

Chompy 03-23-2008 01:43 AM

Ninja edit btw (err, I started to think about something I did earlier, then I realised you meant {{foo}, {bar}} arrays (multi-dimensional arrays), realised that after like 10 secs after posting that post xD)

Anyways

PHP Code:

public function numberToRoman(number) {
  
temp.table = {
    {
1000,900,500,400,100,90,50,40,10,9,5,4,1},
    {
"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}
  };
  
temp.out ""temp.number2 number;
  for(
temp.table[0]) {
    while(
number2 >= n) {
      
number2 -= n;
      
out @= table[1][table[0].index(n)];
    }
  }
  return 
out;
}
public function 
romanToNumber(roman) {
  
temp.table = {
    {
1000,900,500,400,100,90,50,40,10,9,5,4,1},
    {
"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}
  };
  
temp.0;
  for(
temp.0roman.length(); ++) {
    if (
roman.substring(i2in table[1]) {
      
+= table[0][table[1].index(roman.substring(i2))];
      
++;
    }else 
+= table[0][table[1].index(roman.charat(i))];
  }
  return 
n;



And, the reason why I posted that I posted was because I thought I did something like that, but I did:

PHP Code:

temp.table = {
       
1"I",
       
5"V",
      
10"X",
      
50"L",
     
100"C",
     
500"D",
    
1000"M"
  
}; 

and that popped into my mind when I saw your post Chris, sorry for the misunderstanding xD

And, some other version of it (romanToNumber()) under:

PHP Code:

public function romanToNumber(number) {
  
temp.table = {
       
1"I",
       
4"IV",
       
5"V",
       
9"IX",
      
10"X",
      
40"XL",
      
50"L",
      
90"XC",
     
100"C",
     
400"CD",
     
500"D",
     
900"CM",
    
1000"M"
  
};
  
temp.number2 number;
  
temp.out "";
  for(
temp.table.size()-1> -1-= 2;) {
    
temp.number3 0;
    while(
number2 >= table[i-1]) {
      if (
number2 0) {
        
number2 -= table[i-1];
        
out @= table[i];
      }
    }
  }
return 
out;



Rapidwolve24 03-23-2008 04:10 AM

Nice idea, :).


All times are GMT +2. The time now is 03:01 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.