数据结构cpp二叉树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using namespace std;


typedef struct btreenode
{
char data;
struct btreenode *lchild;
struct btreenode *rchild;
}BTreeNode, *BTree;

typedef struct
{
BTree link;
int flag;
}Stack;


void InitBTree(BTree &BT)
{
BT = NULL;
}


void CreatBTree(BTree &BT)
{
char ch;
ch = getchar();
if (ch == ' ')
BT = NULL;
else
{
BT = new BTreeNode;
BT->data = ch;
CreatBTree(BT->lchild);
CreatBTree(BT->rchild);
}
}
void PreOrder(BTree BT)
{
if (BT == NULL)
{
cout << "NULL";
return;
}
else
{
cout << BT->data;
if (BT->lchild != NULL)
PreOrder(BT->lchild);
if (BT->rchild != NULL)
PreOrder(BT->rchild);
}
}


void InOrder(BTree BT)
{
if (BT == NULL)
{
cout << "NULL";
return;
}



if (BT->lchild != NULL)
InOrder(BT->lchild);


cout << BT->data;


if (BT->rchild != NULL)
InOrder(BT->rchild);

}


void PostOrder(BTree BT)
{
if (BT == NULL)
{
cout << "NULL";
return;
}


if (BT->lchild != NULL)
PostOrder(BT->lchild);


if (BT->rchild != NULL)
PostOrder(BT->rchild);


cout << BT->data;

}


void LevelOrder(BTree BT)
{
BTree Queue[30];
int front, rear;
if (BT == NULL) return;

front = -1;
rear = 0;
Queue[rear] = BT;


while (front != rear)
{
front++;
cout << Queue[front]->data;
if (Queue[front]->lchild != NULL)
{
rear++;
Queue[rear] = Queue[front]->lchild;
}


if (Queue[front]->rchild != NULL)
{
rear++;
Queue[rear] = Queue[front]->rchild;
}
}
}


void PreOrder_2(BTree BT)
{
BTree stack[30], p;
int top;
if (BT == NULL) return;
top = -1;
p = BT;
while (!(p == NULL && top == -1))
{
while (p != NULL)
{
cout << p->data;
if (top < 30)
{
stack[top + 1] = p;
top++;
}
else
{
cout << "Stack is full!" << endl;
return;
}
p = p->lchild;
}

if (top < 0) return;
else
{
top--;
p = stack[top + 1];
p = p->rchild;
}
}
}


void PostOrder_2(BTree BT)
{
Stack s[30];
BTree p;
int top, sign;
if (BT == NULL) return;
top = -1;
p = BT;
while (!(p == NULL && top == -1))
{
if (p != NULL)
{
s[++top].link = p;
s[top].flag = 1;
p = p->lchild;
}
else
{
p = s[top].link;
sign = s[top].flag;
top--;
if (sign == 1)
{
s[++top].link = p;
s[top].flag = 2;
p = p->rchild;
}
else
{
cout << p->data;
p = NULL;
}
}
}
}


void main()
{
BTree T;
InitBTree(T);
cout << "InitTree completed!" << endl;
cout << "Create Tree:" << endl;
CreatBTree(T);
cout << "/nPreOrder:" << endl;
PreOrder(T);
cout << "/nInOrder:" << endl;
InOrder(T);
cout << "/nPosetOrder:" << endl;
PostOrder(T);
cout << "/nLevelOrder:" << endl;
LevelOrder(T);
cout << "/nPreOrder_2:" << endl;
PreOrder_2(T);
cout << "/nPostOrder_2:" << endl;
PostOrder_2(T);
system("pause");
}

java模拟四则运算软件

写这个程序也为了纪念王江民先生,话说当年他也是写过这个类似程序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.applet.*;   
import java.awt.*;
import java.awt.event.*;

