Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...