2013年6月26日 星期三

LIST變形 C++

假設現在有兩個LIST(圖片one two),我可以選擇要動哪一個list(選擇動哪張圖片移動到新的位置),如果沒動到的那張圖片,希望也要出一個空間,存的XY就是原本的位置。
此時希望可以有恢復的功能(linking list的返回鍵left),也希望可以下一步(linking list的right)



#include<iostream>
using namespace std;
struct list
{
list* left;
int x;
int y;
list* right;
};
int main()
{
list one;
list two;
cout<<"請輸入第1張圖的X和Y位置"<<endl;
cin>>one.x;
cin>>one.y;
one.left=NULL;
one.right=new list;
cout<<"請輸入第2張圖的X和Y位置"<<endl;
cin>>two.x;
cin>>two.y;
two.left=NULL;
two.right=new list;
list* pre;
list* current;
list* next;
int choice,count=0;
while(true)
{
cout<<"請輸入要更動哪一個圖片one or two"<<endl;
cin>>choice;
if(choice==1)
{
current=one.right; //initial 1
pre=&one;
for(int i=0;i<count;i++) //find new one
{
pre=current;
next=current->right;
current=next;
}
cout<<"請輸入第1張圖新的X和Y位置"<<endl;
cin>>current->x; //new
cin>>current->y;
current->left=pre;
current->right=new list;
current=two.right; //initial 2
pre=&two;
for(int j=0;j<count;j++) //find new two
{
pre=current;
next=current->right;
current=next;
}
current->left=pre;
current->right=new list;
current->x=current->left->x; //copy two data
current->y=current->left->y;
count++;
}
else if(choice==2)
{
current=two.right; //initial 2
pre=&two;
for(int j=0;j<count;j++) //find new two
{
pre=current;
next=current->right;
current=next;
}
cout<<"請輸入第2張圖新的X和Y位置"<<endl;
cin>>current->x; //new
cin>>current->y;
current->left=pre;
current->right=new list;
current=one.right; //initial 1
pre=&one;
for(int i=0;i<count;i++) //find new one
{
pre=current;
next=current->right;
current=next;
}
current->left=pre;
current->right=new list;
current->x=current->left->x; //copy one data
current->y=current->left->y;
count++;
}
else
break;
}

/*trace 使用
current=&one;
for(int k=0;k<=count;k++)
{
cout<<current->x<<"   "<<current->y<<endl;
next=current->right;
current=next;
}
*/
return 0;
}

當然此程式可以看出choice==one or two 內有很多地方是做一樣的事情,應該可以額外寫個function去genrealize,但是由於懶惰,就不想優化了....

2013年6月24日 星期一

雙向linking list C++

先按1 在案2


#include<iostream>
using namespace std;
struct typeA
{
typeA *pre;
int x;
int y;
typeA *next;
};
int main()
{
typeA *left;
typeA data; //先弄第一個出來
data.pre=NULL;
data.x=10;
data.y=10;
data.next=new typeA;
left=&data; //讓left記路目前位置

typeA *right; //宣告一個right出來
right=data.next; //指到data的下一個
right->x=20;
right->y=20;
right->pre=left;

typeA *now;
now=&data;
while(true) //測試先按1 在案2
{ int n;
cin>>n;
if(n==1)
{
now=now->next;
cout<<now->x<<endl;
}
if(n==2)
{
now=now->pre;
cout<<now->x<<endl;
}
}

return 0;
}

2013年6月5日 星期三

NCNU LISP HW9

: finding the n-th leaf of a tree

(define (enumerate-tree tree)
  (cond ((null? tree) nil)
        ((not (pair? tree)) (list tree))
        (else (append (enumerate-tree (car tree))
                      (enumerate-tree (cdr tree))))))
(define nil '())

(define x (list 1 (list 2 (list 3 4)) 5))

(define (tree-ref L n)
(do-tree-ref L n 1)
)
(define (do-tree-ref L n flag)
(cond ((= flag 1) (do-tree-ref (enumerate-tree L) n 0))
     ((= n 0) (car L))
     (else (do-tree-ref (cdr L) (- n 1) 0))
)
)

2013年5月29日 星期三

LISP NCNU HW手寫exercise2.1



(define (make-rat n d)
(cond ( (and (< n 0) (< d 0)) (let ((g (gcd n d))) (cons (/ (- n) g) (/ (- d) g))))
     ( (and (> n 0) (< d 0)) (let ((g (gcd n d))) (cons (/ (- n) g) (/ (- d) g))))
     (else (let ((g (gcd n d))) (cons (/ n g) (/ d g))))
)
)
(define (gcd a b)
  (if (= b 0)
      (if (< a 0) (- a)
    a)
      (gcd b (remainder a b))))

NCNU LISP HW7


(define (iterative-improve ok? do)
(define (iim guess)
(if (ok? guess) guess
                    (iim (do guess))
)
)
iim)
(define (square x)
(* x x))
(define (average x y)
    (/ (+ x y) 2))

(define (sqrt x)
   ((iterative-improve (lambda (guess)
                        (< (abs (- (square guess) x)) 0.001))
                       (lambda (guess)
                          (average guess (/ x guess))))
   1.0)
)

(define (fixed-point f guess)
   ((iterative-improve (lambda (guess)
                          (< (abs (- (f guess) guess)) 0.00001))
                       (lambda (guess)
                          (f guess)))
    guess)
)

NCNU LISP HW8

exercise 8: Combining three numbers with exponentiation and multiplication
  • Refer to exercise 2.5 but the pair is replaced with a triple. That is, the interface functions are :
    (combine a b c), (first x), (second x), (third x)
(define (combine a b c)
  (do-combine a b c 1)
(define (do-combine a b c num)
(if (= a 0)
(if (= b 0)
(if (= c 0) num
           (do-combine a b (- c 1) (* 5 num))
)
   (do-combine a (- b 1) c (* 4 num))
)
   (do-combine (- a 1) b c (* 3 num))
  )
)
(define (first x)
(common x 3)
)
(define (second x)
(common x 4)
)
(define (third x)
(common x 5)
)
(define (common n f)
(define (do-common x num)
(if (> (remainder x f) 0)
   num
    (do-common (/ x f) (+ num 1))
)
)
 (do-common n 0)
)



2013年5月16日 星期四

NCNU LISP HW6


computing the continued fractions
  • Refer to exercise 1.37.
  • Both versions of linear recusive and linear iterative are required.
  • Deadline: 2013 Apr 27 00:05 am


(define (cont-frac-iterative N D K)
        (do-cont-frac-iterative N D K 0)
)
(define (do-cont-frac-iterative N D K result)
        (if (= k 0) result
                    (do-cont-frac-iterative N D (- k 1) (/ (N K) (+ (D K)
result) ))
        )
)
(define (cont-frac-recursive N D K)
        (do-cont-frac-recursive N D K 1)
)

(define (do-cont-frac-recursive N D K counter)
        (if (= k 1) (/ (N counter) (D counter))
                    (/ (N counter) (+ (D counter) (do-cont-frac-recursive N
D (- k 1) (+ counter 1))))
        )
)