Karel rozdiel 2 cisel

Programovacie jazyky, rady, poradňa...
kevin_7
Novice
Novice
Príspevky: 4
Registrovaný: 05 dec 2009, 20:48

Karel rozdiel 2 cisel

Príspevok od používateľa kevin_7 »

cawte. potreboval by som pomoct s funkciou roz(), ktorou ma Karel odcitat 2 cisla , ktoru su vlastne pocet znaciek. Skusal som to spravit cez 2 jednorozmerne polia a v cykle odcitat i-ty prvok od j-teho prvku a vysledok ulozit do dalsieho jednorozmerneho pola (vektora) ako k-ty prvok tohto pola ale compilator mi vkuse vypisuje chyby. Preto sa chcem spytat ci som tu ulohu nahodou nepochopil zle? a prikladam do code zdrojak vsetkeho co som si spravil nakodeny v DEV C++. dakujem

Kód: Vybrať všetko

#include <windows.h>
#include <stdlib.h>
#include <string.h>

#define SLEEP 10

// definicia konstant na maximalne rozmery sveta
#define MAX_WIDTH 20
#define MAX_HEIGHT 20

// definicia konstant, ktore vo svete znamenaju volne miesto alebo stenu
#define EMPTY 0
#define WALL -1

// deklaracia globalnych premennych
int width, height;
int world[MAX_HEIGHT][MAX_WIDTH];

// struktura svet
struct World(
int width;
int height;
int world[MAX_HEIGHT][MAX_WIDTH]
);
struct World World;
// aktualna poloha robota
//enum Direction
enum Direction {
     EAST = 0,
     WEST = 180,
     NORTH = 90,
     SOUTH = 270
};

enum Boolean {
     False = 0,
     True = 1
};

//struktura Robot
struct Robot {
       int x;
       int y;
       enum Direction direction;
       int beepers;
};
struct Robot Robot;

// TESTOVACIE SVETY
int world1[5][6] = {
    { 0, 0, 0, -1, 0, 0},
    { 0, -1, 0, 0, -1, 0},
    { 0, -1, -1, -1, 0, 0},
    { 0, -1, 0, 0, -1, 0},
    { 0, -1, 0, 0, -1, 0}
};

int fullOfStars[5][6] = {
    { 25, 26, 27, 28, 29, 30 },
    { 24, 23, 22, 21, 20, 19 },
    { 13, 14, 15, 16, 17, 18 },
    { 12, 11, 10,  9,  8,  7 },
    {  1,  2,  3,  4,  5,  6 }
};


// TURNON FUNKCIE
// nacita informacie o svete z world1
void turnOn1()
{  
     width = sizeof(world1[0]) / 4;
     height = sizeof(world1) / sizeof(world1[0]);
     
     //printf("width=%d\n", width);
     //printf("height=%d\n", height);
     
     int a, b;
     for(a = 0; a < height; ++a)
     {
           for(b = 0; b < width; ++b)
           {
                 world[a][b] = world1[a][b];
                 //printf("world[%d][%d] = %d\n", a, b, world[a][b]);
           }
     }
}

// nacita informacie o svete z fullOfStars
void turnOn2()
{  
     width = sizeof(fullOfStars[0]) / 4;
     height = sizeof(fullOfStars) / sizeof(fullOfStars[0]);
     
     //printf("width=%d\n", width);
     //printf("height=%d\n", height);
     
     int a, b;
     for(a = 0; a < height; ++a)
     {
           for(b = 0; b < width; ++b)
           {
                 world[a][b] = fullOfStars[a][b];
                 //printf("world[%d][%d] = %d\n", a, b, world[a][b]);
           }
     }
}

// nacita informacie o svete zo vstupu zadaneho uzivatelom
// 3 2
// -1 0 -1
// 0 -1 0
void turnOnKeyboard()
{
     scanf("%d %d", &width, &height);
     
     int a, b;
     for(a = 0; a < height; ++a)
     {
           for(b = 0; b < width; ++b)
           {
                 scanf("%d", &world[a][b]);
           }
     }
}

// DRAW
// funkcia ktora vykresli svet
void draw()
{
     system("cls");
     
     // priradi do premennej direction smer orientacie robota
     char direction[10];
     switch(Robot.direction)
     {
          case EAST:
              strcpy(direction,"EAST ");
              break;
          case WEST:
              strcpy(direction,"NORTH");
              break;
          case NORTH:
              strcpy(direction,"WEST ");
              break;
          case SOUTH:
              strcpy(direction,"SOUTH");
              break;
     }
     
     // vypise stavovy riadok
     // ak world na karlovej pozicii je vacsi ako 0, tak su tam beepre a ich pocet to cislo, ak neni vacsie ako 0, tak treba vypisat 0
     printf("CORNER     FACING     BEEP-BAG     BEEP-CORNER\n");
     printf("(%d, %d)     %s          %d             %d\n", Robot.x, Robot.y, Robot.direction, Robot.beepers, (world[Robot.y][Robot.x] > 0)*world[Robot.y][Robot.x]);
     printf("\n");
     
     // vykresli svet
     int a, b;
     for(a = height - 1; a > -1; --a)
     {
           for(b = 0; b < width; ++b)
           {
                 if(a == Robot.y && b == Robot.x)
                 {
                      switch(Robot.direction)
                      {
                          case EAST:
                               printf(">");
                               break;
                          case WEST:
                               printf("^");
                               break;
                          case NORTH:
                               printf("<");
                               break;
                          case SOUTH:
                               printf("v");
                               break;
                      }
                 }
                 else if(world[a][b] == EMPTY)
                 {
                     printf(" ");
                 }
                 else if(world[a][b] == WALL)
                 {
                     printf("#");
                 }
                 else
                 {
                     printf("*");
                 }
           }
           printf("\n");
     }
     
     printf("\n");
     
     sleep(SLEEP);
}

