-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupn3.c
93 lines (79 loc) · 1.54 KB
/
upn3.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
/*
*
* Module: upn3.c
* Author: mp3skater
* Version: 0.1
* License: MIT
* Changelog:
* 2024/03/14 -- mp3skater: Initial commit
* 2
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "calc.h"
#include "getNum.h"
#include "stack.h"
#define MAX_LEN 256
/*
* Parameter: A char[] which containes the equation
* Rückgabe: The answer in double
* Schnittstellen: K.A
* AIAIAIA: K.A
* Aufgabe: Berechnung mehrer funktionen UPN mit Datei
*
*/
double getUPN(char *);
int main(void)
{
// OPEN FILE
FILE * data;
data = fopen("/home/derretter/c/stack/upn/equ.txt", "r");
// ERROR FILE NOT FOUND
if(data == NULL) {
printf("Error, file \"/home/derretter/c/stack/upn/equ.txt\"");
return 1;
}
char buffer[MAX_LEN];
while(fgets(buffer, MAX_LEN, data))
{
// Remove trailing newline
buffer[strcspn(buffer, "\n")] = 0;
printf("%lf\n", getUPN(buffer));
}
fclose(data);
return 0;
}
double getUPN(char *a) {
// ANSWER VARIABLE
double ans = 0;
// STACK VARIABLES
double left;
double right;
int nInd = -1;
int l = strlen(a);
for(int i = 0; i<l; i++) {
// DIGIT
if(isdigit(a[i])) {
// FIRST DIGIT
if(nInd == -1)
nInd = i;
continue;
}
// SPACE
else if(a[i] == 32) {
push(getNum(a, nInd, i-1));
nInd = -1;
continue;
}
// OPERANT
else {
right = pop();
left = pop();
calc(left, a[i], right, &ans);
push(ans);
i++;
}
}
return pop();
}