2015编译器提示表达式必须是可修改的左值
#define STACK_INIT_SIZE 20#define STACRINCREMENT 10
typedef char ElemType;
typedef struct
{
ElemType *base;//栈顶
ElemType *top;//栈底
int stackSize;
}sqStack;
void InitStack(sqStack *s)//初始化栈
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!s->base)
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void push(sqStack *s, ElemType e)//入栈
{
if ( s->top - s->base = s->stackSize)//栈的检查
{
s->base = (ElemType*)realloc(s->base, (s->stackSize + STACRINCREMENT) * sizeof(ElemType));
if (!s->base)
{
exit(0);
}
}
*(s->top) = e;
s->top++;
}
void pop(sqStack *s, ElemType *e)
{
if (s->top == s->base)
{
return;
}
*e= *--(s->top);
}
int StackLen(sqStack s)
{
return(s.top - s.base);
}
int main()
{
ElemType c;
sqStack s;
int len, i, sum = 0;
InitStack(&s);
printf("请输入一个二进制的数,回车结束单前输入:#d\n", len);
scanf("#c", &c);
while (c != ' ')
{
push(&s, c);
scanf("#c", &c);
}
getchar();
len = StackLen(s);
printf("栈的当前容量是:%d\n", len);
for(i = 0; i < len; i++)
{
pop(&s, &c);
sum = sum + (c - 48)*pow(2, i);
}
printf("转换为十进制数是:%d\n", len);
return 0;
}
为什么在栈检查的IF语句这里提示表达式必须是可修改的左值,找了老半天不知道是哪里出错了求高人指教
void push(sqStack *s, ElemType e)这个函数里的判断条件有问题
if ( s->top - s->base = s->stackSize)这个条件是赋值,不是比较,是不是少写了一个=,s->top - s->base == s->stackSize是这个条件吧? 丶忘却的年少o 发表于 2017-9-20 15:52
void push(sqStack *s, ElemType e)这个函数里的判断条件有问题
if ( s->top - s->base = s->stackSize)这 ...
for(i = 0; i < len; i++)
{
pop(&s, &c);
sum = sum + (c - 48)*pow(2, i);
}
为什么在这句还提示错误
943566987 发表于 2017-9-21 20:36
for(i = 0; i < len; i++)
{
pop(&s, &c);
把scanf改成scanf_s试试看,看他的提示好像是这个意思 本帖最后由 943566987 于 2017-9-26 11:22 编辑
丶忘却的年少o 发表于 2017-9-21 22:32
把scanf改成scanf_s试试看,看他的提示好像是这个意思
还是不行,会不会是不同的编译器的问题 我是看视频里面的,小甲鱼老师你好能帮我看看是那里出错了吗?瓦看了公过只鸭不知道那错了{:10_250:} @小甲鱼 scanf("#c", &c); //这个错了
应该是scanf("%c", &c); //格式化输入 完整的代码,请看下面的源代码:
#include<stdio.h>
#include<stdlib.h>
#include<math.h> /*pow()函数调用时用到的库函数*/
#define STACK_INIT_SIZE 20
#define STACRINCREMENT 10
typedef char ElemType;
typedef struct
{
ElemType *base;//栈顶
ElemType *top;//栈底
int stackSize;
}sqStack;
void InitStack(sqStack *s)//初始化栈
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!s->base)
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void push(sqStack *s, ElemType e)//入栈
{
if ( s->top - s->base == s->stackSize)//栈的检查 /*判断语句*/
{
s->base = (ElemType*)realloc(s->base, (s->stackSize + STACRINCREMENT) * sizeof(ElemType));
if (!s->base)
{
exit(0);
}
}
*(s->top) = e;
s->top++;
}
void pop(sqStack *s, ElemType *e)
{
if (s->top == s->base)
{
return;
}
*e= *--(s->top);
}
int StackLen(sqStack s)
{
return(s.top - s.base);
}
int main()
{
ElemType c;
sqStack s;
int len, i, sum = 0;
InitStack(&s);
printf("请输入一个二进制的数,回车结束单前输入:%d\n", len); /*格式化输出*/
scanf("%c", &c); /*格式化输入*/
while (c != ' ')
{
push(&s, c);
scanf("%c", &c); /*格式化输入*/
}
getchar();
len = StackLen(s);
printf("栈的当前容量是:%d\n", len);
for(i = 0; i < len; i++)
{
pop(&s, &c);
sum = sum + (c - 48)*pow(2,3); /*pow(x,y), 注:x,y必须要是常量,否则会报错*/
}
printf("转换为十进制数是:%d\n", len);
return 0;
} #include<stdio.h>
#include<stdlib.h>
#include<math.h> /*pow()函数调用时用到的库函数*/
#define STACK_INIT_SIZE 20
#define STACRINCREMENT 10
typedef char ElemType;
typedef struct
{
ElemType *base;//栈顶
ElemType *top;//栈底
int stackSize;
}sqStack;
void InitStack(sqStack *s)//初始化栈
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if (!s->base)
{
exit(0);
}
s->top = s->base;
s->stackSize = STACK_INIT_SIZE;
}
void push(sqStack *s, ElemType e)//入栈
{
if ( s->top - s->base == s->stackSize)//栈的检查 /*判断语句*/
{
s->base = (ElemType*)realloc(s->base, (s->stackSize + STACRINCREMENT) * sizeof(ElemType));
if (!s->base)
{
exit(0);
}
}
*(s->top) = e;
s->top++;
}
void pop(sqStack *s, ElemType *e)
{
if (s->top == s->base)
{
return;
}
*e= *--(s->top);
}
int StackLen(sqStack s)
{
return(s.top - s.base);
}
int main()
{
ElemType c;
sqStack s;
int len, i, sum = 0;
InitStack(&s);
printf("请输入一个二进制的数,回车结束单前输入:%d\n", len); /*格式化输出*/
scanf("%c", &c); /*格式化输入*/
while (c != ' ')
{
push(&s, c);
scanf("%c", &c); /*格式化输入*/
}
getchar();
len = StackLen(s);
printf("栈的当前容量是:%d\n", len);
for(i = 0; i < len; i++)
{
pop(&s, &c);
sum = sum + (c - 48)*pow(2,3); /*pow(x,y), 注:x,y必须要是常量,否则会报错*/
}
printf("转换为十进制数是:%d\n", len);
return 0;
}
页:
[1]