經過認真的上課聽講以及努力的自學,終于明白MFC是怎么工作的了,呵呵,接下來的目標是做個小游戲,比如掃雷。 呵呵 ,要加油哦!~
摘要: #include<iostream>#include<cmath>#include<algorithm>using namespace std;template<class T>class MinHeap{private: T *heap; &n...
閱讀全文
在看了孫鑫的 《C++深入詳解》 后,終于能寫出個人的第一個MFC程序了,雖然是模仿孫鑫的例程,不過個人非常有成就感呵,希望能早日寫出一個功能完整的程序;
#include<windows.h>
#include<stdio.h>
#include<cmath>
#include<iostream>
using namespace std;


LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)


{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_PEN);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="abilitytao";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow("abilitytao","歡迎來到MFC世界",WS_OVERLAPPEDWINDOW,
0,0,600,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))

{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)


{
switch(uMsg)

{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char code is %d",wParam);
MessageBox(hwnd,szChar,"char",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",0);
HDC hdc;
hdc=GetDC(hwnd);
//ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,260,100,"hello,MFC",strlen("hello,MFC"));
TextOut(hDC,350,120,"by -abilitytao",strlen("by -abilitytao"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"真的要退出嗎?","提示",MB_YESNO))

{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}

感謝那些在我學習過程中給我指點和建議的人!
最近選了一門MFC的選修課,本以為MFC應該不難(畢竟C++和各種算法已經研究得很詳細了),可是聽了幾節課下來,感覺自己好像還是沒有很大的提高,呵呵,究竟MFC該如何快速入門呢?希望各位牛人能夠指點一二,只要讓我掌握門道知道怎樣自學就行了,不勝感激呵&
昨天是ACM世界總決賽的日子,所以我特別關注了一下,特別是樓教主呵,聽說他是為了拿世界總決賽冠軍才復出的,不過最后的結果很遺憾
啊,上屆冠軍SPSU以罰時優勢成功衛冕,讓清華再次飲憾屈居亞軍。
冠軍:St. Petersburg State University of IT, Mechanics and Optics 俄羅斯
亞軍:Tsinghua University 中國
季軍:St. Petersburg State University 俄羅斯
ZSU最終5題排名20,本次中國總共有15支隊在3題以上。
詳細的排名如下:
----------------------------------------------------------------------------------
Place
|
Name |
Solved |
Time |
1 |
St. Petersburg State University of IT, Mechanics and Optics |
9 |
1381 |
2 |
Tsinghua University |
9 |
1800 |
3 |
St. Petersburg State University |
8 |
1176 |
4 |
Saratov State University |
8 |
1305 |
5 |
University of Oxford |
7 |
998 |
6 |
Zhejiang University |
7 |
1117 |
7 |
Massachusetts Institute of Technology |
7 |
1143 |
8 |
Altai State Technical University |
7 |
1254 |
9 |
University of Warsaw |
7 |
1413 |
10 |
University of Waterloo |
6 |
787 |
11 |
I. Javakhishvili Tbilisi State University |
6 |
933 |
12 |
Carnegie Mellon University |
6 |
1045 |
13 |
South China University of Technology |
6 |
1058 |
14 |
Sharif University of Technology |
6 |
|
14 |
Seoul National University |
6 |
|
14 |
Fudan University |
6 |
|
14 |
Moscow State University |
6 |
|
14 |
National Taiwan University |
6 |
|
14 |
Shanghai Jiaotong University |
6 |
|
20 |
Stanford University |
5 |
|
20 |
Novosibirsk State University |
5 |
|
20 |
Ural State University |
5 |
|
20 |
University of Maryland |
5 |
|
20 |
Universidad de Buenos Aires - FCEN |
5 |
|
20 |
University of Cambridge - Trinity College |
5 |
|
20 |
University of Tokyo |
5 |
|
20 |
Peking University |
5 |
|
20 |
University of Melbourne |
5 |
|
20 |
Huazhong University of Science & Technology |
5 |
|
20 |
Zhejiang University of Technology |
5 |
|
20 |
Zhongshan (Sun Yat-sen) University |
5 |
|
20 |
Taurida V.I. Vernadsky National University |
5 |
|
20 |
Chinese University of Hong Kong |
5 |
|
34 |
University of British Columbia |
4 |
|
34 |
Bangladesh University of Engineering and Technology |
4 |
|
34 |
National Technical University of Ukraine "KPI" |
4 |
|
34 |
Belarusian State University |
4 |
|
34 |
Taras Shevchenko Kiev National University |
4 |
|
34 |
University of California at Berkeley |
4 |
|
34 |
Tianjin University |
4 |
|
34 |
Universidade Federal do Paraná |
4 |
|
34 |
Amirkabir University of Technology |
4 |
|
34 |
Sichuan University |
4 |
|
34 |
Jagiellonian University in Krakow |
4 |
|
34 |
KTH - Royal Institute of Technology |
4 |
|
34 |
Beijing Jiaotong University |
4 |
|
34 |
École Normale Supérieure de Lyon |
4 |
|
34 |
Beijing University of Posts and Telecommunications |
4 |
|
49 |
Nanjing University |
3 |
|
49 |
Universitat Politècnica de Catalunya |
3 |
|
49 |
Instituto Tecnológico de Culiacán |
3 |
|
49 |
German University in Cairo |
3 |
|
49 |
University of Cape Town |
3 |
|
49 |
South Ural State University |
3 |
|
49 |
University of Aizu |
3 |
|
49 |
Nanyang Technological University |
3 |
|
49 |
Universidad Nacional de Colombia - Bogotá |
3 |
|
49 |
University of Canterbury |
3 |
|
49 |
Korea Advanced Institute of Science and Technology |
3 |
|
49 |
Universidad Nacional del Sur |
3 |
|
49 |
Iowa State University |
3 |
|
49 |
Cornell University |
3 |
|
49 |
University of Tasmania |
3 |
|
49 |
University of Texas at Austin |
3 |
|
49 |
University of Wisconsin - Madison |
3 |
|
49 |
University of Dhaka |
3 |
|
49 |
University of Illinois - Urbana-Champaign |
3 |
|
Floyd-Warshall算法,簡稱Floyd算法,用于求解任意兩點間的最短距離,時間復雜度為O(n^3)。我們平時所見的Floyd算法的一般形式如下:
1 void Floyd(){
2 int i,j,k;
3 for(k=1;k<=n;k++)
4 for(i=1;i<=n;i++)
5 for(j=1;j<=n;j++)
6 if(dist[i][k]+dist[k][j]<dist[i][j])
7 dist[i][j]=dist[i][k]+dist[k][j];
8 }
注意下第6行這個地方,如果dist[i][k]或者dist[k][j]不存在,程序中用一個很大的數代替。最好寫成if(dist[i][k]!=INF && dist[k][j]!=INF && dist[i][k]+dist[k][j]<dist[i][j]),從而防止溢出所造成的錯誤。
上面這個形式的算法其實是Floyd算法的精簡版,而真正的Floyd算法是一種基于DP(Dynamic Programming)的最短路徑算法。
設圖G中n 個頂點的編號為1到n。令c [i, j, k]表示從i 到j 的最短路徑的長度,其中k 表示該路徑中的最大頂點,也就是說c[i,j,k]這條最短路徑所通過的中間頂點最大不超過k。因此,如果G中包含邊<i, j>,則c[i, j, 0] =邊<i, j> 的長度;若i= j ,則c[i,j,0]=0;如果G中不包含邊<i, j>,則c (i, j, 0)= +∞。c[i, j, n] 則是從i 到j 的最短路徑的長度。
對于任意的k>0,通過分析可以得到:中間頂點不超過k 的i 到j 的最短路徑有兩種可能:該路徑含或不含中間頂點k。若不含,則該路徑長度應為c[i, j, k-1],否則長度為 c[i, k, k-1] +c [k, j, k-1]。c[i, j, k]可取兩者中的最小值。
狀態轉移方程:c[i, j, k]=min{c[i, j, k-1], c [i, k, k-1]+c [k, j, k-1]},k>0。
這樣,問題便具有了最優子結構性質,可以用動態規劃方法來求解。
為了進一步理解,觀察上面這個有向圖:若k=0, 1, 2, 3,則c[1,3,k]= +∞;c[1,3,4]= 28;若k = 5, 6, 7,則c [1,3,k] = 10;若k=8, 9, 10,則c[1,3,k] = 9。因此1到3的最短路徑長度為9。
下面通過程序來分析這一DP過程,對應上面給出的有向圖:
1 #include <iostream>
2 using namespace std;
3
4 const int INF = 100000;
5 int n=10,map[11][11],dist[11][11][11];
6 void init(){
7 int i,j;
8 for(i=1;i<=n;i++)
9 for(j=1;j<=n;j++)
10 map[i][j]=(i==j)?0:INF;
11 map[1][2]=2,map[1][4]=20,map[2][5]=1;
12 map[3][1]=3,map[4][3]=8,map[4][6]=6;
13 map[4][7]=4,map[5][3]=7,map[5][8]=3;
14 map[6][3]=1,map[7][8]=1,map[8][6]=2;
15 map[8][10]=2,map[9][7]=2,map[10][9]=1;
16 }
17 void floyd_dp(){
18 int i,j,k;
19 for(i=1;i<=n;i++)
20 for(j=1;j<=n;j++)
21 dist[i][j][0]=map[i][j];
22 for(k=1;k<=n;k++)
23 for(i=1;i<=n;i++)
24 for(j=1;j<=n;j++){
25 dist[i][j][k]=dist[i][j][k-1];
26 if(dist[i][k][k-1]+dist[k][j][k-1]<dist[i][j][k])
27 dist[i][j][k]=dist[i][k][k-1]+dist[k][j][k-1];
28 }
29 }
30 int main(){
31 int k,u,v;
32 init();
33 floyd_dp();
34 while(cin>>u>>v,u||v){
35 for(k=0;k<=n;k++){
36 if(dist[u][v][k]==INF) cout<<"+∞"<<endl;
37 else cout<<dist[u][v][k]<<endl;
38 }
39 }
40 return 0;
41 }
輸入 1 3
輸出 +∞
+∞
+∞
+∞
28
10
10
10
9
9
9
Floyd-Warshall算法不僅能求出任意2點間的最短路徑,還可以保存最短路徑上經過的節點。下面用精簡版的Floyd算法實現這一過程,程序中的圖依然對應上面的有向圖。
1 #include <iostream>
2 using namespace std;
3
4 const int INF = 100000;
5 int n=10,path[11][11],dist[11][11],map[11][11];
6 void init(){
7 int i,j;
8 for(i=1;i<=n;i++)
9 for(j=1;j<=n;j++)
10 map[i][j]=(i==j)?0:INF;
11 map[1][2]=2,map[1][4]=20,map[2][5]=1;
12 map[3][1]=3,map[4][3]=8,map[4][6]=6;
13 map[4][7]=4,map[5][3]=7,map[5][8]=3;
14 map[6][3]=1,map[7][8]=1,map[8][6]=2;
15 map[8][10]=2,map[9][7]=2,map[10][9]=1;
16 }
17 void floyd(){
18 int i,j,k;
19 for(i=1;i<=n;i++)
20 for(j=1;j<=n;j++)
21 dist[i][j]=map[i][j],path[i][j]=0;
22 for(k=1;k<=n;k++)
23 for(i=1;i<=n;i++)
24 for(j=1;j<=n;j++)
25 if(dist[i][k]+dist[k][j]<dist[i][j])
26 dist[i][j]=dist[i][k]+dist[k][j],path[i][j]=k;
27 }
28 void output(int i,int j){
29 if(i==j) return;
30 if(path[i][j]==0) cout<<j<<' ';
31 else{
32 output(i,path[i][j]);
33 output(path[i][j],j);
34 }
35 }
36 int main(){
37 int u,v;
38 init();
39 floyd();
40 while(cin>>u>>v,u||v){
41 if(dist[u][v]==INF) cout<<"No path"<<endl;
42 else{
43 cout<<u<<' ';
44 output(u,v);
45 cout<<endl;
46 }
47 }
48 return 0;
49 }
輸入 1 3
輸出 1 2 5 8 6 3
轉自:http://www.shnenglu.com/mythit/archive/2009/04/21/80579.html
Trie,又稱字典樹、單詞查找樹,是一種樹形結構,用于保存大量的字符串。它的優點是:利用字符串的公共前綴來節約存儲空間。
相對來說,Trie樹是一種比較簡單的數據結構.理解起來比較簡單,正所謂簡單的東西也得付出代價.故Trie樹也有它的缺點,Trie樹的內存消耗非常大.當然,或許用左兒子右兄弟的方法建樹的話,可能會好點.
其基本性質可以歸納為:
1. 根節點不包含字符,除根節點外每一個節點都只包含一個字符。
2. 從根節點到某一節點,路徑上經過的字符連接起來,為該節點對應的字符串。
3. 每個節點的所有子節點包含的字符都不相同。
其基本操作有:查找 插入和刪除,當然刪除操作比較少見.我在這里只是實現了對整個樹的刪除操作,至于單個word的刪除操作也很簡單.
搜索字典項目的方法為:
(1) 從根結點開始一次搜索;
(2) 取得要查找關鍵詞的第一個字母,并根據該字母選擇對應的子樹并轉到該子樹繼續進行檢索;
(3) 在相應的子樹上,取得要查找關鍵詞的第二個字母,并進一步選擇對應的子樹進行檢索。
(4) 迭代過程……
(5) 在某個結點處,關鍵詞的所有字母已被取出,則讀取附在該結點上的信息,即完成查找。
其他操作類似處理.


/**//*
Name: Trie樹的基本實現
Author: MaiK
Description: Trie樹的基本實現 ,包括查找 插入和刪除操作*/
#include<algorithm>
#include<iostream>
using namespace std;

const int sonnum=26,base='a';
struct Trie


{
int num;//to remember how many word can reach here,that is to say,prefix
bool terminal;//If terminal==true ,the current point has no following point
struct Trie *son[sonnum];//the following point
};
Trie *NewTrie()// create a new node


{
Trie *temp=new Trie;
temp->num=1;temp->terminal=false;
for(int i=0;i<sonnum;++i)temp->son[i]=NULL;
return temp;
}
void Insert(Trie *pnt,char *s,int len)// insert a new word to Trie tree


{
Trie *temp=pnt;
for(int i=0;i<len;++i)

{
if(temp->son[s[i]-base]==NULL)temp->son[s[i]-base]=NewTrie();
else temp->son[s[i]-base]->num++;
temp=temp->son[s[i]-base];
}
temp->terminal=true;
}
void Delete(Trie *pnt)// delete the whole tree


{
if(pnt!=NULL)

{
for(int i=0;i<sonnum;++i)if(pnt->son[i]!=NULL)Delete(pnt->son[i]);
delete pnt;
pnt=NULL;
}
}
Trie* Find(Trie *pnt,char *s,int len)//trie to find the current word


{
Trie *temp=pnt;
for(int i=0;i<len;++i)
if(temp->son[s[i]-base]!=NULL)temp=temp->son[s[i]-base];
else return NULL;
return temp;
}


轉自:http://hi.baidu.com/luyade1987/blog/item/2667811631106657f2de320a.html