الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

اله حاسبة

بدأه رااحيل في 9 أبريل 2010 · 10 رد · 2,119 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

ألسلام عليكم :::

الكود::

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Color;

public class Calculater extends JFrame implements ActionListener{
    String num1,num2,oper;
    JTextField text;

    public Calculater (){
        //create the first panel
             JPanel p1 = new JPanel();
         p1.setLayout(new GridLayout (4,3,2,2));



          //add the numbes buttons in the fist panel

            JButton jbt1=new JButton("1");
            p1.add(jbt1);
            jbt1.addActionListener(this);
            jbt1.setBackground(new Color (0,75,151));
            jbt1.setForeground(new Color (255,255,255));

            JButton jbt2=new JButton("2");
            p1.add(jbt2);
            jbt2.addActionListener(this);
            jbt2.setBackground(new Color (0,75,151));
            jbt2.setForeground(new Color (255,255,255));


            JButton jbt3=new JButton("3");
            p1.add(jbt3);
            jbt3.addActionListener(this);
            jbt3.setBackground(new Color (0,75,151));
            jbt3.setForeground(new Color (255,255,255));

            JButton jbt4=new JButton("4");
            p1.add(jbt4);
            jbt4.addActionListener(this);
            jbt4.setBackground(new Color (0,88,176));
            jbt4.setForeground(new Color (255,255,255));


            JButton jbt5=new JButton("5");
            p1.add(jbt5);
            jbt5.addActionListener(this);
            jbt5.setBackground(new Color (0,88,176));
            jbt5.setForeground(new Color (255,255,255));

            JButton jbt6=new JButton("6");
            p1.add(jbt6);
            jbt6.addActionListener(this);
            jbt6.setBackground(new Color (0,88,176));
            jbt6.setForeground(new Color (255,255,255));

            JButton jbt7=new JButton("7");
            p1.add(jbt7);
            jbt7.addActionListener(this);
            jbt7.setBackground(new Color (0,88,176));
            jbt7.setForeground(new Color (255,255,255));

            JButton jbt8=new JButton("8");
            p1.add(jbt8);
            jbt8.addActionListener(this);
            jbt8.setBackground(new Color (0,102,204));
            jbt8.setForeground(new Color (255,255,255));

            JButton jbt9=new JButton("9");
            p1.add(jbt9);
            jbt9.addActionListener(this);
            jbt9.setBackground(new Color (0,102,204));
            jbt9.setForeground(new Color (255,255,255));

            JButton jbt0=new JButton("0");
            p1.add(jbt0);
            jbt0.addActionListener(this);
            jbt0.setBackground(new Color (0,88,176));
            jbt0.setForeground(new Color (255,255,255));


            JButton jbtm=new JButton("+/-");
            p1.add(jbtm);
            jbtm.addActionListener(this);
            jbtm.setBackground(new Color (0,102,204));
            jbtm.setForeground(new Color (255,255,255));

            JButton jbtn=new JButton(".");
            p1.add(jbtn);
            jbtn.addActionListener(this);
            jbtn.setBackground(new Color (0,102,204));
            jbtn.setForeground(new Color (255,255,255));



        //create the second panel
        JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout (4,2,2,2));

        JButton jbtd=new JButton("/");
        p2.add(jbtd);
        jbtd.addActionListener(this);
        jbtd.setBackground(new Color (0,75,151));


        JButton jbtsq=new JButton(""+"sqrt");
        p2.add(jbtsq);
        jbtsq.addActionListener(this);
        jbtsq.setBackground(new Color (0,75,151));


        JButton jbts=new JButton("*");
        p2.add(jbts);
        jbts.addActionListener(this);
        jbts.setBackground(new Color (0,88,176));


        JButton jbtr=new JButton("%");
        p2.add(jbtr);
        jbtr.addActionListener(this);
        jbtr.setBackground(new Color (0,88,176));


        JButton jbtM=new JButton("-");
        p2.add(jbtM);
        jbtM.addActionListener(this);
        jbtM.setBackground(new Color (0,102,204));



