01 | #include <iostream> |
02 | using namespace std; |
03 |
04 | typedef struct Node { //二叉树的结构 |
05 | int data; //存储的数据 |
06 | struct Node *lchild; //左孩子 |
07 | struct Node *rchild; //右孩子 |
08 | }*BiTree; |
09 |
10 | void CreateTree(BiTree &T) //创建一棵二叉树 |
11 | { |
12 | int i; |
13 | T=NULL; //输入数据之前先让该节点置空 |
14 | cin>>i; |
15 | if (i!=-1) //如果输入的数据不是结束符,则申请内存,创建二叉树或者子二叉树 |
16 | { |
17 | T= new Node; |
18 | T->data=i; //数据存储 |
19 |
20 | cout<< "请输入左孩子数据:" <<endl; |
21 | CreateTree(T->lchild); //创建左孩子 |
22 |
23 | cout<< "请输入右孩子数据:" <<endl; |
24 | CreateTree(T->rchild); //创建右孩子 |
25 | } |
26 | } |
27 |
28 | int Visit(BiTree &T) //访问某一节点指针指向该节点的数据并输出 |
29 | { |
30 | cout<<T->data<< " " ; |
31 | return T->data; |
32 | } |
33 |
34 | void PreOrder(BiTree &T) //先序遍历 |
35 | { |
36 | if (T==NULL) return ; //如果该节点为空,则停止继续访问 |
37 | Visit(T); //访问并输出根节点的值 |
38 | PreOrder(T->lchild); //再访问左孩子 |
39 | PreOrder(T->rchild); //再访问右孩子 |
40 | } |
41 |
42 | void MidOrder(BiTree &T) //中序遍历 |
43 | { |
44 | if (T==NULL) return ; //如果该节点为空,则停止继续访问 |
45 | MidOrder(T->lchild); //再访问左孩子 |
46 | Visit(T); //访问并输出根节点的值 |
47 | MidOrder(T->rchild); //再访问右孩子 |
48 | } |
49 |
50 | void PostOrder(BiTree &T) //后序遍历 |
51 | { |
52 | if (T==NULL) return ; //如果该节点为空,则停止继续访问 |
53 | |
54 | PostOrder(T->lchild); //再访问左孩子 |
55 | PostOrder(T->rchild); //再访问右孩子 |
56 | Visit(T); //访问并输出根节点的值 |
57 | } |
58 |
59 | void main() |
60 | { |
61 | BiTree T; |
62 | T=NULL; |
63 | CreateTree(T); |
64 | cout<< "先序遍历的结果是:" <<endl; |
65 | PreOrder(T); |
66 | cout<< "中序遍历的结果是:" <<endl; |
67 | MidOrder(T); |
68 | cout<< "后序遍历的结果是:" <<endl; |
69 | PostOrder(T); |
70 | } |
vector.cpp
01 | #include <iostream> |
02 | #include <vector> |
03 | #include <bitset> |
04 | #include <string> |
05 | using namespace std; |
06 | using std::vector; |
07 | using std::bitset; |
08 | /*void fun1() |
09 | { |
10 | vector <string> str; |
11 | vector <string>::iterator ite; |
12 | string s; |
13 | string s1="\00"; |
14 | while(true) |
15 | { |
16 | cin>>s; |
17 | if(strcmp(s,s1)==0)break; |
18 | str.push_back(s); |
19 | } |
20 | |
21 | for(ite=str.begin(); ite!=str.end();++ite) |
22 | { |
23 | cout<<*ite<<" "; |
24 | } |
25 | cout<<endl; |
26 | }*/ |
27 |
28 | void fun2() |
29 | { |
30 | vector<string> str(10); |
31 | vector<string>::iterator ite; |
32 | for (vector<string>::size_type id=0; id!=10; ++id) |
33 | { |
34 | cin>>str[id]; |
35 | } |
36 | for (ite=str.begin(); ite!=str.end(); ++ite) |
37 | { |
38 | cout<<*ite<< " " ; |
39 | } |
40 | cout<<endl; |
41 | } |
42 |
43 | void fun3() |
44 | { |
45 | bitset<32> bt; |
46 | int id; |
47 | for (id=0;id!=32;++id) |
48 | { |
49 | cout<<bt[id]<< " " ; |
50 | } |
51 | cout<<endl; |
52 | for (id=0;id!=32;id+=2) |
53 | { |
54 | bt[id]=1; |
55 | } |
56 | for (id=0;id!=32;++id) |
57 | { |
58 | cout<<bt[id]<< " " ; |
59 | } |
60 | cout<<endl; |
61 | for (id=1; id!=33; id+=2) |
62 | { |
63 | bt[id]=1; |
64 | } |
65 | for (id=0;id!=32;++id) |
66 | { |
67 | cout<<bt[id]<< " " ; |
68 | } |
69 | cout<<endl; |
70 | unsigned long p=bt.to_ulong(); |
71 | cout<< "p = " <<p<<endl; |
72 | bt.flip(); |
73 | for (id=0;id!=32;++id) |
74 | { |
75 | cout<<bt[id]<< " " ; |
76 | } |
77 | cout<<endl; |
78 | } |
79 | void main() |
80 | { |
81 | // fun1(); |
82 | fun2(); |
83 | fun3(); |
84 | } |
Thread.cpp
01 | #include <windows.h> |
02 | #include <iostream.h> |
03 |
04 | DWORD WINAPI ThProc1( |
05 | LPVOID lpParameter // thread data |
06 | ); |
07 |
08 | DWORD WINAPI ThProc2( |
09 | LPVOID lpParameter // thread data |
10 | ); |
11 |
12 | int index = 0; |
13 | int ticket = 10; |
14 | HANDLE hMutex; |
15 |
16 | void main() |
17 | { |
18 | HANDLE thread ; |
19 | HANDLE thread2; |
20 |
21 | thread = CreateThread(NULL,0,ThProc1,NULL,0,NULL); |
22 | thread2 = CreateThread(NULL,0,ThProc2,NULL,0,NULL); |
23 |
24 | CloseHandle( thread ); |
25 | CloseHandle(thread2); |
26 | |
27 | hMutex = CreateMutex(NULL, false ,NULL); |
28 | Sleep(6000); |
29 | /* while(index++<1000) |
30 | cout<<"main thread is running"<<endl;*/ |
31 | // while(ticket>0); |
32 |
33 | /* WaitForSingleObject(hMutex,INFINITE); |
34 | ReleaseMutex(hMutex); |
35 | ReleaseMutex(hMutex);*/ |
36 | } |
37 | DWORD WINAPI ThProc1( LPVOID lpParameter) |
38 | { |
39 | /* while(index++<1000) |
40 | cout<<"ThProc1 thread is running"<<endl;*/ |
41 | while ( true ) { |
42 | WaitForSingleObject(hMutex,INFINITE); |
43 | if (ticket>0) |
44 | { |
45 | Sleep(500); |
46 | cout<< "thread1 sell ticket:" <<ticket--<<endl; |
47 | } |
48 | else { |
49 | break ; |
50 | } |
51 | ReleaseMutex(hMutex); |
52 | } |
53 | return 0; //must have the return value |
54 | } |
55 |
56 | DWORD WINAPI ThProc2( LPVOID lpParameter) |
57 | { |
58 | /* while(index++<1000) |
59 | cout<<"ThProc1 thread is running"<<endl;*/ |
60 |
61 | while ( true ) { |
62 | WaitForSingleObject(hMutex,INFINITE); |
63 | if (ticket>0) |
64 | { |
65 | Sleep(500); |
66 | cout<< "thread2 sell ticket:" <<ticket--<<endl; |
67 | } |
68 | else { |
69 | break ; |
70 | } |
71 | ReleaseMutex(hMutex); |
72 | } |
73 | return 0; //must have the return value |
74 | } |