2015-02-09 28 views
0

所以我有一个任务,我需要通过将指针操作替换为数组操作,并将字符串操作替换为字符操作来更改某些函数。现在我对指针,数组,字符串等有了基本的了解,但我不能理解我必须做什么,以及我应该如何去做。下面是代码:C编程指针和字符串操作

#include <stdio.h> 
#pragma warning(disable: 4996) 

// This program exercises the operations of pointers and arrays 
#define maxrow 50 
#define maxcolumn 50 

char maze[maxrow][maxcolumn]; // Define a static array of arrays of characters. 
int lastrow = 0; 

// Forward Declarations 
#define triple(x) x % 3 == 0 
void initialization(int, int); 
void randommaze(int, int); 
void printmaze(int, int); 



void initialization(int r, int c) { 
    int i, j; 
    for (i = 0; i < r; i++){ 
     maze[i][0] = 'X';  // add border 
     maze[i][c - 1] = 'X'; // add border 
     maze[i][c] = '\0'; // add string terminator 

     for (j = 1; j < c - 1; j++) 
     { 
      if ((i == 0) || (i == r - 1)) 
       maze[i][j] = 'X'; // add border 
      else 
       maze[i][j] = ' '; // initialize with space 
     } 
    } 
} 

// Add 'X' into the maze at random positions 
void randommaze(int r, int c) { 
    int i, j, d; 
    for (i = 1; i < r - 1; i++) { 
     for (j = 1; j < c - 2; j++) { 
      d = rand(); 
      if (triple(d)) 
      { 
       maze[i][j] = 'X'; 
      } 
     } 
    } 
    i = rand() % (r - 2) + 1; 
    j = rand() % (c - 3) + 1; 
    maze[i][j] = 'S'; // define Starting point 
    do 
    { 
     i = rand() % (r - 2) + 1; 
     j = rand() % (c - 3) + 1; 
    } while (maze[i][j] == 'S'); 

    maze[i][j] = 'G'; // define Goal point 
} 

// Print the maze 
void printmaze(int r, int c) { 
    int i, j; 
    for (i = 0; i < r; i++) { 
     for (j = 0; j < c; j++) 
      printf("%c", maze[i][j]); 
     printf("\n"); 
    } 
} 

void main() { 
    int row, column; 
    printf("Please enter two integers, which must be greater than 3 and less than maxrow and maxcolomn, respectively\n"); 
    scanf("%d\n%d", &row, &column); 
    while ((row <= 3) || (column <= 3) || (row >= maxrow) || (column >= maxcolumn)) { 
     printf("both integers must be greater than 3. Row must be less than %d, and column less than %d. Please reenter\n", maxrow, maxcolumn); 
     scanf("%d\n%d", &row, &column); 
    } 
    initialization(row, column); 
    randommaze(row, column); 
    printmaze(row, column); 
    //encryptmaze(row, column); 
    //printmaze(row, column); 
    //decryptmaze(row, column); 
    //printmaze(row, column); 
} 

这里有问题我奋力上:

  • 重写功能randommaze(行,列)通过为所有代指针操作数组操作。除了获取指针的初始值之外,您不能使用像迷宫[i] [j]这样的索引操作。

  • 通过将字符串操作替换为所有字符操作来重写函数printmaze(row,column)。

  • 如果有人可以向我解释我应该做什么以及我应该如何做,我真的很感激。谢谢!

    +0

    好吧,迷宫的地址是迷宫所以pMaze =迷宫获取迷宫中第一个地点的地址。 pMaze ++获取迷宫中下一个位置的地址。 (pMaze + 1)也是如此。 (迷宫+最大列)获取迷宫中第二排的地址。这是完成作业所需的全部内容。 – user3629249 2015-02-09 02:03:21

    +0

    通常的做法是将#define名称中的所有字母大写,并将每个单词与'_'分开,以便建议#define MAX_COLUMN(50)等。另外,为了避免大量非常困难的调试工作,现在或在将来,#define'd的值应该被parens包围当您使用#define开始编写宏时,使用parens IE #define MIN(a,b)((a)<(b))? (a):(b)..并记住宏被调用时,在宏名称和开始的paren I.E之间没有空格。使用MY_MACRO(a)不是MY_MACRO(a) – user3629249 2015-02-09 02:08:30

    回答

    0

    问题2:

    数组可以用作指向其第一个成员的指针。因此,例如,array[0]*array返回相同的内容 - 数组第一个元素的值。由于数组是连续的内存块,因此如果您增加(或添加偏移量)指向数组开头的指针,则指向数组的下一个元素。这意味着array[1]*(array + 1)是一回事。

    如果你有一个迭代索引数组的循环,你可以使用指针增量来编写它。示例:

    /* Loop indexing an array */ 
    int my_array [10]; 
    int i = 0; 
    for(; i < 10; ++i) { 
        my_array[i] = 0; 
    } 
    
    /* Loop by offsetting a pointer */ 
    int my_array [10]; 
    int i = 0; 
    int *ptr = my_array; /* Make it point to the first member of the array*/ 
    for(; i < 10; ++i) [ 
        *(ptr + i) = 0; 
    } 
    
    /* Looping by incrementing the pointer */ 
    int my_array [10]; 
    int *ptr = my_array; /* Make it point to the first member of the array */ 
    int *end_ptr = my_array + 10; /* Make a pointer pointing to one past the end of the array */ 
    for(; ptr != end; ++ptr) [ 
        *ptr = 0; 
    } 
    

    所有这些代码示例都执行相同的操作。将0分配给数组的所有成员。如果你有一个多维数组,只要记住它仍然只是一个连续的内存块。

    问题3:

    这个问题不是那么清楚,我,所以我的你是意料中的事可能有点过,但因为你仅仅使用printf打印单个字符解释什么,我猜你应该使用一个函数来输出一个单一的字符。类似于putchar

    希望这会引导你走向正确的方向。

    0

    听起来好像你参与了数据结构课程。第一个挑战是构建一个数组映射函数。例如:

    int main(int argc, char **argv) 
    { 
        int values[20][40]; 
        values[0][0] = 1; 
        values[10][10] = 20; 
        /* Let's print these two ways */ 
    
        printf("0,0: %d 10,10: %d\n", values[0][0], values[10][10]); 
        printf("0,0: %d 10,10: %d\n", *((*values) + (sizeof(int) * 0) + sizeof(int) * 0)), *((*values) + (sizeof(int) * 10) + sizeof(int) * 10))); 
    } 
    

    我们所做的是获得的存储器中的2D阵列中的非常第一字节的(*值)中的地址,然后加入字节的原始数作为偏移到它来定位值从我们想要访问的“数组”中选择。

    像这样练习的一个要点就是向你展示这门语言实际上是如何工作的。这是他的数组映射函数的一般工作原理,可以作为基础,例如,以后的语言或编译器设计课程,除了快速实现更复杂的内存结构。因为在C中没有实际的“字符串”操作,所以我不太清楚这一点。我需要更多的细节。