Bug 追踪系统解析:从概念到 C 语言实现

什么是 Bug 追踪系统?

Bug 追踪系统是一种软件工具,用于记录和管理我们在软件开发或项目过程中遇到的各类 Bug。

Bug 追踪系统主要包含以下三个核心功能:

  • 创建一个新的文本文件,并将用户输入的详细信息写入该文件。
  • 提供修改 Bug 状态的选项。
  • 生成特定 Bug 文件的报告。

接下来,让我们深入探讨一下其中涉及的具体函数。

驱动函数 (Driver Function)

这个函数的核心思路是维护一个变量 id,用来存储当前已注册的 Bug 的 ID。系统主要提供了三个选项供用户选择功能:

  • 创建新 Bug (Create New Bug)
  • 修改 Bug 状态 (Change Status of Bug)
  • 报告 Bug (Report a Bug)
  • 退出 (Exit)

我们使用 Switch 语句 来根据用户的选择切换到相应的功能模块。

创建新 Bug

这个函数会要求用户输入名字,并创建一个以此名字命名、且附加了 ID 号码的新文本文件。

例如:

如果用户是第一次创建 Bug 文件,初始 ID 为 0,自增后变为 1。假设用户输入的名字为 bugfile,那么我们的程序创建的文件将被命名为 bugfile1.txt*。
如果用户是第三次创建 Bug 文件,ID 自增三次变为 3。假设用户再次输入名字 bugfile,那么程序创建的文件将被命名为 bugfile3.txt*。

在命名文件之后,程序会获取用户输入的信息,并将其连同创建时间一起添加到文本文件中。

用户需要提供的 Bug 信息包括:

  • Bug 提交者
  • Bug 类型
  • Bug 优先级
  • Bug 描述
  • Bug 状态

修改 Bug 状态

接收关于 Bug 的信息,修改目标文件中 Bug 的状态。同时,更新 Bug 的“最后更新时间”。

报告 Bug

获取关于 Bug 的信息(例如 Bug 文件名),并打印出该 Bug 的具体内容。

下面是 Bug 追踪系统的具体实现代码:

#### 驱动函数

// C program for the Driver Function
// of the Bug Tracking System

#include 

