Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

(C) Write doubles outside the stack

I am trying to write numbers from type double outside of the stack but I can't quite work it out how to do it. I have tried doing it with while loop and if statements, sometimes I get only zeros but most of the time I get nothing displayed. Thanks for helping in advance.

#include <stdio.h>

struct element
{
    double data;
    element * next;
};
element *top = NULL;

void Get(double &x)
{
    element *p;
    if(top == NULL)
    {
        printf("
The stack is empty!");
        return;
    }
    x = top -> data;
    p = top;
    top = top -> next;
    delete(p);
}

void Put(double x)
{
    element *p;
    p = new(element);
    if(p == NULL)
    {
        printf("
NOT enough memory!");
        return;
    }
    p -> data = x;
    p -> next = top;
    top = p;
}

int main()
{
    printf("Enter integers in stack. Enter 0 to end!
");
    double val;
    double m;
    do
    {
        printf("Enter number: ");
        scanf("%lf", &val);
        if(val >= 3697 && val <= 3698)
        {
            Put(val);
        }
    }
    while(val != 0);
    printf("
Stack contains: ");
    while(top != NULL)
    {
        Get(val);
        printf("%lf ", val);
    }
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...