gpt4 book ai didi

c - 使用 double 组推送和弹出

转载 作者:太空宇宙 更新时间:2023-11-04 06:01:34 24 4
gpt4 key购买 nike

我有一个作业要求我用随机变量填充一个堆栈,然后按 FILO 顺序将它们弹出。虽然我设法让它只使用 int 类型来工作,但现在我似乎需要更改它以实现一个 double 组,它会使用 double 类型的字符填充数组并继续从中弹出 double 堆叠直到它为空并打印它们。我试过只更改两个函数中的类型,但不断出现错误,我不确定为什么。到目前为止,这就是我所拥有的。任何帮助将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define STACK_SIZE 10

#define STACK_FULL -2
#define STACK_EMPTY -1
#define NORMAL 0

int myerror = NORMAL;

void push(double [],
double, // input - data being pushed onto the stack
double **, // input/output - pointer to pointer to the top of stack
int); // constant - maximum capacity of stack

double // output - data being popped out from the stack
pop(double [], // input/output - the stack
double **); // input/output - pointer to pointer to top of stack

void push(double stack[],
double item,
double **top,
int max_size)
{
stack[++(**top)] = item;
}

double pop(double stack[],
double **top)
{
return stack[(**top)--];
return 0.0;
}

int main()
{
double s[STACK_SIZE];
double *s_top = NULL;

srand(time(NULL));
char randChar = ' ';
double i = 0;
double j=0;
double randNum = 0;

for (i = 0; i < STACK_SIZE; i++){
randNum = 33 + (double)(rand() % ((126-33)+ 1 ));
randChar = (double) randNum;
push(s,randChar, &(*s_top), STACK_SIZE);

printf ("Random characters: %c\n", randChar);}

printf("-----------\n");

for(j=STACK_SIZE; j>0; j--){
printf("Random characters: %c\n", pop(s, & *s_top));
}

return 0;
}

顺便说一句,这些是错误

stack.c: In function ‘push’: stack.c:28: error: array subscript is not an integer stack.c: In function ‘pop’: stack.c:35: error: array subscript is not an integer stack.c: In function ‘main’: stack.c:42: warning: initialization makes pointer from integer without a cast stack.c:53: warning: passing argument 3 of ‘push’ from incompatible pointer type stack.c:60: warning: passing argument 2 of ‘pop’ from incompatible pointer type stack.c:60: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘double’ stack.c:60: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘double’

基本上这是我在使用整数类型之前所拥有的并且它正在工作,现在他给出了指示并希望我们将其更改为 double 类型并且我试图将它们合并但无济于事。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define STACK_SIZE 10
#define STACK_EMPTY -1

void push(char [], // input/ouput - the stack
char, // input - data being pushed onto the stack
int *, // input/output - pointer to the index of the top of stack
int); // constant - maximum size of stack

char // output - data being popped out from the stack
pop(char [], // input/output - the stack
int *); // input/output - pointer to the index of the top of stack

void push(char stack[],
char item,
int *top,
int max_size){

stack[++(*top)] = item;

}

char pop(char stack[],
int *top){


return stack[(*top)--];

// Return STACK_EMPTY if the stack is empty.

return STACK_EMPTY;
}

int main(){
char s[STACK_SIZE];
int s_top = STACK_EMPTY; // Pointer points to the index of the top of the stack

srand(time(NULL));

char randChar = ' ';
int i = 0;
int j=0;
int randNum = 0;

for (i = 0; i < STACK_SIZE; i++){
randNum = 33 + (int)(rand() % ((126-33)+ 1 ));
randChar = (char) randNum;
push(s,randChar, &s_top, STACK_SIZE);

printf ("Random characters: %c\n", randChar);}

printf("-----------\n");

for(j=STACK_SIZE; j>0; j--){
printf("Random characters: %c\n", pop(s, &s_top));
}


return 0;
}

好的,这就是他要求我们做的,我认为编辑而不是对每个人发表长篇评论会更容易

“您之前在堆栈上实现了压入和弹出操作,这里要求您再次执行此操作,但有 4 个主要更改,如下所述:

  • 堆栈将采用 double 组的形式。
  • push() 的第三个参数和 pop() 的第二个参数将用于采用双**顶的形式,即指向存储当前栈顶元素地址的指针。

  • 为您创建了一个全局整型变量 myerror。它的值可以是 STACK_FULL、STACK_EMPTY 和 NORMAL。在 push() 和 pop() 函数中使用此变量来通知 main() 函数操作的状态。 "

最佳答案

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define STACK_SIZE 10

typedef struct
{
double items[STACK_SIZE];
double* next;
} Stack;

void init(Stack* s)
{
s->next = s->items;
}

void push(Stack* s, double val)
{
if (s->next >= s->items + STACK_SIZE)
fprintf(stderr, "stack overflow\n");
else
{
*s->next++ = val;
printf("push %g\n", val);
}
}

void pop(Stack* s)
{
if (s->next == s->items)
fprintf(stderr, "stack underflow\n");
else
printf("pop %g\n", *--s->next);
}

int main(void)
{
Stack s;
init(&s);

srand(time(NULL));

for (int i = 0; i < STACK_SIZE; i++)
push(&s, (double)rand());

printf("-----------\n");

for (int i = 0; i < STACK_SIZE; i++)
pop(&s);

return 0;
}

关于c - 使用 double 组推送和弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18883590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com