public class game extends Applet implements ActionListener{
//定义变量
int num1, num2, num3, jieguo, jilu, error = 0, right = 0, key = 0;
char op;
float percent = 0;
String res, lev;
Image labmag;
AudioClip audio1, audio2;
Label prompt1,prompt2;
TextField qus, ans, info;
Button next, enter, record;

public void init()
{
//初始化图片
labmag = getImage (getCodeBase(),"1.jpg");
//初始化音乐
audio1 = getAudioClip(getDocumentBase(),"Error.au");
audio2 = getAudioClip(getDocumentBase(),"Right.au");
qus = new TextField(15);
//初始化按钮,文本框
ans = new TextField(10);
info = new TextField(30);
enter = new Button("确定");
next = new Button("新题目");
record = new Button("记录");
prompt1 = new Label("问题:");
prompt2 = new Label("结果:");
//建立按钮和文本框
add(prompt1);
add(qus);
add(ans);
add(enter);
add(next);
add(record);
add(prompt2);
add(info);
//三个按钮的监听
next.addActionListener(this);
enter.addActionListener(this);
record.addActionListener(this);
}

public void paint(Graphics g)
{
//绘图
g.drawImage(labmag, 0,0,430,280,this);
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==next)
{
//随机数生成
num1 = (int)(Math.random()*9);
num2 = (int)(Math.random()*9);
num3 = (int)(Math.random()*3);
switch(num3)
{
case 0 : op = '+'; jilu = num1+num2; break;
case 1 : op = '-'; jilu = num1-num2;break;
case 2 : op = '*'; jilu = num1*num2;break;
}
//考虑到整数除法带来的小数问题,暂时取消了生成除法
qus.setText(""+num1+op+num2+"=");
key = 1;
}
if (e.getSource()==enter)
{
if(ans.getText()!="")
jieguo = Integer.parseInt(ans.getText());
if (jieguo == jilu)
{
res="真棒,答对了!";
audio2.play(); //播放音乐
if (key == 1) //答对只记一次分
{
right++;
key = 0;
}
}
else
{
res = "答错了。别灰心!";
audio1.play(); //播放音乐
error++; //答错扣分
}
info.setText(""+res);
}
if (e.getSource()==record)
{
//级别判定
if (right > 0 && right <= 10)
lev = "懒羊羊";
if (right > 10 && right <= 23)
lev = "暖羊羊";
if (right > 23 && right <= 38)
lev = "沸羊羊";
if (right > 38 && right <= 56)
lev = "美羊羊";
if (right > 56 && right <= 76)
lev = "喜绵绵";
if (right > 76 && right <= 100)
lev = "软绵绵";
if (right > 100)
lev = "天神";
info.setText("答对" + right + "题" + " 答错" + error + "题" + " 等级:" + lev);
}
}
}

java Applet实现播放音乐

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.applet.*;
import java.awt.event.*;
import java.awt.*;

@SuppressWarnings("serial")
public class music extends Applet
implements ActionListener
{
AudioClip audio;
Button btExit,btOpen,btPlay,btLoop,btStop;
/**
*
方法说明:初始化Applet
*
输入参数:
*
返回类型:
*/
public void init()
{
//加载声音文件
audio = getAudioClip(getDocumentBase(), "music.au");//这里放上你加在新建文件夹里面的歌曲名称,au格式
//构造按钮
setLayout(new FlowLayout());
btPlay = new Button("Play");
btPlay.addActionListener(this);
btLoop = new Button("Loop");
btLoop.addActionListener(this);
btStop = new Button("Stop");
btStop.addActionListener(this); //给Play按钮添加一个监听事件
//将按钮添加到Applet中
add(btPlay);
add(btLoop);
add(btStop);
}

public void actionPerformed(ActionEvent e)
{

//如果点击的是Play按钮
if (e.getSource() == btPlay)
{
play();
}
//如果点击的是loop按钮
if (e.getSource() == btLoop)
{
loop();
}
//如果点击的是stop按钮
if (e.getSource() == btStop)
{
stop();
}
}
/**
*
方法说明:播放声音
*
输入参数:
*
返回类型:
*/
public void play()
{
if (audio != null) stop();
audio.play();
}
/**
*
方法说明:循环播放声音
*
输入参数:
*
返回类型:
*/
public void loop()
{
if (audio != null)
audio.loop();
}
/**
*
方法说明:停止播放声音
*
输入参数:
*
返回类型:
*/
public void stop()
{
if (audio != null)
audio.stop();
}
}