VeriYapıları C# Örnekleri
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
|
public class Ornek
{
public int icerik;
public Ornek(int icerik)
{
this.icerik = icerik;
}
}
public class Cikin {
Ornek dizi[];
public int ust;
public int N;
public Cikin(int N)
{
dizi=new Ornek(N);
this.N=N;
ust=-1;
}
Ornek ust()
{
return dizi[ust];
}
Boolean cikinDolu()
{
if (ust==N-1)
return true;
else
return false;
}
Boolean cikinBos()
{
if(ust==-1)
return true;
else
return false;
}
}
void cikinEkle(Ornek yeni)
{
if (!cikinDolu())
{
ust++;
dizi[ust]=yeni;
}
}
Ornek cikinSil()
{
if(!cikinBos())
{
ust--;
return dizi[ust+1];
}
return null;
}
public class Cikin{
Eleman ust;
public Cikin()
{
ust=null;
}
boolean cikinBos()
{
if (ust==null)
{
return null;
}
else
return false;
}
}
void cikinEkle(Eleman yeni)
{
yeni.ileri=ust;
ust=yeni;
}
public Eleman cikinSil()
{
public Eleman else=ust;
if(!cikinBos())
ust=ust.ileri;
return e;
}
public class Ornek{
public int tip;
public int islenen;
public char islem;
public int oncelik;
public Ornek(int islenen)
{
this.tip=0;
this.islenen=islenen;
}
public Ornek(char islem)
{
this.tip=1;
this.islem=islem;
switch(islem)
{
case '(':
oncelik=0;
break;
case '+':
case '-':
oncelik=1;
break;
case '*':
case '/':
oncelik=2;
break;
case ')':
oncelik=3;
break;
}
}
}
public int hesapla(Ornek[] ifade)
{
public int i;
Ornek e,e1,e2,s;
Cikin c=new Cikin(100);
for ( i = 0; i < ifade.length; i++)
{
e=ifade[i];
if (e.tip==0)
{
c.cikinEkle(e);
}
else
{
e2=c.cikinSil();
e1=c.cikinSil();
s=islem(e.islem,e1.islenen,e2.islenen);
c.cikinEkle(s);
}
}
e=c.cikinSil();
return e.islenen;
}
Ornek islem(char ch,int e1,int e2)
{
public int sonuc;
switch(char ch,int e1,int e2)
{
public int sonuc;
switch (ch)
{
case '+':
sonuc=e1+e2;
break;
case '-':
sonuc=e1-e2;
break;
case '*':
sonuc=e1*e2;
break;
case '/':
sonuc=e1/e2;
break;
}
return new Ornek(sonuc);
}
}
|
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Infix
{
class Program
{
static bool sayılar(ref string infixsayı, out string postfixsayı)
{
int sayı = 0;
postfixsayı = "";
Stack<Char> s1 = new Stack<char>();
for (int i = 0; i < infixsayı.Length; i++)
{
char ch = infixsayı[i];
if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
if (s1.Count <= 0)
s1.Push(ch);
else
{
if (s1.Peek() == '*' || s1.Peek() == '/')
sayı = 1;
else
sayı = 0;
if (sayı == 1)
{
if (ch == '+' || ch == '-')
{
postfixsayı += s1.Pop();
i--;
}
else
{
postfixsayı += s1.Pop();
i--;
}
}
else
{
if (ch == '+' || ch == '-')
{
postfixsayı += s1.Pop();
s1.Push(ch);
}
else
s1.Push(ch);
}
}
}
else
{
postfixsayı += ch;
}
}
int len = s1.Count;
for (int j = 0; j < len; j++)
postfixsayı += s1.Pop();
return true;
}
static void Main(string[] args)
{
string infixsayı = "";
string postfixsayı = "";
if (args.Length == 1)
{
infixsayı = args[0];
sayılar(ref infixsayı, out postfixsayı);
System.Console.WriteLine("InFix :\t" + infixsayı);
System.Console.WriteLine("PostFix:\t" + postfixsayı);
}
else
{
infixsayı= "(5+6)*8";
sayılar(ref infixsayı, out postfixsayı);
System.Console.WriteLine("InFix :\t" + infixsayı);
System.Console.WriteLine("PostFix:\t" + postfixsayı);
System.Console.WriteLine();
Console.ReadKey();
}
}
}
}
|
Leave a reply