Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit ab7c422ff23248dca98add08147af21b381a02ee 2 parents 7caf373 + ac5050a
WangXiao authored
129 C_files/bin_search_20111126.c
... ... @@ -0,0 +1,129 @@
  1 +/*
  2 + * search.c
  3 + *
  4 + * Created on: 2011-11-19
  5 + * Author: wangxiao
  6 + * email: zjjhwxc@gmail.com
  7 + */
  8 +
  9 +#include <stdio.h>
  10 +#include <stdlib.h>
  11 +#include <time.h>
  12 +
  13 +#define NUM 1000
  14 +
  15 +/* line_search and binary_search
  16 + *
  17 + * binary_search has two editions
  18 + * iteration and recursion
  19 + *
  20 + */
  21 +
  22 +int line_search(int *A, int numOfArray, int key)
  23 +{
  24 +
  25 + int i;
  26 + printf(" lin_se ");
  27 + for (i = 0; i < numOfArray; i++) {
  28 + if (*(A + i) == key)
  29 + return i;
  30 + }
  31 + return -1;
  32 +}
  33 +
  34 +int bin_search(int *A, int i, int j, int key)
  35 +{
  36 + int mid = ((i + j) / 2.0 + 0.5);
  37 + if (i + 1 >= j)
  38 + return -1;
  39 +
  40 + if (A[mid] == key)
  41 + return mid;
  42 + if (A[mid] < key)
  43 + return bin_search(A, mid, j, key);
  44 + if (A[mid] > key)
  45 + return bin_search(A, i, mid, key);
  46 + return -1;
  47 +
  48 +}
  49 +
  50 +int Re_Binary_search(int *A, int numOfArray, int key)
  51 +{
  52 + printf(" Re_Bin ");
  53 + return bin_search(A, 0, numOfArray - 1, key);
  54 +}
  55 +
  56 +int Binary_search(int *A, int numOfArray, int key)
  57 +{
  58 + int i = 0;
  59 + int j = numOfArray;
  60 + int mid = ((i + j) / 2.0 + 0.5);
  61 + int loops = 0;
  62 + printf(" bin_se ");
  63 + while (mid > 1 && i < j) {
  64 + if (loops > numOfArray){
  65 + printf("Loop error.\n");
  66 + break;
  67 + }
  68 + if (A[mid] == key)
  69 + return mid;
  70 + if (A[mid] > key)
  71 + j = mid;
  72 + if (A[mid] < key)
  73 + i = mid;
  74 + mid = ((i + j) / 2.0 + 0.5);
  75 + loops ++;
  76 + }
  77 + return -1;
  78 +
  79 +}
  80 +
  81 +int __inline cmp(const void *a, const void *b)
  82 + //used for qsort function
  83 +{
  84 + return *(int*) a - *(int*) b;
  85 +
  86 +}
  87 +
  88 +int test(void)
  89 +{
  90 + static int times = 0;
  91 + int *array;
  92 + int randKey, key;
  93 + int tmp;
  94 + int ret1,ret2,ret3;
  95 + printf("#%7d ", times);
  96 + array = (int*) malloc(NUM * sizeof(int));
  97 + if (array == NULL)
  98 + return -1;
  99 + for (tmp = 0; tmp < NUM; tmp++) {
  100 + *(array + tmp) = rand() ;
  101 + }
  102 + qsort(array, NUM, sizeof(int), cmp);
  103 + randKey = rand() % NUM;
  104 + if (randKey > NUM)
  105 + key = 10000000;
  106 + else
  107 + key = array[randKey];
  108 + ret1 = line_search(array, NUM, key);
  109 + ret2 = Binary_search(array, NUM, key);
  110 + ret3 = Re_Binary_search(array, NUM, key);
  111 + printf(": %s\n", (array[ret1] == key && array[ret2] == key && array[ret3] == key)?
  112 + "The result is right." : "The result is wrong."
  113 + );
  114 + free(array);
  115 + times++;
  116 + return 0;
  117 +}
  118 +
  119 +
  120 +int main(void)
  121 +{
  122 + int i = 0;
  123 + srand((unsigned) time(NULL));
  124 + for (;i < 10000;i++)
  125 + test();
  126 + return 0;
  127 +
  128 +}
  129 +
