mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 09:10:14 +00:00
restruct 1
This commit is contained in:
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user