Skip to content

Commit 3c9f7bd

Browse files
committed
add first
1 parent 98b3aa6 commit 3c9f7bd

File tree

51 files changed

+1372
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1372
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6" />
4+
<Project>
5+
<Option title="experiment01" />
6+
<Option pch_mode="2" />
7+
<Option compiler="gcc" />
8+
<Build>
9+
<Target title="Debug">
10+
<Option output="bin/Debug/experiment01" prefix_auto="1" extension_auto="1" />
11+
<Option object_output="obj/Debug/" />
12+
<Option type="1" />
13+
<Option compiler="gcc" />
14+
<Compiler>
15+
<Add option="-g" />
16+
</Compiler>
17+
</Target>
18+
<Target title="Release">
19+
<Option output="bin/Release/experiment01" prefix_auto="1" extension_auto="1" />
20+
<Option object_output="obj/Release/" />
21+
<Option type="1" />
22+
<Option compiler="gcc" />
23+
<Compiler>
24+
<Add option="-O2" />
25+
</Compiler>
26+
<Linker>
27+
<Add option="-s" />
28+
</Linker>
29+
</Target>
30+
</Build>
31+
<Compiler>
32+
<Add option="-Wall" />
33+
<Add option="-fexceptions" />
34+
</Compiler>
35+
<Unit filename="main.cpp" />
36+
<Extensions>
37+
<code_completion />
38+
<envvars />
39+
<debugger />
40+
</Extensions>
41+
</Project>
42+
</CodeBlocks_project_file>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# depslib dependency file v1.0
2+
1490450162 source:c:\users\dell\desktop\curriculum\semester_4\datastructure\experiment\experiment01\main.cpp
3+
<iostream>
4+
<fstream>
5+
<stdlib.h>
6+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_layout_file>
3+
<ActiveTarget name="Debug" />
4+
<File name="main.cpp" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
5+
<Cursor>
6+
<Cursor1 position="497" topLine="4" />
7+
</Cursor>
8+
</File>
9+
</CodeBlocks_layout_file>

experiment/experiment01/in1.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0.5 2
2+
0.5 3
3+
1 1
4+
0 0

experiment/experiment01/in2.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1.5 3
2+
5.5 5
3+
3 2
4+
0 0

experiment/experiment01/input.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2 2
2+
4 4
3+
0 0
4+
5 5
5+
3 3
6+
0 0
7+
3 3
8+
2 2
9+
1 1
10+
0 0
11+
7 7
12+
8 8
13+
9 9
14+
0 0
15+
1 1
16+
3 3
17+
2 2
18+
4 4
19+
6 6
20+
0 0
21+
7 7
22+
9 9
23+
2 2
24+
5 5
25+
0 0

