变量数组值取决于增加的行数

时间:2014-02-09 10:03:05

标签: c++ arrays if-statement

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

在我的代码结束时是一个for循环,它一次输出所有输入,因为我不希望它在每次输入后立即输出。

我的问题是,我输入了:

2 //任务或行数
5 8 // a b尺寸
Uloha 1(任务1):
4 5 S // x y s(S表示y + 1)
Uloha 2:
2 3 Z // x y s(Z表示x-1)

输出:

1 3 Z //此处应为4 6 S

1 3 Z

我按回车键,我得到两行输出,但x y s的值只是第二行的最终值,所以逻辑上它不记得每个任务。每次输入新行时,x y s的最后一个输入值都会更改。而且,如果x y低于0或高于ab,那么if条件也不起作用,它应该打印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");
}

编辑产品:&gt;

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

1 个答案:

答案 0 :(得分:0)

使用或条件(a&amp;&amp; b)|时放置括号(C&安培;&安培; d)。

if(!(((x>=0)&&(x<=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)];
}
// 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;
        }

    } 


}