#华为坐标移动的一个编程题,不知道为什么,代码碰到B10A11就自动停止对输入的读取了?
下面是我的代码:
#include <stdio.h>
#include <string.h>
int main(){
char str[10000];
char res[20];
int i=0,j=0;
int x=0,y=0;
scanf("%s",str);
for(i=0;i<strlen(str);i++){
while(str[i]!=';'){
res[j]=str[i];
j++;
i++;
}
//做加减运算
if(strlen(res)==2 && res[1]>='0' && res[1]<='9'){
if(res[0]=='A'){
x=x-res[1]+48;
}
else if(res[0]=='D'){
x=x+res[1]-48;
}
else if(res[0]=='W'){
y=y+res[1]-48;
}
else if(res[0]=='S'){
y=y-res[1]+48;
}
else;
}
else if(strlen(res)==3 && res[1]>='0' && res[1]<='9' && res[2]>='0' && res[2]<='9'){
if(res[0]=='A'){
x=x-res[2]+48-(res[1]-48)*10;
}
else if(res[0]=='D'){
x=x+res[2]-48+(res[1]-48)*10;
}
else if(res[0]=='W'){
y=y+res[2]-48+(res[1]-48)*10;
}
else if(res[0]=='S'){
y=y-res[2]+48-(res[1]-48)*10;
}
else;
}
else;
//进入下一次while循环
j=0;
}
printf("%d,%d",x,y);
return 0;
}
运行结果如下图:
没有B10A11,结果正确:
有B10A11时,B10A11后边的输入都不执行:
到底是为什么啊?
第15行插入:res[j] = '\0';
#include <stdio.h>
#include <string.h>
int main(){
char str[10000];
char res[20];
int i=0,j=0;
int x=0,y=0;
scanf("%s",str);
for(i=0;i<strlen(str);i++){
while(str[i]!=';'){
res[j]=str[i];
j++;
i++;
}
res[j] = '\0'; // 修改
//做加减运算
if(strlen(res)==2 && res[1]>='0' && res[1]<='9'){
if(res[0]=='A'){
x=x-res[1]+48;
}
else if(res[0]=='D'){
x=x+res[1]-48;
}
else if(res[0]=='W'){
y=y+res[1]-48;
}
else if(res[0]=='S'){
y=y-res[1]+48;
}
else;
}
else if(strlen(res)==3 && res[1]>='0' && res[1]<='9' && res[2]>='0' && res[2]<='9'){
if(res[0]=='A'){
x=x-res[2]+48-(res[1]-48)*10;
}
else if(res[0]=='D'){
x=x+res[2]-48+(res[1]-48)*10;
}
else if(res[0]=='W'){
y=y+res[2]-48+(res[1]-48)*10;
}
else if(res[0]=='S'){
y=y-res[2]+48-(res[1]-48)*10;
}
else;
}
else;
//进入下一次while循环
j=0;
}
printf("%d,%d",x,y);
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:核心代码:
/* 位置互换元素 */
/* nums[]指的是要进行互换元素的数组;i指的是起始下标;j指的是结束下标 */
void reverse(int nums[],int i,int j){
int temp;
while(i<j){
temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
i++;
j--;
}
}
/* 将B中的所有元素移动到A的前面 */
void move(int A[],int m,int n){
reverse(A,0,m-1);
reverse(A,m,m+n-1);
reverse(A,0,m+n-1);
}
完整代码:
#include<stdio.h>
#define maxSize 30
/* 打印数组 */
void print(int nums[],int n) {
printf("\n");
for(int i=0; i<n; i++) {
printf("%d\t",nums[i]);
}
printf("\n");
}
/* 位置互换元素 */
/* nums[]指的是要进行互换元素的数组;i指的是起始下标;j指的是结束下标 */
void reverse(int nums[],int i,int j){
int temp;
while(i<j){
temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
i++;
j--;
}
}
/* 将B中的所有元素移动到A的前面 */
void move(int A[],int m,int n){
reverse(A,0,m-1);
reverse(A,m,m+n-1);
reverse(A,0,m+n-1);
}
int main() {
int A[maxSize]= {1,2,3,4,5,6,7};
int An=7;
int m=3;
int n=4;
move(A,m,n);
print(A,An);
return 0;
}
这个问题的原因是,在C语言中,scanf
函数默认以空格、制表符、换行符作为输入的结束符号。而B10A11中间是没有空格的,所以scanf
会一直等待用户输入结束,导致代码停止执行后续操作。
解决这个问题的方法是修改scanf
的格式控制字符串,使其匹配B10A11输入。
下面是一种可能的解决方案:
#include<stdio.h>
#include<string.h>
int main() {
char input[10];
while (1) {
printf("输入命令:");
// 修改scanf的格式控制字符串
scanf("%9[^\n]s", input);
// 清空输入缓冲区
while (getchar() != '\n');
// 根据输入的命令执行相应的操作
if (strcmp(input, "B10A11") == 0) {
break;
} else {
// 执行其他操作
// ...
}
}
printf("程序已结束。\n");
return 0;
}
这里使用了%9[^\n]s
作为scanf
的格式控制字符串,9
表示最大输入字符的数量,[^\n]
表示匹配除了换行符之外的所有字符,s
表示字符串类型。这样就可以读入包含空格的输入了。
值得注意的是,在使用[^\n]
时,我们需要手动清空输入缓冲区,以防止多次循环输入导致的问题。可以使用while (getchar() != '\n');
来清空输入缓冲区。
通过以上修改,当输入B10A11时,代码将继续执行后续的操作。如果输入其他命令,代码也会按照预期执行其他操作。