Il peut être amusant d'effectuer des calculs avec des chiffres romains, toutefois les calculs à la mains nous sembles fastidieux ou on a l'impression d'avoir oublié quelque chose! Pour résoudre le problème, créez-vous une calculatrice! Il suffit simplement d'effectuer une conversion des nombres romains en nombre arabes et vice versa et de l'intégrer à une calculatrice classique. A l'aide du code source Java suivant, vous trouverez la réponse que vous souhaitez :
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JButton;
- import javax.swing.JPanel;
-
- public class RomanCalculator extends JFrame implements ActionListener {
-
- boolean eqFlg = false;
- char lastBut = '+';
- char lastOp = '+';
- boolean newNum = true;
- int Total = 0;
-
- JLabel Resultat = new JLabel(".", JLabel.RIGHT);
- JButton EntNumM = new JButton("M");
- JButton EntNumD = new JButton("D");
- JButton EntNumC = new JButton("C");
- JButton EntNumPlus = new JButton("+");
- JButton EntNumL = new JButton("L");
- JButton EntNumX = new JButton("X");
- JButton EntNumV = new JButton("V");
- JButton EntNumMoins = new JButton("-");
- JButton EntNumI = new JButton("I");
- JButton EntNumDivision = new JButton("÷");
- JButton EntNumMult = new JButton("×");
- JButton EntNumEqual = new JButton("=");
-
- int RomainToNumber(String num) {
- int i = 0, n = 0;
- num = num.toUpperCase();
- // Calcul les milliers
- while((i<num.length())&&(num.charAt(i) == 'M')) {
- n+=1000;
- i++;
- }
- // Calcul les centaines
- if((num.length()>=i+2) && (num.substring(i,2) == "CM")) {
- n+=900;
- i+=2;
- } else if((num.length()>=i+2) && (num.substring(i,2) == "CD")) {
- n+=400;
- i+=2;
- } else {
- if((i<num.length())&&(num.charAt(i) == 'D')) {
- n+=500;
- i++;
- }
- if(i<num.length()) while(num.charAt(i) == 'C') {
- n+=100;
- i++;
- if(i>=num.length()) break;
- }
- }
-
- // Calcul les dizaines
- if((num.length()>=i+2) && (num.substring(i,2) == "XC")) {
- n+=90;
- i += 2;
- } else if((num.length()>=i+2) && (num.substring(i,2) == "XL")) {
- n+=40;
- i += 2;
- }
- else {
- if((i<num.length())&&(num.charAt(i) == 'L')) {
- n+=50;
- i++;
- }
- if(i<num.length()) while (num.charAt(i) == 'X') {
- n+=10;
- i++;
- if(i>=num.length()) break;
- }
- }
-
- // Calcul les unités
- if((num.length()>=i+2) && (num.substring(i,2) == "IX")) {
- n += 9;
- i+=2;
- } else if((num.length()>=i+2) && (num.substring(i,2) == "IV")) {
- n += 4;
- i += 2;
- } else {
- if((i<num.length())&&(num.charAt(i) == 'V')) {
- n += 5;
- i++;
- }
- if(i<num.length()) while(num.charAt(i) == 'I') {
- n++;
- i++;
- if(i>=num.length()) break;
- }
- }
- return(n);
- }
-
- static String NumberToRomain(int X) {
- String S;
- S = "";
- switch((X % 1000) / 100) {
- case 9: S+="CM";break;
- case 8: S+="DCCC";break;
- case 7: S+="DCC";break;
- case 6: S+="DC";break;
- case 5: S+="D";break;
- case 4: S+="CD";break;
- case 3: S+="CCC";break;
- case 2: S+="CC";break;
- case 1: S+="C";break;
- }
- switch((X % 100) / 10) {
- case 9: S+="XC";break;
- case 8: S+="LXXX";break;
- case 7: S+="LXX";break;
- case 6: S+="LX";break;
- case 5: S+="L";break;
- case 4: S+="XL";break;
- case 3: S+="XXX";break;
- case 2: S+="XX";break;
- case 1: S+="X";break;
- }
- switch(X % 10) {
- case 9: S+="IX";break;
- case 8: S+="VIII";break;
- case 7: S+="VII";break;
- case 6: S+="VI";break;
- case 5: S+="V";break;
- case 4: S+="IV";break;
- case 3: S+="III";break;
- case 2: S+="II";break;
- case 1: S+="I";break;
- }
- return S;
- }
-
- int eval(int Total,char lastOp,int n) {
- switch(lastOp) {
- case '+':return Total + n;
- case '-':return Total - n;
- case '*':return Total * n;
- case '/':return Total / n;
- }
- return 0;
- }
-
- public void EntOperator(char Operator) {
- if(lastBut != '+' && lastBut != '-' && lastBut != '*' && lastBut != '/') {
- boolean negFlg = false;
- int n;
- String Num = Resultat.getText().trim();
- if(Num.charAt(0) == '-') {
- negFlg = true;
- Num = Num.substring(1);
- }
- if(Num != ".") {
- n = RomainToNumber(Num);
- if(negFlg) n = -n;
- Total = Math.round(eval(Total,lastOp,n));
- if(Total < -4999 || Total > 4999) Num = "Erreur de débordement de valeur"; else
- if(Total == 0) Num = "."; else
- if(Total < 0) Num = "-" + NumberToRomain(-Total);
- else Num = NumberToRomain(Total);
- Resultat.setText(Num);
- newNum = true;
- eqFlg = false;
- }
- }
- lastBut = Operator;
- lastOp = Operator;
- }
-
- public void ComputeTotal() {
- boolean negFlg = false;
- int n;
- String Num;
- if(eqFlg) {
- Num = ".";
- eqFlg = false;
- } else {
- Num = Resultat.getText().trim();
- eqFlg = true;
- if(Num.charAt(0) == '-') {
- negFlg = true;
- Num = Num.substring(1);
- }
- if(Num != ".") {
- n = RomainToNumber(Num);
- if(negFlg) n = -n;
- Total = Math.round(eval(Total,lastOp,n));
- if(Total < -4999 || Total > 4999) Num = "Erreur de débordement de capacité"; else
- if(Total == 0) Num = "."; else
- if(Total < 0) Num = "-" + NumberToRomain(-Total);
- else Num = NumberToRomain(Total);
- }
- lastOp = '+';
- newNum = true;
- Total = 0;
- }
- Resultat.setText(Num);
- }
-
- public void EntNum(char RomanNumber) {
- eqFlg = false; lastBut = RomanNumber;
- if(newNum) {
- Resultat.setText("");
- newNum = false;
- }
- Resultat.setText(Resultat.getText()+RomanNumber);
- }
-
- public void actionPerformed(ActionEvent e){
- if(e.getSource() == EntNumM) EntNum('M');
- else if(e.getSource() == EntNumD) EntNum('D');
- else if(e.getSource() == EntNumC) EntNum('C');
- else if(e.getSource() == EntNumPlus) EntOperator('+');
- else if(e.getSource() == EntNumL) EntNum('L');
- else if(e.getSource() == EntNumX) EntNum('X');
- else if(e.getSource() == EntNumV) EntNum('V');
- else if(e.getSource() == EntNumMoins) EntOperator('-');
- else if(e.getSource() == EntNumI) EntNum('I');
- else if(e.getSource() == EntNumDivision) EntOperator('/');
- else if(e.getSource() == EntNumMult) EntOperator('*');
- else if(e.getSource() == EntNumEqual) ComputeTotal();
- }
-
- public RomanCalculator() {
- super("Calculatrice romaine");
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- setLayout(new BorderLayout());
-
- add("North",Resultat);
- JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(3,4,5,5));
-
- panel.add(EntNumM);
- EntNumM.addActionListener(this);
- panel.add(EntNumD);
- EntNumD.addActionListener(this);
- panel.add(EntNumC);
- EntNumC.addActionListener(this);
- panel.add(EntNumPlus);
- EntNumPlus.addActionListener(this);
- panel.add(EntNumL);
- EntNumL.addActionListener(this);
- panel.add(EntNumX);
- EntNumX.addActionListener(this);
- panel.add(EntNumV);
- EntNumV.addActionListener(this);
- panel.add(EntNumMoins);
- EntNumMoins.addActionListener(this);
- panel.add(EntNumI);
- EntNumI.addActionListener(this);
- panel.add(EntNumDivision);
- EntNumDivision.addActionListener(this);
- panel.add(EntNumMult);
- EntNumMult.addActionListener(this);
- panel.add(EntNumEqual);
- EntNumEqual.addActionListener(this);
- add("Center",panel);
- pack();
- setLocationRelativeTo(null);
- }
-
- public static void main(String[] args) {
- new RomanCalculator().setVisible(true);
- }
-
- }
Voici de quoi devrait avoir l'air la calculatrice :
Dernière mise à jour : Mercredi, le 3 juin 2015