-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnum_2.cpp
More file actions
117 lines (106 loc) · 1.75 KB
/
num_2.cpp
File metadata and controls
117 lines (106 loc) · 1.75 KB
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
#include<iostream>
#include<forward_list>
#include<string>
using namespace std;
bool add_numbers(forward_list<int>& ptr_to_add)
//require a forwardList
{
int previous = 0;
int tempt;
auto inter_to_add = ptr_to_add.before_begin();
while (cin>>tempt)
{
if (tempt >= previous)
{
ptr_to_add.insert_after(inter_to_add,tempt);
previous = tempt;
inter_to_add++;
}
else
{
if (tempt == -1)
{
return 1;
}
else
{
return 0;
}
}
}
return 0;
}
int main()
{
forward_list<int> first;
forward_list<int> second;
forward_list<int> target;
while (true)
{
if (add_numbers(first) && add_numbers(second))
{
auto itr_first = first.begin();
auto itr_second = second.begin();
auto itr_target = target.before_begin();
while (itr_first!=first.end() && itr_second!=second.end())
{
int num_first = *itr_first;
int num_second = *itr_second;
if (num_first > num_second)
{
itr_second++;
}
else if (num_first < num_second)
{
itr_first++;
}
else
{
target.insert_after(itr_target, num_first);
itr_first++;
itr_second++;
itr_target++;
}
}
if (!target.empty())
{
for (int& ele : target)
{
cout << ele <<' ';
}
cout << endl;
}
else
{
cout << "NULL" << endl;
}
first.clear();
second.clear();
target.clear();
cout << "已经完成处理,是否继续?(y/n)" << endl;
string option;
cin >> option;
if (option.empty())
{
exit(0);
}
else
{
if (option[0] == 'y' || option[0] == 'Y')
{
continue;
}
else
{
exit(0);
}
}
}
else
{
cout << "输入格式有误,请重新输入" << endl;
first.clear();
second.clear();
}
}
}