问题在于:
输入规范
第一行输入包含要遵循的测试用例数T(T <= 500)。每个测试用例由一行组成,其中包含要评估的后缀表达式。该表达式保证是在后缀中编写的有效表达式,由一系列非负整数和运算符+, - ,*组成,所有这些都由一个空格分隔。每行的长度不超过100个字符。所有输入,中间和最终值都适合32位有符号整数。
输出规范
对于每个测试用例,输出一个具有三个间隔分隔的整数的行:分别用堆栈,队列和最小优先级队列计算时表达式的值。
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
typedef struct stack
{
int * data;
int size;
int initialized;
}stack;
void init(stack *);
void stackCopy(stack *, const stack*);
void push(stack *, int);
int findIndexOfMin(stack *);
int findIndexOfMax(stack *);
int minOrMaxPop(stack *, char);
int pop(stack *);
int queuePop(stack *);
int indexOfNextChar(char *, int);
void flush()
{
char c;
while(c = getchar())
{
if(c == '\n' || c == EOF)
{
break;
}
}
}
int main()
{
int testCases = 0;
char results[600][110];
scanf("%d", &testCases);
flush();
int count = 0;
for(count =0; count < testCases; count++)
{
memset(results[count], '\0', 109);
stack numStack = {.data = calloc(1,sizeof(int)), .size = 0, .initialized = 0};
stack queue = {.data = calloc(1,sizeof(int)), .size = 0, .initialized = 0};
stack numMinQ = {.data = calloc(1,sizeof(int)), .size = 0, .initialized = 0};
char string[103];
memset(string, '\0', 102);
fgets(string, 101, stdin);
int counter = 0;
for(counter = 0; counter < strlen(string); counter++)
{
if(isdigit(string[counter]))
{
int num = atoi(string+counter);
push(&numStack, num);
push(&numMinQ, num);
push(&queue, num);
counter = indexOfNextChar(string, counter);
}
else
{
int num1 = 0;
int num2 = 0;
switch(string[counter])
{
case '+':
;
{
num1 = pop(&numStack);
num2 = pop(&numStack);
push(&numStack, num2 + num1);
}
{
num1 = minOrMaxPop(&numMinQ,'s');
num2 = minOrMaxPop(&numMinQ,'s');
push(&numMinQ, num2 + num1);
}
{
num1 = queuePop(&queue);
num2 = queuePop(&queue);
push(&queue, num2 + num1);
}
break;
case '-':
;
{
num1 = pop(&numStack);
num2 = pop(&numStack);
push(&numStack, num2 - num1);
}
{
num1 = minOrMaxPop(&numMinQ,'s');
num2 = minOrMaxPop(&numMinQ,'s');
push(&numMinQ, num2 - num1);
}
{
num1 = queuePop(&queue);
num2 = queuePop(&queue);
push(&queue, num2 - num1);
}
break;
case '*':
;
{
num1 = pop(&numStack);
num2 = pop(&numStack);
push(&numStack, num2 * num1);
}
{
num1 = minOrMaxPop(&numMinQ,'s');
num2 = minOrMaxPop(&numMinQ,'s');
push(&numMinQ, num2 * num1);
}
{
num1 = queuePop(&queue);
num2 = queuePop(&queue);
push(&queue, num2 * num1);
}
break;
case '/':
;
{
num1 = pop(&numStack);
num2 = pop(&numStack);
push(&numStack, num2 / num1);
}
{
num1 = minOrMaxPop(&numMinQ,'s');
num2 = minOrMaxPop(&numMinQ,'s');
push(&numMinQ, num2 / num1);
}
{
num1 = queuePop(&queue);
num2 = queuePop(&queue);
push(&queue, num2 / num1);
}
break;
default:
break;
}
}
}
// sprintf(results[count],"%d %d %d", numStack.data[0], queue.data[0],numMinQ.data[0]);
printf("%d %d %d", numStack.data[0], queue.data[0],numMinQ.data[0]);
free(numStack.data);
free(queue.data);
free(numMinQ.data);
}
/*
for(count = 0; count < testCases; count++)
{
printf("%s\n", results[count]);
}
*/
return 0;
}
void init(stack * stackToInit)
{
stackToInit->data = calloc(1, sizeof(int));
stackToInit->size = 1;
stackToInit->initialized = 1;
}
void push(stack * stackToAdd, int data)
{
//printf("Stack size %d data: %d\n", stackToAdd->size, data);
if(!stackToAdd->initialized > 0)
{
int * temp = realloc(stackToAdd->data, (stackToAdd->size+1)*sizeof(int));
if(temp)
{
stackToAdd->data = temp;
stackToAdd->data[stackToAdd->size] = data;
stackToAdd->size++;
}
else
{
puts("Error reallocating memory!");
}
}
else
{
init(stackToAdd);
int * temp = realloc(stackToAdd->data, (stackToAdd->size+1)*sizeof(int));
if(temp)
{
stackToAdd->data = temp;
stackToAdd->data[stackToAdd->size-1] = data;
}
else
{
puts("Error reallocating memory!");
}
}
}
void stackCopy(stack * copy, const stack * original)
{
int counter = 0;
for(counter = 0; counter < original->size; counter++)
{
push(copy, original->data[counter]);
}
}
int findIndexOfMin(stack * queue)
{
int counter = 0;
int smallest = INT_MAX;
int indexOfSmallest = 0;
for(counter = 0;counter < queue->size; counter++)
{
if(queue->data[counter] < smallest)
{
smallest = queue->data[counter];
indexOfSmallest = counter;
}
}
return indexOfSmallest;
}
int findIndexOfMax(stack * queue)
{
int counter = 0;
int largest = INT_MIN;
int indexOfLargest = 0;
for(counter = 0;counter < queue->size; counter++)
{
if(queue->data[counter] > largest)
{
largest = queue->data[counter];
indexOfLargest = counter;
}
}
return indexOfLargest;
}
int minOrMaxPop(stack * queue, char mode)
{
int index;
if(mode == 's')
index = findIndexOfMin(queue);
if(mode == 'l')
index = findIndexOfMax(queue);
int result = queue->data[index];
if(index == queue->size-1 && queue->size > 1)
{
int * temp = realloc(queue->data, (queue->size-1)*sizeof(int));
if(temp)
{
queue->data = temp;
queue->size--;
}
else
{
puts("Error decreasing minimum queue!");
}
}
else if (index == 0 && queue->size > 1)
{
memmove(queue->data, queue->data+1, (queue->size-1)*sizeof(int));
int * temp = realloc(queue->data, (queue->size-1)*sizeof(int));
if(temp)
{
queue->data = temp;
queue->size--;
}
else
{
puts("Error decreasing minimum queue!");
}
}
else if(queue->size == 0)
{
}
else if (index == 0)
{
memmove(queue->data, queue->data+1, (queue->size-1)*sizeof(int));
int * temp = realloc(queue->data, (queue->size)*sizeof(int));
if(temp)
{
queue->data = temp;
queue->size--;
}
else
{
puts("Error decreasing minimum queue!");
}
}
else
{
queue->size--;
memmove(queue->data+index,queue->data+index+1,((queue->size)-index)*sizeof(int));
}
return result;
}
int pop(stack * numStack)
{
if(numStack->size > 0)
{
int result = numStack->data[numStack->size-1];
int * temp;
if(numStack->size == 1)
{
temp = realloc(numStack->data, (numStack->size)*sizeof(int));
}
else
{
temp = realloc(numStack->data, (numStack->size-1)*sizeof(int));
}
if(temp)
{
numStack->data = temp;
numStack->size--;
}
else
puts("Error popping from stack!");
return result;
}
}
int indexOfNextChar(char * string, int begin)
{
int counter = begin;
for(counter = begin; counter < strlen(string); counter++)
{
if(!isdigit(string[counter]))
{
return counter;
}
}
}
int queuePop(stack * queue)
{
int result = queue->data[0];
int * temp = NULL;
temp = realloc(queue->data, (queue->size)*sizeof(int));
if(queue->size > 1)
{
memmove(queue->data, queue->data+1, (queue->size-1)*sizeof(int));
temp = realloc(queue->data, (queue->size-1)*sizeof(int));
}
if(temp)
{
queue->data = temp;
queue->size--;
}
else
{
puts("Error popping queue!");
}
return result;
}
我的输入: 5
7 4 - 5 *
3 2 4 * +
3 4 2 - *
42 7 - 3 * 2 4 + *
5 3 5 - * 11 2 - 1 * -
计划的输出:
15 -15 15
11 10 10
6 2 4
630 -412 630
-19 41 3
Valgrind的
==5120== Memcheck, a memory error detector
==5120== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==5120== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==5120== Command: ./postfix
==5120==
1
1 1 +
==5120== Invalid read of size 2
==5120== at 0x4C2E9E0: memcpy@GLIBC_2.2.5 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x401520: queuePop (main.c:345)
==5120== by 0x400B45: main (main.c:79)
==5120== Address 0x51d92c4 is 4 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
==5120== Invalid write of size 2
==5120== at 0x4C2E9E3: memcpy@GLIBC_2.2.5 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x401520: queuePop (main.c:345)
==5120== by 0x400B45: main (main.c:79)
==5120== Address 0x51d92c0 is 0 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
==5120== Invalid free() / delete / delete[] / realloc()
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x401546: queuePop (main.c:346)
==5120== by 0x400B45: main (main.c:79)
==5120== Address 0x51d92c0 is 0 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
Error popping queue!
==5120== Invalid read of size 4
==5120== at 0x4014B0: queuePop (main.c:340)
==5120== by 0x400B57: main (main.c:80)
==5120== Address 0x51d92c0 is 0 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
==5120== Invalid free() / delete / delete[] / realloc()
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B57: main (main.c:80)
==5120== Address 0x51d92c0 is 0 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
==5120== Invalid read of size 2
==5120== at 0x4C2E9E0: memcpy@GLIBC_2.2.5 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x401520: queuePop (main.c:345)
==5120== by 0x400B57: main (main.c:80)
==5120== Address 0x51d92c4 is 4 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
==5120== Invalid write of size 2
==5120== at 0x4C2E9E3: memcpy@GLIBC_2.2.5 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x401520: queuePop (main.c:345)
==5120== by 0x400B57: main (main.c:80)
==5120== Address 0x51d92c0 is 0 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
==5120== Invalid free() / delete / delete[] / realloc()
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x401546: queuePop (main.c:346)
==5120== by 0x400B57: main (main.c:80)
==5120== Address 0x51d92c0 is 0 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
Error popping queue!
==5120== Invalid free() / delete / delete[] / realloc()
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x400EE3: push (main.c:174)
==5120== by 0x400B73: main (main.c:81)
==5120== Address 0x51d92c0 is 0 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
Error reallocating memory!
==5120== Invalid read of size 4
==5120== at 0x400DFF: main (main.c:150)
==5120== Address 0x51d92c0 is 0 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
==5120== Invalid free() / delete / delete[] / realloc()
==5120== at 0x4C2B200: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x400E38: main (main.c:152)
==5120== Address 0x51d92c0 is 0 bytes inside a block of size 8 free'd
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
2 1 2==5120==
==5120== HEAP SUMMARY:
==5120== in use at exit: 8 bytes in 1 blocks
==5120== total heap usage: 20 allocs, 20 frees, 108 bytes allocated
==5120==
==5120== 8 bytes in 1 blocks are definitely lost in loss record 1 of 1
==5120== at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5120== by 0x4014DF: queuePop (main.c:342)
==5120== by 0x400B45: main (main.c:79)
==5120==
==5120== LEAK SUMMARY:
==5120== definitely lost: 8 bytes in 1 blocks
==5120== indirectly lost: 0 bytes in 0 blocks
==5120== possibly lost: 0 bytes in 0 blocks
==5120== still reachable: 0 bytes in 0 blocks
==5120== suppressed: 0 bytes in 0 blocks
==5120==
==5120== For counts of detected and suppressed errors, rerun with: -v
==5120== ERROR SUMMARY: 20 errors from 12 contexts (suppressed: 0 from 0)
通过常规终端运行时,我硬编码的错误消息没有出现,这有点奇怪,但它们出现在Valgrind:/。
我认为问题可能是由于某些reallocs,但我无法弄清楚为什么它可以在我的计算机上运行,但不能与法官一起使用
答案 0 :(得分:1)
最多可能有500个测试用例,但是你的结果数组只能容纳100个。更改:
char results[100][110];
为:
char results[500][110];