dir.main added

This commit is contained in:
ada-dmitry
2023-04-25 08:05:41 +03:00
parent 62d99ba675
commit 1f8e90cb3d
37 changed files with 1018 additions and 321 deletions
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;
}
+72
View File
@@ -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;
}
+18
View File
@@ -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);