package javahomework;
import javax.swing.JOptionPane;
class postfix {
private char stack[], z[];
private int pri[] = { 0, 1, 1, 2, 2, 3 };
private char oper[] = { '(', '+', '-', '*', '/', '^' };
private int top, n, a;
private String postfix = "";
private char x[], y;
public postfix() {
n = 50;
top = 0;
a = 0;
stack = new char[n];
z = new char[n];
}
public String infix_to_postfix(String infix) {
int i = 0, j = infix.length();
x = infix.toCharArray();
for (i = 0; i < j; i++) {
switch (x[i]) {
case '(':
push(x[i]);
break;
case ')':
y = pop();
while (!empty() && y != '(') {
z[a++] = y;
y = pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '^':
y = top();
while (pre(y) >= pre(x[i])) {
z[a++] = pop();
y = top();
}
push(x[i]);
break;
default:
z[a++] = x[i];
}
}
while (!empty()) {
z[a++] = pop();
}
for (i = 0; i <= a - 2; i++) {
postfix += z[i];
postfix += " ";
}
return postfix;
}
private boolean empty() {
return (top < 0) ? true : false;
}
private boolean full() {
return (top >= n - 1) ? true : false;
}
private void push(char sta) {
stack[++top] = sta;
}
private char pop() {
return stack[top--];
}
private char top() {
return stack[top];
}
private int pre(char op) {
for (int i = 0; i < 6; i++)
if (oper[i] == op)
return pri[i];
return -1;
}
private void check(char x, char y) {
y = top();
while (pre(y) >= pre(x)) {
postfix += pop();
y = top();
}
push(x);
}
}
public class HW18 {
public static void main(String arg[]) {
String input = JOptionPane.showInputDialog(null,
"Please input string (ex.1+2-3)");
postfix p = new postfix();
JOptionPane.showMessageDialog(null,
String.valueOf(p.infix_to_postfix(input)));
}
}
2008年8月30日 星期六
中置轉成後置
code
2008年8月29日 星期五
簡易計算機
只計算一次 A+B 或 A-B
code
code
package javahomework;
import javax.swing.JOptionPane;
public class HW17 {
static int toNumber(char array[], int begin, int end) {
String temp = "";
for (int i = begin; i < end; i++)
temp += array[i];
return Integer.parseInt(temp);
}
static long sum_number(String input) {
int index = 0;
long sum = 0;
char a[] = input.toCharArray();
for (int i = 0; i < a.length; i++) {
switch (a[i]) {
case '+': // A+B
sum += toNumber(a, 0, i) + toNumber(a, i + 1, a.length);
break;
case '-': // A-B
sum += toNumber(a, 0, i) - toNumber(a, i + 1, a.length);
break;
}
index++;
}
return sum;
}
public static void main(String args[]) {
String input = JOptionPane.showInputDialog(null,
"Please input 1+2 or 1-2");
JOptionPane.showMessageDialog(null, String.valueOf(sum_number(input)));
}
}
字串大小寫轉換
code
package javahomework;
import javax.swing.JOptionPane;
import java.util.StringTokenizer;
public class HW16 {
public static void main(String args[]) {
String input = JOptionPane.showInputDialog(null, "");
StringTokenizer st = new StringTokenizer(input);
String out = "";
while (st.hasMoreTokens()) {
String tmp = st.nextToken();
StringBuffer buf1 = new StringBuffer(tmp);
StringBuffer buf2 = new StringBuffer(tmp);
out = out + buf1.toString().toLowerCase()
+ buf2.toString().toUpperCase() + " ";
}
JOptionPane.showMessageDialog(null, out);
}
}
定義一個點->圓->圓錐體->雙圓錐體
code
package javahomework;
public class HW15 {
class Point {
private double x, y;
public Point() {
x = 1.0;
y = 1.0;
}
public Point(double n1, double n2) {
x = n1;
y = n2;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
class Circle{
private double r;
private Point p;
public double pi = 3.1415926;
Circle() {
r = 1.0;
}
public double findArea() {
return r * r * pi;
}
public double findlength() {
return r * pi * 2;
}
public void set(double n,Point p) {
r = n;
}
public double get_r() {
return r;
}
public Point get_Point(){
return p;
}
public void set_Point(Point p){
this.p = p;
}
}
class Cone extends Circle {
private double r = super.get_r();
private double h;
Cone() {
h = 1.0;
}
Cone(double n1, double n2) {
r = n1;
h = n2;
}
public void set(double n1, double n2) {
r = n1;
h = n2;
}
public double getR() {
return r;
}
public double getH() {
return h;
}
public double findVolume() {
return (pi * r * r * h) / 3;
}
public double findFaceArea() {
return (Math.pow((r * r) + (h * h), 0.5)) * 2 * pi;
}
}
class DouleCone extends Cone {
private double r = super.getR();
private double h = super.getH();
public double findDoublejVolume() {
return super.findVolume() * 2;
}
public double findDoubleH() {
return h * 2;
}
public double findDoubleVolume() {
return super.findVolume() * 2;
}
public double findDoubleFaceArea1() {// 漏斗
return super.findFaceArea() * 2;
}
public double findDoubleFaceArea2() {// 兩邊尖
return (super.findFaceArea() * 2) - (r * r * pi) * 2;
}
}
}
定義一個父矩形,子長方體並繼承
code
package javahomework;
public class HW14 {
class Rectangle {
private double length1, length2;
public Rectangle() {
length1 = 1.0;
length2 = 1.0;
}
public Rectangle(double n) {
length1 = n;
length2 = n;
}
public Rectangle(double n1, double n2) {
length1 = n1;
length2 = n2;
}
public void set(double n1, double n2) {
length1 = n1;
length2 = n2;
}
public double getL1() {
return length1;
}
public double getL2() {
return length2;
}
public double findArea() {
return length1 * length2;
}
public double find4Length() {
return (length1 + length2) * 2;
}
}
class AdRectangle extends Rectangle {
private double hight;
AdRectangle() {
hight = 1.0;
}
AdRectangle(double n1, double n2, double n3) {
super(n1, n2);
hight = n3;
}
public void set(double n1, double n2, double n3) {
super.set(n1, n2);
hight = n3;
}
public double findVolume() {
return super.findArea() * hight;
}
public double findArea() {
return super.getL1() * super.getL2();
}
public double findFaceArea() {
return super.findArea() * 2 + super.getL1() * hight * 2
+ super.getL2() * hight * 2;
}
}
}
計算1+1/1+1/2!+1/3!+1/4!
code
package javahomework;
import javax.swing.JOptionPane;
public class HW13 {
static double Count(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * (Count(n - 1));
}
public static void main(String arg[]) {
int input;
float ans = 0;
input = Integer.parseInt(JOptionPane.showInputDialog(null,
"How much do you want to count?"));
for (int i = 0; i < input; i++)
ans += 1 / (Count(i));
ans -= (ans % 0.000001);
JOptionPane.showMessageDialog(null, "Number is " + ans);
}
}
算出PI
注意:要計算很久,會把CPU資源吃光光
code
code
package javahomework;
import javax.swing.JOptionPane;
public class HW12 {
public static void main(String arg[]) {
double p = 0, input = 1e10;
int x = -1;
for (double i = 0; i < input; i++) {
x *= -1;
// 4x(1-1/3+1/5-1/7+1/9...)
p += 4 * ((1 / (2 * i + 1)) * x);
}
p -= p % 0.0000000001;
JOptionPane.showMessageDialog(null, "PI is " + p);
}
}
計算產生第幾個新的類別
技巧:設定變數為static
code
code
package javahomework;
import javax.swing.JOptionPane;
class TestC {
static int count = 0;
public static TestC getInstance() {
TestC x = new TestC();
count++;
return x;
}
public static String get_count() {
return String.valueOf(count);
}
}
public class HW11 {
public static void main(String args[]) {
TestC c1 = TestC.getInstance();
TestC c2 = TestC.getInstance();
TestC c3 = TestC.getInstance();
JOptionPane.showMessageDialog(null, TestC.get_count());
}
}
用類別寫陣列堆疊
code
package javahomework;
import javax.swing.JOptionPane;
class Queue {
int Q[], front, rear, newrear, MaxSize;
String screen = "";
public Queue() {
MaxSize = 5;
Q = new int[MaxSize];
front = rear = -1;
}
public void add(int x) {
if (isfull())
JSM("Queue is full");
else {
Q[rear = newrear] = x;
JSM("Add " + x);
}
}
public void delete() {
if (isempty())
JSM("Queuq is empty");
else
front = (front + 1) % MaxSize;
JSM("Delete " + Q[front]);
}
public boolean isempty() {
if (rear == front)
return true;
else
return false;
}
public boolean isfull() {
newrear = (rear + 1) % MaxSize;
if (front == newrear)
return true;
else
return false;
}
private void JSM(String messeng) {
JOptionPane.showMessageDialog(null, messeng);
}
public void dump() {
if (isempty())
JSM("Queuq is empty");
else
for (int i = 0; i < Q.length; i++)
screen = screen + Q[i] + " ";
JSM(screen);
}
}
public class HW10 {
public static void main(String args[]) {
Queue q1 = new Queue();
q1.add(1);
q1.add(2);
q1.delete();
q1.add(3);
q1.add(4);
q1.delete();
q1.add(5);
q1.add(6);
q1.add(7);
q1.dump();
}
}
利用類別特性定義一個圓錐體,並且可以算出體積、表面積
code
package javahomework;
public class HW09 {
public static void main(String args[]) {
Cone r1 = new Cone();
Cone r2 = new Cone(10.0);
Cone r3 = new Cone(15.0, 20.0);
System.out.println("Volume is " + r1.volume() + " FaceArea is "
+ r1.facearea());
System.out.println("Volume is " + r2.volume() + " FaceArea is "
+ r2.facearea());
System.out.println("Volume is " + r3.volume() + " FaceArea is "
+ r3.facearea());
}
}
class Cone {
double r, h;
double pi = 3.14;
Cone(double a, double b) {
r = a;
h = b;
}
Cone() {
r = 1.0;
h = 1.0;
}
Cone(double a) {
r = a;
h = a;
}
double volume() {
return (pi * r * r * h) / 3;
}
double facearea() {
return (Math.pow((r * r) + (h * h), 0.5)) * 2 * pi;
}
}
利用亂數產生兩個二維陣列A,B計算出C=A*B
code
package javahomework;
public class HW08 {
public static void main(String arg[]) {
int a[][] = new int[2][2];
int b[][] = new int[2][2];
int c[][] = new int[2][2];
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
a[i][j] = (int) (Math.random() * 10);
for (int i = 0; i < b.length; i++)
for (int j = 0; j < b[i].length; j++)
b[i][j] = (int) (Math.random() * 10);
for (int i = 0; i < a.length; i++)
for (int j = 0; j < b[i].length; j++)
for (int k = 0; k < b[j].length; k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[i].length; j++)
System.out.print(c[i][j] + " ");
System.out.print("\n");
}
}
}
輸入一個一級陣列,利用氣泡排序法排序
code
package javahomework;
import javax.swing.JOptionPane;
public class HW07 {
public static void main(String args[]) {
int a[] = new int[10];
String screen = "Sort ";
for (int x = 0; x < a.length; x++)
a[x] = Integer.parseInt(JOptionPane.showInputDialog(null,
"Input Number"));
bubblesort(a);
for (int y = 0; y < a.length; y++)
screen = screen + a[y] + " ";
JOptionPane.showMessageDialog(null, screen);
}
public static void bubblesort(int n[]) {
for (int i = 0; i < n.length - 1; i++)
for (int j = 0; j < n.length - 1; j++)
if (n[j + 1] > n[j]) {
int tmp = n[j];
n[j] = n[j + 1];
n[j + 1] = tmp;
}
}
}
輸入一個數,計算其檢查碼
code
package javahomework;
import javax.swing.JOptionPane;
public class HW06 {
public static void main(String args[]) {
int input;
input = Integer.parseInt(JOptionPane.showInputDialog(null, ""));
JOptionPane.showMessageDialog(null, "Check Number is " + Number(input));
}
static int Number(int n) {
return ((ADD(n) % ADD2(n)) % 10);
}
static int ADD(int n) {
if (n < 10)
return (n * n);
else
return (ADD(n / 10) + ADD(n % 10));
}
static int ADD2(int n) {
if (n < 10)
return n;
else
return (ADD2(n / 10) + ADD2(n % 10));
}
}
找出1~2000中所有的Perfect number
Perfect number 是指某一個數,其因數相加會等於其數
code
code
package javahomework;
import javax.swing.JOptionPane;
public class HW05 {
public static void main(String args[]) {
String Screen = "The PerfectNumber is ";
for (int j = 4; j < 2000; j++) {
if (AD(j) == j)
Screen = Screen + j + ",";
}
JOptionPane.showMessageDialog(null, Screen);
}
static int AD(int n) {
int ans = 1;
for (int i = 2; i < n; i++) {
if (n % i == 0)
ans = ans + i;
}
return ans;
}
}
輸入一個數,算出所有數的總和
把輸入的數字一直除以10,剩下10以下的餘數再相加即可
code
code
package javahomework;
import javax.swing.JOptionPane;
public class HW04 {
public static void main(String args[])
{
int input;
input = Integer.parseInt(JOptionPane.showInputDialog(null,"Input Number"));
JOptionPane.showMessageDialog(null,"The sum of "+input+" is "+ADD(input));
}
static int ADD(int n)
{
if (n<10)
return n;
else
return (ADD(n/10)+ADD(n%10));
}
}
2008年8月19日 星期二
求出第一個大於100000的費式數列是第幾項
用遞迴的寫法
/**
* FIB use recursive
*
* @param big_number
* @return big than big_number is
*/
public static int fib_slow(long big_number) {
long f = 1;
int ans = 0;
for (long i = 0; f < big_number; i++) {
f = fib(i);
ans++;
}
return ans;
}
public static long fib(long n) {
if (n == 0 || n == 1)
return n;
else
return (fib(n - 2) + fib(n - 1));
}
用單一迴圈的寫法,所花的時候只有O(n)
/**
* FIB not use recursive
*
* @param number
* @return big than big_number is
*/
public static int fib_quick(long number) {
long a, b, c;
/* A[0]=0,A[1]=1 has been count in first time, so ans=2*/
int ans = 2;
a = 0;
b = 1;
c = a + b;
while (c < number) {
/* A[n+2]=A[n]+A[n+1]; */
c = a + b;
/* next ...., let A be a=A[n+1],b=A[n+2] */
a = b;
b = c;
ans++;
}
return ans;
}
code
最大公因數 Greatest common divisor
code
package javahomework;
import javax.swing.JOptionPane;
public class HW01 {
/**
* Main
*
* @param args
*/
public static void main(String args[]) {
int number1 = Integer.parseInt(JOptionPane.showInputDialog(null,
"Please Input Number1 "));
int number2 = Integer.parseInt(JOptionPane.showInputDialog(null,
"Please Input Number2 "));
JOptionPane.showMessageDialog(null, "GCD is " + GCD(number1, number2));
}
/**
* Retrun GCD
*
* @param number1
* @param number2
* @return GCD
*/
static int GCD(int number1, int number2) {
if (number1 % number2 == 0)
return number2;
else
return (GCD(number2, number1 % number2));
}
}
2007年12月1日 星期六
join w2b
我在前陣子加入了 Yuran 的 w2b (Wordpress to Blogger)
目前把程式一半以上重寫過,並且還在整理中。
可用的版本是命令模式,GUI版本還在設計中(GUI苦手)
其他咩,就等我弄出一個可用的版本再說吧(汗)
目前把程式一半以上重寫過,並且還在整理中。
可用的版本是命令模式,GUI版本還在設計中(GUI苦手)
其他咩,就等我弄出一個可用的版本再說吧(汗)
2007年9月16日 星期日
Hello World!
- Ruby
puts "Hello World!" - jQuery
$(document).ready(function(){$("p").append("Hello World!")}); - JavaScript
document.write("Hello World!") - C#
using System;
namespace HelloWorld {
class Hello{
static void Main(){
System.Console.WriteLine("Hello World!");
}
}
} - Java
class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World!");
}
} - Bash
#!/bin/bash
STR="Hello World!"
echo $STR - HTML
<html><head><title>Hello World</title></head><body><p>Hello World!</p></body></html> - C++
#include <iostream.h>
main(){
cout << "Hello World!";
return 0;
} - C
#include <stdio.h>
main(){
for(;;){
printf ("Hello World!\n");
}
}
訂閱:
文章 (Atom)