-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSayısalTürev.c
293 lines (266 loc) · 9.52 KB
/
SayısalTürev.c
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include<locale.h>
#define MAX_SIZE 100
// Structure to represent a stack
struct Stack
{
int top;
int totalSize;
char *arrayStack;
};
//for evaluation
typedef struct {
int top;
int totalSize;
double *stack;
} stack2;
// Function prototypes
void initializeStack(struct Stack *stack, int size);
void destroyStack(struct Stack *stack);
int isEmpty(struct Stack *stack);
int isFull(struct Stack *stack);
char peek(struct Stack *stack);
char pop(struct Stack *stack);
void push(char element, struct Stack *stack);
int precedence(char ch);
int isOperand(char element);
char* infixToPostfix(char *expressionArray, struct Stack *stack);
void initializeStack2(stack2 *s, int size);
void push2(stack2 *s, double item);
double pop2(stack2 *s);
int is_operator(char symbol);
double evaluate(char* expression, double x);
double evaluateExpression(char* postfix, double x);
void getInputforST(char *fonksiyon, double *x, double *h, int *method);
double SayisalTurev(char *postfix, double x, double h, int method);
int main() {
setlocale(LC_ALL, "turkish");
char fonksiyon[MAX_SIZE];
double x, h, turev;
int fark;
struct Stack stack;
getInputforST(fonksiyon, &x, &h, &fark);
char* postfix=infixToPostfix(fonksiyon,&stack);
turev = SayisalTurev(postfix, x, h, fark);
printf("%.4lf deðeri için türev %.4lf bulundu.\n", x, turev);
free(postfix);
return 0;
}
double SayisalTurev(char *postfix, double x, double h, int method) {
double turev;
switch(method) {
case 1:
// ileri fark
turev = (evaluateExpression(postfix, x + h) - evaluateExpression(postfix, x)) / h;
break;
case 2:
// geri fark
turev = (evaluateExpression(postfix, x) - evaluateExpression(postfix, x - h)) / h;
break;
case 3:
// merkezi fark
turev = (evaluateExpression(postfix, x + h) - evaluateExpression(postfix, x - h)) / (2 * h);
break;
default:
printf("Geçersiz seçenek. Varsayýlan olarak merkezi fark kullanýlacak.\n");
turev = (evaluateExpression(postfix, x + h) - evaluateExpression(postfix, x - h)) / (2 * h);
break;
}
return turev;
}
void getInputforST(char *fonksiyon, double *x, double *h, int *method) {
printf("Türevini bulmak istediðiniz fonksiyonu ve deðerlerini giriniz:\n");
// Get the function name
printf("Türevini bulmak istediðiniz fonksiyonu giriniz\n");
scanf("%s", fonksiyon);
getchar(); // Consume the newline character left by scanf
// Get the derivative value
printf("Türevini bulmak istediðiniz deðerini giriniz:");
scanf("%lf", x);
getchar(); // Consume the newline character left by scanf
// Get the h value
printf("h deðerini giriniz: ");
scanf("%lf", h);
getchar(); // Consume the newline character left by scanf
// Select the method
printf("Hangi yöntemi kullanacaðýnýzý seçiniz.\n");
printf("1. Ýleri farkla\n");
printf("2. Geri farkla\n");
printf("3. Merkezi farkla\n");
scanf("%d", method);
getchar(); // Consume the newline character left by scanf
}
//-------------------------------Infix to Postfix--------------------------------------------//
void initializeStack(struct Stack *stack, int size) {
stack->totalSize = size;
stack->top = -1;
stack->arrayStack = (char *)malloc(stack->totalSize * sizeof(char));
}
void destroyStack(struct Stack *stack) {
free(stack->arrayStack);
}
int isEmpty(struct Stack *stack) {
return stack->top == -1;
}
int isFull(struct Stack *stack) {
return stack->top == stack->totalSize - 1;
}
char peek(struct Stack *stack) {
if (isEmpty(stack))
return -1;
return stack->arrayStack[stack->top];
}
char pop(struct Stack *stack) {
if (isEmpty(stack))
return -1;
return stack->arrayStack[stack->top--];
}
void push(char element, struct Stack *stack) {
if (isFull(stack)) {
printf("Stack is already Full.");
return;
}
stack->arrayStack[++stack->top] = element;
}
int precedence(char ch) {
switch (ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
int isOperand(char element) {
return (element >= 'A' && element <= 'Z') || (element >= 'a' && element <= 'z');
}
int isInteger(char element){
return (element >= '0' && element <= '9');
}
char* infixToPostfix(char *expressionArray, struct Stack *stack) {
initializeStack(stack, strlen(expressionArray)); // Initialize stack
int postfixSize = strlen(expressionArray) * 2; // Initial estimate of postfix size
char *postfix = (char *)malloc(postfixSize * sizeof(char)); // Allocate memory for postfix expression
int postfixIndex = 0; // Index for postfix expression
int currIndex = 0;
while (expressionArray[currIndex] != '\0') {
if (isOperand(expressionArray[currIndex]) || isInteger(expressionArray[currIndex])) {
while (isOperand(expressionArray[currIndex]) || isInteger(expressionArray[currIndex])) {
postfix[postfixIndex++] = expressionArray[currIndex++];
}
postfix[postfixIndex++] = ' '; // Insert space after each operand
} else if (expressionArray[currIndex] == '(') {
push(expressionArray[currIndex], stack);
currIndex++;
} else if (expressionArray[currIndex] == ')') {
while (peek(stack) != '('){
postfix[postfixIndex++] = pop(stack);
postfix[postfixIndex++]=' ';
}
pop(stack); // Pop '('
currIndex++;
} else {
while (!isEmpty(stack) && precedence(peek(stack)) >= precedence(expressionArray[currIndex])){
postfix[postfixIndex++]= pop(stack);
postfix[postfixIndex++]=' ';
}
push(expressionArray[currIndex], stack);
currIndex++;
}
}
// Pop remaining operators from stack
while (!isEmpty(stack)){
postfix[postfixIndex++] = pop(stack);
postfix[postfixIndex++]=' ';
}
postfix[postfixIndex] = '\0'; // Null-terminate the string
destroyStack(stack); // Deallocate memory used by stack
return postfix;
}
void initializeStack2(stack2 *s, int size) {
s->top = -1;
s->totalSize = size;
s->stack = (double *)calloc(s->totalSize , sizeof(double));
if (s->stack == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
}
void push2(stack2 *s, double item) {
if (s->top >= s->totalSize - 1) {
printf("Stack Overflow\n");
exit(EXIT_FAILURE);
}
s->top++;
s->stack[s->top] = item;
}
double pop2(stack2 *s) {
if (s->top < 0) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
}
double item = s->stack[s->top];
s->top--;
return item;
}
int is_operator(char symbol) {
return (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/' || symbol == '^');
}
double evaluate(char* expression, double x) { // Modified to take double instead of double*
stack2 s;
initializeStack2(&s, strlen(expression));
double operand1, operand2, result;
char *token = strtok(expression, " ");
while (token!= NULL) {
if (isdigit(*token) || (*token == '-' && isdigit(*(token + 1)))) {
// If it's a number, push it onto the stack
push2(&s, atof(token));
} else if (*token == 'x') {
// If it's 'x', push the value of x onto the stack
push2(&s, x);
} else if(*token == 'e'){
push2(&s, 2.718281828459);
} else if (is_operator(*token)) {
// If it's an operator, pop operands from the stack, perform the operation, and push the result onto the stack
operand2 = pop2(&s);
operand1 = pop2(&s);
switch (*token) {
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/': if (operand2 == 0) {
printf("Division by zero error\n");
exit(EXIT_FAILURE);
}
result = operand1 / operand2; break;
case '^': result = pow(operand1, operand2); break;
default: printf("Unknown operator: %c\n", *token); exit(EXIT_FAILURE);
}
push2(&s, result);
} else {
printf("Unknown token: %s\n", token);
exit(EXIT_FAILURE);
}
token = strtok(NULL, " ");
}
return result;
}
double evaluateExpression(char* postfix, double x) {
char* postfix_copy = strdup(postfix); // Make a copy of postfix because strtok modifies the original string
if (!postfix_copy) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
double result = evaluate(postfix_copy, x); // Pass the double value directly
free(postfix_copy); // Free the copied postfix expression
return result;
}