我看看
感谢 /*
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define true 1
#define false 0
#define Status int
typedef struct {
int *base;
int *top;
int stacksize;
} SqStack;
/*
Status SS_Init(SqStack *S, int max);
Status SS_IsFull(SqStack S);
Status SS_IsEmpty(SqStack S);
void SS_Length(SqStack S);
Status SS_push(SqStack *S, int e);
Status SS_pop(SqStack *S, int *e);
*/
Status SS_Init(SqStack *S, int max) {
//S->base = (int *)malloc(max * sizeof(int));
S->base = malloc(max * sizeof(int));
//if(!(S->base)) exit(1);
if(!S->base) exit(1);
S->top = S->base;
S->stacksize = max;
return true;
}
// 为什么不写释放函数?
void stack_deinit(SqStack *s) {
free(s->base);
}
//Status SS_IsFull(SqStack S) {
Status SS_IsFull(SqStack *S) {
//if(S.top - S.base == S.stacksize) {
if(S->top - S->base == S->stacksize) {
return true;
} else {
return false;
}
}
//Status SS_IsEmpty(SqStack S) {
Status SS_IsEmpty(SqStack *S) {
//if(S.top == S.base) {
if(S->top == S->base) {
return true;
} else {
return false;
}
}
//void SS_Length(SqStack S) { printf("Stack length:%d\n", S.top - S.base); }
//void SS_Length(SqStack S) { printf("Stack length:%ld\n", S.top - S.base); }
void SS_Length(SqStack *S) { printf("Stack length:%ld\n", S->top - S->base); }
Status SS_push(SqStack *S, int e) {
/*
int m;
m = SS_IsFull(*S);
*/
//int m = SS_IsFull(*S);
int m = SS_IsFull(S);
//if(!m) {
if(m) {
return false;
} else {
*(S->top) = e;
S->top++;
return true;
}
}
Status SS_pop(SqStack *S, int *e) {
/*
int m;
m = SS_IsEmpty(*S);
*/
//int m = SS_IsEmpty(*S);
int m = SS_IsEmpty(S);
//if(!m) {
if(m) {
return false;
} else {
(S->top)--;
*e = *(S->top);
}
return true;
}
//Status SS_print(SqStack S) {
Status SS_print(SqStack *S) {
/*
int m;
m = SS_IsEmpty(S);
*/
int m = SS_IsEmpty(S);
//if(!m) {
if(m) {
printf("stack data: Empty!\n");
return false;
} else {
//printf("stack data (from bottom to top):");
printf("stack data (from top to bottom): ");
//while(S.top != S.base) {
while(S->top != S->base) {
S->top--;
//printf("%d ", *(S->top));
printf("%d ", *S->top);
}
//printf("%d", S.base);
//printf("%d", *S.base);
//printf("%d", *S->base);
}
printf("\n");
return true;
}
int main() {
SqStack S;
//int max, b, i = 0;
//int b, i = 0;
int b;
//char ch;
char a;
/*
scanf("%d", &max);
SS_Init(&S, max);
*/
int temp;
scanf("%d", &temp);
SS_Init(&S, 1024);
while(1) {
scanf("%s", a);
/*
while((ch = getchar()) != ' ') {
a = ch;
i++;
}
*/
if(strcmp(a, "push") == 0) {
scanf("%d", &b);
SS_push(&S, b);
}
if(strcmp(a, "pop") == 0) {
//SS_push(&S, b);
SS_pop(&S, &b);
}
if(strcmp(a, "end") == 0) {
break;
}
}
//SS_Length(S);
SS_Length(&S);
//SS_print(S);
SS_print(&S);
stack_deinit(&S);
return 0;
}
人造人 发表于 2021-11-3 22:41
感谢大佬 人造人 发表于 2021-11-3 22:41
兄弟加个好友 这是不认真导致的吧?
if(strcmp(a, "pop") == 0) {
//SS_push(&S, b);
SS_pop(&S, &b);
} 人造人 发表于 2021-11-3 22:41
大佬这个是什么意思 434773632 发表于 2021-11-4 19:09
大佬这个是什么意思
拿有道翻译一下
multiple definition of 'main' 人造人 发表于 2021-11-4 19:15
拿有道翻译一下
代码在dev上能运行,到这个网站上就这样报错/*************************************************************
date: July 2017
copyright: Zhu En(祝恩)
DO NOT distribute this code.
**************************************************************/
//顺序存储的栈 实现文件
/////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define true 1
#define false 0
typedef int T;
struct SeqStack
{
T* data;
int top;
int max;
};
SeqStack* SS_Create(int maxlen);
void SS_Free(SeqStack* ss);
void SS_MakeEmpty(SeqStack* ss);
bool SS_IsFull(SeqStack* ss);
bool SS_IsEmpty(SeqStack* ss);
int SS_Length(SeqStack* ss);
bool SS_Push(SeqStack* ss, T x);
bool SS_Pop(SeqStack* ss, T &item);
void SS_Print(SeqStack* ss);
/*创建一个栈*/
SeqStack* SS_Create(int maxlen)
{
SeqStack* ss=(SeqStack*)malloc(sizeof(SeqStack));
ss->data=(T*)malloc(maxlen*sizeof(T));
ss->top=-1;
ss->max=maxlen;
return ss;
}
/*释放一个栈*/
void SS_Free(SeqStack* ss)
{
free(ss->data);
free(ss);
}
/*清空一个栈*/
void SS_MakeEmpty(SeqStack* ss)
{
ss->top=-1;
}
/*判断栈是否为满*/
bool SS_IsFull(SeqStack* ss)
{
/*请在BEGIN和END之间实现你的代码*/
/*****BEGIN*****/
if(ss->top==(ss->max-1))
{
return true;
}
else
{
return false;
}
/******END******/
}
/*判断栈是否为空*/
bool SS_IsEmpty(SeqStack* ss)
{
/*请在BEGIN和END之间实现你的代码*/
/*****BEGIN*****/
if(ss->top==-1)
{
return true;
}
else
{
return false;
}
/******END******/
}
/*获取栈元素个数*/
int SS_Length(SeqStack* ss)
{
/*请在BEGIN和END之间实现你的代码*/
/*****BEGIN*****/
int len = ss->top + 1;
printf("Stack length:%d\n", len);
/******END******/
}
/*将x进栈,满栈则无法进栈(返回false)*/
bool SS_Push(SeqStack* ss, T x)
{
/*请在BEGIN和END之间实现你的代码*/
/*****BEGIN*****/
if(SS_IsFull(ss))
{
return false;
}
else
{
ss->top++;
ss->data = x;
}
/******END******/
}
/*出栈,出栈的元素放入item,空栈则返回false*/
bool SS_Pop(SeqStack* ss, T &item)
{
/*请在BEGIN和END之间实现你的代码*/
/*****BEGIN*****/
if(SS_IsEmpty(ss))
{
return false;
}
else
{
item = ss->data;
ss->top--;
}
/******END******/
}
/*获取栈顶元素放入item中,空栈则返回false*/
bool SS_Top(SeqStack* ss, T & item)
{
if (SS_IsEmpty(ss)) {
return false;
}
item = ss->data;
return true;
}
/*从栈底到栈顶打印出所有元素*/
void SS_Print(SeqStack* ss)
{
if (SS_IsEmpty(ss)) {
printf("stack data: Empty!\n");
return;
}
printf("stack data (from bottom to top):");
int curr=0;
while(curr<=ss->top) {
printf(" %d", ss->data);
curr++;
}
//printf("\n");
}
int main()
{
int max, num, item;
char a;
SeqStack *ss;
scanf("%d", &max);
ss = SS_Create(max);
while(1)
{
scanf("%s", &a);
if(strcmp(a, "push")==0)
{
scanf("%d", &num);
SS_Push(ss, num);
}
if(strcmp(a, "pop")==0)
{
SS_Pop(ss, item);
}
if(strcmp(a, "end")==0)
{
break;
}
}
SS_Length(ss);
SS_Print(ss);
}
434773632 发表于 2021-11-4 19:25
代码在dev上能运行,到这个网站上就这样报错
那我就不知道了,报错说 有多个 main 函数
人造人 发表于 2021-11-4 19:30
那我就不知道了,报错说 有多个 main 函数
老师说那个主函数系统给写过了 434773632 发表于 2021-11-4 19:40
老师说那个主函数系统给写过了
嗯
页:
1
[2]