mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 09:10:14 +00:00
dir.main added
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,76 @@
|
||||
#include "header.h"
|
||||
|
||||
void linked_print(LinkedList *l)
|
||||
{
|
||||
while (l != NULL)
|
||||
{
|
||||
printf("%lF\n", l->x);
|
||||
l = l->next;
|
||||
}
|
||||
}
|
||||
|
||||
LinkedList *add_head(LinkedList *l, double x)
|
||||
{
|
||||
if (l == NULL)
|
||||
{
|
||||
l = malloc(sizeof(LinkedList));
|
||||
if (l == NULL)
|
||||
exit(1);
|
||||
l->x = x;
|
||||
l->next = NULL;
|
||||
}
|
||||
|
||||
if (l->x == 0.0)
|
||||
{
|
||||
l->x = x;
|
||||
l->next = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
LinkedList *tmp = malloc(sizeof(LinkedList));
|
||||
if (tmp == NULL)
|
||||
exit(1);
|
||||
|
||||
tmp->next = l;
|
||||
tmp->x = x;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
LinkedList *del_head(LinkedList *l)
|
||||
{
|
||||
if (l == NULL)
|
||||
return NULL;
|
||||
else if (l->next == NULL)
|
||||
{
|
||||
free(l);
|
||||
l = NULL;
|
||||
return NULL;
|
||||
}
|
||||
LinkedList *tmp = l->next;
|
||||
free(l);
|
||||
l = NULL;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int len(LinkedList *l)
|
||||
{
|
||||
int res = 0;
|
||||
while (l != NULL)
|
||||
{
|
||||
res++;
|
||||
l = l->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
LinkedList get(LinkedList *l, int n)
|
||||
{
|
||||
if (n > len(l))
|
||||
exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
l = l->next;
|
||||
}
|
||||
return *l;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
#include "header.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
srand(time(0));
|
||||
LinkedList *numbers = malloc(sizeof(LinkedList));
|
||||
if (numbers == NULL)
|
||||
exit(1);
|
||||
|
||||
char c;
|
||||
char flag = 'n';
|
||||
|
||||
while ((c = getchar()) != EOF)
|
||||
{
|
||||
if (c == '\n')
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (c == ' ')
|
||||
{
|
||||
flag = 's';
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case '0' ... '9':
|
||||
if (flag == 's')
|
||||
{
|
||||
numbers = add_head(numbers, (double)(c - '0'));
|
||||
flag = 'n';
|
||||
}
|
||||
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("res: %lF", get(numbers, 0).x);
|
||||
|
||||
free(numbers);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define N 5
|
||||
|
||||
typedef struct LinkedList
|
||||
{
|
||||
double x;
|
||||
struct LinkedList *next;
|
||||
} LinkedList;
|
||||
|
||||
void linked_print(LinkedList *l);
|
||||
LinkedList *add_head(LinkedList *l, double x);
|
||||
LinkedList *del_head(LinkedList *l);
|
||||
int len(LinkedList *l);
|
||||
LinkedList get(LinkedList *l, int n);
|
||||
Reference in New Issue
Block a user