歡迎光臨
我們一直在努力

大學課後作業C語言程式設計之火車票管理系統

大學計算機軟件技術基礎(VC)課程設計任務書 一、題目:火車票資訊管理系統;二、目的與要求 1. 目的培養學生綜合利用C++語言進行程式設計的能力,培養學生的程式設計能力、用計算機解決實際問題的能力,加強對理論知識的理解和實際運用;培養學生對軟體工程知識和方法的初步認識,提高軟件系統分析能力和程式文件建立、歸納總結的能力;提高學生的實際動手能力和獨立思考的能力。

最後的版本,不想更新了。更新也沒有什麼功能自己可以弄了。該版本修改了前幾個版本中錯誤的地方,相對完整了許多,功能也加強了。

/*----------該程式主要實現火車票資訊的管理----------*/
#include 
#include 
/*----------定義車次、出發站、終點站、發車時間、到達時間、票價陣列----------*/
char    trains[10][10], start_station[10][10], end_station[10][10], start_time[10][10], end_time[10][10];
int price[10], i = 0, j = 0, k = 0, l = 0, x = 0; /* ----------定義全域性變數---------- */
void set( void );
void search( void );
void sum( void );
void sort( void );
void edit( void );
void del( void );
int main( void )
{
    int o;  /* ----------o在此處為選項數值---------- */
/*----------以下程式碼為主選單選項----------*/
    for ( o = 1; o != 0; o )
    {
        printf( "n------------------------------------------------n" );
        printf( "----------歡迎使用火車票管理系統主選單----------" );
        printf( "n------------------------------------------------n" );
        printf( "          1.set                 (錄入)n" );
        printf( "          2.search              (查詢)n" );
        printf( "          3.sum                 (統計)n" );
        printf( "          4.sort                (排序)n" );
        printf( "          5.edit                (編輯)n" );
        printf( "          6.del                 (刪除)n" );
        printf( "          0.exit                (退出)n" );
        printf( "n請輸入要選擇的服務專案:" );
        scanf( "%d", &o );
        if ( o == 1 )
            set();
        else if ( o == 2 )
            search();
        else if ( o == 3 )
            sum();
        else if ( o == 4 )
            sort();
        else if ( o == 5 )
            edit();
        else if ( o == 6 )
            del();
        else if ( o == 0 )
            break;
        else
            printf( "n請輸入正確的數字!nn" );
    }
}
/*----------錄入子函式---------*/
void set( void )
{
    printf( "n請輸入準備錄入的資訊數:" );
    scanf( "%d", &i );
    if ( i <= 0 || i > 10 )
    {
        printf( "n----------請正確輸入1-10之間的整數!----------n" );
        return;
    }
    i = i + x;
    for ( j = x; j < i; j++ )
    {
        ++x; /* ----------記錄錄入的資訊總條數---------- */
        printf( "n請輸入車次:" );
        scanf( "%s", &trains[j] );
        printf( "n請輸入出發站:" );
        scanf( "%s", &start_station[j] );
        printf( "n請輸入終點站:" );
        scanf( "%s", &end_station[j] );
        printf( "n請輸入發車時間:" );
        scanf( "%s", &start_time[j] );
        printf( "n請輸入到達時間:" );
        scanf( "%s", &end_time[j] );
        printf( "n請輸入票價:" );
        scanf( "%d", &price[j] );
    }
    printf( "n----------資訊錄入完畢----------nn" );
}
/*----------查詢子函式----------*/
void search( void )
{
    char    search[10];
    int search1;
    k = 0;
    if ( x == 0 )
    {
        printf( "n----------暫無資料可查詢!----------n" );
        return;
    }
    printf( "n--------------------------------n" );
    printf( "----------查詢功能選單----------" );
    printf( "n--------------------------------n" );
    printf( "          1.車次n" );
    printf( "          2.出發站n" );
    printf( "          3.終點站n" );
    printf( "          4.發車時間n" );
    printf( "          5.到達時間n" );
    printf( "          6.票價n" );
    printf( "n請輸入要查詢方式的編號:" );
    scanf( "%d", &l );
    if ( x == 0 )
    {
        printf( "n----------當前資料庫為空----------n" );
    }else if ( l == 1 )
    {
        printf( "n請輸入需要查詢的車次:" );
        scanf( "%s", search );
        for ( i = 0; i < x; i++ )
        {
            j = strcmp( search, trains[i] );
            if ( j == 0 )
            {
                ++k;
                if ( k == 1 )
                {
                    printf( "n-------------------------------------------------------------n" );
                    printf( "車次 出發站 終點站 發車時間 到達時間 票價" );
                    printf( "n-------------------------------------------------------------n" );
                }
                printf( "%-8s ", trains[i] );
                printf( "%-10s ", start_station[i] );
                printf( "%-10s ", end_station[i] );
                printf( "%-12s ", start_time[i] );
                printf( "%-12s ", end_time[i] );
                printf( "%-8d", price[i] );
                printf( "n" );
            }
        }
        printf( "n----------查詢結束,本次共查詢的%d個結果----------nn", k );
    }else if ( l == 2 )
    {
        printf( "n請輸入需要查詢的出發站:" );
        scanf( "%s", search );
        for ( i = 0; i < x; i++ )
        {
            j = strcmp( search, start_station[i] );
            if ( j == 0 )
            {
                ++k;
                if ( k == 1 )
                    printf( "n車次 出發站 終點站 發車時間 到達時間 票價n" );
                printf( "%-8s ", trains[i] );
                printf( "%-10s ", start_station[i] );
                printf( "%-10s ", end_station[i] );
                printf( "%-12s ", start_time[i] );
                printf( "%-12s ", end_time[i] );
                printf( "%-8d", price[i] );
                printf( "n" );
            }
        }
        printf( "n----------查詢結束,本次共查詢的%d個結果----------nn", k );
    }else if ( l == 3 )
    {
        printf( "n請輸入需要查詢的終點站:" );
        scanf( "%s", search );
        for ( i = 0; i < x; i++ )
        {
            j = strcmp( search, end_station[i] );
            if ( j == 0 )
            {
                ++k;
                if ( k == 1 )
                    printf( "n車次 出發站 終點站 發車時間 到達時間 票價n" );
                printf( "%-8s ", trains[i] );
                printf( "%-10s ", start_station[i] );
                printf( "%-10s ", end_station[i] );
                printf( "%-12s ", start_time[i] );
                printf( "%-12s ", end_time[i] );
                printf( "%-8d", price[i] );
                printf( "n" );
            }
        }
        printf( "n----------查詢結束,本次共查詢的%d個結果----------nn", k );
    }else if ( l == 4 )
    {
        printf( "n請輸入需要查詢的發車時間:" );
        scanf( "%s", search );
        for ( i = 0; i < x; i++ )
        {
            j = strcmp( search, start_time[i] );
            if ( j == 0 )
            {
                ++k;
                if ( k == 1 )
                    printf( "n-------------------------------------------------------------n" );
                printf( "車次 出發站 終點站 發車時間 到達時間 票價" );
                printf( "n-------------------------------------------------------------n" );
                printf( "%-8s ", trains[i] );
                printf( "%-10s ", start_station[i] );
                printf( "%-10s ", end_station[i] );
                printf( "%-12s ", start_time[i] );
                printf( "%-12s ", end_time[i] );
                printf( "%-8d", price[i] );
                printf( "n" );
            }
        }
        printf( "n----------查詢結束,本次共查詢的%d個結果----------nn", k );
    }else if ( l == 5 )
    {
        printf( "n請輸入需要查詢的到達時間:" );
        scanf( "%s", search );
        for ( i = 0; i < x; i++ )
        {
            j = strcmp( search, end_time[i] );
            if ( j == 0 )
            {
                ++k;
                if ( k == 1 )
                    printf( "n-------------------------------------------------------------n" );
                printf( "車次 出發站 終點站 發車時間 到達時間 票價" );
                printf( "n-------------------------------------------------------------n" );
                printf( "%-8s ", trains[i] );
                printf( "%-10s ", start_station[i] );
                printf( "%-10s ", end_station[i] );
                printf( "%-12s ", start_time[i] );
                printf( "%-12s ", end_time[i] );
                printf( "%-8d", price[i] );
                printf( "n" );
            }
        }
        printf( "n----------查詢結束,本次共查詢的%d個結果----------nn", k );
    }else if ( l == 6 )
    {
        printf( "n請輸入需要查詢的票價:" );
        scanf( "%d", &search1 );
        for ( i = 0; i < x; i++ )
        {
            j = search1 == price[i] ? 0 : 1;
            if ( j == 0 )
            {
                ++k;
                if ( k == 1 )
                    printf( "n-------------------------------------------------------------n" );
                printf( "車次 出發站 終點站 發車時間 到達時間 票價" );
                printf( "n-------------------------------------------------------------n" );
                printf( "%-8s ", trains[i] );
                printf( "%-10s ", start_station[i] );
                printf( "%-10s ", end_station[i] );
                printf( "%-12s ", start_time[i] );
                printf( "%-12s ", end_time[i] );
                printf( "%-8d", price[i] );
                printf( "n" );
            }
        }
        printf( "n----------查詢結束,本次共查詢的%d個結果----------nn", k );
    }else
        printf( "n請正確輸入排序方式編號!n" );
}
/*----------列表子函式----------*/
void sum( void )
{
    if ( x != 0 )
    {
        printf( "n----------------------------------------------------------------------n" );
        printf( "編號 車次 出發站 終點站 發車時間 到達時間 票價" );
        printf( "n----------------------------------------------------------------------n" );
        for ( i = 0; i < x; i++ )
        {
            printf( "%-8d ", i + 1 );
            printf( "%-8s ", trains[i] );
            printf( "%-10s ", start_station[i] );
            printf( "%-10s ", end_station[i] );
            printf( "%-12s ", start_time[i] );
            printf( "%-12s ", end_time[i] );
            printf( "%-8d", price[i] );
            printf( "n" );
        }
    }
    printf( "n----------共有%d條記錄----------n", x );
}
/*----------排序子函式----------*/
void sort( void )
{
    char    sort[1][10];
    int sort1;
    if ( x == 0 )
    {
        printf( "n----------無資料可排序!----------n" );
        return;
    }
    printf( "n--------------------------------n" );
    printf( "----------排序功能選單----------" );
    printf( "n--------------------------------n" );
    printf( "          1.車次n" );
    printf( "          2.出發站n" );
    printf( "          3.終點站n" );
    printf( "          4.發車時間n" );
    printf( "          5.到達時間n" );
    printf( "          6.票價n" );
    printf( "n請輸入要排序的序號:" );
    scanf( "%d", &k );
/*----------以下為排序工作流程----------*/
    if ( k == 1 )
    {
        for ( i = 0; i < x; i++ )
            for ( j = i; j < x - 1; j++ )
            {
                l = strcmp( trains[i], trains[j + 1] );
                if ( l > 0 )
                {
                    strcpy( sort[0], trains[i] );
                    strcpy( trains[i], trains[j + 1] );
                    strcpy( trains[j + 1], sort[0] );
                    strcpy( sort[0], start_station[i] );
                    strcpy( start_station[i], start_station[j + 1] );
                    strcpy( start_station[j + 1], sort[0] );
                    strcpy( sort[0], end_station[i] );
                    strcpy( end_station[i], end_station[j + 1] );
                    strcpy( end_station[j + 1], sort[0] );
                    strcpy( sort[0], start_time[i] );
                    strcpy( start_time[i], start_time[j + 1] );
                    strcpy( start_time[j + 1], sort[0] );
                    strcpy( sort[0], end_time[i] );
                    strcpy( end_time[i], end_time[j + 1] );
                    strcpy( end_time[j + 1], sort[0] );
                    sort1       = price[i];
                    price[i]    = price[j + 1];
                    price[j + 1]    = sort1;
                }
            }
    }else if ( k == 2 )
    {
        for ( i = 0; i < x; i++ )
            for ( j = i; j < x - 1; j++ )
            {
                l = strcmp( start_station[i], start_station[j + 1] );
                if ( l > 0 )
                {
                    strcpy( sort[0], trains[i] );
                    strcpy( trains[i], trains[j + 1] );
                    strcpy( trains[j + 1], sort[0] );
                    strcpy( sort[0], start_station[i] );
                    strcpy( start_station[i], start_station[j + 1] );
                    strcpy( start_station[j + 1], sort[0] );
                    strcpy( sort[0], end_station[i] );
                    strcpy( end_station[i], end_station[j + 1] );
                    strcpy( end_station[j + 1], sort[0] );
                    strcpy( sort[0], start_time[i] );
                    strcpy( start_time[i], start_time[j + 1] );
                    strcpy( start_time[j + 1], sort[0] );
                    strcpy( sort[0], end_time[i] );
                    strcpy( end_time[i], end_time[j + 1] );
                    strcpy( end_time[j + 1], sort[0] );
                    sort1       = price[i];
                    price[i]    = price[j + 1];
                    price[j + 1]    = sort1;
                }
            }
    }else if ( k == 3 )
    {
        for ( i = 0; i < x; i++ )
            for ( j = i; j < x - 1; j++ )
            {
                l = strcmp( end_station[i], end_station[j + 1] );
                if ( l > 0 )
                {
                    strcpy( sort[0], trains[i] );
                    strcpy( trains[i], trains[j + 1] );
                    strcpy( trains[j + 1], sort[0] );
                    strcpy( sort[0], start_station[i] );
                    strcpy( start_station[i], start_station[j + 1] );
                    strcpy( start_station[j + 1], sort[0] );
                    strcpy( sort[0], end_station[i] );
                    strcpy( end_station[i], end_station[j + 1] );
                    strcpy( end_station[j + 1], sort[0] );
                    strcpy( sort[0], start_time[i] );
                    strcpy( start_time[i], start_time[j + 1] );
                    strcpy( start_time[j + 1], sort[0] );
                    strcpy( sort[0], end_time[i] );
                    strcpy( end_time[i], end_time[j + 1] );
                    strcpy( end_time[j + 1], sort[0] );
                    sort1       = price[i];
                    price[i]    = price[j + 1];
                    price[j + 1]    = sort1;
                }
            }
    }else if ( k == 4 )
    {
        for ( i = 0; i < x; i++ )
            for ( j = i; j < x - 1; j++ )
            {
                l = strcmp( start_time[i], start_time[j + 1] );
                if ( l > 0 )
                {
                    strcpy( sort[0], trains[i] );
                    strcpy( trains[i], trains[j + 1] );
                    strcpy( trains[j + 1], sort[0] );
                    strcpy( sort[0], start_station[i] );
                    strcpy( start_station[i], start_station[j + 1] );
                    strcpy( start_station[j + 1], sort[0] );
                    strcpy( sort[0], end_station[i] );
                    strcpy( end_station[i], end_station[j + 1] );
                    strcpy( end_station[j + 1], sort[0] );
                    strcpy( sort[0], start_time[i] );
                    strcpy( start_time[i], start_time[j + 1] );
                    strcpy( start_time[j + 1], sort[0] );
                    strcpy( sort[0], end_time[i] );
                    strcpy( end_time[i], end_time[j + 1] );
                    strcpy( end_time[j + 1], sort[0] );
                    sort1       = price[i];
                    price[i]    = price[j + 1];
                    price[j + 1]    = sort1;
                }
            }
    }else if ( k == 5 )
    {
        for ( i = 0; i < x; i++ )
            for ( j = i; j < x - 1; j++ )
            {
                l = strcmp( end_time[i], end_time[j + 1] );
                if ( l > 0 )
                {
                    strcpy( sort[0], trains[i] );
                    strcpy( trains[i], trains[j + 1] );
                    strcpy( trains[j + 1], sort[0] );
                    strcpy( sort[0], start_station[i] );
                    strcpy( start_station[i], start_station[j + 1] );
                    strcpy( start_station[j + 1], sort[0] );
                    strcpy( sort[0], end_station[i] );
                    strcpy( end_station[i], end_station[j + 1] );
                    strcpy( end_station[j + 1], sort[0] );
                    strcpy( sort[0], start_time[i] );
                    strcpy( start_time[i], start_time[j + 1] );
                    strcpy( start_time[j + 1], sort[0] );
                    strcpy( sort[0], end_time[i] );
                    strcpy( end_time[i], end_time[j + 1] );
                    strcpy( end_time[j + 1], sort[0] );
                    sort1       = price[i];
                    price[i]    = price[j + 1];
                    price[j + 1]    = sort1;
                }
            }
    }else if ( k == 6 )
    {
        for ( i = 0; i < x; i++ )
            for ( j = i; j < x - 1; j++ )
            {
                l = price[i] > price[j + 1] ? 1 : 0;
                if ( l > 0 )
                {
                    strcpy( sort[0], trains[i] );
                    strcpy( trains[i], trains[j + 1] );
                    strcpy( trains[j + 1], sort[0] );
                    strcpy( sort[0], start_station[i] );
                    strcpy( start_station[i], start_station[j + 1] );
                    strcpy( start_station[j + 1], sort[0] );
                    strcpy( sort[0], end_station[i] );
                    strcpy( end_station[i], end_station[j + 1] );
                    strcpy( end_station[j + 1], sort[0] );
                    strcpy( sort[0], start_time[i] );
                    strcpy( start_time[i], start_time[j + 1] );
                    strcpy( start_time[j + 1], sort[0] );
                    strcpy( sort[0], end_time[i] );
                    strcpy( end_time[i], end_time[j + 1] );
                    strcpy( end_time[j + 1], sort[0] );
                    sort1       = price[i];
                    price[i]    = price[j + 1];
                    price[j + 1]    = sort1;
                }
            }
    }else
        printf( "n----------你的輸入錯誤!----------nn" );
/*----------排序工作流程結束----------*/
    printf( "n-------------------------------------------------------------n" );
    printf( "車次 出發站 終點站 發車時間 到達時間 票價" );
    printf( "n-------------------------------------------------------------n" );
    for ( i = 0; i < x; i++ )
    {
        printf( "%-8s ", trains[i] );
        printf( "%-10s ", start_station[i] );
        printf( "%-10s ", end_station[i] );
        printf( "%-12s ", start_time[i] );
        printf( "%-12s ", end_time[i] );
        printf( "%-8d", price[i] );
        printf( "n" );
    }
}
/*----------編輯子函式----------*/
void edit( void )
{
    if ( x != 0 )
    {
        printf( "n----------------------------------------------------------------------n" );
        printf( "編號 車次 出發站 終點站 發車時間 到達時間 票價" );
        printf( "n----------------------------------------------------------------------n" );
        for ( i = 0; i < x; i++ )
        {
            printf( "%-8d ", i + 1 );
            printf( "%-8s ", trains[i] );
            printf( "%-10s ", start_station[i] );
            printf( "%-10s ", end_station[i] );
            printf( "%-12s ", start_time[i] );
            printf( "%-12s ", end_time[i] );
            printf( "%-8d", price[i] );
            printf( "n" );
        }
        printf( "n----------------------------------n" );
        printf( "----------編輯功能主選單----------" );
        printf( "n----------------------------------n" );
    }
    if ( x == 0 )
        printf( "n----------沒有資料可編輯!----------n" );
    else{
        printf( "n請輸入要修改資訊的編號:" );
        scanf( "%d", &j );
        if ( j >= 1 && j <= x )
        {
            printf( "n--------------" );
            printf( "n  1.車次      " );
            printf( "n  2.出發站    " );
            printf( "n  3.終點站    " );
            printf( "n  4.發車時間  " );
            printf( "n  5.到達時間  " );
            printf( "n  6.票價      " );
            printf( "n--------------" );
            printf( "nn請輸入要修改的專案:" );
            scanf( "%d", &k );
            if ( k == 1 || k == 2 || k == 3 || k == 4 || k == 5 || k == 6 )
            {
                printf( "n請輸入你要修改的資料:" );
                if ( k == 1 )
                {
                    scanf( "%s", &trains[j - 1] );
                    printf( "n----------編輯操作成功----------n" );
                }else if ( k == 2 )
                {
                    scanf( "%s", &start_station[j - 1] );
                    printf( "n----------編輯操作成功----------n" );
                }else if ( k == 3 )
                {
                    scanf( "%s", &end_station[j - 1] );
                    printf( "n----------編輯操作成功----------n" );
                }else if ( k == 4 )
                {
                    scanf( "%s", &start_time[j - 1] );
                    printf( "n----------編輯操作成功----------n" );
                }else if ( k == 5 )
                {
                    scanf( "%s", &end_time[j - 1] );
                    printf( "n----------編輯操作成功----------n" );
                }else  {
                    scanf( "%d", &price[j - 1] );
                    printf( "n----------編輯操作成功----------n" );
                }
            }else
                printf( "n----------請輸入正確的專案!----------n" );
        }else
            printf( "n----------請輸入正確的編號!----------n" );
    }
}
/*----------刪除子函式----------*/
void del( void )
{
    if ( x != 0 )
    {
        printf( "n----------------------------------------------------------------------n" );
        printf( "編號 車次 出發站 終點站 發車時間 到達時間 票價" );
        printf( "n----------------------------------------------------------------------n" );
        for ( i = 0; i < x; i++ )
        {
            printf( "%-8d ", i + 1 );
            printf( "%-8s ", trains[i] );
            printf( "%-10s ", start_station[i] );
            printf( "%-10s ", end_station[i] );
            printf( "%-12s ", start_time[i] );
            printf( "%-12s ", end_time[i] );
            printf( "%-8d", price[i] );
            printf( "n" );
        }
        printf( "n----------------------------------n" );
        printf( "----------刪除功能主選單----------" );
        printf( "n----------------------------------n" );
        printf( "n請輸入以上要刪除資訊的編號:" );
        scanf( "%d", &j );
    }
    if ( x == 0 )
        printf( "n----------沒有資料可刪除!----------n" );
    else if ( j < x && j > 0 )
    {
        for (; j < x + 1; j++ )
        {
            strcpy( trains[j - 1], trains[j] );
            strcpy( start_station[j - 1], start_station[j] );
            strcpy( end_station[j - 1], end_station[j] );
            strcpy( start_time[j - 1], start_time[j] );
            strcpy( end_time[j - 1], end_time[j] );
            price[j - 1] = price[j];
        }
        x = x - 1;
        printf( "n----------刪除操作成功!----------n" );
    }else if ( j == x )
    {
        x = x - 1;
        printf( "n----------刪除操作成功!----------n" );
    }else
        printf( "n----------請輸入正確的編號!----------n" );
}

效果圖:

呵呵噠~~~

未經允許不得轉載:頭條楓林網 » 大學課後作業C語言程式設計之火車票管理系統