            JButton jbt1x=new JButton("+/-");
            p2.add(jbt1x);
            jbt1x.addActionListener(this);
            jbt1x.setBackground(new Color (0,102,204));



        JButton jbtp=new JButton("+");
        p2.add(jbtp);
        jbtp.addActionListener(this);
        jbtp.setBackground(new Color (0,118,236));

        JButton jbte=new JButton("=");
        p2.add(jbte);
        jbte.addActionListener(this);
        jbte.setBackground(new Color (0,118,236));


        //create the third panel
        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout (4,1,2,2));


        JButton jbtmc=new JButton(""+"MC");
        p3.add(jbtmc);
        jbtmc.addActionListener(this);
        jbtmc.setBackground(new Color (0,75,151));


        JButton jbtmr=new JButton("MR");
           p3.add(jbtmr);
        jbtmr.addActionListener(this);
        jbtmr.setBackground(new Color (0,75,151));


        JButton jbtms=new JButton ("MS");
        p3.add(jbtms);
        jbtms.addActionListener(this);
        jbtms.setBackground(new Color (0,75,151));


        JButton jbtmm=new JButton("M+");
        p3.add(jbtmm);
        jbtmm.addActionListener(this);
        jbtmm.setBackground(new Color (0,75,151));




        //create the fourth panel
        JPanel p4 = new JPanel();
        p4.setLayout(new GridLayout (1,4,2,2));

        JButton jbtback=new JButton("BackSpace");
        p4.add(jbtback);
        jbtback.addActionListener(this);

        JButton jbtce=new JButton("CE");
        p4.add(jbtce);
        jbtce.addActionListener(this);

        JButton jbtC=new JButton("C");
        p4.add(jbtC);
        jbtC.addActionListener(this);

        //create the fifth panel
        JPanel p5 = new JPanel();
        p5.setLayout(new BorderLayout());
        p5.add(p1,BorderLayout.CENTER);
        p5.add(p2,BorderLayout.EAST);
        p5.add(p3,BorderLayout.WEST);
        p5.add(p4,BorderLayout.NORTH);

        //create the sixth panel
        JPanel p6 = new JPanel();
        p6.setLayout(new BorderLayout());
        text=new JTextField(30);

        p6.add(text,BorderLayout.NORTH);
        p6.add(p5,BorderLayout.CENTER);
        setLayout(new FlowLayout());
        add(p6);

    }
    public static void main(String[] args){
    Calculater frame = new Calculater();

    frame.setSize(345,200);
    frame.setTitle("Calculater");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);


}
        public void actionPerformed(ActionEvent e){
        String str = e.getActionCommand();

        if (str=="1"){
            String temp=text.getText();
            temp+="1";
            text.setText(temp);
        }


        else if(str=="2"){
            String temp=text.getText();
            temp+="2";
            text.setText(temp);
        }


        else if(str=="3"){
            String temp=text.getText();
            temp+="3";
            text.setText(temp);
        }


        else if(str=="4"){
            String temp=text.getText();
            temp+="4";
            text.setText(temp);
        }            



        else if(str=="5"){
            String temp=text.getText();
            temp+="5";
            text.setText(temp);
        }


        else if(str=="6"){
            String temp=text.getText();
            temp+="6";
            text.setText(temp);
        }


        else if(str=="7"){
            String temp=text.getText();
            temp+="7";
            text.setText(temp);
        }


        else if(str=="8"){
            String temp=text.getText();
            temp+="8";
            text.setText(temp);
        }


        else if(str=="9"){
            String temp=text.getText();
            temp+="9";
            text.setText(temp);
        }


        else if(str=="0"){
            String temp=text.getText();
            temp+="0";
            text.setText(temp);
        }

        else if(str=="."){
            String string=text.getText();
            int ret=string.indexOf(".");
            if (ret<0){
                text.setText(string+".");
            }
            else if(ret>=0){
                text.setText(string);

            }
        }



        else if(str=="+"){
            num1=text.getText();
            oper="+";
            text.setText("");
        }


        else if(str=="-"){
            num1=text.getText();
            oper="-";
            text.setText("");
        }


        else if(str=="*"){
            num1=text.getText();
            oper="*";
            text.setText("");
        }


        else if(str=="/"){
            num1=text.getText();
            oper="/";
            text.setText("");
        }

        else if(str=="%"){
            num1=text.getText();
            oper="%";
            text.setText("");
        }


        else if(str=="="){
        double n1=Double.parseDouble(num1);

        num2=text.getText();
        double n2=Double.parseDouble(num2);

        if(oper=="+"){
            double sum=n1+n2;
            String s=""+sum;
            text.setText(s);
        }

        else if(oper=="-"){
            double sum=n1-n2;
            String s=""+sum;
            text.setText(s);
        }

            else if(oper=="*"){
            double sum=n1*n2;
            String s=""+sum;
            text.setText(s);
        }

            else if(oper=="/"){
            double sum=n1/n2;
            String s=""+sum;
            text.setText(s);
        }

            else if(oper=="%"){
            int nn1=Integer.parseInt(num1);
            int nn2=Integer.parseInt(num2);
            int sum=nn1%nn2;
            String s=""+sum;
            text.setText(s);
        }

        }
        else if (str=="BackSpace"){
            String b1=text.getText();
            b1= b1.substring(0,b1.length()-1);
            text.setText(b1);
        }
        else if (str=="CE"||str=="C"){
            text.setText("");
        }
        else if (str=="+/-"){
            String plusOrMin=text.getText();
            double dPlusOrMin=Double.parseDouble(plusOrMin);
            double plOrMin= dPlusOrMin*-1;
            String strpOrM=""+plOrMin;
            text.setText(strpOrM);

              }
         else if (str=="sqrt"){

             String strSq=text.getText();
             double doubleSq=Double.parseDouble(strSq);
             double res =Math.sqrt(doubleSq);
             String sqrt=""+res;
             text.setText(sqrt);


         }     

       }
    }
}
1 −1
00020309t.gif

