有一段時間沒寫博客啦!今天來分享我的經驗,希望對大家的編程之路有用!
今天我在學數據結構棧部分裏的棧的應用之括號匹配,不幸的是調bug調很久啦!
第一部分寫的是進制的轉換,第二部分是括號匹配。
在進行執行代碼的時候,第二部分的gets_s始終不執行,相當於空語句一樣,我一開始懷疑我的另一個文件裏“棧的應用_後綴表達式”,裏的gets_s()的使用,隨即我立即註釋掉,但還是不起任何作用。我再換一種思路,把本源程序文件裏的gets_s()換成scanf語句並執行,結果是可以執行scanf並等待我輸入。
接着我又換回原來的gets_s(),還是不行,後面我百度沒有發現有此類問題的解答,我覺得臺太不科學啊!調試一下是不執行的,直接跳過的。
接着我就把第一部分進制轉換 的代碼註釋掉,果然再執行的時候是等待我輸入的,說明gets_s()執行啦!
好啦!感謝你聽我說這麼多廢話,到這裏來相信你已經知道問題所在啦!是的
在vs2019裏一個源程序文件裏若前面有scanf的輸入語句時後面的gets_s()是不起任何作用的,本人親自測試過,
不信你自己試試,其他編譯器我就不知道,想知道結果自己試試不就知道啦!哈哈哈
下面附上本人源代碼
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
typedef int SElemType;
typedef char ElemType;
typedef int Status;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FLASE 0
#define MAXSIZE 30
#define len sizeof(struct Stack)
typedef struct Stack
{
SElemType* data;
int top;
}SqStack;
typedef struct Stackfloat
{
ElemType* data;
int top;
}SqStackFloat;
void initStack(SqStack*& S);
void initStackfloat(SqStackFloat& S);
//DestroyStack(*S);
//ClearStack(*S);
Status StackEmpty(SqStack* S);
Status StackEmpty(SqStackFloat* S);
Status GetTop(SqStack S, SElemType& e);
Status Push(SqStack* S, SElemType e);
Status Pop(SqStack* S, SElemType* e);
void PrintStack(SqStack* S);
Status StackLength(SqStack* S);
void convert(int number, SqStack* S);
Status match(SqStackFloat* S, char* str);
int main()
{
SqStack wan;
SqStackFloat zhipeng;
SqStack* STheadboss = &wan;
SElemType x;
SElemType j;
ElemType string1[MAXSIZE];
SElemType exp[MAXSIZE], postexp[MAXSIZE];
printf("對棧進行初始化:\n");
initStack(STheadboss);
Sleep(300);
if (StackEmpty(STheadboss) == OK)
printf("棧空\n");
else
printf("棧非空\n");
Sleep(500);
printf("進棧測試:\n");
printf("輸入要轉換的十進制數:");
//scanf("%d", &x);
convert(15, STheadboss);
Sleep(500);
printf("打印該棧結果如下:");
PrintStack(STheadboss);
printf("此時棧長爲%d\n", StackLength(STheadboss));
printf("對棧再次進行初始化:\n");
initStackfloat(zhipeng);
if (StackEmpty(&zhipeng) == OK)
printf("棧空\n");
else
printf("棧非空\n");
//scanf("%s", string1);//不執行就爲空
gets_s(string1);
if (match(&zhipeng, string1) == ERROR)
printf("匹配失敗!");
return 0;
}
void initStack(SqStack*& S)
{
S->data = (SElemType*)malloc(MAXSIZE);
S->top = -1;
}
void initStackfloat(SqStackFloat& S)
{
S.data = (ElemType*)malloc(MAXSIZE * sizeof(ElemType));
S.top = -1;
}
Status StackEmpty(SqStack* S)
{
if (S->top == -1)//非指針
return TRUE;
else
return FLASE;
}
Status StackEmpty(SqStackFloat* S)
{
if (S->top == -1)//非指針
return TRUE;
else
return FLASE;
}
void PrintStack(SqStack* S)
{
int sum = S->top;
printf("順序棧結果爲:");
while (sum != -1)
{
printf("%d", S->data[sum--]);
}
printf("\n");
}
Status StackLength(SqStack* S)
{
return S->top + 1;
}
//二進制轉換
void convert(int number, SqStack* S)
{
while (number)
{
S->data[++S->top] = number % 2;
number = number / 2;
}
S->data[++S->top] = number;
}
//括號匹配
Status match(SqStackFloat* S, char* str)
{
int i = 0, flag=0;
char ch = str[i],e;
while (ch != '\0')
{
switch (ch)
{
case '(':
S->data[++S->top] = ch;
break;
case '[':
S->data[++S->top] = ch;
break;
case '{':
S->data[++S->top] = ch;
break;
case ')':
e=S->data[S->top--] ;
if (e != '(')
flag = 1;
break;
case ']':
e = S->data[S->top--];
if (e != '[')
flag = 1;
break;
case '}':
e = S->data[S->top--];
if (e != '{')
flag = 1;
break;
default:
break;
}
if (flag)
return ERROR;
i++;
ch = str[i];
}
if (!flag && S->top == -1)
{
printf("匹配成功wanzhipeng!\n");
return OK;
}
else
{
return FLASE;
}
}