105 C_files/float_nan_inf.cpp
... ... @@ -0,0 +1,105 @@
  1 +/*
  2 + * float.cpp
  3 + *
  4 + * Created on: 2011-11-25
  5 + * Author: wangxiao
  6 + * Email: zjjhwxc@gmail.com
  7 + */
  8 +
  9 +#include <iostream>
  10 +using namespace std;
  11 +
  12 +/* This is based on the IEEE floating point number.
  13 + * Mainly related to the two special floating pointer numbers.
  14 + * (Not a Number)NaN
  15 + * (Infinity) Inf
  16 + * A floating point number consists of three parts.
  17 + * Single precision: sign bit : 1b + exponent : 8b + mantissa : 23b
  18 + * Double precision: sign bit : 1b + exponent : 11b + mantissa : 52b
  19 + * Extended precision: sign bit : 1b + exponent : 14b + mantissa : 65b
  20 + *
  21 + * Two number:NaN and Inf
  22 + *
  23 + * NaN: sign bit : 1 or 1,both OK; exponent:all 1; mantissa: at least one bit is not 0
  24 + *
  25 + * Inf: sign bit show the sign of the Inf, 1 means Inf; 0 means -Inf
  26 + * exponent:all 1; mantissa: all 0
  27 + *
  28 + * For example:
  29 + * float : NaN: 0_11111111_00000000000000000000001 32bit
  30 + * Inf 0_11111111_00000000000000000000000 32bit
  31 + *
  32 + */
  33 +
  34 +/* Language : Simplified Chinese (Character set : UTF-8)
  35 + *
  36 + * 此源程序与IEEE浮点数相关, 此程序主要实现了两个特殊浮点数 NaN和Inf
  37 + * NaN(Not a Number) 非数字。
  38 + * Inf(Infinity) 无穷大。
  39 + * 一个浮点数由三部分组成。
  40 + * 单精度 :符号位 1位 + 指数位 8 位 + 尾数位 23位
  41 + * 双精度 :符号位 1位 + 指数位 11位 + 尾数位 52位
  42 + * 扩展精度:符号位 1位 + 指数位 14位 + 尾数位 65位
  43 + *
  44 + * 非数字: 符号位随意;指数位全1;尾数位至少有一个不为0
  45 + * 无穷大: 符号位代表正负无穷大;指数位全1;尾数位全为0
  46 + * 例子 :
  47 + * 单精度浮点数: NaN 0_11111111_00000000000000000000001 32bit
  48 + * Inf 0_11111111_00000000000000000000000 32bit
  49 + */
  50 +
  51 +float set_nan(float *flN)
  52 +{
  53 + *(int*) (flN) = 0x1FE;
  54 + *(int*) (flN) = *(int*) (flN) << 22;
  55 + *(int*) (flN) = *(int*) (flN) | 0x1;
  56 + return *flN;
  57 +}
  58 +
  59 +double set_nan(double *dlN)
  60 + //Fix this function, for it cannot work correctly on 32-bit machine.
  61 + //Just call the set_nan of float edition and assign it to the dlN.
  62 + //修正一个错误,原有程序无法在32位机上正常运行。
  63 + //仅仅调用了单精度版的set_nan,进行赋值,字节级别操作由硬件完成
  64 +{
  65 + float tmp;
  66 + set_nan(&tmp);
  67 + *dlN = tmp;
  68 + return *dlN;
  69 +}
  70 +
  71 +float set_inf(float *flN, int neg)
  72 + //the neg means whether the result double is negative
  73 + //参数neg代表了是正无穷大或负无穷大
  74 +{
  75 + set_nan(flN);
  76 + *((int*) (flN)) -= 0x1;
  77 + if (neg < 0) {
  78 + *(int*) flN = *(int*) flN | 0x80000000;
  79 + }
  80 +
  81 + return *flN;
  82 +}
  83 +
  84 +double set_inf(double *dlN, int neg)
  85 +{
  86 + set_nan(dlN);
  87 + *((int*) (dlN)) -= 0x1;
  88 + if (neg < 0) {
  89 + *((int*) dlN + 1) |= 0x80000000;
  90 + }
  91 +
  92 + return *dlN;
  93 +}
  94 +
  95 +int main(void)
  96 +{
  97 + float a_f;
  98 + double a_d;
  99 + cout << set_nan(&a_f) << endl;
  100 + cout << set_nan(&a_d) << endl;
  101 + cout << set_inf(&a_f, 1) << endl;
  102 + cout << set_inf(&a_d, -1) << endl;
  103 + return 0;
  104 +}
  105 +
129 C_files/merge_sort_20111126.c
... ... @@ -0,0 +1,129 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <time.h>
  4 +#include <memory.h>
  5 +#define NUM 100
  6 +
  7 +/*
  8 + * Merge sort donot use the max sentinel variable.
  9 + *
  10 + * Recursion edition of insert_sort
  11 + *
  12 + *
  13 + *
  14 + *
  15 + *
  16 + */
  17 +int merge(int *A, int p, int q, int r)
  18 + //This version of merge do not need a sentinel variable.
  19 +{
  20 + int n1 = q - p + 1;
  21 + int n2 = r - q;
  22 + int *L, *R;
  23 + int i, j;
  24 + int k;
  25 + L = (int*) malloc(n1 * sizeof(int));
  26 + R = (int*) malloc(n2 * sizeof(int));
  27 +
  28 + for (i = 0; i < n1; i++)
  29 + *(L + i) = A[p + i - 1];
  30 + for (j = 0; j < n2; j++)
  31 + *(R + j) = A[q + j];
  32 +
  33 + i = 0;
  34 + j = 0;
  35 + k = p - 1;
  36 + while (i < n1 && j < n2) {
  37 + if (L[i] < R[j]) {
  38 + A[k] = L[i];
  39 + i++;
  40 + }
  41 + else {
  42 + A[k] = R[j];
  43 + j++;
  44 + }
  45 + k++;
  46 + }
  47 + if (i < n1)
  48 + for (; k < r; k++) {
  49 + A[k] = L[i];
  50 + i++;
  51 + }
  52 + if (j < n2)
  53 + for (; k < r; k++) {
  54 + A[k] = R[j];
  55 + j++;
  56 + }
  57 + return 0;
  58 +}
  59 +
  60 +int merge_sort(int *A, int p, int r)
  61 +{
  62 + int q;
  63 + if (p < r) {
  64 + q = (p + r) / 2;
  65 + merge_sort(A, p, q);
  66 + merge_sort(A, q + 1, r);
  67 + merge(A, p, q, r);
  68 + }
  69 + return 0;
  70 +}
  71 +
  72 +int cmp(const void *a, const void *b)
  73 +{
  74 + return *(int*) a - *(int*) b;
  75 +}
  76 +
  77 +int insert(int *A, int r, int key)
  78 +{
  79 + int i = r;
  80 + while (i >= 0 && A[i] > key) {
  81 + A[i + 1] = A[i];
  82 + i--;
  83 + }
  84 + A[i + 1] = key;
  85 + return 0;
  86 +
  87 +}
  88 +
  89 +int insert_sort(int *A, int r)
  90 + //This version of insert_sort
  91 +{
  92 + if (r >= 1) {
  93 + insert_sort(A, r - 1);
  94 + insert(A, r - 1, A[r]);
  95 + }
  96 +
  97 + return 0;
  98 +}
  99 +
  100 +int main(void)
  101 +{
  102 + int *array;
  103 + int *array2;
  104 + int i;
  105 + array2 = (int*) malloc(NUM * sizeof(int));
  106 + array = (int*) malloc(NUM * sizeof(int));
  107 + if (array == NULL)
  108 + return -1;
  109 + srand((int) time(NULL));
  110 + for (i = 0; i < NUM; i++)
  111 + *(array + i) = rand() % 1000;
  112 + memcpy(array2, array, NUM * sizeof(int));
  113 + memset(array + sizeof(int) * NUM,0x00,10);
  114 + qsort(array2, NUM, sizeof(int), cmp);
  115 +
  116 + for (i = 0; i < NUM; i++)
  117 + printf("%d ", *(array + i));
  118 + insert_sort(array, NUM - 1);
  119 + printf("\n\n");
  120 + for (i = 0; i < NUM; i++)
  121 + printf("%d ", *(array + i));
  122 + printf("\n\n");
  123 + for (i = 0; i < NUM; i++)
  124 + printf("%d ", *(array2 + i));
  125 + fflush(stdout);
  126 + free(array);
  127 + free(array2);
  128 + return 0;
  129 +}
699 Java_files/calc.java
... ... @@ -0,0 +1,699 @@
  1 +/**
  2 + *
  3 + * This is a calculator, which can calculate some simple operands, such as plus,
  4 + * minus, multiply,divide. But the whole frame for a science calculator has been
  5 + * created. It can be easily extended to a complete science calculator in future.
  6 + * This frame use awt(Abstract Window Toolkit) package, which may not be very
  7 + * portable.
  8 + *
  9 + *
  10 + * @author WangXiao
  11 + * @StudentID 080520090026
  12 + * @date Nov. 06, 2011
  13 + *
  14 + */
  15 +
  16 +import java.awt.*;
  17 +import java.awt.event.*;
  18 +
  19 +class calc extends calc_box
  20 +{
  21 + public static void main(String[] args)
  22 + {
  23 + new calc_box();
  24 + }
  25 +}
  26 +
  27 +/**
  28 + * class class_box draw the calculator in the window.
  29 + *
  30 + * @author WangXiao
  31 + *
  32 + */
  33 +
  34 +/*
  35 + * This is the close tag of the eclipse formatter, can be deleted anytime.
  36 + * offfor.java: public @interface offfor {
  37 + *
  38 + * }
  39 + */
  40 +
  41 +//@offfor
  42 +
  43 +/** Structural picture of the frame.
  44 + * ______________________________________________________________________________________
  45 + * |me_bar______________________________________________________________________________|
  46 + * | |pan_yell_up______________________________________________________________| |
  47 + * | |--------------------------------|----------------------------------------| |
  48 + * |pan_red_|pan_down_left___________________|pan_down_right___________pan_yell_down__| |
  49 + * | |________________________________________________________________________________| |
  50 + * | | pan_blue_up | |
  51 + * | |--------------------------------------------------------------------------------| |
  52 + * | | pan_blue_center | |
  53 + * | | -------------------------------------------------------------------------------| |
  54 + * | | pan_blue_down | |
  55 + * | | | |
  56 + * | | | |
  57 + * | | | |
  58 + * | | | |
  59 + * | | | |
  60 + * | | | |
  61 + * | |pan_blue________________________________________________________________________| |
  62 + * |____________________________________________________________________________________|
  63 + *
  64 + * @author WangXiao
  65 + *
  66 + */
  67 +
  68 +//@onfor
  69 +/*
  70 + * This is the open tag of the eclipse formatter, can be deleted anytime.
  71 + * onfor.java: public @interface onfor {
  72 + *
  73 + * }
  74 + */
  75 +class calc_box implements WindowListener, ActionListener, ItemListener
  76 +{
  77 + protected Frame fr;
  78 + private Panel pan_Red, pan_Blue;
  79 + public TextField tf_Result, tf_Mem, tf_Blanket;
  80 + private Panel pan_Yell_up, pan_Yell_down;
  81 + private Panel pan_Down_left, pan_Down_right;
  82 + private MenuItem mi_Cut, mi_Copy, mi_Paste, mi_Science, mi_Comm,
  83 + mi_Help, mi_About, mi_Exit;
  84 + private Checkbox cb_Radio_2, cb_Radio_8, cb_Radio_10, cb_Radio_16;
  85 + private Checkbox cb_Degree, cb_Radian, cb_Grads;
  86 + private CheckboxGroup cbg_Radio, cbg_Angle;
  87 + private Panel pan_Blue_center;
  88 + private Checkbox cb_Inv, cb_Hyp;
  89 + private Button bt_Bksp, bt_Ce, bt_C;
  90 + private Panel pan_Blue_down;
  91 + private String operator[][]; // the string on the Button in the panel
  92 + private Button bt_oper[][]; // main Buttons of the calculator
  93 + private double operand_1 = 0, operand_2 = 0, result = 0, tmp = 0;
  94 + @SuppressWarnings("unused")
  95 + private String strOper_1, strOper_2, resString;
  96 + @SuppressWarnings("unused")
  97 + private boolean bInv, bHyp; // prevent. used as the flag of Hyp and Inv
  98 + private String tmpString = null;
  99 + private final static int BIN = 0;
  100 + private final static int OCT = 1;
  101 + private final static int DEC = 2;
  102 + private final static int HEX = 3;
  103 + private final static int DEGREE = 4;
  104 + private final static int RADIAN = 5;
  105 + private final static int GRADS = 6;
  106 + @SuppressWarnings("unused")
  107 + private int RadioState = DEC; // prevent. used as the RadioState e.g.
  108 + // BIN
  109 + @SuppressWarnings("unused")
  110 + private int AngleState = DEGREE; // prevent. used as the AngleState e.g.
  111 + // DRGREE
  112 + private boolean edited; // whether the number has been edited
  113 + private boolean doted; // whether the number has a dot
  114 + private static int decimal = 0; // decimal place
  115 + private int formOperator; // the formOperator which was pressed e.g. +
  116 +
  117 + private void OutputStr(String outStr)
  118 + {
  119 + int len = outStr.length();
  120 + if (95 < len) {
  121 + // string which will be output is longer than the
  122 + // TextField
  123 +
  124 + tf_Result.setText((outStr.substring(0, len - 95))
  125 + .toUpperCase());
  126 + }
  127 + else {
  128 + char sp[] = new char[95 - len];
  129 + for (int i = 0; i < 95 - len; i++)
  130 + sp[i] = ' ';
  131 + String strT = new String(sp);
  132 + String out = new String();
  133 + out = strT + outStr;
  134 + tf_Result.setText(out.toUpperCase());
  135 + }
  136 + }
  137 +
  138 + /**
  139 + * private void draw_menu() draw the menu bar of the window.
  140 + */
  141 + private void draw_menu()
  142 + {
  143 + mi_Cut = new MenuItem("Cut");
  144 + mi_Copy = new MenuItem("Copy");
  145 + mi_Paste = new MenuItem("Paste");
  146 + mi_Science = new MenuItem("Science");
  147 + mi_Comm = new MenuItem("Common");
  148 + mi_Help = new MenuItem("Help");
  149 + mi_About = new MenuItem("About");
  150 + mi_Exit = new MenuItem("Exit");
  151 + Menu me_file = new Menu("File");
  152 + me_file.add(mi_Cut);
  153 + me_file.add(mi_Copy);
  154 + me_file.add(mi_Paste);
  155 + me_file.addSeparator();
  156 + me_file.add(mi_Exit);
  157 + Menu me_view = new Menu("View");
  158 + me_view.add(mi_Science);
  159 + me_view.add(mi_Comm);
  160 + Menu me_help = new Menu("Help");
  161 + me_help.add(mi_Help);
  162 + me_help.add(mi_About);
  163 + MenuBar me_bar = new MenuBar();
  164 + me_bar.add(me_file);
  165 + me_bar.add(me_view);
  166 + me_bar.add(me_help);
  167 + fr.setMenuBar(me_bar);
  168 + }
  169 +
  170 + /**
  171 + * private void draw_check() draw the check of the window, such as radio
  172 + * or angle.
  173 + */
  174 + private void draw_check()
  175 + {
  176 + pan_Down_left.add(new Label(" "));
  177 + cbg_Radio = new CheckboxGroup();
  178 + cb_Radio_2 = new Checkbox("Bin", cbg_Radio, false);
  179 + cb_Radio_8 = new Checkbox("Oct", cbg_Radio, false);
  180 + cb_Radio_10 = new Checkbox("Dec", cbg_Radio, true);
  181 + cb_Radio_16 = new Checkbox("Hec", cbg_Radio, false);
  182 + pan_Down_left.add(cb_Radio_2);
  183 + pan_Down_left.add(cb_Radio_8);
  184 + pan_Down_left.add(cb_Radio_10);
  185 + pan_Down_left.add(cb_Radio_16);
  186 + cbg_Angle = new CheckboxGroup();
  187 + cb_Degree = new Checkbox("Degree", cbg_Angle, true);
  188 + cb_Radian = new Checkbox("Radian", cbg_Angle, false);
  189 + cb_Grads = new Checkbox("Grads", cbg_Angle, false);
  190 + pan_Down_right.add(cb_Degree);
  191 + pan_Down_right.add(cb_Radian);
  192 + pan_Down_right.add(cb_Grads);
  193 + pan_Down_right.add(new Label(" "));
  194 + }
  195 +
  196 + private void init_opera()
  197 + {
  198 + operator = new String[][] {
  199 + { "Sta", "Ave", "Sum", "s", "Dat" },
  200 + { "F-E", "dms", "sin", "cos", "tan" },
  201 + { "(", "Exp", "x^y", "x^3", "x^2" },
  202 + { ")", "In", "log", "n!", "1/x" },
  203 + { "MC", "MR", "MS", "M+", "pi" },
  204 + { "7", "4", "1", "0", "A" },
  205 + { "8", "5", "2", "+/-", "B" },
  206 + { "9", "6", "3", ".", "C" },
  207 + { "/", "*", "-", "+", "D" },
  208 + { "Mod", "Or", "Lsh", "=", "E" },
  209 + { "And", "Xor", "Not", "Int", "F" } };
  210 +
  211 + }
  212 +
  213 + private void draw_frame()
  214 + {
  215 + fr = new Frame();
  216 + fr.setLayout(new BorderLayout());
  217 + fr.setSize(750, 380);
  218 + fr.setResizable(false);
  219 + fr.setVisible(true);
  220 + fr.addWindowListener(this);
  221 + fr.setBackground(new Color(238, 238, 238));
  222 + fr.setTitle("Calculator");
  223 + }
  224 +
  225 + private void draw_panel_Lev_1()
  226 + // draw the panel on level one. Split the frame into two parts, upper is
  227 + // pan_red, down is pan_blue.
  228 + {
  229 + pan_Red = new Panel();
  230 + pan_Blue = new Panel();
  231 + fr.add("North", pan_Red);
  232 + fr.add("Center", pan_Blue);
  233 + pan_Yell_up = new Panel();
  234 + pan_Yell_down = new Panel();
  235 + pan_Red.setLayout(new BorderLayout());
  236 + pan_Red.add("North", pan_Yell_up);
  237 + pan_Red.add("South", pan_Yell_down);
  238 + }
  239 +
  240 + private void draw_blue_upper()
  241 + {
  242 + Panel blue_center_up;
  243 + pan_Blue_center.setLayout(new BorderLayout());
  244 + blue_center_up = new Panel();
  245 + pan_Blue_center.add("North", blue_center_up);
  246 + Panel pan_blue_center_up_left = new Panel();
  247 + blue_center_up.setLayout(new BorderLayout());
  248 + blue_center_up.add("West", pan_blue_center_up_left);
  249 + pan_blue_center_up_left.add(new Label(" "));
  250 + cb_Inv = new Checkbox("Inv");
  251 + pan_blue_center_up_left.add(cb_Inv);
  252 + pan_blue_center_up_left.add(new Label(" "));
  253 + cb_Hyp = new Checkbox("Hyp");
  254 + pan_blue_center_up_left.add(cb_Hyp);
  255 + pan_blue_center_up_left.add(new Label(" "));
  256 + tf_Mem = new TextField(2);
  257 + tf_Mem.setEnabled(false);
  258 + pan_blue_center_up_left.add(tf_Mem);
  259 + tf_Blanket = new TextField(2);
  260 + tf_Blanket.setEnabled(false);
  261 + pan_blue_center_up_left.add(tf_Blanket);
  262 + pan_blue_center_up_left
  263 + .add(new Label(" "));
  264 + bt_Bksp = new Button(" Backspace ");
  265 + bt_C = new Button(" C ");
  266 + bt_Ce = new Button(" CE ");
  267 + bt_Bksp.setSize(30, 30);
  268 + pan_blue_center_up_left.add(bt_Bksp);
  269 + pan_blue_center_up_left.add(bt_C);
  270 + pan_blue_center_up_left.add(bt_Ce);
  271 + }
  272 +
  273 + private void draw_Button()
  274 + {
  275 + bt_oper = new Button[11][5];
  276 + pan_Blue_down = new Panel();
  277 + pan_Blue_center.add("Center", pan_Blue_down);
  278 + pan_Blue_down.setLayout(new GridLayout(5, 11, 5, 5));
  279 + for (int t = 0; t < 5; t++)
  280 + for (int j = 0; j < 11; j++) {
  281 + if (j == 5) // set separator of two Buttons such
  282 + // as between MC and 7
  283 + pan_Blue_down.add(new Label(" "));
  284 + bt_oper[j][t] = new Button(operator[j][t]);
  285 + bt_oper[j][t].addActionListener(this);
  286 + pan_Blue_down.add(bt_oper[j][t]);
  287 + }
  288 + }
  289 +
  290 + private void draw_panel_blue()
  291 + {
  292 + pan_Blue.setLayout(new BorderLayout());
  293 + pan_Blue.add("West", new Panel());
  294 + pan_Blue.add("East", new Panel());
  295 + pan_Blue.add("North", new Panel());
  296 + pan_Blue.add("South", new Panel());
  297 + pan_Blue_center = new Panel();
  298 + pan_Blue.add("Center", pan_Blue_center);
  299 + }
  300 +
  301 + private void draw_panel_yellow()
  302 + {
  303 + tf_Result = new TextField(80);
  304 + tf_Result.setEnabled(false);
  305 + pan_Yell_up.add(tf_Result);
  306 + pan_Down_left = new Panel();
  307 + pan_Down_right = new Panel();
  308 + pan_Yell_down.setLayout(new BorderLayout());
  309 + pan_Yell_down.add("West", pan_Down_left);
  310 + pan_Yell_down.add("East", pan_Down_right);
  311 + draw_check();
  312 + }
  313 +
  314 + public calc_box()
  315 + // default ctor, draw the window
  316 + {
  317 + init_opera();
  318 + draw_frame();
  319 + draw_menu();
  320 + draw_panel_Lev_1();
  321 + draw_panel_yellow();
  322 + draw_panel_blue();
  323 + draw_blue_upper();
  324 + draw_Button();
  325 + addListener();
  326 + fr.validate();
  327 + EnableButton(10);
  328 + }
  329 +
  330 + private void EnableButton(int n)
  331 + {
  332 + if (n == 10) {
  333 + for (int i = 5; i < 8; i++)
  334 + for (int j = 0; j < 4; j++)
  335 + bt_oper[i][j].setEnabled(true);
  336 + for (int i = 5; i < 10; i++)
  337 + bt_oper[i][4].setEnabled(false);
  338 + bt_oper[10][4].setEnabled(false);
  339 + }
  340 + if (n == 16) {
  341 + for (int i = 5; i < 8; i++)
  342 + for (int j = 0; j < 5; j++)
  343 + bt_oper[i][j].setEnabled(true);
  344 + for (int i = 5; i < 11; i++) {
  345 + bt_oper[i][4].setEnabled(true);
  346 + }
  347 + }
  348 + if (n == 8) {
  349 + EnableButton(10);
  350 + bt_oper[6][0].setEnabled(false);
  351 + bt_oper[7][0].setEnabled(false);
  352 +
  353 + }
  354 + if (n == 2) {
  355 + for (int i = 5; i < 8; i++)
  356 + for (int j = 0; j < 5; j++)
  357 + bt_oper[i][j].setEnabled(false);
  358 + bt_oper[5][2].setEnabled(true);
  359 + bt_oper[5][3].setEnabled(true);
  360 + }
  361 +
  362 + }
  363 +
  364 + private void addListener()
  365 + {
  366 + cb_Degree.addItemListener(this);
  367 + cb_Grads.addItemListener(this);
  368 + cb_Hyp.addItemListener(this);
  369 + cb_Inv.addItemListener(this);
  370 + cb_Radian.addItemListener(this);
  371 + cb_Radio_10.addItemListener(this);
  372 + cb_Radio_16.addItemListener(this);
  373 + cb_Radio_2.addItemListener(this);
  374 + cb_Radio_8.addItemListener(this);
  375 + bt_Bksp.addActionListener(this);
  376 + bt_C.addActionListener(this);
  377 + bt_Ce.addActionListener(this);
  378 + }
  379 +
  380 + private void InitData()
  381 + {
  382 + operand_2 = tmp;
  383 + tmp = 0;
  384 + strOper_2 = new String(tmpString);
  385 + decimal = 0;
  386 + edited = false;
  387 + doted = false;
  388 + }
  389 +
  390 + private double InputNum(int n)
  391 + // This function will deal with the tmpString variable
  392 + {
  393 + if (edited == false && doted == false) {
  394 + tmp = tmp * 10;
  395 + tmp = tmp + n;
  396 + tmpString = new String(Integer.toString((int) tmp));
  397 + }
  398 + else if (edited == true) {
  399 + InitData();
  400 + InputNum(n);
  401 + }
  402 +
  403 + if (edited == false && doted == true) {
  404 + decimal++;
  405 + tmp = tmp + n * pow((0.1), decimal);
  406 + tmpString = new String(Double.toString(tmp));
  407 + }
  408 + else if (edited == true) {
  409 + InitData();
  410 + InputNum(n);
  411 + }
  412 + return tmp;
  413 + }
  414 +
  415 + private double pow(double d, int decimal2)
  416 + {
  417 + double ret = 1;
  418 + for (int i = 0; i < decimal2; i++) {
  419 + ret = ret * d;
  420 + }
  421 + return ret;
  422 + }
  423 +
  424 + private void DoBpsk()
  425 + {
  426 + tmp = tmp / 10;
  427 + if (doted == false)
  428 + tmpString = new String(Integer.toString((int) tmp));
  429 + else tmpString = new String(Double.toString(tmp));
  430 + }
  431 +
  432 + @Override
  433 + public void windowClosing(WindowEvent e)
  434 + {
  435 + System.exit(0);
  436 + }
  437 +
  438 + @Override
  439 + public void windowOpened(WindowEvent e)
  440 + {
  441 + /* do nothing */
  442 + }
  443 +
  444 + @Override
  445 + public void windowClosed(WindowEvent e)
  446 + {
  447 + /* do nothing */
  448 + }
  449 +
  450 + @Override
  451 + public void windowIconified(WindowEvent e)
  452 + {
  453 + /* do nothing */
  454 + }
  455 +
  456 + @Override
  457 + public void windowDeiconified(WindowEvent e)
  458 + {
  459 + /* do nothing */
  460 + }
  461 +
  462 + @Override
  463 + public void windowActivated(WindowEvent e)
  464 + {
  465 + /* do nothing */
  466 + }
  467 +
  468 + @Override
  469 + public void windowDeactivated(WindowEvent e)
  470 + {
  471 + /* do nothing */
  472 + }
  473 +
  474 + private void actionPer_GetNum(ActionEvent e)
  475 + {
  476 + for (int i = 0; i < 11; i++)
  477 + for (int j = 0; j < 5; j++) {
  478 + if (e.getSource() == bt_oper[i][j]) {
  479 + switch (operator[i][j].charAt(0)) {
  480 + case '0':
  481 + InputNum(0);
  482 + break;
  483 + case '1':
  484 + InputNum(1);
  485 + break;
  486 + case '2':
  487 + InputNum(2);
  488 + break;
  489 + case '3':
  490 + InputNum(3);
  491 + break;
  492 + case '4':
  493 + InputNum(4);
  494 + break;
  495 + case '5':
  496 + InputNum(5);
  497 + break;
  498 + case '6':
  499 + InputNum(6);
  500 + break;
  501 + case '7':
  502 + InputNum(7);
  503 + break;
  504 + case '8':
  505 + InputNum(8);
  506 + break;
  507 + case '9':
  508 + InputNum(9);
  509 + break;
  510 + }
  511 + OutputStr(tmpString);
  512 + break;
  513 + }
  514 + }
  515 + }
  516 +
  517 + public void actionPer_Calc(ActionEvent e)
  518 + {
  519 + if (e.getSource() == bt_oper[8][1]) {
  520 + // the *
  521 + formOperator = '*';
  522 + if (strOper_2 == null)
  523 + InitData();
  524 + else {
  525 + operand_1 = tmp;
  526 + strOper_1 = new String(
  527 + Double.toString(operand_1));
  528 + do_calc('*');
  529 + operand_1 = 1;
  530 + }
  531 + }
  532 + if (e.getSource() == bt_oper[8][0]) {
  533 + // the /
  534 + formOperator = '/';
  535 + if (strOper_2 == null)
  536 + InitData();
  537 + else {
  538 + operand_1 = tmp;
  539 + strOper_1 = new String(
  540 + Double.toString(operand_1));
  541 + do_calc('/');
  542 + operand_1 = 1;
  543 + }
  544 + }
  545 + if (e.getSource() == bt_oper[8][3]) {
  546 + // the +
  547 + formOperator = '+';
  548 + if (strOper_2 == null)
  549 + InitData();
  550 + else {
  551 + operand_1 = tmp;
  552 + strOper_1 = new String(
  553 + Double.toString(operand_1));
  554 + do_calc('+');
  555 + operand_1 = 0;
  556 + }
  557 + }
  558 + if (e.getSource() == bt_oper[8][2]) {
  559 + // the -
  560 + formOperator = '-';
  561 + if (strOper_2 == null)
  562 + InitData();
  563 + else {
  564 + operand_1 = tmp;
  565 + strOper_1 = new String(
  566 + Double.toString(operand_1));
  567 + do_calc('-');
  568 + operand_1 = 0;
  569 + }
  570 + }
  571 +
  572 + if (e.getSource() == bt_oper[9][3]) {
  573 + // the =
  574 + if (strOper_2 == null)
  575 + InitData();
  576 + else {
  577 + operand_1 = tmp;
  578 + strOper_1 = new String(
  579 + Double.toString(operand_1));
  580 + do_calc(formOperator);
  581 + strOper_2 = null;
  582 + }
  583 +
  584 + }
  585 +
  586 + }
  587 +
  588 + public void actionPerformed(ActionEvent e)
  589 + {
  590 + if (e.getSource() == bt_oper[7][3]) {
  591 + // the dot
  592 + // make sure whether the dot Button had been pressed.
  593 + // this should has a higher response level than input a
  594 + // number.
  595 + doted = true;
  596 + }
  597 + actionPer_GetNum(e);
  598 + if (e.getSource() == bt_C) {
  599 + InitAll();
  600 + OutputStr("");
  601 + }
  602 + if (e.getSource() == bt_Bksp) {
  603 + DoBpsk();
  604 + OutputStr(tmpString);
  605 + }
  606 + if (e.getSource() == bt_Ce) {
  607 + InitAll();
  608 + OutputStr("");
  609 + }
  610 + actionPer_Calc(e);
  611 +
  612 + }
  613 +
  614 + private void do_calc(int op)
  615 + {
  616 + if (op == '+') {
  617 + result = operand_1 + operand_2;
  618 + resString = new String(Double.toString(result));
  619 + operand_2 = result;
  620 + strOper_2 = new String(Double.toString(result));
  621 + }
  622 + if (op == '-') {
  623 + result = operand_1 - operand_2;
  624 + resString = new String(Double.toString(result));
  625 + operand_2 = result;
  626 + strOper_2 = new String(Double.toString(result));
  627 + }
  628 + if (op == '*') {
  629 + result = operand_1 * operand_2;
  630 + resString = new String(Double.toString(result));
  631 + operand_2 = result;
  632 + strOper_2 = new String(Double.toString(result));
  633 + }
  634 + if (op == '/') {
  635 + result = operand_1 / operand_2;
  636 + resString = new String(Double.toString(result));
  637 + operand_2 = result;
  638 + strOper_2 = new String(Double.toString(result));
  639 + }
  640 + OutputStr(resString);
  641 +
  642 + }
  643 +
  644 + private void InitAll()
  645 + {
  646 + operand_1 = 0;
  647 + operand_2 = 0;
  648 + result = 0;
  649 + tmp = 0;
  650 + strOper_1 = null;
  651 + strOper_2 = null;
  652 + tmpString = null;
  653 + resString = null;
  654 + edited = false;
  655 + doted = false;
  656 + decimal = 0;
  657 + }
  658 +
  659 + @Override
  660 + public void itemStateChanged(ItemEvent e)
  661 + {
  662 + if (e.getSource() == cb_Degree) {
  663 + AngleState = DEGREE;
  664 + }
  665 + else if (e.getSource() == cb_Grads) {
  666 + AngleState = GRADS;
  667 + }
  668 + else if (e.getSource() == cb_Radian) {
  669 + AngleState = RADIAN;
  670 + }
  671 + else if (e.getSource() == cb_Inv) {
  672 + bInv = true;
  673 + }
  674 + else if (e.getSource() == cb_Hyp) {
  675 + bHyp = true;
  676 + }
  677 + else if (e.getSource() == cb_Radio_10) {
  678 + OutputStr(Double.toString(tmp));
  679 + RadioState = DEC;
  680 + EnableButton(10);
  681 + }
  682 + else if (e.getSource() == cb_Radio_16) {
  683 + OutputStr(Integer.toHexString((int) tmp));
  684 + RadioState = HEX;
  685 + EnableButton(16);
  686 + }
  687 + else if (e.getSource() == cb_Radio_2) {
  688 + OutputStr(Integer.toBinaryString((int) tmp));
  689 + RadioState = BIN;
  690 + EnableButton(2);
  691 + }
  692 + else if (e.getSource() == cb_Radio_8) {
  693 + OutputStr(Integer.toOctalString((int) tmp));
  694 + RadioState = OCT;
  695 + EnableButton(8);
  696 + }
  697 + edited = true;
  698 + }
  699 +}
11 README
... ... @@ -0,0 +1,11 @@
  1 +This is my personal source file back up dir.
  2 +
  3 +
  4 +/C_file is the C language file.
  5 +/Java_file is the Java language.
  6 +README this file.
  7 +
  8 +
  9 +Last modify: Nov. 26 2011
  10 +Athenacle
  11 +zjjhwxc@gmail.com

0 comments on commit ab7c422

Please sign in to comment.
Something went wrong with that request. Please try again.