C 使用双重数组进行推送和弹出
我有一个任务,要求我用随机变量填充一个堆栈,然后按FILO顺序将其弹出。虽然我只使用int类型使它工作,但现在我似乎需要更改它以实现一个double数组,它将使用double类型用字符填充数组,并不断从堆栈中弹出double,直到它为空并打印它们。我尝试过在这两个函数中更改类型,但不断出现错误,我不知道为什么。顺便说一句,这就是我目前所拥有的。如有任何帮助,我们将不胜感激C 使用双重数组进行推送和弹出,c,arrays,C,Arrays,我有一个任务,要求我用随机变量填充一个堆栈,然后按FILO顺序将其弹出。虽然我只使用int类型使它工作,但现在我似乎需要更改它以实现一个double数组,它将使用double类型用字符填充数组,并不断从堆栈中弹出double,直到它为空并打印它们。我尝试过在这两个函数中更改类型,但不断出现错误,我不知道为什么。顺便说一句,这就是我目前所拥有的。如有任何帮助,我们将不胜感激 #include <stdio.h> #include <stdlib.h> #include &l
#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;
}
#包括
#包括
#包括
#包括
#定义堆栈大小为10
#定义堆栈_FULL-2
#将堆栈定义为空-1
#定义法线0
int myerror=正常;
无效推送(双[],
double,//输入-数据被推送到堆栈上
double**,//输入/输出-指向堆栈顶部指针的指针
int);//恒定-堆栈的最大容量
double//output-从堆栈中弹出数据
pop(double[],//输入/输出-堆栈
双**);//输入/输出-指向堆栈顶部指针的指针
无效推送(双堆栈[],
双项,
双**顶,
整数(最大值)
{
堆栈[++(**顶部)]=项;
}
双pop(双堆栈[],
双**顶)
{
返回堆栈[(**顶部)--];
返回0.0;
}
int main()
{
双s[堆栈大小];
double*s_top=NULL;
srand(时间(空));
char randChar='';
双i=0;
双j=0;
双随机数=0;
对于(i=0;i0;j--){
printf(“随机字符:%c\n”,pop(s,&*s_-top));
}
返回0;
}
顺便说一句,这些是错误
stack.c:在函数“push”中:stack.c:28:错误:数组下标不正确
函数“pop”中的整数stack.c::stack.c:35:错误:数组
下标不是整数堆栈。c:在函数“main”中:堆栈。c:42:
警告:初始化将指针从整数转换为不带强制转换的整数
stack.c:53:警告:从不兼容传递'push'的参数3
指针类型堆栈。c:60:警告:正在从
不兼容的指针类型堆栈。c:60:警告:需要“%c”格式
类型为“int”,但参数2的堆栈类型为“double”。c:60:警告:
格式“%c”要求类型为“int”,但参数2的类型为“double”
基本上,这就是我在使用integer类型之前所做的,它是有效的,现在他给出了指示,希望我们将其更改为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;
}
#包括
#包括
#包括
#定义堆栈大小为10
#将堆栈定义为空-1
void push(char[],//输入/输出-堆栈
char,//输入-数据被推送到堆栈上
int*,//输入/输出-指向堆栈顶部索引的指针
int);//常量-堆栈的最大大小
char//output-从堆栈中弹出的数据
pop(char[],//输入/输出-堆栈
int*);//输入/输出-指向堆栈顶部索引的指针
无效推送(字符堆栈[],
字符项,
int*top,
整数(最大值){
堆栈[++(*顶部)]=项;
}
char pop(char堆栈[],
整数*顶部){
返回堆栈[(*顶部)--];
//如果堆栈为空,则返回堆栈\u EMPTY。
返回堆栈_为空;
}
int main(){
字符s[堆栈大小];
int s_top=STACK_EMPTY;//指针指向堆栈顶部的索引
srand(时间(空));
char randChar='';
int i=0;
int j=0;
int randNum=0;
对于(i=0;i0;j--){
printf(“随机字符:%c\n”,pop(s,&s_-top));
}
返回0;
}
好的,这就是他要求我们做的,我会编辑,而不是对每个人发表长篇大论,我想这会更容易
“在此之前,您在堆栈上实现了推送和弹出操作,要求您再次执行,但有4个主要更改,如下所述:
- 堆栈将是一个双精度数组的形式
- push()的第三个参数和pop()的第二个参数将 以双**顶部的形式,即指向指针的指针,该指针将当前顶部元素的地址存储在堆栈上
- 将为您创建一个全局整型变量myerror。它的值可以是STACK_FULL、STACK_EMPTY和NORMAL。在push()和pop()函数中使用此变量可通知main()函数操作的状态。”
#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 push(double stack[], double value, double **top, int sizemax)
{
if (*top - stack < sizemax)
{
**top = value;
++*top;
return NORMAL;
}
return STACK_FULL;
}
int pop(double stack[], double **top, double *result)
{
if (*top - stack > 0)
{
--*top;
*result = **top;
return NORMAL;
}
return STACK_EMPTY;
}
int main()
{
double s[STACK_SIZE];
double *s_top = s;
double randNum = 0;
double i = 0;
srand((unsigned)time(NULL));
for (i = 0; i < STACK_SIZE; i++)
{
randNum = 33 + (double)(rand() % ((126-33)+ 1 ));
printf("Random value: %f\n", randNum);
push(s, randNum, &s_top, STACK_SIZE);
}
printf("-----------\n");
while (pop(s, &s_top, &randNum) != STACK_EMPTY)
printf("Random value: %f\n", randNum);
return 0;
}
#包括
#包括
#包括
#包括
#定义堆栈大小为10
#定义堆栈_FULL-2
#将堆栈定义为空-1
#定义法线0
int-push(双堆栈[],双值,双**顶部,int-sizemax)
{
if(*顶部-堆栈#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;
}