mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 09:10:14 +00:00
calk added
This commit is contained in:
Executable
BIN
Binary file not shown.
@@ -0,0 +1,65 @@
|
||||
#include "head.h"
|
||||
|
||||
Stack get(Stack *l, int n) {
|
||||
if (n > len(l))
|
||||
exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
l = l->next;
|
||||
}
|
||||
return *l;
|
||||
}
|
||||
|
||||
Stack *add_head(Stack *l, double x) {
|
||||
if (l == NULL) {
|
||||
l = malloc(sizeof(Stack));
|
||||
if (l == NULL)
|
||||
exit(1);
|
||||
l->x = x;
|
||||
l->next = NULL;
|
||||
}
|
||||
|
||||
if (l->x == 0.0) {
|
||||
l->x = x;
|
||||
l->next = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
Stack *tmp = malloc(sizeof(Stack));
|
||||
if (tmp == NULL)
|
||||
exit(1);
|
||||
|
||||
tmp->next = l;
|
||||
tmp->x = x;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void stack_print(Stack *l) {
|
||||
while (l != NULL) {
|
||||
printf("%lF\n", l->x);
|
||||
l = l->next;
|
||||
}
|
||||
}
|
||||
|
||||
int len(Stack *l) {
|
||||
int ans = 0;
|
||||
while (l != NULL) {
|
||||
ans++;
|
||||
l = l->next;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
Stack *del_head(Stack *l) {
|
||||
if (l == NULL)
|
||||
return NULL;
|
||||
else if (l->next == NULL) {
|
||||
free(l);
|
||||
l = NULL;
|
||||
return NULL;
|
||||
}
|
||||
Stack *tmp = l->next;
|
||||
free(l);
|
||||
l = NULL;
|
||||
return tmp;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define N 6
|
||||
|
||||
typedef struct Stack {
|
||||
double x;
|
||||
struct Stack *next;
|
||||
} Stack;
|
||||
|
||||
Stack get(Stack *l, int n);
|
||||
Stack *add_head(Stack *l, double x);
|
||||
void stack_print(Stack *l);
|
||||
int len(Stack *l);
|
||||
Stack *del_head(Stack *l);
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "head.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
srand(time(0));
|
||||
Stack *numbers = malloc(sizeof(Stack));
|
||||
if (numbers == NULL)
|
||||
exit(1);
|
||||
|
||||
char c;
|
||||
char flag = 'f';
|
||||
|
||||
while ((c = getchar()) != EOF) {
|
||||
if (c == '\n') {
|
||||
break;
|
||||
} else if (c == ' ') {
|
||||
flag = 't';
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
case '0' ... '9':
|
||||
if (flag == 't') {
|
||||
numbers = add_head(numbers, (double)(c - '0'));
|
||||
flag = 'f';
|
||||
} else {
|
||||
double a = get(numbers, 0).x;
|
||||
numbers = del_head(numbers);
|
||||
a = a * 10 + c - '0';
|
||||
numbers = add_head(numbers, a);
|
||||
}
|
||||
break;
|
||||
|
||||
case '+':
|
||||
case '-':
|
||||
case '*':
|
||||
case '/':
|
||||
if (len(numbers) < 2) {
|
||||
printf("Invalid expression\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
double a = get(numbers, 0).x;
|
||||
numbers = del_head(numbers);
|
||||
double b = get(numbers, 0).x;
|
||||
numbers = del_head(numbers);
|
||||
|
||||
b = (c == '+') ? b + a : b;
|
||||
b = (c == '-') ? b - a : b;
|
||||
b = (c == '*') ? b * a : b;
|
||||
b = (c == '/') ? b / a : b;
|
||||
|
||||
numbers = add_head(numbers, b);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("result: %lF\n", get(numbers, 0).x);
|
||||
|
||||
free(numbers);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user