// Driver Code
void main()
{
    printf("***************");
    printf("BUG TRACKING SYSTEM");
    printf("***************
");

    int number, i = 1;

    // Id initialised to 0
    int id = 0;

    // while loop to run
    while (i != 0) {
        printf("
1. FILE A NEW BUG
");
        printf("2. CHANGE THE STATUS OF THE BUG
");
        printf("3. GET BUG REPORT
4. EXIT");
        printf("

 ENTER YOUR CHOICE:");

        scanf("%d", &number);

        // Using switch to go case by case
        switch (number) {
        case 1:
            id++;

            // Creating a New Bug
            filebug(id);
            break;
        case 2:

            // Change Status of Bug
            changestatus();
            break;
        case 3:

            // Report the Bug
            report();
            break;
        case 4:
            i = 0;
            break;
        default:
            printf("
invalid entry");
            break;
        }
    }
}

#### 创建 Bug

// C program for filing a bug
// in Bug Tracking System

#include 
#include 
#include 
#include 
#include 

// Function to file the Bug into
// the Bug Tracking System
void filebug(int id)
{
    printf("**********");
    printf("FILING A BUG");
    printf("***********
");

    // Current Time
    time_t CurrentTime;
    time(&CurrentTime);

    char name[20], bugtype[50];
    char bugdescription[1000];
    char bugpriority[30];
    int bugstatus;

    FILE* ptr;

    // User name
    printf("Enter your name:
");
    scanf("%s", name);
    char ids[10];
    itoa(id, ids, 10);
    strcat(name, ids);
    char ex[] = ".txt";
    strcat(name, ex);

    // Filename of the Bug
    printf("Filename :%s
", name);
    ptr = fopen(name, "w");

    // Case when file cannot be created
    if (ptr == NULL)
        printf("cannot create file!!!
");

    fprintf(ptr, "DATE AND TIME : %s",
            ctime(&CurrentTime));

    // ID in the Text File
    fprintf(ptr, "BUG ID    :    %d
", id);

    // Adding New Line in Text File
    fprintf(ptr, "
");

    // Bug ID
    printf("BUG ID:%d
", id);

    fprintf(ptr, "BUG FILED BY: %s
",
            name);
    fprintf(ptr, "
");

    printf("Enter bug type:
");
    scanf(" %[^
]", bugtype);

    // Bug Type
    fprintf(ptr, "TYPE OF BUG: %s",
            bugtype);
    fprintf(ptr, "
");

    // Bug Priority
    printf("Enter bug priority:
");
    scanf(" %[^
]s", bugpriority);

    fprintf(ptr, "BUG PRIORITY: %s
",
            bugpriority);
    fprintf(ptr, "
");

    // Bug Description
    printf("Enter the bug description:
");
    scanf(" %[^
]s", bugdescription);

    fprintf(ptr, "BUG DESCRIPTION: %s
",
            bugdescription);
    fprintf(ptr, "
");

    printf("Status of bug:
");
    printf("1. NOT YET ASSIGNED
");
    printf("2. IN PROCESS
");
    printf("3. FIXED
");
    printf("4. DELIVERED
");
    printf("ENTER YOUR STATUS:
");

    // Taking the input of status
    scanf("%d", &bugstatus);

    // Writing the status
    fprintf(ptr, "BUG STATUS: ");

    // Switch for writing the appropriate status
    switch (bugstatus) {
    case 1:
        fprintf(ptr, "NOT YET ASSIGNED
");
        break;
    case 2:
        fprintf(ptr, "IN PROCESS
");
        break;
    case 3:
        fprintf(ptr, "FIXED
");
        break;
    case 4:
        fprintf(ptr, "DELIVERED
");
        break;
    default:
        fprintf(ptr, "INVALID STATUS
");
        break;
    }

    // Closing the file
    fclose(ptr);
    printf("BUG FILED SUCCESSFULLY...
");
}

#### 修改 Bug 状态

// C program for Change Status
// of a Bug

#include 
#include 
#include 
#include 
#include 

// Function to Change the Status
void changestatus()
{
    printf("************");
    printf("CHANGE STATUS");
    printf("************
");

    // Current Time
    time_t CurrentTime;
    time(&CurrentTime);

    char name[50], name1[50];
    char line[1000];
    int bugstatus;

    FILE *fp, *fp1;

    printf("ENTER THE BUG FILE NAME:");
    scanf("%s", name);

    // Opening the file in read mode
    fp = fopen(name, "r");

    // If file is not found
    if (fp == NULL) {
        printf("FILE NOT FOUND
");
        return;
    }

    printf("ENTER THE NEW STATUS:
");
    printf("1. NOT YET ASSIGNED
");
    printf("2. IN PROCESS
");
    printf("3. FIXED
");
    printf("4. DELIVERED
");
    printf("ENTER YOUR STATUS:
");

    // Taking the input of status
    scanf("%d", &bugstatus);

    // Creating a new file for writing
    strcpy(name1, name);
    strcat(name1, "1.txt");
    fp1 = fopen(name1, "w");

    // Updating the Last Update Time
    fprintf(fp1, "DATE AND TIME OF UPDATE: %s",
            ctime(&CurrentTime));

    // Copying the contents of the file to the new file
    while (fgets(line, sizeof(line), fp)) {
        if (strstr(line, "BUG STATUS")) {
            fprintf(fp1, "BUG STATUS: ");
            switch (bugstatus) {
            case 1:
                fprintf(fp1, "NOT YET ASSIGNED
");
                break;
            case 2:
                fprintf(fp1, "IN PROCESS
");
                break;
            case 3:
                fprintf(fp1, "FIXED
");
                break;
            case 4:
                fprintf(fp1, "DELIVERED
");
                break;
            default:
                fprintf(fp1, "INVALID STATUS
");
                break;
            }
        }
        else {
            fprintf(fp1, "%s", line);
        }
    }

    fclose(fp);
    fclose(fp1);

    // Removing the original file
    remove(name);

    // Renaming the new file with the original name
    rename(name1, name);

    printf("BUG STATUS UPDATED SUCCESSFULLY...
");
}

#### 报告 Bug

// C program for Reporting
// a Bug

#include 
#include 
#include 
#include 

// Function to report the bug
void report()
{
    printf("***********");
    printf("REPORT BUG");
    printf("***********
");

    char name[50];
    char line[1000];

    FILE *fp;

    printf("ENTER THE BUG FILE NAME:");
    scanf("%s", name);

    // Opening the file in read mode
    fp = fopen(name, "r");

    if (fp == NULL) {
        printf("FILE NOT FOUND
");
        return;
    }

    printf("
DETAILS OF THE BUG:
");

    // Printing the file contents
    while (fgets(line, sizeof(line), fp)) {
        printf("%s", line);
    }

    fclose(fp);
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/24868.html
点赞
0.00 平均评分 (0% 分数) - 0