Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RzymArab
- {
- private static String[] rzymskie = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
- private static int[] arabskie = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
- private static boolean isInt(String line)
- {
- boolean isInt=true;
- try
- {
- Integer.parseInt(line);
- }
- catch(NumberFormatException ex)
- {
- isInt=false;
- }
- return isInt;
- }
- static boolean allCharactersSame(String rzym)
- {
- int n = rzym.length();
- for (int i = 1; i < n; i++)
- if (rzym.charAt(i) != 'X' && rzym.charAt(i) != 'M' && rzym.charAt(i) != 'C' && rzym.charAt(i) != 'D' && rzym.charAt(i) != 'L' && rzym.charAt(i) != 'V' && rzym.charAt(i) != 'I')
- return false;
- return true;
- }
- static boolean aarabskie(String t)
- {
- int n = t.length();
- for (int i = 1; i < n; i++)
- if (t.charAt(i) != '0' && t.charAt(i) != '1' && t.charAt(i) != '2' && t.charAt(i) != '3' && t.charAt(i) != '4' && t.charAt(i) != '5' && t.charAt(i) != '6' && t.charAt(i) != '7' && t.charAt(i) != '8' && t.charAt(i) != '9')
- return false;
- return true;
- }
- public static int rzym2arab (String rzym) throws RzymArabException
- {
- int parametr=0;
- int parametr2=0;
- int tmp=0;
- int counter=0;
- int end=0;
- int kek=0;
- boolean lol=allCharactersSame(rzym);
- int j=0;
- int nameLength = rzym.length();
- int[] array=new int[nameLength];
- while(j<nameLength){
- j=j+1;
- //System.out.println(j+"j");
- }
- if(lol==false){
- kek=kek+1;
- }
- for(int i=0; i<rzymskie.length; i++)
- {
- while(rzym.startsWith(rzymskie[i], tmp))
- {
- parametr=parametr+arabskie[i];
- tmp=tmp+rzymskie[i].length();
- parametr2=j-1;
- }
- }
- if( parametr>3999 || kek>0 ){
- throw new RzymArabException("Nieprawidłowa dana"+nameLength);
- }
- if( parametr2>=parametr){
- throw new RzymArabException("Nieprawidłowa dana");
- }
- return parametr;
- }
- public static String arab2rzym (int arab,int sumuj) throws RzymArabException
- {
- if(arab<1 || arab>3999 || sumuj>0)
- throw new RzymArabException("Nieprawidłowa dana");
- StringBuilder parametr = new StringBuilder("");
- for(int i=0; i<arabskie.length; ++i)
- {
- while(arab>=arabskie[i])
- {
- parametr.append(rzymskie[i]);
- arab=arab-arabskie[i];
- }
- }
- return parametr.toString();
- }
- public static void main(String[] args)
- {
- for(String t: args)
- {
- try
- {
- if(isInt(t))
- {
- int n=Integer.parseInt(t);
- boolean element=aarabskie(t);
- int sumuj=0;
- if(element==false){
- sumuj=sumuj+1;
- }
- System.out.println(arab2rzym(n,sumuj));
- }
- else
- {
- System.out.println(rzym2arab(t));
- }
- }
- catch(RzymArabException e)
- {
- System.out.println(e.getMessage());
- continue;
- }
- }
- }
- }
- class RzymArabException extends Exception
- {
- public RzymArabException(String message)
- {
- super(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement