package core;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
public class CalculatorByGui {
protected Shell shell;
private Text text;
private Text text_1;
private Text text_2;
public static void main(String[] args) {
try {
CalculatorByGui window = new CalculatorByGui();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
return;
}
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return;
}
protected void createContents() {
shell = new Shell();
shell.setSize(800, 400);
shell.setText("Calculator By Gui");
text = new Text(shell, SWT.CENTER);
text.setBounds(60, 60, 420, 30);
text.setTextLimit(22);
text.setVisible(true);
text.setOrientation(SWT.LEFT_TO_RIGHT);
text_1 = new Text(shell, SWT.CENTER);
text_1.setBounds(60, 160, 420, 30);
text_1.setTextLimit(22);
text_1.setVisible(true);
text_1.setOrientation(SWT.LEFT_TO_RIGHT);
text_2 = new Text(shell, SWT.CENTER);
text_2.setBounds(60, 260, 420, 30);
text_1.setVisible(true);
text_1.setOrientation(SWT.LEFT_TO_RIGHT);
Button btnPlus = new Button(shell, SWT.CENTER);
btnPlus.setBounds(570, 60, 120, 30);
btnPlus.setText("plus");
btnPlus.addSelectionListener((SelectionListener) new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
text.selectAll();
DynamicArrayFloat a = new DynamicArrayFloat();
a.init(text.getSelectionText());
text_1.selectAll();
DynamicArrayFloat b = new DynamicArrayFloat();
b.init(text_1.getSelectionText());
DynamicArrayFloat c = DynamicArrayFloat.plus(a, b);
text_2.setText("");
text_2.append(c.getString());
}
});
Button btnMinus = new Button(shell, SWT.CENTER);
btnMinus.setBounds(570, 125, 120, 30);
btnMinus.setText("minus");
btnMinus.addSelectionListener((SelectionListener) new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
text.selectAll();
DynamicArrayFloat a = new DynamicArrayFloat();
a.init(text.getSelectionText());
text_1.selectAll();
DynamicArrayFloat b = new DynamicArrayFloat();
b.init(text_1.getSelectionText());
DynamicArrayFloat c = DynamicArrayFloat.minus(a, b);
text_2.setText("");
text_2.append(c.getString());
}
});
Button btnTimes = new Button(shell, SWT.CENTER);
btnTimes.setBounds(570, 190, 120, 30);
btnTimes.setText("times");
btnTimes.addSelectionListener((SelectionListener) new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
text.selectAll();
DynamicArrayFloat a = new DynamicArrayFloat();
a.init(text.getSelectionText());
text_1.selectAll();
DynamicArrayFloat b = new DynamicArrayFloat();
b.init(text_1.getSelectionText());
DynamicArrayFloat c = DynamicArrayFloat.times(a, b);
text_2.setText("");
text_2.append(c.getString());
}
});
Button btnDividedBy = new Button(shell, SWT.CENTER);
btnDividedBy.setBounds(570, 255, 120, 30);
btnDividedBy.setText("divided by");
btnDividedBy.addSelectionListener((SelectionListener) new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
text.selectAll();
DynamicArrayFloat a = new DynamicArrayFloat();
a.init(text.getSelectionText());
text_1.selectAll();
DynamicArrayFloat b = new DynamicArrayFloat();
b.init(text_1.getSelectionText());
DynamicArrayFloat c = DynamicArrayFloat.dividedBy(a, b);
text_2.setText("");
text_2.append(c.getString());
}
});
Label lblMeansZero = new Label(shell, SWT.CENTER);
lblMeansZero.setBounds(60, 300, 520, 30);
lblMeansZero.setText("\"0\" means zero itself, but \"-0\" means nothing can be divided by zero.");
return;
}
}
package core;
public class DynamicArrayFloat {
//Data:
private char[] majorData;
private int majorSize = 0;//size <= data.length;
private char[] minorData;
private int minorSize = 1;//minorData[0] = '.';
private boolean pn = true;//true means positive(+), false means negative(-);
private static final int NUM = 8;
//Constructor:
public DynamicArrayFloat() {
this(NUM, NUM);
return;
}
public DynamicArrayFloat(int majorNum) {//MAJOR!!!
this(majorNum, NUM);
return;
}
public DynamicArrayFloat(int majorNum, int minorNum) {
int tempNum;
tempNum = (majorNum > NUM) ? majorNum : NUM;
majorData = new char[tempNum];
tempNum = (minorNum > NUM) ? minorNum : NUM;
minorData = new char[tempNum];
minorData[0] = '.';
return;
}
public DynamicArrayFloat(DynamicArrayFloat another) {
majorData = new char[another.getMajorLength()];
majorSize = another.getMajorSize();
for(int i = 0; i < majorSize; i++)
majorData[i] = another.getMajorData(i);
minorData = new char[another.getMinorLength()];
minorSize = another.getMinorSize();
for(int i = minorSize - 1; i >= 0; i--)
minorData[i] = another.getMinorData(i);
pn = another.getPn();
return;
}
//Data-getter:
public int getMajorLength() {
return majorData.length;
}
public int getMinorLength() {
return minorData.length;
}
public int getMajorSize() {
return majorSize;
}
public int getMinorSize() {
return minorSize;
}
public char getMajorData(int n) {
if(n < 0)
return '?';
else
if(n >= majorSize)
return '0';
return majorData[n];
}
public char getMinorData(int n) {
if(n < 0)
return '?';
else
if(n >= minorSize)
return '0';
return minorData[n];
}
public boolean getPn() {
return pn;
}
//Support-function:
private void changePn() {
pn = (true == pn) ? false : true;
return;
}
private void setPn(boolean pn) {
this.pn = pn;
return;
}
private void majorCapacity(int c) {
int nowC = majorData.length;
if(nowC >= c)
return;
else
while(nowC < c)
nowC *= 2;
char[] newData = new char[nowC];
for(int i = 0; i < majorSize; i++)
newData[i] = majorData[i];
majorData = newData;//the old data[] will be automatically released;
return;
}
private void minorCapacity(int c) {
int nowC = minorData.length;
if(nowC >= c)
return;
else
while(nowC < c)
nowC *= 2;
char[] newData = new char[nowC];
for(int i = minorSize - 1; i >= 0; i--)
newData[i] = minorData[i];
minorData = newData;//the old data[] will be automatically released;
return;
}
private void insertMajorData(char d) {
if(d < '0' || d > '9')
{
System.out.println("d must from '0' to '9'");
return;
}
majorCapacity(majorSize + 1);
for(int i = majorSize - 1; i >= 0; i--)
majorData[i + 1] = majorData[i];
majorData[0] = d;
majorSize++;
return;
}
private void insertMinorData(char d) {
if(d < '0' || d > '9')
{
System.out.println("d must from '0' to '9'");
return;
}
minorCapacity(minorSize + 1);
minorData[minorSize] = d;
minorSize++;
return;
}
private void setMajorData(int n, char d) {
if(n < 0)
{
System.out.println("major n must >= 0");
return;
}
if(d < '0' || d > '9')
{
System.out.println("d must from '0' to '9'");
return;
}
if(n >= majorSize)//n >= size, may need to extend capacity and even need to add '0';
{
majorCapacity(n + 1);//n >= size;
for(int i = majorSize; i < n; i++)
{
majorData[i] = '0';//it is a char;
majorSize++;
}
majorSize++;//for the data[n];
}
majorData[n] = d;
return;
}
private void setMinorData(int n, char d) {
if(n < 1)
{
System.out.println("minor n must >= 1");
return;
}
if(d < '0' || d > '9')
{
System.out.println("d must from '0' to '9'");
return;
}
if(n >= minorSize)//n >= size, may need to extend capacity and even need to add '0';
{
minorCapacity(n + 1);//n >= size;
for(int i = minorSize; i < n; i++)
{
minorData[i] = '0';//it is a char;
minorSize++;
}
minorSize++;//for the data[n];
}
minorData[n] = d;
return;
}
private static int ctoi(char c) {
return (int)(c - '0');
}
private static char itoc(int i) {
return (char)(i + '0');
}
private void addMajor(int n, int addData) {
if(n < 0 || n >= majorSize)
{
setMajorData(n, itoc(addData));
return;
}
for(int i = n, temp; i < majorSize & addData != 0; i++)
{
temp = addData + ctoi(majorData[i]);
setMajorData(i, itoc(temp % 10));
addData = temp /10;
}
if(addData != 0)//it needs to be set in majorData[size];
setMajorData(majorSize, itoc(addData));
return;
}
private void addMinor(int n, int addData) {
if(n < 1 || n >= minorSize)
{
setMinorData(n, itoc(addData));
return;
}
for(int i = n, temp; i > 0 & addData != 0; i--)
{
temp = addData + ctoi(minorData[i]);
setMinorData(i, itoc(temp % 10));
addData = temp /10;
}
if(addData != 0)//now (-)0 == i, means minorData[0], '.', so it needs to be added in majorData[0];
addMajor(0, addData);
return;
}
private static boolean bigger(DynamicArrayFloat a, DynamicArrayFloat b) {
if(a.getMajorSize() > b.getMajorSize())
return true;
else
if(a.getMajorSize() < b.getMajorSize())
return false;
else//major ==;
{
for(int i = a.getMajorSize() - 1; i >= 0; i--)
if(a.getMajorData(i) > b.getMajorData(i))
return true;
else
if(a.getMajorData(i) < b.getMajorData(i))
return false;
else//still ==;
;
int maxMinor = (a.getMinorSize() >= b.getMinorSize()) ? a.getMinorSize() : b.getMinorSize();
for(int i = 1; i < maxMinor; i++)
if(a.getMinorData(i) > b.getMinorData(i))
return true;
else
if(a.getMinorData(i) < b.getMinorData(i))
return false;
else//still ==;
;
}
return true;//totally equal;
}
private void clearExtraZero() {
for(int i = majorSize - 1; i > 0 && '0' == majorData[i]; i--)
majorSize--;
for(int i = minorSize - 1; i > 0 && '0' == minorData[i]; i--)
minorSize--;
return;
}
//Practical-function:
public void init(String s) {
int i = 0;
if('-' == s.charAt(i))
{
changePn();
i++;
}
while(i < s.length() && s.charAt(i) != '.')
{
insertMajorData(s.charAt(i));
i++;
}
if(i < s.length() && '.' == s.charAt(i))
for(i++; i < s.length(); i++)//initial i will be ^-1, not ^-0, '.';
insertMinorData(s.charAt(i));
return;
}
public String getString() {
char[] temp = new char[majorSize + minorSize + 1];
int j = 0;
if(false == pn)
{
temp[0] = '-';
j++;
}
for(int i = majorSize - 1; i >= 0; i--)
{
temp[j] = majorData[i];
j++;
}
if(0 == majorSize)
{
temp[j] = '0';
j++;
}
if(minorSize > 1)
{
temp[j] = '.';
j++;
}
for(int i = 1; i < minorSize; i++)
{
temp[j] = minorData[i];
j++;
}
String s = String.valueOf(temp);
return s;
}
public static DynamicArrayFloat plus(DynamicArrayFloat a, DynamicArrayFloat b) {
DynamicArrayFloat c = new DynamicArrayFloat();
DynamicArrayFloat tempA = bigger(a, b) ? new DynamicArrayFloat(a) : new DynamicArrayFloat(b);
DynamicArrayFloat tempB = bigger(a, b) ? new DynamicArrayFloat(b) : new DynamicArrayFloat(a);
c.pn = tempA.getPn();
int maxMinor = (a.getMinorSize() >= b.getMinorSize()) ? a.getMinorSize() : b.getMinorSize();
for(int i = - (maxMinor - 1); i < tempA.getMajorSize(); i++)
{
int ta, tb, pa, temp;
if(i < 0)//i < 0, minor;
{
ta = ctoi(tempA.getMinorData(- i));
tb = ctoi(tempB.getMinorData(- i));
}
else//i >= 0, major;
{
ta = ctoi(tempA.getMajorData(i));
tb = ctoi(tempB.getMajorData(i));
}
if(a.getPn() == b.getPn())//both + or both -;
{
temp = ta + tb;//+;
if(i < -1)//i <= -2, both minor;
{
c.addMinor(- i, temp % 10);
tempA.addMinor(- i - 1, temp / 10);
}
else
if(-1 == i)//one minor, one major;
{
c.addMinor(- i, temp % 10);
tempA.addMajor(0, temp / 10);
}
else//i >= 0, both major;
{
c.addMajor(i, temp % 10);
if(temp / 10 != 0)
tempA.addMajor(i + 1, temp / 10);
}
}
else//one +, one -;
{
boolean need = true;
temp = ta - tb;//-;
if(i < 0)//minor[- i];
{
if(temp >= 0)
c.addMinor(- i, temp);
else
{
c.addMinor(- i, temp + 10);//temp < 0;
int j;
for(j = i + 1; j < 0 && true == need; j++)
{
pa = ctoi(tempA.getMinorData(- j));//minor[- j];
if(pa >= 1)
{
tempA.addMinor(- j, -1);
need = false;
}
else//0 == pa;
tempA.addMinor(- j, 9);//(-1) + 10;
}
while(true == need && j < tempA.getMajorSize())
{
pa = ctoi(tempA.getMajorData(j));//major[j];
if(pa >= 1)
{
tempA.addMajor(j, -1);
need = false;
}
else//0 == pa;
tempA.addMajor(j, 9);
j++;
}//j must < maxMajor, finally false == need, if bigger(a, b) is right;
}
}
else//major[i];
{
if(temp >= 0)
c.addMajor(i, temp);
else
{
c.addMajor(i, temp + 10);//temp < 0;
for(int j = i + 1; j < tempA.getMajorSize() && true == need; j++)
{
pa = ctoi(tempA.getMajorData(j));
if(pa >= 1)
{
tempA.addMajor(j, -1);
need = false;
}
else//0 == pa;
tempA.addMajor(j, 9);
}//j must < maxMajor, finally false == need, if bigger(a, b) is right;
}
}
}
}
c.clearExtraZero();
return c;
}
public static DynamicArrayFloat minus(DynamicArrayFloat a, DynamicArrayFloat b) {
DynamicArrayFloat temp = new DynamicArrayFloat(b);
temp.changePn();//- x == + (- x);
return plus(a, temp);
}
public static DynamicArrayFloat times(DynamicArrayFloat a, DynamicArrayFloat b) {
DynamicArrayFloat c = new DynamicArrayFloat();
int ta, tb, temp, n;
for(int i = - (a.getMinorSize() - 1); i < a.getMajorSize(); i++)
for(int j = - (b.getMinorSize() - 1); j < b.getMajorSize(); j++)
{
if(i < 0)//minor[- i];
ta = ctoi(a.getMinorData(- i));
else//i >= 0, major[i];
ta = ctoi(a.getMajorData(i));
if(j < 0)//minor[- j];
tb = ctoi(b.getMinorData(- j));
else//j >= 0, major[j];
tb = ctoi(b.getMajorData(j));
temp = ta * tb;
n = i + j;
if(n < -1)//n <= -2, both minor;
{
c.addMinor(- n, temp % 10);
c.addMinor(- (n + 1), temp / 10);
}
else
if(-1 == n)//one minor, one major;
{
c.addMinor(- n, temp % 10);
c.addMajor(0, temp / 10);
}
else//n >= 0, both major;
{
c.addMajor(n, temp % 10);
if(temp / 10 != 0)
c.addMajor(n + 1, temp / 10);
}
}
if(a.getPn() != b.getPn())
c.changePn();
c.clearExtraZero();
return c;
}
public static DynamicArrayFloat dividedBy(DynamicArrayFloat a, DynamicArrayFloat b) {
DynamicArrayFloat zero = new DynamicArrayFloat();
zero.init("0");
DynamicArrayFloat _zero = new DynamicArrayFloat();
_zero.init("-0");
DynamicArrayFloat c = new DynamicArrayFloat();
if((bigger(b, zero) == bigger(zero, b)) || (bigger(b, _zero) == bigger(_zero, b)))//can't be divided by zero;
{
c.init("-0");
return c;
}
if((bigger(a, zero) == bigger(zero, a)) || (bigger(a, _zero) == bigger(_zero, a)))//zero divided by anything will still be zero;
{
c.init("0");
return c;
}
c.init("1");
DynamicArrayFloat r = new DynamicArrayFloat(a);
r.setPn(true);
DynamicArrayFloat t = new DynamicArrayFloat(b);
t.setPn(true);
DynamicArrayFloat moveLeft = new DynamicArrayFloat();
moveLeft.init("10");
DynamicArrayFloat moveRight = new DynamicArrayFloat();
moveRight.init("0.1");
while(r.getMajorSize() > t.getMajorSize())
{
t = times(t, moveLeft);
c = times(c, moveLeft);
}
while(r.getMajorSize() < t.getMajorSize())
{
t = times(t, moveRight);
c = times(c, moveRight);
}
while(r.getMinorSize() > 1 || t.getMinorSize() > 1)//then, both will have nothing after '.';
{
r = times(r, moveLeft);
t = times(t, moveLeft);
}
DynamicArrayFloat one = new DynamicArrayFloat(c);
if(bigger(r, t))//actually r >= t, the highest is unique, initial c is 1 * 10 ^ ?;
r = minus(r, t);
else//r < t;
{
c = minus(c, one);
t = times(t, moveRight);
one = times(one, moveRight);
}
while(c.getMajorSize() + c.getMinorSize() - 1 < 44 && (bigger(r, zero) != bigger(zero, r)))//length limit not including '-' and '.';
{
while(bigger(r, t))
{
r = minus(r, t);
c = plus(c, one);
}
t = times(t, moveRight);
one = times(one, moveRight);
}
if(a.getPn() != b.getPn())
c.changePn();
c.clearExtraZero();
return c;
}
}
The above is an independent creation completely from the NUC programming god, all rights reserved, and the infringer must be sanctioned by the tiger security.