0

ランダム変数でスタックを埋めて、それらを FILO 順序でポップアウトするように求める課題があります。int型だけを使用して動作させることができましたが、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: 関数 'push' 内: stack.c:28: エラー: 配列の添字が整数スタックではありません。 c: 関数 'main' 内: stack.c:42: 警告: 初期化により、キャストされていない整数からポインターが作成されます。 : 互換性のないポインター型 stack.c:60 から 'pop' の引数 2 を渡しています: 警告: 形式 '%c' は型 'int' を想定していますが、引数 2 の型は 'double' です stack.c:60: 警告: 形式 '% c' はタイプ 'int' を想定していますが、引数 2 はタイプ 'double' です</p>

基本的に、これは整数型を使用する前に私が持っていたもので、機能していましたが、彼は指示を出し、それを 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() の 3 番目の引数と pop() の 2 番目の引数は double **top の形式になります。つまり、スタック上の現在の最上位要素のアドレスを格納するポインターへのポインターです。

  • グローバル整数変数 myerror が作成されます。その値は、STACK_FULL、STACK_EMPTY、および NORMAL です。この変数を push() および pop() 関数で使用して、操作のステータスを main() 関数に通知します。"

4

2 に答える 2

3
#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;
}     
于 2013-09-18T23:45:19.670 に答える