Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:Athenacle/backup

  • Loading branch information...
commit ab7c422ff23248dca98add08147af21b381a02ee 2 parents 7caf373 + ac5050a
@Athenacle authored
View
129 C_files/bin_search_20111126.c
@@ -0,0 +1,129 @@
+/*
+ * search.c
+ *
+ * Created on: 2011-11-19
+ * Author: wangxiao
+ * email: zjjhwxc@gmail.com
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#define NUM 1000
+
+/* line_search and binary_search
+ *
+ * binary_search has two editions
+ * iteration and recursion
+ *
+ */
+
+int line_search(int *A, int numOfArray, int key)
+{
+
+ int i;
+ printf(" lin_se ");
+ for (i = 0; i < numOfArray; i++) {
+ if (*(A + i) == key)
+ return i;
+ }
+ return -1;
+}
+
+int bin_search(int *A, int i, int j, int key)
+{
+ int mid = ((i + j) / 2.0 + 0.5);
+ if (i + 1 >= j)
+ return -1;
+
+ if (A[mid] == key)
+ return mid;
+ if (A[mid] < key)
+ return bin_search(A, mid, j, key);
+ if (A[mid] > key)
+ return bin_search(A, i, mid, key);
+ return -1;
+
+}
+
+int Re_Binary_search(int *A, int numOfArray, int key)
+{
+ printf(" Re_Bin ");
+ return bin_search(A, 0, numOfArray - 1, key);
+}
+
+int Binary_search(int *A, int numOfArray, int key)
+{
+ int i = 0;
+ int j = numOfArray;
+ int mid = ((i + j) / 2.0 + 0.5);
+ int loops = 0;
+ printf(" bin_se ");
+ while (mid > 1 && i < j) {
+ if (loops > numOfArray){
+ printf("Loop error.\n");
+ break;
+ }
+ if (A[mid] == key)
+ return mid;
+ if (A[mid] > key)
+ j = mid;
+ if (A[mid] < key)
+ i = mid;
+ mid = ((i + j) / 2.0 + 0.5);
+ loops ++;
+ }
+ return -1;
+
+}
+
+int __inline cmp(const void *a, const void *b)
+ //used for qsort function
+{
+ return *(int*) a - *(int*) b;
+
+}
+
+int test(void)
+{
+ static int times = 0;
+ int *array;
+ int randKey, key;
+ int tmp;
+ int ret1,ret2,ret3;
+ printf("#%7d ", times);
+ array = (int*) malloc(NUM * sizeof(int));
+ if (array == NULL)
+ return -1;
+ for (tmp = 0; tmp < NUM; tmp++) {
+ *(array + tmp) = rand() ;
+ }
+ qsort(array, NUM, sizeof(int), cmp);
+ randKey = rand() % NUM;
+ if (randKey > NUM)
+ key = 10000000;
+ else
+ key = array[randKey];
+ ret1 = line_search(array, NUM, key);
+ ret2 = Binary_search(array, NUM, key);
+ ret3 = Re_Binary_search(array, NUM, key);
+ printf(": %s\n", (array[ret1] == key && array[ret2] == key && array[ret3] == key)?
+ "The result is right." : "The result is wrong."
+ );
+ free(array);
+ times++;
+ return 0;
+}
+
+
+int main(void)
+{
+ int i = 0;
+ srand((unsigned) time(NULL));
+ for (;i < 10000;i++)
+ test();
+ return 0;
+
+}
+
View
105 C_files/float_nan_inf.cpp
@@ -0,0 +1,105 @@
+/*
+ * float.cpp
+ *
+ * Created on: 2011-11-25
+ * Author: wangxiao
+ * Email: zjjhwxc@gmail.com
+ */
+
+#include <iostream>
+using namespace std;
+
+/* This is based on the IEEE floating point number.
+ * Mainly related to the two special floating pointer numbers.
+ * (Not a Number)NaN
+ * (Infinity) Inf
+ * A floating point number consists of three parts.
+ * Single precision: sign bit : 1b + exponent : 8b + mantissa : 23b
+ * Double precision: sign bit : 1b + exponent : 11b + mantissa : 52b
+ * Extended precision: sign bit : 1b + exponent : 14b + mantissa : 65b
+ *
+ * Two number:NaN and Inf
+ *
+ * NaN: sign bit : 1 or 1,both OK; exponent:all 1; mantissa: at least one bit is not 0
+ *
+ * Inf: sign bit show the sign of the Inf, 1 means Inf; 0 means -Inf
+ * exponent:all 1; mantissa: all 0
+ *
+ * For example:
+ * float : NaN: 0_11111111_00000000000000000000001 32bit
+ * Inf 0_11111111_00000000000000000000000 32bit
+ *
+ */
+
+/* Language : Simplified Chinese (Character set : UTF-8)
+ *
+ * 此源程序与IEEE浮点数相关, 此程序主要实现了两个特殊浮点数 NaN和Inf
+ * NaN(Not a Number) 非数字。
+ * Inf(Infinity) 无穷大。
+ * 一个浮点数由三部分组成。
+ * 单精度 :符号位 1位 + 指数位 8 位 + 尾数位 23位
+ * 双精度 :符号位 1位 + 指数位 11位 + 尾数位 52位
+ * 扩展精度:符号位 1位 + 指数位 14位 + 尾数位 65位
+ *
+ * 非数字: 符号位随意;指数位全1;尾数位至少有一个不为0
+ * 无穷大: 符号位代表正负无穷大;指数位全1;尾数位全为0
+ * 例子 :
+ * 单精度浮点数: NaN 0_11111111_00000000000000000000001 32bit
+ * Inf 0_11111111_00000000000000000000000 32bit
+ */
+
+float set_nan(float *flN)
+{
+ *(int*) (flN) = 0x1FE;
+ *(int*) (flN) = *(int*) (flN) << 22;
+ *(int*) (flN) = *(int*) (flN) | 0x1;
+ return *flN;
+}
+
+double set_nan(double *dlN)
+ //Fix this function, for it cannot work correctly on 32-bit machine.
+ //Just call the set_nan of float edition and assign it to the dlN.
+ //修正一个错误,原有程序无法在32位机上正常运行。
+ //仅仅调用了单精度版的set_nan,进行赋值,字节级别操作由硬件完成
+{
+ float tmp;
+ set_nan(&tmp);
+ *dlN = tmp;
+ return *dlN;
+}
+
+float set_inf(float *flN, int neg)
+ //the neg means whether the result double is negative
+ //参数neg代表了是正无穷大或负无穷大
+{
+ set_nan(flN);
+ *((int*) (flN)) -= 0x1;
+ if (neg < 0) {
+ *(int*) flN = *(int*) flN | 0x80000000;
+ }
+
+ return *flN;
+}
+
+double set_inf(double *dlN, int neg)
+{
+ set_nan(dlN);
+ *((int*) (dlN)) -= 0x1;
+ if (neg < 0) {
+ *((int*) dlN + 1) |= 0x80000000;
+ }
+
+ return *dlN;
+}
+
+int main(void)
+{
+ float a_f;
+ double a_d;
+ cout << set_nan(&a_f) << endl;
+ cout << set_nan(&a_d) << endl;
+ cout << set_inf(&a_f, 1) << endl;
+ cout << set_inf(&a_d, -1) << endl;
+ return 0;
+}
+
View
129 C_files/merge_sort_20111126.c
@@ -0,0 +1,129 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <memory.h>
+#define NUM 100
+
+/*
+ * Merge sort donot use the max sentinel variable.
+ *
+ * Recursion edition of insert_sort
+ *
+ *
+ *
+ *
+ *
+ */
+int merge(int *A, int p, int q, int r)
+ //This version of merge do not need a sentinel variable.
+{
+ int n1 = q - p + 1;
+ int n2 = r - q;
+ int *L, *R;
+ int i, j;
+ int k;
+ L = (int*) malloc(n1 * sizeof(int));
+ R = (int*) malloc(n2 * sizeof(int));
+
+ for (i = 0; i < n1; i++)
+ *(L + i) = A[p + i - 1];
+ for (j = 0; j < n2; j++)
+ *(R + j) = A[q + j];
+
+ i = 0;
+ j = 0;
+ k = p - 1;
+ while (i < n1 && j < n2) {
+ if (L[i] < R[j]) {
+ A[k] = L[i];
+ i++;
+ }
+ else {
+ A[k] = R[j];
+ j++;
+ }
+ k++;
+ }
+ if (i < n1)
+ for (; k < r; k++) {
+ A[k] = L[i];
+ i++;
+ }
+ if (j < n2)
+ for (; k < r; k++) {
+ A[k] = R[j];
+ j++;
+ }
+ return 0;
+}
+
+int merge_sort(int *A, int p, int r)
+{
+ int q;
+ if (p < r) {
+ q = (p + r) / 2;
+ merge_sort(A, p, q);
+ merge_sort(A, q + 1, r);
+ merge(A, p, q, r);
+ }
+ return 0;
+}
+
+int cmp(const void *a, const void *b)
+{
+ return *(int*) a - *(int*) b;
+}
+
+int insert(int *A, int r, int key)
+{
+ int i = r;
+ while (i >= 0 && A[i] > key) {
+ A[i + 1] = A[i];
+ i--;
+ }
+ A[i + 1] = key;
+ return 0;
+
+}
+
+int insert_sort(int *A, int r)
+ //This version of insert_sort
+{
+ if (r >= 1) {
+ insert_sort(A, r - 1);
+ insert(A, r - 1, A[r]);
+ }
+
+ return 0;
+}
+
+int main(void)
+{
+ int *array;
+ int *array2;
+ int i;
+ array2 = (int*) malloc(NUM * sizeof(int));
+ array = (int*) malloc(NUM * sizeof(int));
+ if (array == NULL)
+ return -1;
+ srand((int) time(NULL));
+ for (i = 0; i < NUM; i++)
+ *(array + i) = rand() % 1000;
+ memcpy(array2, array, NUM * sizeof(int));
+ memset(array + sizeof(int) * NUM,0x00,10);
+ qsort(array2, NUM, sizeof(int), cmp);
+
+ for (i = 0; i < NUM; i++)
+ printf("%d ", *(array + i));
+ insert_sort(array, NUM - 1);
+ printf("\n\n");
+ for (i = 0; i < NUM; i++)
+ printf("%d ", *(array + i));
+ printf("\n\n");
+ for (i = 0; i < NUM; i++)
+ printf("%d ", *(array2 + i));
+ fflush(stdout);
+ free(array);
+ free(array2);
+ return 0;
+}
View
699 Java_files/calc.java
@@ -0,0 +1,699 @@
+/**
+ *
+ * This is a calculator, which can calculate some simple operands, such as plus,
+ * minus, multiply,divide. But the whole frame for a science calculator has been
+ * created. It can be easily extended to a complete science calculator in future.
+ * This frame use awt(Abstract Window Toolkit) package, which may not be very
+ * portable.
+ *
+ *
+ * @author WangXiao
+ * @StudentID 080520090026
+ * @date Nov. 06, 2011
+ *
+ */
+
+import java.awt.*;
+import java.awt.event.*;
+
+class calc extends calc_box
+{
+ public static void main(String[] args)
+ {
+ new calc_box();
+ }
+}
+
+/**
+ * class class_box draw the calculator in the window.
+ *
+ * @author WangXiao
+ *
+ */
+
+/*
+ * This is the close tag of the eclipse formatter, can be deleted anytime.
+ * offfor.java: public @interface offfor {
+ *
+ * }
+ */
+
+//@offfor
+
+/** Structural picture of the frame.
+ * ______________________________________________________________________________________
+ * |me_bar______________________________________________________________________________|
+ * | |pan_yell_up______________________________________________________________| |
+ * | |--------------------------------|----------------------------------------| |
+ * |pan_red_|pan_down_left___________________|pan_down_right___________pan_yell_down__| |
+ * | |________________________________________________________________________________| |
+ * | | pan_blue_up | |
+ * | |--------------------------------------------------------------------------------| |
+ * | | pan_blue_center | |
+ * | | -------------------------------------------------------------------------------| |
+ * | | pan_blue_down | |
+ * | | | |
+ * | | | |
+ * | | | |
+ * | | | |
+ * | | | |
+ * | | | |
+ * | |pan_blue________________________________________________________________________| |
+ * |____________________________________________________________________________________|
+ *
+ * @author WangXiao
+ *
+ */
+
+//@onfor
+/*
+ * This is the open tag of the eclipse formatter, can be deleted anytime.
+ * onfor.java: public @interface onfor {
+ *
+ * }
+ */
+class calc_box implements WindowListener, ActionListener, ItemListener
+{
+ protected Frame fr;
+ private Panel pan_Red, pan_Blue;
+ public TextField tf_Result, tf_Mem, tf_Blanket;
+ private Panel pan_Yell_up, pan_Yell_down;
+ private Panel pan_Down_left, pan_Down_right;
+ private MenuItem mi_Cut, mi_Copy, mi_Paste, mi_Science, mi_Comm,
+ mi_Help, mi_About, mi_Exit;
+ private Checkbox cb_Radio_2, cb_Radio_8, cb_Radio_10, cb_Radio_16;
+ private Checkbox cb_Degree, cb_Radian, cb_Grads;
+ private CheckboxGroup cbg_Radio, cbg_Angle;
+ private Panel pan_Blue_center;
+ private Checkbox cb_Inv, cb_Hyp;
+ private Button bt_Bksp, bt_Ce, bt_C;
+ private Panel pan_Blue_down;
+ private String operator[][]; // the string on the Button in the panel
+ private Button bt_oper[][]; // main Buttons of the calculator
+ private double operand_1 = 0, operand_2 = 0, result = 0, tmp = 0;
+ @SuppressWarnings("unused")
+ private String strOper_1, strOper_2, resString;
+ @SuppressWarnings("unused")
+ private boolean bInv, bHyp; // prevent. used as the flag of Hyp and Inv
+ private String tmpString = null;
+ private final static int BIN = 0;
+ private final static int OCT = 1;
+ private final static int DEC = 2;
+ private final static int HEX = 3;
+ private final static int DEGREE = 4;
+ private final static int RADIAN = 5;
+ private final static int GRADS = 6;
+ @SuppressWarnings("unused")
+ private int RadioState = DEC; // prevent. used as the RadioState e.g.
+ // BIN
+ @SuppressWarnings("unused")
+ private int AngleState = DEGREE; // prevent. used as the AngleState e.g.
+ // DRGREE
+ private boolean edited; // whether the number has been edited
+ private boolean doted; // whether the number has a dot
+ private static int decimal = 0; // decimal place
+ private int formOperator; // the formOperator which was pressed e.g. +
+
+ private void OutputStr(String outStr)
+ {
+ int len = outStr.length();
+ if (95 < len) {
+ // string which will be output is longer than the
+ // TextField
+
+ tf_Result.setText((outStr.substring(0, len - 95))
+ .toUpperCase());
+ }
+ else {
+ char sp[] = new char[95 - len];
+ for (int i = 0; i < 95 - len; i++)
+ sp[i] = ' ';
+ String strT = new String(sp);
+ String out = new String();
+ out = strT + outStr;
+ tf_Result.setText(out.toUpperCase());
+ }
+ }
+
+ /**
+ * private void draw_menu() draw the menu bar of the window.
+ */
+ private void draw_menu()
+ {
+ mi_Cut = new MenuItem("Cut");
+ mi_Copy = new MenuItem("Copy");
+ mi_Paste = new MenuItem("Paste");
+ mi_Science = new MenuItem("Science");
+ mi_Comm = new MenuItem("Common");
+ mi_Help = new MenuItem("Help");
+ mi_About = new MenuItem("About");
+ mi_Exit = new MenuItem("Exit");
+ Menu me_file = new Menu("File");
+ me_file.add(mi_Cut);
+ me_file.add(mi_Copy);
+ me_file.add(mi_Paste);
+ me_file.addSeparator();
+ me_file.add(mi_Exit);
+ Menu me_view = new Menu("View");
+ me_view.add(mi_Science);
+ me_view.add(mi_Comm);
+ Menu me_help = new Menu("Help");
+ me_help.add(mi_Help);
+ me_help.add(mi_About);
+ MenuBar me_bar = new MenuBar();
+ me_bar.add(me_file);
+ me_bar.add(me_view);
+ me_bar.add(me_help);
+ fr.setMenuBar(me_bar);
+ }
+
+ /**
+ * private void draw_check() draw the check of the window, such as radio
+ * or angle.
+ */
+ private void draw_check()
+ {
+ pan_Down_left.add(new Label(" "));
+ cbg_Radio = new CheckboxGroup();
+ cb_Radio_2 = new Checkbox("Bin", cbg_Radio, false);
+ cb_Radio_8 = new Checkbox("Oct", cbg_Radio, false);
+ cb_Radio_10 = new Checkbox("Dec", cbg_Radio, true);
+ cb_Radio_16 = new Checkbox("Hec", cbg_Radio, false);
+ pan_Down_left.add(cb_Radio_2);
+ pan_Down_left.add(cb_Radio_8);
+ pan_Down_left.add(cb_Radio_10);
+ pan_Down_left.add(cb_Radio_16);
+ cbg_Angle = new CheckboxGroup();
+ cb_Degree = new Checkbox("Degree", cbg_Angle, true);
+ cb_Radian = new Checkbox("Radian", cbg_Angle, false);
+ cb_Grads = new Checkbox("Grads", cbg_Angle, false);
+ pan_Down_right.add(cb_Degree);
+ pan_Down_right.add(cb_Radian);
+ pan_Down_right.add(cb_Grads);
+ pan_Down_right.add(new Label(" "));
+ }
+
+ private void init_opera()
+ {
+ operator = new String[][] {
+ { "Sta", "Ave", "Sum", "s", "Dat" },
+ { "F-E", "dms", "sin", "cos", "tan" },
+ { "(", "Exp", "x^y", "x^3", "x^2" },
+ { ")", "In", "log", "n!", "1/x" },
+ { "MC", "MR", "MS", "M+", "pi" },
+ { "7", "4", "1", "0", "A" },
+ { "8", "5", "2", "+/-", "B" },
+ { "9", "6", "3", ".", "C" },
+ { "/", "*", "-", "+", "D" },
+ { "Mod", "Or", "Lsh", "=", "E" },
+ { "And", "Xor", "Not", "Int", "F" } };
+
+ }
+
+ private void draw_frame()
+ {
+ fr = new Frame();
+ fr.setLayout(new BorderLayout());
+ fr.setSize(750, 380);
+ fr.setResizable(false);
+ fr.setVisible(true);
+ fr.addWindowListener(this);
+ fr.setBackground(new Color(238, 238, 238));
+ fr.setTitle("Calculator");
+ }
+
+ private void draw_panel_Lev_1()
+ // draw the panel on level one. Split the frame into two parts, upper is
+ // pan_red, down is pan_blue.
+ {
+ pan_Red = new Panel();
+ pan_Blue = new Panel();
+ fr.add("North", pan_Red);
+ fr.add("Center", pan_Blue);
+ pan_Yell_up = new Panel();
+ pan_Yell_down = new Panel();
+ pan_Red.setLayout(new BorderLayout());
+ pan_Red.add("North", pan_Yell_up);
+ pan_Red.add("South", pan_Yell_down);
+ }
+
+ private void draw_blue_upper()
+ {
+ Panel blue_center_up;
+ pan_Blue_center.setLayout(new BorderLayout());
+ blue_center_up = new Panel();
+ pan_Blue_center.add("North", blue_center_up);
+ Panel pan_blue_center_up_left = new Panel();
+ blue_center_up.setLayout(new BorderLayout());
+ blue_center_up.add("West", pan_blue_center_up_left);
+ pan_blue_center_up_left.add(new Label(" "));
+ cb_Inv = new Checkbox("Inv");
+ pan_blue_center_up_left.add(cb_Inv);
+ pan_blue_center_up_left.add(new Label(" "));
+ cb_Hyp = new Checkbox("Hyp");
+ pan_blue_center_up_left.add(cb_Hyp);
+ pan_blue_center_up_left.add(new Label(" "));
+ tf_Mem = new TextField(2);
+ tf_Mem.setEnabled(false);
+ pan_blue_center_up_left.add(tf_Mem);
+ tf_Blanket = new TextField(2);
+ tf_Blanket.setEnabled(false);
+ pan_blue_center_up_left.add(tf_Blanket);
+ pan_blue_center_up_left
+ .add(new Label(" "));
+ bt_Bksp = new Button(" Backspace ");
+ bt_C = new Button(" C ");
+ bt_Ce = new Button(" CE ");
+ bt_Bksp.setSize(30, 30);
+ pan_blue_center_up_left.add(bt_Bksp);
+ pan_blue_center_up_left.add(bt_C);
+ pan_blue_center_up_left.add(bt_Ce);
+ }
+
+ private void draw_Button()
+ {
+ bt_oper = new Button[11][5];
+ pan_Blue_down = new Panel();
+ pan_Blue_center.add("Center", pan_Blue_down);
+ pan_Blue_down.setLayout(new GridLayout(5, 11, 5, 5));
+ for (int t = 0; t < 5; t++)
+ for (int j = 0; j < 11; j++) {
+ if (j == 5) // set separator of two Buttons such
+ // as between MC and 7
+ pan_Blue_down.add(new Label(" "));
+ bt_oper[j][t] = new Button(operator[j][t]);
+ bt_oper[j][t].addActionListener(this);
+ pan_Blue_down.add(bt_oper[j][t]);
+ }
+ }
+
+ private void draw_panel_blue()
+ {
+ pan_Blue.setLayout(new BorderLayout());
+ pan_Blue.add("West", new Panel());
+ pan_Blue.add("East", new Panel());
+ pan_Blue.add("North", new Panel());
+ pan_Blue.add("South", new Panel());
+ pan_Blue_center = new Panel();
+ pan_Blue.add("Center", pan_Blue_center);
+ }
+
+ private void draw_panel_yellow()
+ {
+ tf_Result = new TextField(80);
+ tf_Result.setEnabled(false);
+ pan_Yell_up.add(tf_Result);
+ pan_Down_left = new Panel();
+ pan_Down_right = new Panel();
+ pan_Yell_down.setLayout(new BorderLayout());
+ pan_Yell_down.add("West", pan_Down_left);
+ pan_Yell_down.add("East", pan_Down_right);
+ draw_check();
+ }
+
+ public calc_box()
+ // default ctor, draw the window
+ {
+ init_opera();
+ draw_frame();
+ draw_menu();
+ draw_panel_Lev_1();
+ draw_panel_yellow();
+ draw_panel_blue();
+ draw_blue_upper();
+ draw_Button();
+ addListener();
+ fr.validate();
+ EnableButton(10);
+ }
+
+ private void EnableButton(int n)
+ {
+ if (n == 10) {
+ for (int i = 5; i < 8; i++)
+ for (int j = 0; j < 4; j++)
+ bt_oper[i][j].setEnabled(true);
+ for (int i = 5; i < 10; i++)
+ bt_oper[i][4].setEnabled(false);
+ bt_oper[10][4].setEnabled(false);
+ }
+ if (n == 16) {
+ for (int i = 5; i < 8; i++)
+ for (int j = 0; j < 5; j++)
+ bt_oper[i][j].setEnabled(true);
+ for (int i = 5; i < 11; i++) {
+ bt_oper[i][4].setEnabled(true);
+ }
+ }
+ if (n == 8) {
+ EnableButton(10);
+ bt_oper[6][0].setEnabled(false);
+ bt_oper[7][0].setEnabled(false);
+
+ }
+ if (n == 2) {
+ for (int i = 5; i < 8; i++)
+ for (int j = 0; j < 5; j++)
+ bt_oper[i][j].setEnabled(false);
+ bt_oper[5][2].setEnabled(true);
+ bt_oper[5][3].setEnabled(true);
+ }
+
+ }
+
+ private void addListener()
+ {
+ cb_Degree.addItemListener(this);
+ cb_Grads.addItemListener(this);
+ cb_Hyp.addItemListener(this);
+ cb_Inv.addItemListener(this);
+ cb_Radian.addItemListener(this);
+ cb_Radio_10.addItemListener(this);
+ cb_Radio_16.addItemListener(this);
+ cb_Radio_2.addItemListener(this);
+ cb_Radio_8.addItemListener(this);
+ bt_Bksp.addActionListener(this);
+ bt_C.addActionListener(this);
+ bt_Ce.addActionListener(this);
+ }
+
+ private void InitData()
+ {
+ operand_2 = tmp;
+ tmp = 0;
+ strOper_2 = new String(tmpString);
+ decimal = 0;
+ edited = false;
+ doted = false;
+ }
+
+ private double InputNum(int n)
+ // This function will deal with the tmpString variable
+ {
+ if (edited == false && doted == false) {
+ tmp = tmp * 10;
+ tmp = tmp + n;
+ tmpString = new String(Integer.toString((int) tmp));
+ }
+ else if (edited == true) {
+ InitData();
+ InputNum(n);
+ }
+
+ if (edited == false && doted == true) {
+ decimal++;
+ tmp = tmp + n * pow((0.1), decimal);
+ tmpString = new String(Double.toString(tmp));
+ }
+ else if (edited == true) {
+ InitData();
+ InputNum(n);
+ }
+ return tmp;
+ }
+
+ private double pow(double d, int decimal2)
+ {
+ double ret = 1;
+ for (int i = 0; i < decimal2; i++) {
+ ret = ret * d;
+ }
+ return ret;
+ }
+
+ private void DoBpsk()
+ {
+ tmp = tmp / 10;
+ if (doted == false)
+ tmpString = new String(Integer.toString((int) tmp));
+ else tmpString = new String(Double.toString(tmp));
+ }
+
+ @Override
+ public void windowClosing(WindowEvent e)
+ {
+ System.exit(0);
+ }
+
+ @Override
+ public void windowOpened(WindowEvent e)
+ {
+ /* do nothing */
+ }
+
+ @Override
+ public void windowClosed(WindowEvent e)
+ {
+ /* do nothing */
+ }
+
+ @Override
+ public void windowIconified(WindowEvent e)
+ {
+ /* do nothing */
+ }
+
+ @Override
+ public void windowDeiconified(WindowEvent e)
+ {
+ /* do nothing */
+ }
+
+ @Override
+ public void windowActivated(WindowEvent e)
+ {
+ /* do nothing */
+ }
+
+ @Override
+ public void windowDeactivated(WindowEvent e)
+ {
+ /* do nothing */
+ }
+
+ private void actionPer_GetNum(ActionEvent e)
+ {
+ for (int i = 0; i < 11; i++)
+ for (int j = 0; j < 5; j++) {
+ if (e.getSource() == bt_oper[i][j]) {
+ switch (operator[i][j].charAt(0)) {
+ case '0':
+ InputNum(0);
+ break;
+ case '1':
+ InputNum(1);
+ break;
+ case '2':
+ InputNum(2);
+ break;
+ case '3':
+ InputNum(3);
+ break;
+ case '4':
+ InputNum(4);
+ break;
+ case '5':
+ InputNum(5);
+ break;
+ case '6':
+ InputNum(6);
+ break;
+ case '7':
+ InputNum(7);
+ break;
+ case '8':
+ InputNum(8);
+ break;
+ case '9':
+ InputNum(9);
+ break;
+ }
+ OutputStr(tmpString);
+ break;
+ }
+ }
+ }
+
+ public void actionPer_Calc(ActionEvent e)
+ {
+ if (e.getSource() == bt_oper[8][1]) {
+ // the *
+ formOperator = '*';
+ if (strOper_2 == null)
+ InitData();
+ else {
+ operand_1 = tmp;
+ strOper_1 = new String(
+ Double.toString(operand_1));
+ do_calc('*');
+ operand_1 = 1;
+ }
+ }
+ if (e.getSource() == bt_oper[8][0]) {
+ // the /
+ formOperator = '/';
+ if (strOper_2 == null)
+ InitData();
+ else {
+ operand_1 = tmp;
+ strOper_1 = new String(
+ Double.toString(operand_1));
+ do_calc('/');
+ operand_1 = 1;
+ }
+ }
+ if (e.getSource() == bt_oper[8][3]) {
+ // the +
+ formOperator = '+';
+ if (strOper_2 == null)
+ InitData();
+ else {
+ operand_1 = tmp;
+ strOper_1 = new String(
+ Double.toString(operand_1));
+ do_calc('+');
+ operand_1 = 0;
+ }
+ }
+ if (e.getSource() == bt_oper[8][2]) {
+ // the -
+ formOperator = '-';
+ if (strOper_2 == null)
+ InitData();
+ else {
+ operand_1 = tmp;
+ strOper_1 = new String(
+ Double.toString(operand_1));
+ do_calc('-');
+ operand_1 = 0;
+ }
+ }
+
+ if (e.getSource() == bt_oper[9][3]) {
+ // the =
+ if (strOper_2 == null)
+ InitData();
+ else {
+ operand_1 = tmp;
+ strOper_1 = new String(
+ Double.toString(operand_1));
+ do_calc(formOperator);
+ strOper_2 = null;
+ }
+
+ }
+
+ }
+
+ public void actionPerformed(ActionEvent e)
+ {
+ if (e.getSource() == bt_oper[7][3]) {
+ // the dot
+ // make sure whether the dot Button had been pressed.
+ // this should has a higher response level than input a
+ // number.
+ doted = true;
+ }
+ actionPer_GetNum(e);
+ if (e.getSource() == bt_C) {
+ InitAll();
+ OutputStr("");
+ }
+ if (e.getSource() == bt_Bksp) {
+ DoBpsk();
+ OutputStr(tmpString);
+ }
+ if (e.getSource() == bt_Ce) {
+ InitAll();
+ OutputStr("");
+ }
+ actionPer_Calc(e);
+
+ }
+
+ private void do_calc(int op)
+ {
+ if (op == '+') {
+ result = operand_1 + operand_2;
+ resString = new String(Double.toString(result));
+ operand_2 = result;
+ strOper_2 = new String(Double.toString(result));
+ }
+ if (op == '-') {
+ result = operand_1 - operand_2;
+ resString = new String(Double.toString(result));
+ operand_2 = result;
+ strOper_2 = new String(Double.toString(result));
+ }
+ if (op == '*') {
+ result = operand_1 * operand_2;
+ resString = new String(Double.toString(result));
+ operand_2 = result;
+ strOper_2 = new String(Double.toString(result));
+ }
+ if (op == '/') {
+ result = operand_1 / operand_2;
+ resString = new String(Double.toString(result));
+ operand_2 = result;
+ strOper_2 = new String(Double.toString(result));
+ }
+ OutputStr(resString);
+
+ }
+
+ private void InitAll()
+ {
+ operand_1 = 0;
+ operand_2 = 0;
+ result = 0;
+ tmp = 0;
+ strOper_1 = null;
+ strOper_2 = null;
+ tmpString = null;
+ resString = null;
+ edited = false;
+ doted = false;
+ decimal = 0;
+ }
+
+ @Override
+ public void itemStateChanged(ItemEvent e)
+ {
+ if (e.getSource() == cb_Degree) {
+ AngleState = DEGREE;
+ }
+ else if (e.getSource() == cb_Grads) {
+ AngleState = GRADS;
+ }
+ else if (e.getSource() == cb_Radian) {
+ AngleState = RADIAN;
+ }
+ else if (e.getSource() == cb_Inv) {
+ bInv = true;
+ }
+ else if (e.getSource() == cb_Hyp) {
+ bHyp = true;
+ }
+ else if (e.getSource() == cb_Radio_10) {
+ OutputStr(Double.toString(tmp));
+ RadioState = DEC;
+ EnableButton(10);
+ }
+ else if (e.getSource() == cb_Radio_16) {
+ OutputStr(Integer.toHexString((int) tmp));
+ RadioState = HEX;
+ EnableButton(16);
+ }
+ else if (e.getSource() == cb_Radio_2) {
+ OutputStr(Integer.toBinaryString((int) tmp));
+ RadioState = BIN;
+ EnableButton(2);
+ }
+ else if (e.getSource() == cb_Radio_8) {
+ OutputStr(Integer.toOctalString((int) tmp));
+ RadioState = OCT;
+ EnableButton(8);
+ }
+ edited = true;
+ }
+}
View
11 README
@@ -0,0 +1,11 @@
+This is my personal source file back up dir.
+
+
+/C_file is the C language file.
+/Java_file is the Java language.
+README this file.
+
+
+Last modify: Nov. 26 2011
+Athenacle
+zjjhwxc@gmail.com
Please sign in to comment.
Something went wrong with that request. Please try again.