restruct 1

This commit is contained in:
ada-dmitry
2023-09-11 17:15:34 +03:00
parent 096d8dea1a
commit 6809e2ef5f
67 changed files with 0 additions and 0 deletions
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+48
View File
@@ -0,0 +1,48 @@
#include "head.h"
#include <stdio.h>
#include <stdlib.h>
void linked_print(Stack *l) // Вывод стека
{
while (l != NULL)
{
printf("%lF\n", l->x);
l = l->next;
}
}
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;
}
Stack *tmp = malloc(sizeof(Stack));
if (tmp == NULL)
exit(1);
tmp->next = l;
tmp->x = x;
return tmp;
}
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;
}
+11
View File
@@ -0,0 +1,11 @@
#define N 5
typedef struct Stack
{
double x;
struct Stack *next;
} Stack;
void linked_print(Stack *l);
Stack *add_head(Stack *l, double x);
Stack *del_head(Stack *l);
+53
View File
@@ -0,0 +1,53 @@
#include "head.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
srand(time(0));
Stack *numbers = NULL;
char c;
while ((c = getchar()) != EOF)
{
if (c == '\n')
{
break;
}
else if (c == ' ')
{
continue;
}
switch (c)
{
case '0' ... '9':
numbers = add_head(numbers, (double)(c - '0'));
break;
case '+':
case '-':
case '*':
case '/':
double a = numbers->x;
numbers = del_head(numbers);
double b = numbers->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("res: %lF\n", numbers->x);
free(numbers);
return 0;
}