Skip to content

Commit f09f4d4

Browse files
authored
Create 商品归类.cpp
1 parent af5c9a0 commit f09f4d4

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

第一章/商品归类.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*变量名要用有意义的名字!!!
2+
*变量名要用有意义的名字!!!
3+
*变量名要用有意义的名字!!!*/
4+
#include <iostream>
5+
#include<string>
6+
using namespace std;
7+
8+
class Node{
9+
public:
10+
char name;
11+
int num;
12+
Node *next;
13+
void getData()
14+
{
15+
cin>>this->name;
16+
cin>>this->num;
17+
}
18+
};
19+
20+
class Nlist{
21+
public:
22+
Nlist(){
23+
head=NULL;
24+
}
25+
void insert(Node *node);
26+
void print();
27+
Node *head;
28+
};
29+
30+
void Nlist::insert(Node *node){
31+
if (head==NULL){
32+
head=node;
33+
return;
34+
}
35+
Node *current=head;
36+
while (current->next!=NULL){
37+
current=current->next;
38+
}
39+
current->next=node;
40+
return;
41+
}
42+
43+
void Nlist::print(){
44+
if (head==NULL){
45+
return;
46+
}
47+
Node *current=head;
48+
while (current->next!=NULL){
49+
cout<<current->name<<' '<<current->num<<',';
50+
current=current->next;
51+
}
52+
cout<<current->name<<' '<<current->num<<';'<<endl;
53+
return;
54+
}
55+
56+
void update(Nlist L,Nlist Lin){
57+
Node *p,*q,*tmp;
58+
p=L.head;
59+
q=Lin.head;
60+
while (q!=NULL){
61+
while (p->next!=NULL&&p->next->name<q->name){
62+
p=p->next;
63+
}
64+
if (p->next==NULL){
65+
p->next=q;
66+
return;
67+
}
68+
if (p->name==q->name){
69+
p->num+=q->num;
70+
q=q->next;
71+
}
72+
else if (p->next->name==q->name){
73+
p->next->num+=q->num;
74+
q=q->next;
75+
}
76+
else if (q->name<p->next->name){
77+
tmp=q->next;
78+
q->next=p->next;
79+
p->next=q;
80+
q=tmp;
81+
p=p->next;
82+
}
83+
else{
84+
p->next->num+=q->num;
85+
q=q->next;
86+
}
87+
}
88+
return;
89+
}
90+
91+
92+
int main(){
93+
Nlist base_list,in_list;
94+
char base[80],
95+
in[80];
96+
int i,j;
97+
while (true)
98+
{
99+
Node *node =new Node();
100+
node->getData();
101+
base_list.insert(node);
102+
if (cin.get()==';')
103+
{
104+
break;
105+
}
106+
}
107+
while (true)
108+
{
109+
Node *node =new Node();
110+
node->getData();
111+
in_list.insert(node);
112+
if (cin.get()==';')
113+
{
114+
break;
115+
}
116+
}
117+
update(base_list,in_list);
118+
base_list.print();
119+
return 0;
120+
}

0 commit comments

Comments
 (0)