1958_1963.gif
#2

شكرا لكي اختي

تم تعديل هذه المشاركة بواسطة moonmoon في 9 أبريل 2010 في 17:54

سبحان الله وبحمده --- سبحان الله العظيم

#3

أختي اسراء بعد قراءة الاكواد جيدا الحمد لله فهمتها لكن اتوقع ان الكود يوجد به خطأ اذا جربت ان تقومي بهذه العمليه الحسابيه

5+5-2

مباشره ثم الضغط على زر = اتوقع ان الناتج سيظهر خطأ لان قمية المتغير oper = - مش عارف كلامي صح ولا غلط لكن اتوقع ذلك

تم تعديل هذه المشاركة بواسطة moonmoon في 9 أبريل 2010 في 17:55

سبحان الله وبحمده --- سبحان الله العظيم

#4

اممممم

كلام مبنى على التوقعات.....حاول ان تجرب؟؟

اطلعنى على النتيجة

00020309t.gif

1958_1963.gif
#5

فعلا رائعة ,, لكن رأيي أن تعليم المبتدئين للجافا يجب أن يكون مقرونا ببرمجة الكائنات لأنها مصدر قوة الجافا ولا يمكن الاستغناء عنها حتى في أبسط البرامج.

#6

very awesome work

#7

اعذريني ... لكن الكود سيء جداً !

ليست المشكلة بغرضية التوجه فقط بل المشكلة بالتكرار وسوء التنظيم.

