scanf("%c",&kongge); if(kongge=="\0") void xitong;
时间: 2023-07-06 21:23:32 浏览: 30
这段代码也有问题,应该改成以下形式:
```c++
char kongge;
scanf("%c", &kongge);
if (kongge == '\0') {
void xitong;
}
```
原因是:
1. `kongge` 应该是字符类型,而不是字符串类型;
2. 判断空字符应该使用单引号包裹的 `\0`,而不是双引号包裹的 `\0`;
3. `void xitong` 不是有效的语句,应该根据实际需要进行修改。
相关问题
scanf("%s",&s)==1
scanf("%s",&s)==1是用来读取字符串的输入函数,其中%s是用来读取字符串的格式控制符,&s是指向字符串的指针。当scanf函数成功读取到一个字符串时,返回值为1,否则返回值为0。下面是一个例子:
```c
#include <stdio.h>
int main() {
char s[100];
if (scanf("%s", &s) == 1) {
printf("Input string is: %s\n", s);
} else {
printf("Failed to read input string.\n");
}
return 0;
}
```
int Matching( void ) {Stack S; InitStack( S ); int flag = 1; char ch; scanf(“%c”, &ch ); while( ch!=‘#’ && flag ){ switch( ch){ case ‘[‘: case ‘(‘: push( S, ch ); break; case ‘)’: if( !StackEmpty(S)&&GetTop(S)==‘(‘ ) pop(S,x); else flag = 0; break; case ‘]’: if( !StackEmpty(S)&&GetTop(S)==‘[‘ ) pop(S,x); else flag = 0; break; } scanf(“%c”, &ch ); } if (StackEmpty( S ) && flag) return 1; else return 0; } void main( ) { int i; i = Matching( ); if( i==1 ) printf(“匹配!”); else printf(“不匹配!”); } 改进匹配函数(函数另外命名),使之能通过返回值进行更细致的区分:1表示正确、0表示交叉错误、-1表示右边多、-2表示左边多。
可以参考下面的代码实现改进后的匹配函数:
```
int Matching(void) {
Stack S;
InitStack(S);
char ch, x;
int flag = 1, result = 1, count_left = 0, count_right = 0;
scanf("%c", &ch);
while (ch != '#' && flag) {
switch (ch) {
case '[':
case '(':
Push(S, ch);
count_left++;
break;
case ')':
if (!StackEmpty(S) && GetTop(S, x) && x == '(') {
Pop(S, x);
count_right++;
} else {
flag = 0;
result = -1;
}
break;
case ']':
if (!StackEmpty(S) && GetTop(S, x) && x == '[') {
Pop(S, x);
count_right++;
} else {
flag = 0;
result = -2;
}
break;
}
scanf("%c", &ch);
}
if (!StackEmpty(S) && flag) {
result = 0;
} else if (count_left != count_right && flag) {
result = 0;
}
return result;
}
```
增加了计数变量 `count_left` 和 `count_right`,用于记录左括号和右括号的数量,以便判断左右括号数量是否相等。同时,根据题目要求,修改了返回值的意义,通过不同的返回值表示不同的匹配结果。