// SENZORY
int frontIsClear()
{
    switch(Robot.direction)
    {
        case EAST:
             // >
             if(Robot.x + 1 >= width || world[Robot.y][Robot.x + 1] == WALL)
             {
                 return False;
             }
             else
             {
                 return True;
             }
             break;
        case WEST:
             // ^
             if(Robot.y + 1 >= height || world[Robot.y + 1][Robot.x] == WALL)
             {
                 return False;
             }
             else
             {
                 return True;
             }
             break;
        case NORTH:
             // <
             if(Robot.x - 1 < 0 || world[Robot.y][Robot.x - 1] == WALL)
             {
                 return False;
             }
             else
             {
                 return True;
             }
             break;
        case SOUTH:
             // v
             if(Robot.y - 1 < 0 || world[Robot.y - 1][Robot.x] == WALL)
             {
                 return False;
             }
             else
             {
                 return True;
             }
             break;
    }
}

int frontIsBlocked()
{
    return !frontIsClear();
}

int beepersInBag()
{
    return Robot.beepers > 0;
}

int noBeepersInBag()
{
    return !beepersInBag();
}

int beepersPresent()
{
    return world[Robot.y][Robot.x] > 0;
}

int noBeepersPresent()
{
    return !beepersPresent();
}

// BEEPER AKCE
void putBeeper()
{
     if(Robot.beepers > 0)
     {
         world[Robot.y][Robot.x]++;
         Robot.beepers--;
         draw();
     }
}

void pickBeeper()
{
     if(world[Robot.y][Robot.x] > 0)
     {
         world[Robot.y][Robot.x]--;
         Robot.beepers++;
         draw();
     }
}

// POHYB KARLA
void turnLeft()
{
     Robot.direction += 90;
     // ak je 360, tak to da naspat na 0
     Robot.direction = Robot.direction % 360;
     draw();
}

void turnRight()
{
     Robot.direction += 270;
     // ak je 360, tak to da naspat na 0
     Robot.direction = Robot.direction % 360;
     draw();
}

void move()
{
     if(frontIsClear())
     {
         switch(Robot.direction)
         {
             case EAST: // >
                  ++Robot.x;
                  break;
             case WEST: // ^
                  ++Robot.y;
                  break;
             case NORTH: // <
                  --Robot.x;
                  break;
             case SOUTH: // V
                  --Robot.y;
                  break;
         }
         
         draw();
     }
}

// TESTOVACIE FUNKCIE
void TestSensors()
{
    Robot.y = 3;
    Robot.x = 2;
    Robot.direction = EAST;
    printf("FIC %d %d (%d) = %d\n", Robot.y, Robot.x, Robot.direction, frontIsClear());
    Robot.direction = WEST;
    printf("FIC %d %d (%d) = %d\n", Robot.y, Robot.x, Robot.direction, frontIsClear());
    Robot.direction = NORTH;
    printf("FIC %d %d (%d) = %d\n", Robot.y, Robot.x, Robot.direction, frontIsClear());
    Robot.direction = SOUTH;
    printf("FIC %d %d (%d) = %d\n", Robot.y, Robot.x, Robot.direction, frontIsClear());
    printf("BIB %d %d\n", beepersInBag(), noBeepersInBag());
    Robot.beepers = 1;
    printf("BIB %d %d\n", beepersInBag(), noBeepersInBag());
}

void TestMovement()
{
     move();
     move();
     turnLeft();
     turnLeft();
     turnLeft();
     turnLeft();
     turnLeft();
     turnLeft();
     turnLeft();
     move();
     move();
}
     
void harvestAll()
{
     int aRight = 0;
     
     while(1)
     {
         while(beepersPresent())
         {
             pickBeeper();
         }
             
         while(frontIsClear())
         {         
             move();
             
             while(beepersPresent())
             {
                 pickBeeper();
             }
         }
     
         if(aRight)
         {
             turnRight();
         }
         else
         {
             turnLeft();
         }
     
         if(frontIsBlocked())
         {
             while(beepersInBag())
             {
                 putBeeper();
             }
             
             if(aRight)
             {
                 turnRight();
             }
             else
             {
                 turnLeft();
             }
             
             while(frontIsClear())
             {
                 move();
             }
             
             if(aRight)
             {
                 turnRight();
             }
             else
             {
                 turnLeft();
             }
             
             while(frontIsClear())
             {
                 move();
             }
             
             break;
         }
     
         move();
     
         if(aRight)
         {
             turnRight();
         }
         else
         {
             turnLeft();
         }
     
         aRight = !aRight; 
     }   
}
Napísať odpoveď