C++ / Küme İslemleri Yapan Bir Program


C ve C++ Programlama    C ve C++ Programlama İle İlgili Paylaşım Kategorisi

Yazar: x60    0 Yorum    123 Görüntüleme
  • Derecelendirme: 0/5 - 0 oy

Paylaşım Tarihi: 15.03.2016, 18:40:59 #1
x60 #Requiem
Cezalı Üye
Status: Çevrimdışı Yorumları:545 Konuları:161 Kayıt Tarihi:18.11.2015

PHP Kod:
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//class bölümü
#ifndef SET_H
#define SET_H




class Set{
private:
  int members[100];
  int index;
  int size;
public:
  Set();
  **** add_element(int i);
  **** remove();
  **** sort();
  Set intersection(Set &s);
  Set unions(Set &s);
  Set difference(Set &s);
  int get_size();
  **** display_set();
};




#endif












//bu bölüm cpp dosyası olacak
Set.cpp Fonksiyonların olduğu dosyaya yazılacak
#include"Set.h"
#include<iostream>
using std::cout;
using std::cin;
using std::endl;




Set::Set()
{
for(int i=0;i<100;i++) members[i]=0;
index=0;
size=0;
}




**** Set::add_element(int i)
{
members[index]=i;
index++;
size=index;
}




**** Set::remove()
{
size=size-1;
}




**** Set::sort()
{
int hold=0;
for(int pass=0;pass<size-1;pass++)
{
  for(int j=0;j<size-1;j++)
  {
    if(members[j]>members[j+1])
    {
      hold=members[j];
      members[j]=members[j+1];
      members[j+1]=hold;
    }
  }
}
}




Set Set::intersection(Set &s)
{
Set Sin;
for(int i=0;i<size;i++)
{
  for(int t=0;t<s.size;t++)
  {
    if(members[i]==s.members[t]) Sin.add_element(members[i]);
  }
}
return Sin;
}




Set Set::unions(Set &s)
{
Set Sun;
int num=0;
for(int j=0;j<size;j++)
{
  Sun.add_element(members[j]);
}
for(int y=0;y<s.size;y++)
{
  int hold=0;
  for(int r=0;r<size;r++)
  {
    if(s.members[y]!=Sun.members[r]) hold++;
  }
  if(hold==size) Sun.add_element(s.members[y]);
}
return Sun;
}




**** Set::display_set()
{
for(int i=0;i<size;i++)
{
  cout<<members[i]<<"  ";
}
}




Set Set::difference(Set &s)
{
Set Sdif;
for(int i=0;i<size;i++)
{
  int hold=0;
  for(int t=0;t<s.size;t++)
  {
    if(members[i]!=s.members[t]) hold++;
  }
  if(hold==s.size) Sdif.add_element(members[i]);
}
return Sdif;
}




int Set::get_size()
{
return size;
}




//main.cpp Main program
#include "Set.h"
#include <iostream>
using std::cout;
using std::endl;




int main()
{
Set s1,s2,s3,s4,s5;
 
//add elemts to the Sets and display them
for(int i=0;i<20;i++) s1.add_element(i);
cout<<"Set 1: "<<endl;
s1.display_set();
for(i=0;i<20;i+=2) s2.add_element(i);
cout<<endl<<endl<<"Set 2: "<<endl;
s2.display_set();
for(i=1;i<40;i*=2) s3.add_element(i);
cout<<endl<<"Set 3: "<<endl;
s3.display_set();
      
//find the intersection of the set 1 and set 3
s4=s1.intersection(s3);
cout<<endl<<endl<<"The intersection of the set_1 and set_3 (set_4): "<<endl;
s4.display_set();




//find the difference of set_3 and set_1
s3=s3.difference(s1);
cout<<endl<<endl<<"the difference of set_3 and set_1: "<<endl;
s3.display_set();




//find the union of the set_2 and set_4
s5=s4.unions(s2);
cout<<endl<<endl<<"The union of the set_2 and set_4(set_5): "<<endl;
s5.display_set();




//sort set_5
s5.sort();
cout<<endl<<endl<<"Sorted set(set_5): "<<endl;
s5.display_set();




//find the differnce of set_5 and set_1
s1=s1.difference(s5);
cout<<endl<<endl<<"the difference of set_5 and set_1(set_1): "<<endl;
s1.display_set();
       
//remove five elemnts from set_1
for(i=0;i<5;i++) s1.remove();
int a=0;
cout<<endl<<endl<<"remove five elements from set_1: "<<endl;
s1.display_set();
cout<<endl;
return 0;
}











Aradığınızı Bulamadınız Mı ?

Konuyu Okuyanlar:
1 Ziyaretçi