experiment/experiment01/main.cpp

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
顺序表的链式存储结构
3+
*/
4+
5+
#include <iostream>
6+
#include <fstream>
7+
#include <stdlib.h>
8+
using namespace std;
9+
#define CreateNode(p) p=(Polyn)malloc(sizeof(PNode))
10+
#define DeleteNode(p) free((void *)(p))
11+
//结构体的定义
12+
typedef struct Polynode
13+
{
14+
15+
int c; //系数
16+
int e; //指数
17+
struct Polynode *next; //指针
18+
}PNode, *Polyn;
19+
20+
ifstream is("input.txt"); //从文件input.txt读取数据
21+
ofstream os("output.txt"); //从文件output.txt写入程序运行结果数据
22+
23+
//按照先入先出发创建顺序表,即先输入的数据存储在前面
24+
Polyn create()
25+
{
26+
27+
Polyn last, p;
28+
Polyn h = new PNode; h->next = NULL;
29+
last = h;
30+
int c, e;
31+
while(is >> c >> e && c!= 0)
32+
{
33+
p = new PNode;
34+
p->c = c;
35+
p->e = e;
36+
last->next = p;
37+
last = p;
38+
}
39+
last->next = NULL;
40+
return h;
41+
}
42+
//打印出多项式
43+
void print(Polyn h)
44+
{
45+
Polyn p = h->next;
46+
while(p)
47+
{
48+
os << p->c << " " << p->e << endl;
49+
p = p->next;
50+
}
51+
}
52+
53+
//按照指数升序排列,默认输入数据没有相同的指数
54+
void upsort(Polyn h)
55+
{
56+
Polyn q, s, pr, p;
57+
q = h->next;
58+
h->next = NULL; //断开头结点
59+
while(q)
60+
{
61+
s = q;q = q->next; //摘下节点
62+
pr = h; p = h->next; //初始化主从动指针
63+
while(p && p->e < s->e )
64+
{
65+
pr = p;
66+
p = p->next;
67+
}
68+
//following insert node *s
69+
pr->next = s;
70+
s->next = p;
71+
}
72+
}
73+
/*
74+
两个多项式相加算法类似于两个升序表的归并算法;
75+
以下算法考虑利用两个源多项式结点生成和式的结点;
76+
算法要求两个源多项式结点按指数e升序连接;
77+
算法结束后,两个源多项式成为空链表。
78+
*/
79+
Polyn add(Polyn h1, Polyn h2)//函数返回和式头指针
80+
{
81+
Polyn p1, p2, p3, h, p;
82+
p1 = h1->next; p2 = h2->next;
83+
CreateNode(h); p3 = h;//h为和式附加头结点指针
84+
while(p1 && p2)
85+
{
86+
if(p1->e < p2->e)
87+
{
88+
p = p1;
89+
p1 = p1->next;
90+
}else if(p2->e < p1->e)
91+
{
92+
p = p2;
93+
p2 = p2->next;
94+
}else//p1->e==p2->e为真时
95+
{
96+
p1->c = p1->c + p2->c;
97+
if( p1->c == 0)//p1->c==0应删除两个结点
98+
{
99+
p = p1; p1 = p1->next; DeleteNode(p);
100+
p = p2; p2 = p2->next; DeleteNode(p);
101+
continue;
102+
}
103+
p = p2; p2 = p2->next; DeleteNode(p);
104+
p = p1; p1 = p1->next;
105+
}
106+
p3->next = p; p3 = p;//插入*p结点至和式末尾
107+
}
108+
if(p1)
109+
p3->next = p1;
110+
else if(p2)
111+
p3->next = p2;
112+
else
113+
p2->next = NULL;
114+
return h;
115+
}
116+
117+
/*
118+
//原理性算法如下
119+
R(x)=0;
120+
for(i=0; i<n; i++)
121+
{ T(x)=P(x)cixdi; //T(x)结点数与P(x)相同
122+
R(x)=R(x)+T(x); //利用加法算法
123+
}
124+
*/
125+
Polyn mul(Polyn hp, Polyn hq)
126+
{
127+
Polyn hr, ht, p, q, pt;
128+
CreateNode(hr); hr->next = NULL;//R(x)=0;
129+
CreateNode(ht); ht->next = NULL;//T(x)=0;
130+
q = hq->next;
131+
while(q)//实现for(i=0; i<n; i++)
132+
{
133+
pt = ht;
134+
p = hp->next;
135+
while(p)
136+
{
137+
//以下实现T(x)=P(x)cixdi;
138+
CreateNode(pt->next);
139+
pt = pt->next;
140+
pt->c = p->c * q->c;
141+
pt->e = p->e + q->e;
142+
p = p->next;
143+
}
144+
pt->next = NULL;
145+
q = q->next;
146+
//以下实现R(x)=R(x)+T(x)
147+
p = add(hr, ht);
148+
DeleteNode(hr);
149+
hr = p;
150+
}
151+
DeleteNode(ht);
152+
return hr;
153+
}
154+
int main()
155+
{
156+
157+
int T = 3; //测试样例次数
158+
while(T--)
159+
{
160+
Polyn hp, hq, hmul;
161+
hp = create();
162+
hq = create();
163+
upsort(hp);
164+
upsort(hq);
165+
os << "case" << 3-T << " :"<< endl;
166+
os << "hp排序后:" << endl;
167+
print(hp);
168+
os << "hp排序后:" << endl;
169+
print(hq);
170+
hmul = mul(hp, hq);
171+
os << "hp*hq的结果:" << endl;
172+
print(hmul);
173+
}
174+
return 0;
175+
}
18 KB
Binary file not shown.

experiment/experiment01/out.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
1.50,4
2+
2.25,5
3+
3.00,3
4+
0.75,6
5+
1.50,4
6+
2.75,7
7+
2.75,8
8+
5.50,6

experiment/experiment01/output.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
case1 :
2+
hp�����
3+
2 2
4+
4 4
5+
hp�����
6+
3 3
7+
5 5
8+
hp*hq�Ľ��:
9+
6 5
10+
22 7
11+
20 9
12+
case2 :
13+
hp�����
14+
1 1
15+
2 2
16+
3 3
17+
hp�����
18+
7 7
19+
8 8
20+
9 9
21+
hp*hq�Ľ��:
22+
7 8
23+
22 9
24+
46 10
25+
42 11
26+
27 12
27+
case3 :
28+
hp�����
29+
1 1
30+
2 2
31+
3 3
32+
4 4
33+
6 6
34+
hp�����
35+
2 2
36+
5 5
37+
7 7
38+
9 9
39+
hp*hq�Ľ��:
40+
2 3
41+
4 4
42+
6 5
43+
13 6
44+
10 7
45+
34 8
46+
34 9
47+
30 10
48+
76 11
49+
27 12
50+
78 13
51+
54 15

0 commit comments

Comments
 (0)