字符处理和字符迭代

时间:2011-03-05 20:12:29

标签: c

来源:

#include <stdio.h>

void main()

{

int i, j, tree_Size, spaces, total_Spaces, x;
char tree_Characters;

printf("Enter characters to use --> ");
 scanf("%c", &tree_Characters);
printf("Enter size of tree --> ");
 scanf("%d", &tree_Size);

   //For handling top part of tree
   for (i = 0; i <= tree_Size; i++) {
      printf("\n");
      total_Spaces = tree_Size - i;

        //Determine spaces before each number for pyramid
      for (spaces = 1; spaces <= total_Spaces; spaces++) 
         printf(" ");

      //Make first line always one number
      for (j = -1; j < i; j++)
         if (j <= -1)
            printf("%c", tree_Characters);
         else if (j > -1)
            printf("%c%c", tree_Characters, tree_Characters);
   } 

   //For handling stem and base of tree
   for (i = 0; i <= tree_Size; i++) {

      //if handling stem
      if (i < tree_Size) {
         printf("\n");  
         for (spaces = 1; spaces <= tree_Size; spaces++)
            printf(" ");
         printf("%c", tree_Characters);
      }

      //else if handling base

      else if (i == tree_Size) {
         printf("\n");

         for (x = 0; x <= tree_Size * 2; x++) {
            printf("%c", tree_Characters);
         }
      }
   }

printf("\n");

}

Here is the output it produces so far
Here is a thread I made on reddit for better understanding (comments)

作业的目标是看起来像这样:

*** Print a Tree ***

Enter characters to use ---   *|=+
Enter size of tree      ---   4

    *
   ***
  *****
 *******
*********
    |
    |
    |
    |
====+====

我不明白的是如何处理字符。老师提到将字符放入缓冲区然后清除它等等,我不知道。有人可以帮忙吗?我必须使用scanf,不能将任何东西转换成字符串或使用任何高级的东西。

2 个答案:

答案 0 :(得分:0)

    Enter characters to use --> *|=+
    Enter size of tree --> 4

        *
       ***
      *****
     *******
    *********
        |
        |
        |
        |
    ====+====

这是修改后的来源,我改变的行我用// **标记:

    #include <stdio.h>

    void main()

    {

    int i, j, tree_Size, spaces, total_Spaces, x;
    char tree_Characters[4]; //**

    printf("Enter characters to use --> ");
     scanf("%s", tree_Characters);          // **
    printf("Enter size of tree --> ");
     scanf("%d", &tree_Size);

       //For handling top part of tree
       for (i = 0; i <= tree_Size; i++) {
          printf("\n");
          total_Spaces = tree_Size - i;

            //Determine spaces before each number for pyramid
          for (spaces = 1; spaces <= total_Spaces; spaces++) 
             printf(" ");

          //Make first line always one number
          for (j = -1; j < i; j++)
             if (j <= -1)
                printf("%c", tree_Characters[0]); // **
             else if (j > -1)
                printf("%c%c", tree_Characters[0], tree_Characters[0]); // **
       } 

       //For handling stem and base of tree
       for (i = 0; i <= tree_Size; i++) {

          //if handling stem
          if (i < tree_Size) {
             printf("\n");  
             for (spaces = 1; spaces <= tree_Size; spaces++)
                printf(" ");
             printf("%c", tree_Characters[1]); // **
          }

          //else if handling base

          else if (i == tree_Size) {
             printf("\n");

             for (x = 0; x < tree_Size; x++) { // **
                printf("%c", tree_Characters[2]); // **
             }
             printf("%c", tree_Characters[3]); // **
              for (x = 0; x < tree_Size; x++) { // **
                printf("%c", tree_Characters[2]); // **
             }
          }
       }

    printf("\n");

    getch();
    }

答案 1 :(得分:0)

你需要为一个五元素数组tree_Characters建一个数组(char tree_Characters[5],它将保存你的四个树字符和一个nul终结符),然后改变第一个scanf。

scanf("%4s%*[^\n]",tree_Characters);
//      vv\_____/  ^-- array will decay to char*
//      ||   \-- discard the rest of the line
//      ||         ("*" indicates discard, "[^\n]" matches everything but '\n')
//      |\-- string (non-whitespace characters only)
//      \-- at most four characters scanned

有关详细信息,请参阅http://linux.die.net/man/3/scanf

编辑:您当然必须更改程序的其余部分,以便为每个树元素选择适当的字符。 (见@eznme's answer。)

编辑:无阵列版本:

char tree_leaf,tree_stem,tree_ground,tree_root;
scanf("%c%c%c%c%*[^\n]",&tree_leaf,&tree_stem,&tree_ground,&tree_root);

这会对太短的输入线做出反应。