C - 按输出顺序打印的问题

C - Problem with printing in output order

我在按我希望的顺序打印到控制台时遇到问题,我不知道为什么。代码的逻辑似乎很好,但它无法按照所需的顺序打印内容。输入取自逐行读取的文件,直到文件末尾。

输出:

Author: Brian Kernighan
Class: Student
Returns: the matric
Method: getMatric
Parameter: matric the matric number
Method: setMatric
Returns: the name
Method: getName
Parameter: name the name
Method: setName
Returns: the address
Method: getAddress
Parameter: address the address
Method: setAddress
Returns: a string representation of the Student
Method: toString
The total number of lines is 65
The total number of  non-blank lines is 56
The total number of comments is 8

这是所需的输出:

Class: Student
Author: Brian Kernighan 
Method: getMatric
Returns: the matric 
Method: setMatric
Parameter: matric the matric number 
Method: getName
Returns: the name 
Method: setName
Parameter: name the name 
Method: getAddress
Returns: the address 
Method: setAddress
Parameter: address the address 
Method: toString
Returns: a string representation of the Student 
Total number of lines: 65
Number of non-blank lines: 56
Number of Javadoc comments: 8

有谁知道为什么会这样?在这里我也附上我当前的代码:

#include <stdio.h>
#include <string.h>

int main(int argc, char** argv)
{
    char line[1000];
    char line_copy [1000];
    char delimeters[] = ".,; \t\n";
    int total_lines = 0;
    int total_comments = 0;
    int nonblank_lines = 0;
    char method [50];
    char class [50];
    char author_fullname [50];
    char parameters_full [50];
    char return_full[50];
    char* space = " ";

  FILE* input = fopen(argv[2], "r");
  FILE* output = fopen(argv[4], "w");

    while (fgets(line, 1000, input) != NULL) 
    {
        
        int print_author = 0;
        int print_param = 0;
        int print_return = 0;
        int print_method = 0;
        int print_class = 0;
        
        strcpy(line_copy,line);
        char* word = strtok(line, delimeters);
        total_lines++;

        if (word != NULL) 
        {
        nonblank_lines++;
        }

        if (word != NULL && strcmp(word, "/**") == 0) 
        {
            total_comments++;
            fprintf(output, "%s\n", line);
            int comment_finished = 0;
            while (comment_finished != 1) 
            {
                fgets(line, 1000, input);
                fprintf(output, "%s", line);
                total_lines++;
                word = strtok(line, delimeters);
                if (word != NULL) 
                {
                    nonblank_lines++;
                }
                
                while (word != NULL) 
                {
                    if (strcmp(word, "*/") == 0) 
                    {
                        comment_finished = 1;
                        break;
                    }
                    
                    
                    if (word != NULL && strcmp(word, "@author") == 0) 
                    {
                        char* author_name = strtok(NULL, delimeters);
                        print_author = 1;
                        author_fullname[0] = '[=12=]';
                        strcat(author_fullname, author_name);
                        strcat(author_fullname, space);
                        strcat(author_fullname, strtok(NULL, delimeters));
                        
                        printf("Author: %s\n", author_fullname);
                    }
                    
                    if (word != NULL && strcmp(word, "@param") == 0) 
                    {
                        char* parameters = strtok(NULL, delimeters);
                        parameters_full[0] = '[=12=]';
                        while (parameters != NULL) 
                        {
                            strcat(parameters_full, parameters);
                            strcat(parameters_full, space);
                            parameters = strtok(NULL, delimeters);
                        }
                        print_param = 1;
                    }
                    
                    if (word != NULL && strcmp(word, "@return") == 0) 
                    {
                        char* return_value = strtok(NULL, delimeters);
                        return_full[0] = '[=12=]';
                        while (return_value != NULL) 
                        {
                            strcat(return_full, return_value);
                            strcat(return_full, space);
                            return_value = strtok(NULL, delimeters);
                        }
                        print_return = 1;
                    }
                    word = strtok(NULL, delimeters);
                }
            }
        }
        while (word != NULL) 
        {
            if (word != NULL && strcmp(word, "public") == 0) 
            {
                char* jmp = strtok(NULL, delimeters);

                if (jmp != NULL && strcmp(jmp, "class") == 0) 
                {
                    char* class_name = strtok(NULL, delimeters);
                    
                    class[0] = '[=12=]';
                    strcat(class, class_name);
                    fprintf (output,"%s\n", line_copy);
                    print_class = 1;
                } 
                else 
                {
                    char* method_name = strtok(NULL, delimeters);
                    print_method = 1;
                    method[0] = '[=12=]';
                    strcat(method, method_name);
                    fprintf (output,"%s\n", line_copy);
                }
            }
            word = strtok(NULL, delimeters);
        }
        
        if(print_class == 1)
        {
            printf("Class: %s\n", class);
            class[0] = '[=12=]';
            
            if(print_author == 1)
            {
            printf("Author: %s\n", author_fullname);
            author_fullname[0] = '[=12=]';
            }
        }
            
        if(print_method == 1)
        {
            printf("Method: %s\n", method);
            method[0] = '[=12=]';   
        }
        
        if(print_param == 1)
        {
            printf("Parameter: %s\n", parameters_full);
            parameters_full[0] = '[=12=]';
            print_param = 0;
        }
        
        if(print_return == 1)
        {
            printf("Returns: %s\n", return_full);
            return_full[0] = '[=12=]';
            print_return = 0;
        }
        
    }

  printf("The total number of lines is %d\n", total_lines);
  printf("The total number of  non-blank lines is %d\n", nonblank_lines);
  printf("The total number of comments is %d\n", total_comments);

  fclose(input);
  fclose(output);
  return 0;
}

