变量数组值取决于增加的行号 [英] variable array value depending on increasing row number

查看:96
本文介绍了变量数组值取决于增加的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的我的程序的简短描述:我输入一个数字 n 这意味着行数。 a b 变量表示矩形的尺寸 - 其第二个输入。然后,最重要的部分。我有一个人站在这个矩形上,起始位置 x y ,起始方向 s x y 在范围内,您可以在代码中看到。方向是S,J,V,Z(N,S,E,W)。所以这个输入在第三行,它由3个参数组成: x y s 。你可以在我的代码中看到,如果我输入S,我的 y 值增加+1,J减少 y 值为-1,等等。



在我的代码的末尾是一个for循环,它一次输出所有的输入,因为我不想立即输出



2

我的问题是, / code> //任务或行数

5 8 // ab尺寸

Uloha 1 1):

4 5 S // xys(S表示y + 1)

Uloha 2:

2 3 Z // xys(Z表示x-1)



输出:



1 3 Z //这里应该是 4 6 S p>

1 3 Z



得到两行输出,但是 xys 的值只是第二行的最终值,因此它不会在逻辑上记住每个任务。每次输入新行时, x y s 的最后一个输入值会更改。此外,如果 x y 低于0或高于<$,则有一个if条件不起作用c $ c> a b ,它应该打印 SPADOL



你觉得什么建议? Btw,我只是一个初学者在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>输入;


int main(){

cin>> n;

while(!((n> = 1)&&(n <= 15000)))
{
cout< max 15000<<冲洗;
cin>> n;
}


cin>> a>> b;

while(!((a> = 1)&&(a< = 100)|(b> = 1)& )
{
cout<< max 100<<冲洗;
cin>> a>> b;
}


for(i = 0; i {
cout< Uloha< i + 1<< :<< endl;

cin>> X;
cin>> y;
cin>> s;

while(!((x> = 0)&&(x< = a))){
cout< Try Again x:<<冲洗;
cin>> X;
}
while(!((y> = 0)&&(y <= b))){
cout< Try Again y:<<冲洗;
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)&& b $ 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;

}

系统(暂停);
}

EDIT:>

  #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)];
}
//生成器结尾


int main(){

cin>> n;

while(!((n> = 1)&&(n <= 15000)))
{
cout< max 15000<<冲洗;
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 {
cout< Uloha< i + 1<< :<< endl;

cin>> x [i];
cin>>义];
cin>> s [i];

while(!((x [i]> = 0)&&(x [i] <= a))){
cout<< Try Again x:<<冲洗;
cin>> x [i];}
while(!((y [i]> = 0)&&(y [i] <= b))){
cout< Try Again y:<<冲洗;
cin>> y [i];};


if(s [i] ==S){
y [i] =
} 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:<<冲洗;
cin>> d;}


for(int counter = 0; counter {
cout< genRandom();
}
cout<< endl;

} // koniec for

for(i = 0; i {
if(! i] = 0)&(x [i] <= a)和(y [i]
cout<< x [i]< ''<< y [i] ''<< s [i]<< ''<< SPADOL< endl;
} else {
cout<< x [i]< ''<< y [i] ''<< s [i]<< endl;
}

}

free(x); free(y); free(s);

系统(pause);
}


解决方案

或条件(a& b)| ((x> = 0)&&(x< a>)的情况下, = a))|((y≥0)&(y≤b)))){

这段代码为我工作。您可以使用c ++向量来存储值并在最后打印。

  #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)];
}
//生成器结尾

int main(){

cin>> n;
vector< int> x(n);
vector< int> y(n)。
vector< string> s(n)。

while(!((n> = 1)&&(n <= 15000)))
{
cout< max 15000<<冲洗;
cin>> n;
}


cin>> a>> b;

while(!((a> = 1)&&(a< = 100)&(b> = 1)& &&(a!= b)))
{
cout< chyba max 100 alebo a!= b< endl;
cin>> a>> b;
}


for(i = 0; i {
cout< Uloha< i + 1<< :<< endl;
cout<< X: ;
cin>> x [i];
cout<< y:;
cin>>义];
cout<< s:;
cin>> s [i];

while(!((x [i]> = 0)&&(x [i] <= a))){
cout<< Try Again x:<<冲洗;
cin>> x [i];}
while(!((y [i]> = 0)&&(y [i] <= b))){
cout< Try Again y:<<冲洗;
cin>> y [i];};


if(s [i] ==S){
y [i] =
} 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:<<冲洗;
cin>> d;}


for(int counter = 0; counter< d; counter ++)
{
cout< genRandom();
}
cout<< endl;

} // koniec for

for(i = 0; i {
if(! i] = 0)&(x [i] <= a)和(y [i]
cout< x [i]< ''<< y [i] ''<< s [i]<< ''<< SPADOL< endl;
} else {
cout<< x [i]< ''<< y [i] ''<< s [i]<< endl;
}

}


}


A short description of my program that I am working on: I input a number n which means number of rows. a b variables stands for dimensions of rectangle - its second input. Then, most important part. I have a person which is standing on this rectangle and has a starting position x y and starting direction s. x y are in range as you can see in the code. Directions are S,J,V,Z (N,S,E,W). So this input is in 3rd row and it consists of 3 parameters: x y s. As you can see in my code, if I input S, my y value increases by +1, J decreases y value by -1, etc..

At the end of my code is a for loop, which outputs all inputs at once, because I don not want it output right away, after each input.

My problem is, I input:

2 // number of tasks or rows
5 8 // a b dimensions
Uloha 1 (Task 1):
4 5 S // x y s (S means y+1)
Uloha 2:
2 3 Z // x y s (Z means x-1)

Output:

1 3 Z // here should be 4 6 S

1 3 Z

I press enter, and I get two rows of output but the values of x y s are only final values of the second row, so it doesnt remember each task, logically. The last input value of x y s changes every time I input new row. And also, there is a if condition which is not working too, when x y goes under 0 or above a or b, it should print SPADOL.

What do you think, any suggestions? Btw, Im just a beginner in C++. Thank you.

#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");
}

EDIT:>

    #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");
}

解决方案

Put brackets when you use the or condition (a&&b) | (c&&d).

if(!(((x>=0)&&(x<=a)) | ((y>=0)&&(y<=b)))){

This code is working for me. You can use the c++ vector to store the values and print them at the end.

#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;
        }

    } 


}

这篇关于变量数组值取决于增加的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