【发布时间】:2014-02-15 18:32:07
【问题描述】:
我在记住变量以供下次使用并将其打印出来时遇到问题。我会详细解释它,以便了解我在我的程序中要做什么。
我有一个人在大小为a b 的矩形上行走。我输入起始位置x y 和人的起始方向(North = y+1, South = y-1, East = x+1, West = x-1 // 在我的代码中它是 S,J,V,Z )。所以我的输入看起来像这样:
5 6 // a b
3 3 S // x y s(代表起始方向-北)
现在,我输入移动次数d 为应该移动的人生成。
我输入数字 4,它可以由 3 个字母生成:D、L、P(前进,左转 90 度,右转 90 度)。
4 // d
PLDL // 移动
现在,这个人应该按照这些动作走路。所以如果人的位置和起始方向是3 3 S,它应该右转(我的方向是东,但位置相同),然后左转(方向又是北,相同的位置),然后向前(现在我移动y+1,我的方向仍然是北),最后一步是左转(西方向)。所以mi最终位置和方向(输出)是:
3 4 Z
希望你能理解。如果有不清楚的地方,请在评论中提问。
我现在得到奇怪的输出,不真实的数字。我无法弄清楚如何将变量放在一起以及解决它的条件。我的代码首先采用起始方向和位置,但稍后当我生成移动时,它应该根据生成的字符串更改为最终输出。可悲的是,它没有按我的预期工作。你有什么建议吗?我的问题有点广泛,但我希望我们能一起解决。
#include <iostream>
#include <string>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
int n; // pocet uloh
int a; // rozmer obdlznika a
int b; // rozmer obdlznika b
int i;
static const char alpha[] = {'D', 'L', 'P'};
char genRandom()
{
return alpha[rand() % strlen(alpha)];
}
// end of generator
// funkcia na pohyb
void pohyb (){
int x[i];
int y[i];
string sD = ""; // starting direction
string mD = ""; // middle direction (stored one for next use)
string eD = ""; // ending direction to print out in output
string d = ""; // number of generated directions eg. d=6 ==> PDDLPPD
for (int i=0; i < d.size(); i++){
if (sD[i] == 'S'){
if(d[i] == 'D'){
y[i] = (y[i]+1);
}else if(d[i] == 'L'){
mD[i] == 'Z';
}else if(d[i] == 'P'){
mD[i] == 'V';
}
}else if (sD[i] == 'J'){
if(d[i] == 'D'){
y[i] = (y[i]-1);
}else if(d[i] == 'L'){
mD[i] == 'V';
}else if(d[i] == 'P'){
mD[i] == 'Z';
}
}else if (sD[i] == 'V'){
if(d[i] == 'D'){
x[i] = (x[i]+1);
}else if(d[i] == 'L'){
mD[i] == 'S';
}else if(d[i] == 'P'){
mD[i] == 'J';
}
}else if (sD[i] == 'Z'){
if(d[i] == 'D'){
x[i] = (x[i]-1);
}else if(d[i] == 'L'){
mD[i] == 'J';
}else if(d[i] == 'P'){
mD[i] == 'S';
}
} // koniec if podmienky
eD = mD[i];
} // koniec for loopu
// vystup
for ( i = 0 ; i < n ; i++ )
{
if(!((x[i]>=0)&&(x[i]<=a) & (y[i]>=0)&&(y[i]<=b))){
cout << x[i] << ' ' << y[i] << ' ' << eD[i] << ' ' << "SPADOL" << endl;
}else{
cout << x[i] << ' ' << y[i] << ' ' << eD[i] << endl;
}
}
} // koniec funkcie pohyb
int main() {
cin >> n;
vector<int> x(n); // x position
vector<int> y(n); // y position
vector<int> d(n); // zombie directions generation ex. DPLDDP
vector<string> sD(n); // starting direction
vector<string> eD(n); // ending direction
while(!((n >= 1)&&(n <=15000)))
{
cout << "max 15000" << flush;
cin >> n;
}
cin >> a >> b;
while(!((a >= 1)&&(a <=100) & (b >= 1)&&(b <= 100)&&(a!=b)))
{
cout << "chyba max 100 alebo a!=b" << endl;
cin >> a >> b;
}
for (i = 0; i < n; i++)
{
cout << "Uloha " << i+1 << ":" << endl;
cin >> x[i];
cin >> y[i];
cin >> sD[i];
while(!((x[i]>=0)&&(x[i]<=a))) {
cout << "Try Again x: " << flush;
cin >> x[i];}
while(!((y[i]>=0)&&(y[i]<=b))) {
cout << "Try Again y: " << flush;
cin >> y[i];}
cin >> d[i];
while(!((d[i]>=1)&& (d[i]<=200))) {
cout << "Try Again d: " << flush;
cin >> d[i];}
for (int counter=0; counter<d[i]; counter++)
{
cout << genRandom();
}
cout << endl;
} // koniec for
pohyb();
system("pause");
}
示例输入:
3
3 5
2 2 S
8
DPLDLPDD
2 4 Z
7
PDDPDPD
2 1 J
8
PPDLDDDD
和输出
2 5 S SPADOL // spadol means his location is out of the rectangle
3 4 J
0 2 Z SPADOL
【问题讨论】:
-
您应该使用
switch()而不是if,这个y[I] = (y[I]+1)与y[I] ++或y += 1相同,变量sD、mD、eD 应该是char而不是@ 987654340@如果你只想记住一个符号 -
您应该提供一个示例输入、其预期和实际输出。
-
@Quest 好的,我该如何更改其余代码,因为我得到
[Error] invalid typeschar[int]' 为数组下标` 为== S等...当我更改 sD , mD, eD 转字符 -
@febeks 这是一个小问题,您可以在您的代码工作后解决。你读过我回答的第 2 项吗?
-
@febeks 在我的答案中添加了一个编辑,解释了如何将变量而不是它的值传递给函数。
标签: c++ variables memory if-statement