2016-02-26 61 views
-3

我想打印矩形。它工作,如果我使用打印語句,但它不會打印,否則。如果有人能引導我在儀式方向。在此感謝您的幫助程序不打印所需字符

#include<stdio.h> 
#include<stdlib.h> 


char * drawRectangle(unsigned int height, unsigned int width){ 
    int row, col; 
    char *myArray = malloc(100); 

    int i = 0; 

    while(myArray[i] != '\0'){ 
     for(row = 0; row<height; row++){ 
      printf("\n"); 
      for(col = 0; col < width; col++){ 
       if(row ==0 || row == height-1 || col == 0 || col == width-1){ 
        //printf("*"); 
        myArray[i++] = "*"; 
       } 
       else{ 
        //printf(" "); 
        myArray[i++] = " "; 
       } 
      } 
     } 
     i++; 
    } 
    return myArray; 
} 

int main() { 
    char *c = drawRectangle(3,3); 
    printf("%c", c); 
    return (0); 
} 

是鏈接到節目

https://ideone.com/WxJuwx

+2

與錯誤的類型爲'printf的傳遞數據()'調用*未定義的行爲*。 '%c'調用'int',而不是'char *'。 – MikeCAT

+0

@MikeCAT,我試過了,它仍然不打印。 – Maddy

+2

還有一個未定義的行爲:在通過'malloc()'分配的緩衝區中使用值而不指定某個值 – MikeCAT

回答

3

直接的方法可以看看下面的方式

#include <stdio.h> 

void drawRectangle(unsigned int height, unsigned int width, char c) 
{ 
    for (unsigned int i = 0; i < height; i++) 
    { 
     for (unsigned int j = 0; j < width; j++) 
     { 
      int blank = i > 0 && i < height - 1 && j > 0 && j < width - 1; 
      printf("%c", blank ? ' ' : c); 
     } 
     printf("\n"); 
    } 
} 

int main(void) 
{ 
    drawRectangle(3, 3, '*'); 
}    

程序輸出是

*** 
* * 
*** 

如果你需要創建一個相應的數組(s)那麼程序可以看起來像

#include <stdlib.h> 
#include <stdio.h> 

char ** drawRectangle(unsigned int height, unsigned int width, char c) 
{ 
    char **rectangle = malloc(height * sizeof(char *)); 

    for (unsigned int i = 0; i < height; i++) 
    { 
     rectangle[i] = malloc(width * sizeof(char)); 
     for (unsigned int j = 0; j < width; j++) 
     { 
      int blank = i > 0 && i < height - 1 && j > 0 && j < width - 1; 
      rectangle[i][j] = blank ? ' ' : c; 
     } 
    } 

    return rectangle; 
} 

int main(void) 
{ 
    unsigned int n = 3; 
    char **rectangle = drawRectangle(n, n, '*'); 

    for (unsigned int i = 0; i < n; i++) 
    { 
     for (unsigned int j = 0; j < n; j++) printf("%c", rectangle[i][j]); 
     printf("\n"); 
    } 

    for (unsigned int i = 0; i < n; i++) free(rectangle[i]); 
    free(rectangle); 
}    

它的輸出是與上述相同的

*** 
* * 
*** 

另一種方法是創建字符串,將(或不會)包括新行字符的陣列。

或者創建一個字符數組,該字符數組將包含一個帶有嵌入的新行字符的字符串。

例如

#include <stdlib.h> 
#include <stdio.h> 

char * drawRectangle(unsigned int height, unsigned int width, char c) 
{ 
    char *rectangle = malloc(height * (width + 1) + 1); 

    unsigned int k = 0; 
    for (unsigned int i = 0; i < height; i++) 
    { 
     for (unsigned j = 0; j < width; j++) 
     { 
      int blank = i > 0 && i < height - 1 && j > 0 && j < width - 1; 
      rectangle[k++] = blank ? ' ' : c; 
     } 
     rectangle[k++] = '\n'; 
    } 

    rectangle[k] = '\0'; 

    return rectangle; 
} 

int main(void) 
{ 
    unsigned int n = 3; 
    char *rectangle = drawRectangle(n, n, '*'); 

    puts(rectangle); 

    free(rectangle); 
}    
6
  • 刪除無用的,調用未定義的行爲循環。
  • 使用%s而不是%c,通過printf()打印字符串。
  • 別忘了free()您通過malloc()分配的內容。
  • 檢查malloc()是否成功。
  • 將字符而不是指針分配給myArray
  • 終止通過添加空字符創建的字符串。

更正代碼:

#include<stdio.h> 
#include<stdlib.h> 

char * drawRectangle(unsigned int height, unsigned int width){ 
    unsigned int row, col; 
    char *myArray = malloc(100); /* you should calculate the size of required buffer and use it here */ 

    int i = 0; 

    if (myArray == NULL) return NULL; 

    for(row = 0; row<height; row++){ 
     //printf("\n"); 
     myArray[i++] = '\n'; 
     for(col = 0; col < width; col++){ 
      if(row ==0 || row == height-1 || col == 0 || col == width-1){ 
       //printf("*"); 
       myArray[i++] = '*'; 
      } 
      else{ 
       //printf(" "); 
       myArray[i++] = ' '; 
      } 
     } 
    } 
    myArray[i] = '\0'; 
    return myArray; 
} 

int main(void) { 
    char *c = drawRectangle(3,3); 
    if (c != NULL) printf("%s", c); 
    free(c); 
    return (0); 
} 
+2

我建議在內循環之後移動myArray [i ++] ='\ n';'。 –