Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions Roman-calculator.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
62 changes: 57 additions & 5 deletions src/nyc/c4q/ac21/romancalc/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
package nyc.c4q.ac21.romancalc;

import com.sun.org.apache.xpath.internal.operations.Bool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Calculator {

/*public static boolean validate(String num){

for(int index=0;index < num.length(); index=index+1){

String currentChar=String.valueOf(num.charAt(index));

System.out.println(currentChar);

if ( currentChar.equals("M")
|| currentChar.equals("D")
|| currentChar.equals("C")
|| currentChar.equals("L")
|| currentChar.equals("X")
|| currentChar.equals("V")
|| currentChar.equals("I") ){
} else {
return false;
}
}

return true;
}

/**
* Performs calculations on Roman numerals and prints the result.
*
Expand All @@ -16,6 +42,7 @@ public class Calculator {
* less than 1 or larger than 3999, prints a message indicating this
* instead.
*
*
* @param leftNumber
* The left operand, in Roman numerals.
* @param operation
Expand All @@ -31,8 +58,32 @@ public class Calculator {
*/
public static void calculate(String leftNumber, String operation, String rightNumber) {
// TODO: Group 3: Write this function!

int leftInt = RomanNumerals.parse(leftNumber);
int rightInt = RomanNumerals.parse(rightNumber);
int result = 0;

if (operation.equals("+")){
result = leftInt + rightInt;
}
else if (operation.equals("-")){
result = leftInt - rightInt;
}
else if (operation.equals("*")){
result = leftInt * rightInt;
}
else if (operation.equals("/")){
result = leftInt / rightInt;
}
else if (operation.equals("#")) {
result = (leftInt + rightInt) / 2;
}

System.out.println(RomanNumerals.format(result));

}


/**
* Parses a decimal number.
* @param number
Expand All @@ -57,9 +108,11 @@ public static int parseDecimalNumber(String number) {
* You do not need to understand how this function works.
*/
public static void main(String[] args) throws IOException {


final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// Loop forever.
while (true) {
while (true) {
// Show the prompt.
System.out.print("> ");
// Read a line of input.
Expand All @@ -81,11 +134,10 @@ public static void main(String[] args) throws IOException {
System.out.println();
continue;
}

// Perform the calculation and show the result.
calculate(leftNumber, operation, rightNumber);

System.out.println();
}

}
}
}}

123 changes: 116 additions & 7 deletions src/nyc/c4q/ac21/romancalc/RomanNumerals.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package nyc.c4q.ac21.romancalc;

import java.util.Scanner;

/**
* Code to convert to and from Roman numerals.
*/

public class RomanNumerals {
/**
* Formats a number in Roman numerals.
Expand All @@ -11,9 +14,70 @@ public class RomanNumerals {
* @return
* The value in Roman numerals.
*/
static String format(int value) {
// TODO: Group 1: Write this function!
return "???";

public static String format(int value) {
if (value <= 0 || value > 3999)
return "Error: not a valid Roman Numeral";
String num$ = ""; //represents integer # inputted.

while (value >= 1000) { //Start with largest place. 1000's place.
num$ += "M"; //Print M for each multiple of 1000;
value = value - 1000; //Remove 1000 from number. ex: 1984 = 1000 + 900 + 80 + 4
} //MCMLXXIV
if (value >= 900) {
num$ += "CM"; // //100 less than a 1000. ex: 984 = 900 (CM) + 80 + 4
value = value - 900;
}
if (value >= 500) {
num$ += "D";
value = value - 500;
}
if (value >= 400) {
num$ += "CD";
value = value - 400;
}
while (value >= 100) {
num$ += "C";
value -= 100;
}
if (value >= 90) {
num$ += "XC";
value -= 90;
}
if (value >= 60) {
num$ += "LX";
value -= 60;
}
if (value >= 50) {
num$ += "L";
value -= 50;
}
if (value >= 40) {
num$ += "XL";
value -= 40;
}
while (value >= 10) {
num$ += "X";
value -= 10;
}
if (value == 9) {
num$ += "IX";
value -= 9;
}
if (value >= 5) {
num$ += "V";
value -= 5;
}
if (value == 4) {
num$ += "IV";
value -= 4;
}
while (value >= 1) {
num$ += "I";
value -= 1;
}
//System.out.println(num$);
return num$;
}

/**
Expand All @@ -23,18 +87,63 @@ static String format(int value) {
* @return
* The value, or -1 if the number isn't in Roman numerals.
*/
public static int parse(String number) {
public static int parse(String romanNum) {
// TODO: Group 2: Write this function!

// You will need:
// `number.length()` gives the length (number of characters) of the number
// `number.charAt(i)` gives the i'th character

return -1;

int total = 0;


//romanNum.trim();

for (int i = 0; i < romanNum.length(); i++) {
if (romanNum.charAt(i) == 'M' || romanNum.charAt(i) == 'm')
total = total + 1000;
else if (romanNum.charAt(i) == 'D' || romanNum.charAt(i) == 'd')
total = total + 500;
else if (romanNum.charAt(i) == 'C' || romanNum.charAt(i) == 'c')
total = total + 100;
else if (romanNum.charAt(i) == 'L' || romanNum.charAt(i) == 'l')
total = total + 50;
else if (romanNum.charAt(i) == 'X' || romanNum.charAt(i) == 'x')
total = total + 10;
else if (romanNum.charAt(i) == 'V' || romanNum.charAt(i) == 'v')
total = total + 5;
else if (romanNum.charAt(i) == 'I' || romanNum.charAt(i) == 'i')
total = total + 1;

}

if (romanNum.indexOf("CM") > -1 ||romanNum.indexOf("cm") > -1 )
total = total - 200;
if (romanNum.indexOf("CD") > -1 || romanNum.indexOf("cd") > -1)
total = total - 200;
if (romanNum.indexOf("XC") > -1 || romanNum.indexOf("xc") > -1)
total = total - 20;
if (romanNum.indexOf("XL") > -1 || romanNum.indexOf("xl") > -1)
total = total - 20;
if (romanNum.indexOf("IX") > -1 || romanNum.indexOf("ix") > -1)
total = total - 2;
if (romanNum.indexOf("IV") > -1 || romanNum.indexOf("iv") > -1)
total = total - 2;

//System.out.println(total);
return total;


}
//return -1;


public static void main(String[] argv) {
// TODO: Group 3: Write this function!
// It should test that format() and parse() work correctly.



}
}