posted @ 2008-12-21 20:38 山泉彎延 閱讀(3391) | 評論 (0) | 編輯 收藏





























































































posted @ 2008-12-13 19:04 山泉彎延 閱讀(741) | 評論 (0) | 編輯 收藏
#include <iostream>
#include <String>
using namespace std;
class ia{
public:
ia(string a){this->a=a;}
~ia(){cout<<"~"<<a<<endl;getchar();}
private:
string a;
};
int main()
{
auto_ptr<ia> ap(new ia("newplan"));
ia *bp = new ia("zhaoziming");
return 1;
}
posted @ 2008-10-16 20:12 山泉彎延 閱讀(865) | 評論 (0) | 編輯 收藏
bash myshell.sh
*/
echo "doing jobs...."
yacc -d b3.y
lex b3.l
cc lex.yy.c y.tab.c -o b3
echo "jobs end."
posted @ 2008-09-17 00:05 山泉彎延 閱讀(501) | 評論 (1) | 編輯 收藏
/*
編譯原理實驗3
B3.L
*/
%{
#include"y.tab.h"
#include<string.h>
extern FILE * yyin;
extern FILE * yyout;
extern int yylineno;
%}
delim [ \t]
ws {delim}+
letter [A-Za-z]
digit [0-9]
id {letter}({letter}|{digit})*
number {digit}+
addop [+-]
mulop [*/]
%%
\r\n {yylineno++;}
{ws} {/*d*/}
while {return WHILE;}
do {return DO;}
if {return IF;}
else {return ELSE;}
for {return FOR;}
int {return INT;}
char {return CHAR;}
void {return VOID;}
return {return RETURN;}
\'[a-zA-Z0-9]\' {strcpy(yylval._ident,yytext);return CONST_CHAR;}
\"[a-zA-Z]+\" {strcpy(yylval._ident,yytext);return STRING;}
{id} {strcpy(yylval._ident,yytext);return ID;}
{number} {strcpy(yylval._ident,yytext);return NUM;}
"[" {return '[';}
"]" {return ']';}
"<" {return LT;}
"=" {return '=';}
">" {return GT;}
"<=" {return LE;}
">=" {return GE;}
"!=" {return NE;}
"==" {return EQ;}
{addop} {yylval._op=yytext[0]; return ADDOP;}
{mulop} {yylval._op=yytext[0]; return MULOP;}
";" {return ';';}
"{" {return '{';}
"}" {return '}';}
"(" {return '(';}
")" {return ')';}
"," {return ',';}
%%
int yywrap(){
return 1;
}
posted @ 2008-09-17 00:01 山泉彎延 閱讀(375) | 評論 (0) | 編輯 收藏
/*
編譯原理實驗三實驗代碼
B3.Y
newplan
08-09-16
*/
%{
/*-----------head file---------*/
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/*-----------macros------------*/
#define HASHSIZE 128
#define _INT 34
#define _CHAR 35
#define _VOID 36
#define _STRING 37
#define _FUNC_TYPE 38
#define _ERROR 39
extern int yylex();
extern FILE* yyin;
extern FILE* yyout;
int yylineno;
typedef struct {
int a[5];
int n;
int h;
int ret_type;
}param;
/*¶¨Òå±êʶ·û*/
typedef struct {
char name[10];
int scope;/*ËùÔڵIJã´Î*/
int type;/*±êʶ·ûÀàÐÍ*/
param *p;
}symbol;
/*¶¨ÒåhashͰ*/
struct sym_entry{
symbol sym;
struct sym_entry *next ;
};
/*¶¨ÒåhashÁ´±í*/
struct table {
int level ;
struct table *previous;
struct sym_entry *buckets[HASHSIZE] ;
};
int table_len = 0;/*ÓжàÉÙ¸ö±í*/
struct table *table_stack[100];/*±íÕ»*/
struct table *global_table ;/*È«¾Ö±íÖ¸Õë*/
param *global_func_p;
int level = 0 ;/*È«¾Ö²ã´ÎÉèÖÃΪ 0 */
int error = 0;
int trace = 0;
/*hash º¯Êý*/
int hash(char *s)
{
char *p ;
unsigned h = 0,g=0;
for(p=s ;*p != '\0' ;p++)
{
h=(h<<4)+(*p) ;
if(g=h & 0xf0000000)
{
h=h^(g>>24);
h=h^g;
}
}
return h%128;
}
/*----------------²éÕÒ·ûºÅ±í------------------*/
symbol *lookupall(char *name,struct table *tp)
{
int h =hash(name);
struct sym_entry *p = NULL;
int tag=0;
do
{
if(tp->level ==level && tag >0 )continue;
for(p = tp->buckets[h];p;p=p->next)
if(!strcmp(name,p->sym.name)) return &p->sym ;
tag = 1 ;
}
while(tp=tp->previous);
return NULL;
}
/*---------------------------------------------*/
symbol *lookup(char *name,struct table *tp)
{
int h= hash(name);
struct sym_entry *p =NULL;
for(p = tp->buckets[h];p;p = p->next)
if(!strcmp(name,p->sym.name)){return &p->sym ;}
return NULL;
}
/*---------------´´½¨Ò»¸ö·ûºÅ±í----------------*/
struct table * mktable(struct table *previous ,int level)
{
struct table *new =(struct table *)malloc(sizeof *new);
new->previous = previous ;
new->level = level ;
int i;
for(i= 0; i<HASHSIZE ;i++)new->buckets[i]=0;
return new;
}
/*---------------²åÈëÒ»¸ö±êʶ·û----------------*/
symbol *insert(char *name,struct table *tpp)
{
int h = hash(name);
struct table *tp = tpp;
struct sym_entry *p =(struct sym_entry *)malloc(sizeof *p);
if(tp->level<level)
{
tp =(struct table *)mktable(tp,level);
table_stack[table_len] = tp ;
table_len ++;
}
strcpy(p->sym.name,name);
p->sym.scope= level;
p->sym.type = 0;
p->next = tp->buckets[h];
tp->buckets[h]=p;
return &p->sym ;
}
%}
%start program
%right '='
%left ADDOP
%left '*' '/'
%left LE LT GT GE EQ NE
%union {char _ident[9]; int value;char _op;};
%token <_ident> ID
%token NUM
%token <_ident> STRING
%token IF
%token ELSE
%token WHILE
%token INT
%token CHAR
%token <_ident> CONST_CHAR
%token VOID
%token RETURN
%token FOR
%token DO
%token <_op> ADDOP
%type <value> type_specifer
%type <value> var
%type <value> factor
%type <value> term
%type <value> additive_expression
%type <value> simple_expression
%type <value> expression
%type <value> arg_list
%token <_op> MULOP
%nonassoc IFX
%nonassoc ELSE
%%
program : M declaration_list {if(1 == trace)printf("program ==> M declaration_list.\n");if(error == 0 ) fprintf(yyout,"no error\n");}
;
M : {
global_table = mktable(NULL,0);
table_stack[table_len] = global_table ;
table_len++;
}
;
declaration_list: declaration_list declaration
{
if(trace == 1) fprintf(yyout,"declaration_list ==> declaration_list declaration.\n");
}
| declaration
{
if(trace == 1) fprintf(yyout,"declaration_list ==> declaration.\n");
}
;
declaration :var_declaration
{
if (trace ==1) fprintf(yyout,"declaration ==> var_declaration.\n");
}
|fun_declaration
{
if (trace ==1) fprintf(yyout,"declaration ==> fun_declaration.\n");
}
;
var_declaration :type_specifer ID ';'
{
if(trace == 1) printf("var_declaration ==>type_specifer ID.\n");
struct table *tp = table_stack[table_len-1];
if(lookup($2,tp) == NULL)
{
symbol *p = insert($2,tp);
p->type = $1 ;
}
else {error = 1 ; fprintf(yyout, "line %d:error:%s:redefinition\n ",yylineno,$2);}
}
|type_specifer ID '[' NUM ']' ';'
;
type_specifer :INT {$$ =_INT ;}
|CHAR {$$ = _CHAR;}
|VOID {$$ =_VOID ;}
;
fun_declaration :type_specifer fun_tag '(' LL params ')' compound_stmt {level--;}
|type_specifer fun_tag '(' LL params ')' ';'{level--;}
;
fun_tag : ID
{
struct table *tp = table_stack[table_len-1];
symbol *tmp ;
tmp = lookupall($1,tp);
if(tmp == NULL)
{tmp =insert($1,tp);
global_func_p = tmp->p = (param *)malloc(sizeof(*tmp->p));
global_func_p->n =0 ;
global_func_p->ret_type = _INT ;
tmp->type =_FUNC_TYPE;
}
else
{error = 1 ; fprintf(yyout,"line %d:error %s redefine of function.\n",yylineno,$1);}
}
;
LL: {level++;
struct table *tp = mktable(table_stack[table_len-1],level);
table_stack[table_len]=tp;
table_len++;
if(1==trace)printf("level = %d \n",level);
}
;
params :param_list
|VOID{global_func_p->n = 0; if(1 == trace)printf("params==>void\n");}
;
param_list :param_list ',' param {if(1 ==trace )printf("param_list ==> param_list , param.\n");}
|param
;
param :type_specifer ID
{
struct table *tp =table_stack[table_len-1];
symbol *p =insert($2,tp);
p->type =$1;
global_func_p->a[global_func_p->n++]= $1 ;
// printf("type[%d]=%d\n",global_func_p->n-1,global_func_p->a[global_func_p->n-1]);
}
;
compound_stmt :'{' local_declarations statement_list '}'
;
local_declarations: local_declarations var_declaration{if(1 ==trace )printf("local_declarations ==>loacal_declarations var_declaration\n");}
|{if(1 == trace )printf("local_declarations ==> .\n");}
;
statement_list :statement_list statement
|{}
;
statement :expression_stmt{if(1 == trace )printf("statement ==> expression_stmt.\n");}
|if_stmt
|compound_stmt
|while_stmt
|return_stmt
;
expression_stmt :expression ';'
|';'
;
if_stmt :IF '(' expression ')' statement %prec IFX {if(1== trace)printf("if_stmt ==> if expression statement .\n");}
|IF '(' expression ')' statement ELSE statement
{if(1==trace )printf("if_Stmt ==> if expression statement else statement.\n");} ;
while_stmt :WHILE '(' expression ')' statement
;
return_stmt :RETURN ';'
|RETURN expression ';'
;
expression :var '=' expression{if($1 != $3){error =1 ;printf("line %d: '=' must be same type.\n",yylineno);}if(1 == trace )printf("expression ==>var = expression.\n");}
|simple_expression {$$ == $1 ;}
;
var :ID {
symbol *p = lookupall($1,table_stack[table_len-1]);
if(NULL==p){$$ = _ERROR ;error =1 ;fprintf(yyout,"line %d undeclared identifier %s\n",yylineno,$1);}
else
$$ =p->type ;
}
;
simple_expression:additive_expression relop additive_expression{if($1==_INT && $3 ==_INT || $1 ==_CHAR && $3 == _CHAR){;}else{ error =1 ;printf("line %d :relop must int int or char char.\n",yylineno);}}
|additive_expression{$$ =$1 ;}
;
relop :LE
|LT
|GT
|GE
|EQ
|NE
;
additive_expression:additive_expression ADDOP term {if($1 == _INT && $3 ==_INT){;}else{error =1 ;printf("line%d :addop must int int .\n",yylineno);}if(1==trace)printf("additive_expression ==>additive_expression ADDOP term.\n");}
|term{$$ = $1 ;}
;
term :term MULOP factor {if($1==_INT && $3 ==_INT ){;}else{error = 1 ;printf("\nline%d :mulop must int int.\n",yylineno);}}
|factor {$$ =$1;}
;
factor :'(' expression ')'
|var {$$ = $1;}
|call{if(global_func_p){$$=global_func_p->ret_type;}}
|NUM {$$ = _INT;}
|STRING {$$ =_STRING;}
|CONST_CHAR {$$ = _CHAR ;}
;
call :ID { symbol *tmp =lookupall($1,table_stack[table_len-1]);if(NULL ==tmp||tmp->type!=_FUNC_TYPE){error =1;printf("line%d :undeclared identifier %s.\n",yylineno,$1);}else {global_func_p = tmp->p;global_func_p->h = 0;}
} '(' args ')' {if(1 == trace )printf("call ==> ID args .\n");}
;
args :arg_list{if(global_func_p->n > global_func_p->h){error=1;printf("line%d :param num error.\n",yylineno);}}
|{if(global_func_p->n != 0){error =1 ;printf("line%d :param error.\n",yylineno);}}
;
arg_list :arg_list ',' expression{if(global_func_p->n<=global_func_p->h){error=1 ;printf("line%d :param num error.\n",yylineno);}else if(global_func_p->a[global_func_p->h]!=$3){error =1 ;printf("line%d :param error %d.\n",yylineno,$3);}else{global_func_p->h++;}}
|expression {$$ =$1 ; if(global_func_p->a[global_func_p->h]!=$1){error =1 ;printf("line%d :param error $1=%d.\n",yylineno,$1);}else{global_func_p->h++;}}
;
%%
int main(int argc,char *argv[])
{
printf("start checking...\n");
yylineno=1;
if(argc==3)
{
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
}
else if(argc==2)
{ yyin=fopen(argv[1],"r");
yyout = stdout ;
}
yyparse();
return 0;
}
int yyerror(char *string)
{
printf("error:%s in line %d\n",string,yylineno);
return 0;
}
posted @ 2008-09-17 00:00 山泉彎延 閱讀(423) | 評論 (0) | 編輯 收藏
% 實驗名稱:方陣的QR分解
% 實驗描述:先將方陣化為上海申博格陣,再用QR分解法求上海申博格陣的特征值,則所得到的特征值也是方陣的特征值
% 作者:newplan
% 實驗完成日期:6月10號
%下面的A為測試三階的方陣
A=[5,-3,2;6,-4,4;4,-4,5]
%下面的A為測試四階的方陣
%A = [1 2 1 2;2 2 -1 1;1 -1 1 1;2 1 1 1]
%通過調用malab的自帶的函數求得A的所有特征值和特征向量
%特征值保存在v中,特征向量保存的在d中,將其打印出來和我們的算法算出來的特征值進行對比
[v,d]=eig(A)
%求出行和列的大小
msize=size(A);
%取得矩陣的列數,其實行數和列數都為n
n=msize(1);
%生成n階單位陣
Q=eye(n);
%用household的方法求矩陣A的上海森伯格陣
for i=1:n-2%從第一列開始到倒數第三列
%求出每一列的最大值
d=max(abs(A(i+1:n,i)));
%規范化
U(i+1:n,i)=A(i+1:n,i)/d;
delta=U(i+1,i)*norm(U(i+1:n,i))/abs(U(i+1,i));
U(i+1,i)=U(i+1,i)+delta;
beta = delta*U(i+1,i);
%求出R矩陣根據課本316P例題三
R = eye(n-i,n-i)-inv(beta)*U(i+1:n,i)*U(i+1:n,i)';
u=eye(n,n);
for j =i+1:n
for k =i+1:n
u(j,k)=R(j-i,k-i);
end
end
A=u*A*u;%生成新的A=u×A×u
end
%error為我們設定的誤差限制
error = 0.0000001;
%flag為判斷QR法是否繼續進行的標志位
flag =1;
while flag==1
flag =0 ;
R =A;
Q = eye(n,n);
%按照QR分解法求出cos,sin 然后計算V,最終得到R和Q
for i=1:n-1
r = norm(R(i:i+1,i));
icos=R(i,i)/r;
isin=R(i+1,i)/r;
v=eye(n,n);
v(i,i)=icos;
v(i+1,i+1)=icos;
v(i,i+1)=isin;
v(i+1,i)=-isin;
R=v*R;
Q=Q*v';
end
%用R*Q的結果去替換A
A =R*Q;
%下面這個循環檢測A的精度時候足夠,去看A的次對角線各個元素的絕對值是否小于誤差限制
for w =2:n
if abs(A(w,w-1))>error
flag = 1 ;
break;%若有其中一個元素的絕對值還是大于誤差限制則還要繼續進行QR分解
end
end
%判斷的過程完畢
end
%把A打印出來
A
posted @ 2008-06-24 10:52 山泉彎延 閱讀(2481) | 評論 (0) | 編輯 收藏
6.10
*/
/*==========INCLUDES BEGIN===============*/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <Qt>
/*==========INCLUDE END==================*/
/*==========MACROS BEGIN=================*/
#define MAX 100000000
#define BUFFER 300
#define INPUTFILE "./50.txt"
/*==========MACROS END==================*/
/*==========STD DECLRARS BEGIN===========*/
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::ifstream;
using std::sort;
using std::max;
/*==========STD DECLARS END===============*/
/*============STRUCTS BRGIN===============*/
struct Space {
int x;
int y;
int w;
int h;
bool v;//IF VISITED THEN V =TURE ELSE FLASE
};
struct Gadget
{
int x;
int y;
int w;
int h;
};
/*=============STRUCT END=================*/
/*===========GADGET CUT BEGIN=============*/
Gadget result[BUFFER];
Gadget g[BUFFER];
int bestH;
Space space[BUFFER];
int spaceNum ;
int W;
int N;
int H;
ifstream Fin;
int deep;
clock_t start;
clock_t end;
/*-------------FRIENDS METHOD--------------------*/
bool mycmpG(Gadget t1,Gadget t2){return t1.h>t2.h;}
/*-------------FRIENDS METHOD--------------------*/
bool mycompS(Space t1,Space t2){return t1.y<t2.y;}
/*-------------CONSTRUCT METHOD------------------*/
void init()
{
Fin.open(INPUTFILE,ios::in);
Fin>>N;
Fin>>W;
for(int i=0;i<N;i++)
Fin>>g[i].h>>g[i].w;
sort(g,g+N,mycmpG);
space[0].x=space[0].y=0;
space[0].h=MAX;
space[0].w=W;
for(int i=0;i<N;i++)
space[0].v=false;
H=0;
deep=0;
bestH = MAX;
spaceNum = 1;
}
/*-------------CUT METHOD------------------*/
bool canBeCut(Gadget &g,int i,int &TaddSpace)
{
int addSpace = 0;
if((space[i].h>=g.h)&&(space[i].w>=g.w)){
if(space[i].w>g.w){
space[spaceNum].x = space[i].x+g.w;
space[spaceNum].y = space[i].y;
space[spaceNum].h = g.h;
space[spaceNum].w = space[i].w - g.w;
addSpace++;
}
if(space[i].h>g.h){
space[spaceNum+1].x = space[i].x;
space[spaceNum+1].y = space[i].y+g.h;
if(space[i].h==MAX)
space[spaceNum+1].h = MAX;
else
space[spaceNum+1].h = space[i].h - g.h;
space[spaceNum+1].w = space[i].w;
addSpace++;
}
g.x = space[i].x;
g.y = space[i].y;
H = max(H,g.y+g.h);
spaceNum += addSpace;
TaddSpace = addSpace;
return true;
}
return false;
}
/*-------------THE MAIN METHOD--------------------*/
void backTrack(int which)
{ // if(deep==100000)return;
// else deep++;
sort(space,space+spaceNum,mycompS);
Space temp[BUFFER];
for(int i=0;i<spaceNum;i++)
temp[i] = space[i];
if(which==N)
{
if(H<bestH)
{ bestH = H;
for(int i = 0;i<N;i++)
result[i]=g[i];
}
return;
}
int addSpace;
int Num=spaceNum;
for(int i=0;i<Num;i++)
if(space[i].v == false)
{
int tempH=H;
if(canBeCut(g[which],i,addSpace))
{
if(H>bestH)//剪枝
{
H = tempH;
spaceNum -= addSpace;
continue;
}
space[i].v = true;
backTrack(which+1);
spaceNum-=addSpace;
space[i].v = false;
H = tempH;
for(int k=0;k<spaceNum;k++)
space[k] = temp[k];
}
}
}
/*===========GADGET CUT END=============*/
/*========NEWBOX CLASS BEGIN============*/
class NEWBOX:public QWidget
{
public:
NEWBOX(QWidget *parent=0);
protected:
void paintEvent(QPaintEvent *event);
private:
};
/*NEWBOX METHOD*/
/*-----------------------------------*/
NEWBOX::NEWBOX(QWidget *parent):QWidget(parent)
{
setFixedSize(W*15,30*15);
char temp[5];
sprintf(temp,"%d",bestH);
char title[40]="H:";
strcat(title,temp);
char temp2[20]=" Spend TIME:";
char temp3[5];
sprintf(temp3,"%f",(double)(end-start)/CLOCKS_PER_SEC);
strcat(temp2,temp3);
strcat(title,temp2);
setWindowTitle(title);
setPalette(QPalette(QColor(250, 250, 200)));
setAutoFillBackground(true);
}
/*-----------------------------------*/
void NEWBOX::paintEvent(QPaintEvent *)
{ QPainter painter(this);
painter.setPen(Qt::SolidLine);
painter.setBrush(Qt::blue);
painter.translate(0,0);
for(int i=0;i<=N;i++)
painter.drawRect(result[i].x*15,30*15-result[i].y*15,
result[i].w*15,-result[i].h*15);
}
/*=========NEWBOX CLASS END=============*/
int main(int argc, char *argv[])
{ QApplication app(argc, argv);
init();
start=clock();
backTrack(0);
end= clock();//TIME END HERE
NEWBOX newb;
newb.show();
return app.exec();
}
posted @ 2008-06-10 21:47 山泉彎延 閱讀(373) | 評論 (0) | 編輯 收藏
posted @ 2008-06-05 09:05 山泉彎延 閱讀(551) | 評論 (0) | 編輯 收藏
*有N個人排隊到R個水龍頭去打水,他們裝滿水桶的時間
*為T1,T2,…,Tn為整數且各不相等,應如何安排他們
*的打水順序才能使他們花費的時間最少?
*分析:由于排隊時,越靠前面的計算的次數越多,顯然越小
*的排在越前面得出的結果越小(可以用數學方法簡單證明,
*這里就不再贅述),所以這道題可以用貪心法解答
*/
/*------------INCLUDES---------------*/
#include <cstdlib>
#include <iostream>
#include <queue>
#include <fstream>
/*------------INCLUDES---------------*/
/*---------------STD-----------------*/
using std::ifstream;
using std::queue;
using std::vector;
using std::greater;
using std::priority_queue;
/*---------------STD----------------*/
/*------------GLOBAL VAL------------*/
int M[5];
/*------------GLOBAL VAL------------*/
/*---------------MAIN---------------*/
int main(int argc, char *argv[])
{ ifstream Fin;
Fin.open("queue.txt");
priority_queue<int,vector<int>,greater<vector<int>::value_type> > iqueue;
int a;
while(Fin>>a)
{
iqueue.push(a);
}
int flag=0;
int i=0;
while(!iqueue.empty())
{
if(flag==0)
{M[i]=iqueue.top();
iqueue.pop();
i++;
if(i==5)
flag=1;
}
else if(flag==1)
{
M[i]=iqueue.top();
iqueue.pop();
i--;
if(i==0)
flag=0;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
/*---------------MAIN---------------*/
posted @ 2008-05-13 23:48 山泉彎延 閱讀(612) | 評論 (1) | 編輯 收藏