owned this note
owned this note
Published
Linked with GitHub
```c=
#include <stdio.h>
#include "lib1.h"
#include <stdlib.h>
#include <string.h>
#include "lib2.h"
#define MAX_TOKEN_NUM 100000
int main(void)
{
QUEUE_TYPE *q;
STACK_TYPE *stack;
char *token[MAX_TOKEN_NUM];
int i, j, tnum, cnt = 0, frag = 0;
initQueue();
initStack();
tnum = read(token, MAX_TOKEN_NUM);
for (i = 0; i < tnum; i++)
{
/*数字の判別と処理*/
if (isNumber(token[i]) == 1)
{
enqueue(token[i]);
}
/* )の判別と処理*/
else if (priority(token[i]) == 1)
{
if (!isQueueEmpty())
{
while (priority(peek()) != 0)
{
enqueue(pop());
}
}
}
/*+-の判別と処理*/
else if (priority(token[i]) == 2)
{
if (!isStackEmpty())
{
while (priority(token[i]) <= priority(peek()))
{
enqueue(pop());
}
}
push(token[i]);
}
/*その他の演算記号の判別と処理*/
else if (priority(token[i]) == 3)
{
if (!isStackEmpty())
{
while (priority(token[i]) <= priority(peek()))
{
enqueue(pop());
}
}
push(token[i]);
}
else if (priority(token[i]) == 4)
{
push(token[i]);
}
}
while (!isStackEmpty())
{
enqueue(pop());
}
printQueue();
printf("計算開始\n");
initStack();
n=Num();
printf("要素数:%d\n",n);
for (i = 0; i < n; i++)
{
if (strcmp("+", q[i]) == 0)
{
y = atof(pop());
x = atof(pop());
push(ftoa(x + y));
}
else if (strcmp("-", q[i]) == 0)
{
y = atof(pop());
x = atof(pop());
push(ftoa(x - y));
}
else if (strcmp("*", q[i]) == 0)
{
y = atof(pop());
x = atof(pop());
push(ftoa(x * y));
}
else if (strcmp("/", q[i]) == 0)
{
y = atof(pop());
x = atof(pop());
push(ftoa(x / y));
}
else
{
push(q[i]);
}
}
printstack();
return 0;
}
```
実行結果
% ./main.exe
read> 10 + 5 - 3 - 2 + 1
enqueue:待ち行列はフル状態です。
lib2.c内
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib2.h"
#define QUEUE_SIZE 10000
#define STACK_SIZE 10000
QUEUE_TYPE gQueue[QUEUE_SIZE];
int gQNum = 0;
STACK_TYPE gStack[STACK_SIZE];
int gSNum = 0;
void error(char *s)
{
fflush(stdout);
fprintf(stderr, "\n%s\n", s);
exit(1);
}
int isQueueEmpty(void)
{
return gQNum == 0;
}
void initQueue(void)
{
gQNum = 0;
}
int isQueueFull(void)
{
return gQNum == QUEUE_SIZE;
}
void enqueue(QUEUE_TYPE x)
{
if (isQueueFull())
{
error("enqueue:待ち行列はフル状態です。");
}
gQueue[gQNum++] = x;
}
QUEUE_TYPE dequeue(void)
{
QUEUE_TYPE x;
int i;
if (isQueueEmpty())
error("dequeue:待ち行列は空状態です。");
x = gQueue[0];
for (i = 1; i <= gQNum; i++)
{
gQueue[i - 1] = gQueue[i];
}
gQNum--;
return x;
}
void printQueue(void)
{
int i;
printf("QUEUE[");
for (i = 0; i < gQNum; i++)
{
printf("%d ", gQueue[i]);
if (i != gQNum - 1)
printf(" ");
}
printf("]\n");
}
int isStackEmpty(void)
{
return gSNum == 0;
}
int isStackFull(void)
{
return gSNum == STACK_SIZE;
}
void push(STACK_TYPE x)
{
if (isStackFull())
{
error("push:スタックがフル状態です。");
exit(1);
}
else
{
gStack[gSNum++] = x;
}
}
STACK_TYPE pop(void)
{
if (isStackEmpty())
{
error("pop:スタックは空状態です。");
exit(1);
}
else
{
return gStack[--gSNum];
}
}
void printstack(void)
{
int i;
printf("STACK[");
for (i = 0; i < gSNum; i++)
{
printf("%d", gStack[i]);
}
printf("]\n");
}
STACK_TYPE peek(void)
{
STACK_TYPE a;
if (isStackEmpty())
{
error("peek:スタックは空状態です。");
exit(1);
}
else
{
a = gStack[gSNum - 1];
}
return a;
}
int priority(char *x)
{
int y;
if (strcmp("+", x) == 0)
{
y = 2;
}
else if (strcmp("-", x) == 0)
{
y = 2;
}
else if (strcmp("*", x) == 0)
{
y = 3;
}
else if (strcmp("/", x) == 0)
{
y = 3;
}
else if (strcmp("^", x) == 0)
{
y = 4;
}
else if (strcmp("%", x) == 0)
{
y = 3;
}else if(strcmp("(",x)==0)
{
y=0;
}else if(strcmp(")",x)==0)
{
y=1;
}
return y;
}
void initStack(void)
{
gSNum=0;
}
int Num(void)
{
return gQNum;
}
```
lib2.h内
```c=
typedef char *STACK_TYPE;
typedef char *QUEUE_TYPE;
void error(char *s);
int isStackEmpty(void);
int isStackFull(void);
void push(STACK_TYPE x);
STACK_TYPE pop(void);
void printstack(void);
STACK_TYPE peek(void);
int isQueueEmpty(void);
int isQueueFull(void);
void initQueue(void);
void enqueue(QUEUE_TYPE x);
QUEUE_TYPE dequeue(void);
void printQueue(void);
void initStack(void);
int priority(char *x);
int Num(void);
```