输入文件是这个:

/** 
 * @author Brian Kernighan
 */
public class Student {

    private int matric;
    private String name;
    private String address;

    /**
     * Query for the matric number of the student.
     * @return the matric
     */
    public int getMatric (  ) {
        return this.matric;
    }

    /**
     * Sets the matric number of the student.
     * @param matric the matric number
     */
    public void setMatric ( int matric ) {
        this.matric = matric;
    }

    /**
     * Query for the name of the student.
     * @return the name
     */
    public String getName ( ) {
        return this.name;
    }

    /**
     * Sets the name of the student.
     * @param name the name
     */
    public void setName ( String name ) {
        this.name = name;
    }

    /**
     * Query for the address of the student.
     * @return the address
     */
    public String getAddress ( ) {
        return this.address;
    }

    /**
     * Sets the address of the student.
     * @param address the address
     */
    public void setAddress ( String address ) {
        this.address = address;
    }

    /**
     * @return a string representation of the Student
     */
    public String toString ( ) {
        return this.matric + "," + this.name + "," + this.address;
    }

}

代替 while 循环局部的整数标志,变量的第一个字符可用于控制打印。
将参数的打印和 return 放在方法的打印中,因此方法先出现。

#include <stdio.h>
#include <string.h>

int main(int argc, char** argv)
{
    char line[1000] = "";
    char line_copy [1000] = "";
    char delimeters[] = ".,; \t\n";
    int total_lines = 0;
    int total_comments = 0;
    int nonblank_lines = 0;
    char method [50] = "";
    char class [50] = "";
    char author_fullname [50] = "";
    char parameters_full [50] = "";
    char return_full[50] = "";
    char* space = " ";

    FILE* input = fopen(argv[2], "r");
    FILE* output = fopen(argv[4], "w");

    while (fgets(line, 1000, input) != NULL)
    {

        strcpy(line_copy,line);
        char* word = strtok(line, delimeters);
        total_lines++;

        if (word != NULL)
        {
            nonblank_lines++;
        }

        if (word != NULL && strcmp(word, "/**") == 0)
        {
            total_comments++;
            fprintf(output, "%s\n", line);
            int comment_finished = 0;
            while (comment_finished != 1)
            {
                fgets(line, 1000, input);
                fprintf(output, "%s", line);
                total_lines++;
                word = strtok(line, delimeters);
                if (word != NULL)
                {
                    nonblank_lines++;
                }

                while (word != NULL)
                {
                    if (strcmp(word, "*/") == 0)
                    {
                        comment_finished = 1;
                        break;
                    }


                    if (word != NULL && strcmp(word, "@author") == 0)
                    {
                        char* author_name = strtok(NULL, delimeters);
                        author_fullname[0] = '[=10=]';
                        strcat(author_fullname, author_name);
                        strcat(author_fullname, space);
                        strcat(author_fullname, strtok(NULL, delimeters));

                        //printf("Author: %s\n", author_fullname);
                    }

                    if (word != NULL && strcmp(word, "@param") == 0)
                    {
                        char* parameters = strtok(NULL, delimeters);
                        parameters_full[0] = '[=10=]';
                        while (parameters != NULL)
                        {
                            strcat(parameters_full, parameters);
                            strcat(parameters_full, space);
                            parameters = strtok(NULL, delimeters);
                        }
                    }

                    if (word != NULL && strcmp(word, "@return") == 0)
                    {
                        char* return_value = strtok(NULL, delimeters);
                        return_full[0] = '[=10=]';
                        while (return_value != NULL)
                        {
                            strcat(return_full, return_value);
                            strcat(return_full, space);
                            return_value = strtok(NULL, delimeters);
                        }
                    }
                    word = strtok(NULL, delimeters);
                }
            }
        }
        while (word != NULL)
        {
            if (word != NULL && strcmp(word, "public") == 0)
            {
                char* jmp = strtok(NULL, delimeters);

                if (jmp != NULL && strcmp(jmp, "class") == 0)
                {
                    char* class_name = strtok(NULL, delimeters);

                    class[0] = '[=10=]';
                    strcat(class, class_name);
                    fprintf (output,"%s\n", line_copy);
                }
                else
                {
                    char* method_name = strtok(NULL, delimeters);
                    method[0] = '[=10=]';
                    strcat(method, method_name);
                    fprintf (output,"%s\n", line_copy);
                }
            }
            word = strtok(NULL, delimeters);
        }

        if ( class[0])
        {
            printf("Class: %s\n", class);
            class[0] = '[=10=]';

            if( author_fullname[0])
            {
                printf("Author: %s\n", author_fullname);
                author_fullname[0] = '[=10=]';
            }
        }

        if ( method[0])
        {
            printf("Method: %s\n", method);
            method[0] = '[=10=]';

            if ( parameters_full[0])
            {
                printf("Parameter: %s\n", parameters_full);
                parameters_full[0] = '[=10=]';
            }

            if ( return_full[0])
            {
                printf("Returns: %s\n", return_full);
                return_full[0] = '[=10=]';
            }
        }

    }

    printf("The total number of lines is %d\n", total_lines);
    printf("The total number of  non-blank lines is %d\n", nonblank_lines);
    printf("The total number of comments is %d\n", total_comments);

    fclose(input);
    fclose(output);
    return 0;
}