mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 09:10:14 +00:00
work in progress...1
This commit is contained in:
Binary file not shown.
+38
-29
@@ -1,21 +1,20 @@
|
||||
#include "head.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
Stack get(Stack *l, int n) {
|
||||
if (n > len(l))
|
||||
exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
void linked_print(Stack *l)
|
||||
{
|
||||
while (l != NULL)
|
||||
{
|
||||
printf("%lF\n", l->x);
|
||||
l = l->next;
|
||||
}
|
||||
return *l;
|
||||
}
|
||||
|
||||
Stack *add_head(Stack *l, double x) {
|
||||
if (l == NULL) {
|
||||
Stack *add_head(Stack *l, double x)
|
||||
{
|
||||
if (l == NULL)
|
||||
{
|
||||
l = malloc(sizeof(Stack));
|
||||
if (l == NULL)
|
||||
exit(1);
|
||||
@@ -23,7 +22,8 @@ Stack *add_head(Stack *l, double x) {
|
||||
l->next = NULL;
|
||||
}
|
||||
|
||||
if (l->x == 0.0) {
|
||||
if (l->x == 0.0)
|
||||
{
|
||||
l->x = x;
|
||||
l->next = NULL;
|
||||
return l;
|
||||
@@ -38,26 +38,12 @@ Stack *add_head(Stack *l, double 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) {
|
||||
Stack *del_head(Stack *l)
|
||||
{
|
||||
if (l == NULL)
|
||||
return NULL;
|
||||
else if (l->next == NULL) {
|
||||
else if (l->next == NULL)
|
||||
{
|
||||
free(l);
|
||||
l = NULL;
|
||||
return NULL;
|
||||
@@ -66,4 +52,27 @@ Stack *del_head(Stack *l) {
|
||||
free(l);
|
||||
l = NULL;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int len(Stack *l)
|
||||
{
|
||||
int res = 0;
|
||||
while (l != NULL)
|
||||
{
|
||||
res++;
|
||||
l = l->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
+6
-6
@@ -1,13 +1,13 @@
|
||||
#define N 5
|
||||
|
||||
#define N 6
|
||||
|
||||
typedef struct Stack {
|
||||
typedef struct Stack
|
||||
{
|
||||
double x;
|
||||
struct Stack *next;
|
||||
} Stack;
|
||||
|
||||
Stack get(Stack *l, int n);
|
||||
void linked_print(Stack *l);
|
||||
Stack *add_head(Stack *l, double x);
|
||||
void stack_print(Stack *l);
|
||||
int len(Stack *l);
|
||||
Stack *del_head(Stack *l);
|
||||
int len(Stack *l);
|
||||
Stack get(Stack *l, int n);
|
||||
+23
-14
@@ -1,32 +1,40 @@
|
||||
#include "head.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Выдает неадекватные значения
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
srand(time(0));
|
||||
Stack *numbers = malloc(sizeof(Stack));
|
||||
if (numbers == NULL)
|
||||
exit(1);
|
||||
|
||||
char c;
|
||||
char flag = 'f';
|
||||
char flag = 'n';
|
||||
|
||||
while ((c = getchar()) != EOF) {
|
||||
if (c == '\n') {
|
||||
while ((c = getchar()) != EOF)
|
||||
{
|
||||
if (c == '\n')
|
||||
{
|
||||
break;
|
||||
} else if (c == ' ') {
|
||||
flag = 't';
|
||||
}
|
||||
else if (c == ' ')
|
||||
{
|
||||
flag = 's';
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
switch (c)
|
||||
{
|
||||
case '0' ... '9':
|
||||
if (flag == 't') {
|
||||
if (flag == 's')
|
||||
{
|
||||
numbers = add_head(numbers, (double)(c - '0'));
|
||||
flag = 'f';
|
||||
} else {
|
||||
flag = 'n';
|
||||
}
|
||||
else
|
||||
{
|
||||
double a = get(numbers, 0).x;
|
||||
numbers = del_head(numbers);
|
||||
a = a * 10 + c - '0';
|
||||
@@ -38,7 +46,8 @@ int main(int argc, char *argv[]) {
|
||||
case '-':
|
||||
case '*':
|
||||
case '/':
|
||||
if (len(numbers) < 2) {
|
||||
if (len(numbers) < 2)
|
||||
{
|
||||
printf("Invalid expression\n");
|
||||
exit(2);
|
||||
}
|
||||
@@ -58,7 +67,7 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
printf("result: %lF\n", get(numbers, -1).x);
|
||||
printf("res: %lF\n", get(numbers, 0).x);
|
||||
|
||||
free(numbers);
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user