|
摘要: import java.io.BufferedReader;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import&... 阅读全文
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit;
/**//* * 学习使用JDK5以上的线程池操作 */
public class ThreadPool {
public static void main(String[] args) { /**//* * 可以安排线程运行时间 * */ final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); final Runnable beeper = new Runnable() { int count = 0; public void run() { System.out.println( " beep " + (++count)); } }; final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 1, 2,TimeUnit.SECONDS); scheduler.schedule(new Runnable() { public void run() { beeperHandle.cancel(true); scheduler.shutdown(); } }, 200, TimeUnit.SECONDS ); /**//* * 不可以安排线程的运行时间 */ ExecutorService exec = Executors.newFixedThreadPool(2); for(int index = 0; index < 200; index++) { Runnable run = new Runnable() { public void run() { long time =(long)(Math.random() * 1000); System.out.println("Sleeping " + time + "ms"); try { Thread.sleep(time); } catch (InterruptedException e) { } } }; exec.execute(run); } exec.shutdown(); } }
#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; }
/* 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."
/* 编译原理实验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; }
/* 编译原理实验三实验代码 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; }
function[]=iqr() % 实验名称:方阵的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
/* 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();
}
摘要: /*08.6.2*//*------------------INCLUDES BEGIN---------------*/#include <cstdlib>#include <iostream>#include <fstream>#include <string>#include <... 阅读全文
/* *有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---------------*/
|