题目描述
开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
输入:
合法坐标为A(或者D或者W或者S) + 数字(两位以内)
坐标之间以;分隔。
非法坐标点需要进行丢弃。如AA10; A1A; % ; YAD; 等。
下面是一个简单的例子 如:
A10;S20;W10;D30;X;A1A;B10A11;;A10;
处理过程:
起点(0,0)
-
A10 = (-10,0)
-
S20 = (-10,-20)
-
W10 = (-10,-10)
-
D30 = (20,-10)
-
x = 无效
-
A1A = 无效
-
B10A11 = 无效
-
一个空 不影响
-
A10 = (10,-10)
结果 (10, -10)
注意请处理多组输入输出
输入描述:
一行字符串
输出描述:
最终坐标,以,分隔
示例1
输入
A10;S20;W10;D30;X;A1A;B10A11;;A10;
输出
10,-10
代码
通过的:
#include<stdio.h>
int main(void)
{
int count = 0;
char in[2000] = { '\0' };
char temp[20] = { '\0' };
while (gets(in) != NULL )
{
int i=0,x = 0, y = 0, j = 0;
while (in[i] != '\0')
{
if (in[i] != ';')
{
temp[j++] = in[i++];
count++;
}
else
{
j = 0;
i++;
if (count == 3)
{
if ((temp[j] == 'A' || temp[j] == 'D' || temp[j] == 'W' || temp[j] == 'S') &&temp[j + 1] >= '0'&&temp[j + 1] <= '9'&&temp[j + 2] >= '0'&&temp[j + 2] <= '9')
{
switch (temp[j])
{
case 'A':x -= (temp[j + 1]-'0') * 10 + (temp[j + 2]-'0'); break;
case 'D':x += (temp[j + 1] - '0') * 10 + (temp[j + 2] - '0'); break;
case 'W':y += (temp[j + 1] - '0') * 10 + (temp[j + 2] - '0'); break;
case 'S':y -= (temp[j + 1] - '0') * 10 + (temp[j + 2] - '0'); break;
default:break;
}
count = 0;
}
else
{
count = 0;
continue;
}
}
else if (count == 2)
{
if ((temp[j] == 'A' || temp[j] == 'D' || temp[j] == 'W' || temp[j] == 'S') && temp[j + 1] >= '0'&&temp[j + 1] <= '9')
{
switch (temp[j])
{
case 'A':x -= (temp[j + 1] - '0') ; break;
case 'D':x += (temp[j + 1] - '0') ; break;
case 'W':y += (temp[j + 1] - '0') ; break;
case 'S':y -= (temp[j + 1] - '0') ; break;
default:break;
}
count = 0;
}
else
{
count = 0;
continue;
}
}
else
{
count = 0;
continue;
}
}
}
printf("%d,%d\n", x, y);
}
return 0;
}
自己写的(未通过,示例通过)
#include<stdio.h>
#include<string.h>
int main(void)
{
char str[2000];
char temp[20];
int len, x = 0, y = 0, count = 0;
while (gets(str) != NULL)
{
int i=0,j = 0;
len = strlen(str);
for (int i = 0; i < len; )
{
if (str[i] != ';')
{
temp[j++] = str[i++];
/*i++;
j++;*/
count++;
}
else
{
j = 0;
if (count == 3)
{
if ((temp[j] == 'A' || temp[j] == 'W' || temp[j] == 'S' || temp[j] == 'D') && temp[j+1] >= '0' && temp[j + 1] <= '9' && temp[j +2] >= '0' && temp[j + 2] <= '9')
{
switch (temp[j])
{
case 'A':
x = x - (int)((temp[j +1] - 48) * 10 + (temp[j +2] - 48));
break;
case 'S':
y = y - (int)((temp[j + 1] - 48) * 10 + (temp[j + 2] - 48));
break;
case 'W':
y = y + (int)((temp[j + 1] - 48) * 10 + (temp[j + 2] - 48));
break;
case 'D':
x = x + (int)((temp[j + 1] - 48) * 10 + (temp[j + 2] - 48));
break;
}
count = 0;
}
else
{
count = 0;
}
}
else if (count == 2)
{
if ((temp[j] == 'A' || temp[j] == 'W' || temp[j] == 'S' || temp[j] == 'D') && temp[j+1] >= '0' && temp[j+1] <= '9')
{
switch (temp[j])
{
//temp = (int)((str[i + 1] - 48) * 10 + (str[i + 2] - 48));
case 'A':
x = x - (int)(temp[j + 1] - '0');
break;
case 'S':
y = y - (int)(temp[j + 1] - '0');
break;
case 'W':
y = y + (int)(temp[j + 1] - '0');
break;
case 'D':
x = x + (int)(temp[j + 1] - '0');
break;
}
count = 0;
}
else
{
count = 0;
}
}
else
{
count = 0;
}
i++;
}
}
printf("%d,%d\n", x, y);
}
return 0;
}