/*试将下列递归函数改为非递归函数
#include<stdio.h>
void test(int &sum) {
int x;
scanf("%d",&x);
if (x==0) sum = 0;
else {test(sum); sum+=x;}
printf("%d,",sum);
}
void main()
{
int sum;
test(sum);
}
*/
#include<stdio.h>
#include<malloc.h>
typedef struct stack
{
int * top;
int * bottom;
}SqStack;
void creatStack(SqStack &S)
{
S.top=(int *)malloc(100*sizeof(int));
S.bottom=S.top;
}
void push(SqStack & S, int e)
{
*S.top=e;
S.top++;
}
void pop(SqStack & S, int &e)
{
S.top--;
e=*S.top;
}
void main()
{
SqStack S;
creatStack(S);
int sum=0;
int x,e;
scanf("%d",&x);
while(x)
{
push(S,x);
scanf("%d",&x);
}
push(S,x);
while(S.top!=S.bottom)
{
pop(S,e);
sum+=e;
printf("%d,",sum);
}
}
#include<stdio.h>
void test(int &sum) {
int x;
scanf("%d",&x);
if (x==0) sum = 0;
else {test(sum); sum+=x;}
printf("%d,",sum);
}
void main()
{
int sum;
test(sum);
}
*/
#include<stdio.h>
#include<malloc.h>
typedef struct stack
{
int * top;
int * bottom;
}SqStack;
void creatStack(SqStack &S)
{
S.top=(int *)malloc(100*sizeof(int));
S.bottom=S.top;
}
void push(SqStack & S, int e)
{
*S.top=e;
S.top++;
}
void pop(SqStack & S, int &e)
{
S.top--;
e=*S.top;
}
void main()
{
SqStack S;
creatStack(S);
int sum=0;
int x,e;
scanf("%d",&x);
while(x)
{
push(S,x);
scanf("%d",&x);
}
push(S,x);
while(S.top!=S.bottom)
{
pop(S,e);
sum+=e;
printf("%d,",sum);
}
}