هذا اختصار سريع لكودك ليس كوداً جيداً ... لكنه أقل سوأً

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculater extends JFrame implements ActionListener {

    String num1, num2, oper, mem;
    JTextField text = new JTextField(30);

    public Calculater() {
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(4, 6, 2, 2));

        String s[] = {"MC", "1", "2", "3", "+", "%",
            "MR", "4", "5", "6", "-", "sqrt",
            "MS", "7", "8", "9", "x", "+/-",
            "M+" + "", ".", "0", "+/-", "/", "="};

        for (int i = 0; i < s.length; i++) {
            JButton jb = new JButton(s);
            p.add(jb);
            jb.addActionListener(this);
            jb.setBackground(new Color(0, 75 + i * 2, 120 + i * 5));
            jb.setForeground(Color.white);
        }
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1, 3, 2, 2));

        JButton jbtback = new JButton("BackSpace");
        p1.add(jbtback);
        jbtback.addActionListener(this);

        JButton jbtce = new JButton("CE");
        p1.add(jbtce);
        jbtce.addActionListener(this);

        JButton jbtC = new JButton("C");
        p1.add(jbtC);
        jbtC.addActionListener(this);

        JPanel p2 = new JPanel();
        p2.setLayout(new BorderLayout());
        p2.add(p, BorderLayout.CENTER);
        p2.add(p1, BorderLayout.NORTH);

        JPanel p3 = new JPanel();
        p3.setLayout(new BorderLayout());

        p3.add(text, BorderLayout.NORTH);
        p3.add(p2, BorderLayout.CENTER);
        add(p3);

        setSize(400, 200);
        setTitle("Calculater");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();
        if ("0123456789".contains(str)) {
            text.setText(text.getText() + str);
        } else if (str == ".") {
            String string = text.getText();
            if (string.indexOf(".") < 0) {
                string += ".";
            }
            text.setText(string);
        } else if ("+-x/%".contains(str)) {
            num1 = text.getText();
            oper = str;
            text.setText("");
        } else if (str == "=") {
            double n1 = Double.parseDouble(num1);

            num2 = text.getText();
            double n2 = Double.parseDouble(num2);

            if (oper == "+") {
                text.setText(String.valueOf(n1 + n2));
            } else if (oper == "-") {
                text.setText(String.valueOf(n1 - n2));
            } else if (oper == "x") {
                text.setText(String.valueOf(n1 * n2));
            } else if (oper == "/") {
                text.setText(String.valueOf(n1 / n2));
            } else if (oper == "%") {
                int nn1 = Integer.parseInt(num1);
                int nn2 = Integer.parseInt(num2);
                int sum = nn1 % nn2;
                text.setText(String.valueOf(sum));
            }
        } else if (str == "MC") {
            mem = "0";
            text.setText("");
        } else if (str == "MR") {
            text.setText(mem);
        } else if (str == "MS") {
            mem = text.getText();
        } else if (str == "M+") {
            mem = String.valueOf(Double.valueOf(mem) + Double.valueOf(text.getText()));

        } else if (str == "CE" || str == "C") {
            text.setText("");
        } else if (str == "+/-") {
            String plusOrMin = text.getText();
            if (plusOrMin.startsWith("-")) {
                text.setText(plusOrMin.substring(1));
            } else {
                text.setText("-" + plusOrMin);
            }
        } else if (str == "sqrt") {
            double doubleSq = Double.parseDouble(text.getText());
            text.setText(String.valueOf(Math.sqrt(doubleSq)));
        }
    }

    public static void main(String[] args) {
        new Calculater();
    }
}
#8

اعترف بان الكود سىء جداا جداا جداا

انا عملت الكود دة بعد 10 ايام من تعلمى الجافا فقط

00020309t.gif

1958_1963.gif
#9

اعتقد ان الكود واضح من عنوانه "اله حاسبة للمبتدئين جدا-بعيد كل البعد عن البرمجة الشيئية-." اي كود لا يتعلق سوى بالنظرة العامة للبرمجة

يخلو من التعقيدات او الاختصرات و هو موجه للمبتدئين

و بذلك يتوجب علينا شكر الاخت اسراء لا القاء التعديلات و الذم

شكرا لك اخت اسراء

وارجو ان يستفيدو منه المبتدئين

#10

كلام سليم اخي Mohammed Mshari Ersan :)

سبحان الله وبحمده --- سبحان الله العظيم

#11

اعتقد ان الاخ Mohammed Mshari Ersan فهمنى نوعا

انا لما عملت الكزد دة فى الاول كأنى عملت اختراع لانى كنت فعلا لسه مبتدئة جدااااا

و انا متفهمة نقد الاخSpeed_Of_Light لانى منزلة الكود للافادة و مش مستنية كلمة شكرا و لا اى حاجة

00020309t.gif

1958_1963.gif

مواضيع مشابهة