C++ 变量数组值取决于行数的增加
我正在处理的程序的简短描述:我输入一个数字C++ 变量数组值取决于行数的增加,c++,arrays,if-statement,C++,Arrays,If Statement,我正在处理的程序的简短描述:我输入一个数字n,表示行数AB变量代表矩形的尺寸——它的第二个输入。那么,最重要的部分。我有一个人站在这个矩形上,有一个起始位置xy和起始方向sx y在您可以在代码中看到的范围内。方向是S,J,V,Z(N,S,E,W)。因此,该输入位于第三行,由3个参数组成:xyss。正如您在我的代码中所看到的,如果我输入S,我的y值将增加+1,J将y值减少-1,以此类推 在我的代码末尾是一个for循环,它一次输出所有输入,因为我不希望它在每次输入之后立即输出 我的问题是,我输入:
n
,表示行数AB
变量代表矩形的尺寸——它的第二个输入。那么,最重要的部分。我有一个人站在这个矩形上,有一个起始位置xy
和起始方向s
<代码>x y在您可以在代码中看到的范围内。方向是S,J,V,Z(N,S,E,W)。因此,该输入位于第三行,由3个参数组成:xyss
。正如您在我的代码中所看到的,如果我输入S,我的y
值将增加+1,J将y
值减少-1,以此类推
在我的代码末尾是一个for循环,它一次输出所有输入,因为我不希望它在每次输入之后立即输出
我的问题是,我输入:
2
//任务或行数58
//a b维度Uloha 1(任务1):
45s
/xyss(S表示y+1)Uloha 2:
23 Z
//x y s(Z表示x-1)
输出:
13z
//这里应该是46s
13z
我按enter键,得到两行输出,但是xyss
的值只是第二行的最终值,所以它从逻辑上不记得每个任务。每次我输入新行时,xyss
的最后一个输入值都会更改。此外,还有一个if条件也不起作用,当x
y
低于0或高于a
或b
时,它应该打印SPADOL
你觉得怎么样,有什么建议吗?顺便说一下,我只是一个C++初学者。多谢各位
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <string>
using namespace std;
int n; // pocet uloh
int a; // rozmer obdlznika a
int b; // rozmer obdlznika b
int x;
int y;
string s; // smer
int i;
vector<string> inputs;
int main() {
cin >> n;
while(!((n >= 1)&&(n <=15000)))
{
cout << "max 15000" << flush;
cin >> n;
}
cin >> a >> b;
while(!((a >= 1)&&(a <=100) | (b >= 1)&&(b <= 100)))
{
cout << "max 100" << flush;
cin >> a >> b;
}
for (i = 0; i < n; i++)
{
cout << "Uloha " << i+1 << ":" << endl;
cin >> x;
cin >> y;
cin >> s;
while(!((x>=0)&&(x<=a))) {
cout << "Try Again x: " << flush;
cin >> x;
}
while(!((y>=0)&&(y<=b))) {
cout << "Try Again y: " << flush;
cin >> y;
}
if (s == "S"){
y = (y+1);
}else if (s == "J"){
y = (y-1);
}else if (s == "V"){
x = (x+1);
}else if (s == "Z"){
x = (x-1);
}
if(!((x>=0)&&(x<=a) | (y>=0)&&(y<=b))){
cout << x << ' ' << y << ' ' << s << ' ' << "SPADOL" << endl;
}
// inputs.push_back(x);
// inputs.push_back(y);
// inputs.push_back(z);
//for(vector<string>::iterator it = inputs.begin(); it != inputs.end(); ++it)
//{
//cout << x << ' ' << y << ' ' << *it << endl;
//}
} // koniec for
for ( i = 0 ; i < n ; i++ )
{
cout << x << ' ' << y << ' ' << s << endl;
}
system("pause");
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int n;//波切特乌洛
int a;//罗兹默·奥德尔兹尼卡a
int b;//罗兹默·奥德兹尼卡b酒店
int x;
int-y;
字符串s;//斯梅尔
int i;
矢量输入;
int main(){
cin>>n;
而(!((n>=1)和&(n>a>>b;
而(!((a>=1)和&(a=1)和&(b>b);
}
对于(i=0;i>s;
而(!((x>=0)和&(x=0)和&(y=0)和&(x=0)和&(y在使用or条件(a和b)|(c和d)时,请使用括号
如果(!((x>=0)和&(x=0)和&(y>n;
向量x(n);
向量y(n);
向量s(n);
而(!((n>=1)和&(n>a>>b;
而(!((a>=1)和&(a=1)和&(b>b);
}
对于(i=0;id;
while(!((d>=1)和(!((a>=1)和&(a=1)&&(b)你的意思是用&
而不是
?@user3286879至少你应该用
而不是
当你想要一个本地或的时候。但我不认为这是这里的主要问题。@chearsandhth.-Alf我用了
或
,但它无论如何都不起作用……只有
工作..但是让我们回到主要问题上来吧。我不明白你的主要问题是什么。你能更好地解释什么是错的以及你想要实现什么吗?我更新了我的问题,参见示例…我没有得到每一个输入..每次我的x、y和s值都来自上一个输入..我需要每一个输入的输出..不仅仅是来自上一个one..最后一个被复制到每一行,它不应该是..我已经更新了我的答案。检查这是否解决了你的问题我得到错误无效的从void*到int*的转换我将强制转换添加到了我的答案中。现在再试
#include <iostream>
#include <string>
#include <stdlib.h>
#include <string>
using namespace std;
int n; // pocet uloh
int a; // rozmer obdlznika a
int b; // rozmer obdlznika b
int *x = (int*)malloc(n*sizeof(int));
int *y = (int*)malloc(n*sizeof(int));
string *s = (string*)malloc(n*sizeof(string)); // smer
int i;
int d;
static const char alpha[] = {'D', 'L', 'P'};
char genRandom()
{
return alpha[rand() % strlen(alpha)];
}
// end of generator
int main() {
cin >> n;
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 >> s[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];}
if (s[i] == "S"){
y[i] = (y[i]+1);
}else if (s[i] == "J"){
y[i] = (y[i]-1);
}else if (s[i] == "V"){
x[i] = (x[i]+1);
}else if (s[i] == "Z"){
x[i] = (x[i]-1);
}
cin >> d;
while(!((d>=1)&& (d<=200))) {
cout << "Try Again d: " << flush;
cin >> d;}
for (int counter=0; counter<d; counter++)
{
cout << genRandom();
}
cout << endl;
} // koniec for
for ( i = 0 ; i < n ; i++ )
{
if(!((x[i]>=0)&&(x[i]<=a) & (y[i]>=0)&&(y[i]<=b))){
cout << x[i] << ' ' << y[i] << ' ' << s[i] << ' ' << "SPADOL" << endl;
}else{
cout << x[i] << ' ' << y[i] << ' ' << s[i] << endl;
}
}
free(x);free(y);free(s);
system("pause");
}
if(!(((x>=0)&&(x<=a)) | ((y>=0)&&(y<=b)))){
#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;
int d;
static const char alpha[] = {'D', 'L', 'P'};
char genRandom()
{
return alpha[rand() % strlen(alpha)];
}
// end of generator
int main() {
cin >> n;
vector<int> x(n);
vector<int> y(n);
vector<string> s(n);
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;
cout << "x: ";
cin >> x[i];
cout << "y: ";
cin >> y[i];
cout << "s: ";
cin >> s[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];}
if (s[i] == "S"){
y[i] = (y[i]+1);
}else if (s[i] == "J"){
y[i] = (y[i]-1);
}else if (s[i] == "V"){
x[i] = (x[i]+1);
}else if (s[i] == "Z"){
x[i] = (x[i]-1);
}
cin >> d;
while(!((d>=1)&& (d<=200))) {
cout << "Try Again d: " << flush;
cin >> d;}
for (int counter=0; counter<d; counter++)
{
cout << genRandom();
}
cout << endl;
} // koniec for
for ( i = 0 ; i < n ; i++ )
{
if(!((x[i]>=0)&&(x[i]<=a) & (y[i]>=0)&&(y[i]<=b))){
cout << x[i] << ' ' << y[i] << ' ' << s[i] << ' ' << "SPADOL" << endl;
}else{
cout << x[i] << ' ' << y[i] << ' ' << s[i] << endl;
}
}
}