2014-02-09 2 views
-1

작업중인 프로그램에 대한 간단한 설명 : 행 수를 의미하는 숫자 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 // 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

1 3 Z

내가 Enter 키를 누릅니다, 그리고 출력의 두 행을 얻을 수 있지만, x y s의 값이 두 번째 행의 마지막 값이다, 그래서 각 작업 기억 나던, 논리적으로. 새 행을 입력 할 때마다 마지막 입력 값 x y s이 바뀝니다. 또한 xy이 0보다 작거나 a 또는 b 일 때 if 조건이 작동하지 않으면 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> 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"); 
} 

편집 :>

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

'동안 (! ((A> = 1) && (A <=100) | (b > = 1) && (b <= 100)))'는 &&'하셨습니까'아마? –

+0

'| '대신'&&'를 사용 하시겠습니까? – frank17

+0

@ user3286879 최소한 "OR"을 원한다면'||'대신'||'를 사용해야합니다. 그러나 이것이 이것이 주된 문제라고 생각하지 않습니다. – wimh

답변

0

넣고 괄호 당신이 또는 조건 (A & & 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; 
     } 

    } 


} 
+0

이 게시물에서 해결하려고하는 주된 문제가 아닌가요 – frank17

+0

나는 당신의 주된 문제를 이해하지 못합니다. 무엇이 잘못되었으며 무엇을 달성하기를 더 잘 설명 할 수 있습니까? – drolando

+0

내 질문을 업데이트, 예제를 참조하십시오 ... 모든 단일 input..every 점점 내 x 및 y 및 s 값을 마지막 입력 .... 나는 각 하나의 입력 .. 그냥 마지막 하나에서 출력이 필요합니다. .. 마지막 하나는 다른 행에 복사되고 그것은 .. should bent .. – frank17