2012年計算機三級數據庫考試經典試題7

字號:

1.已知在文件IN.dat中存有100個產品銷售記錄,每個產品銷售記錄由產品代碼code(字符型4位)、產品名稱name(字符型10位)、單價uprice(整型)、數量amount(整型)、金額sum(長整型)5部分組成。其中:金額=單價×數量。函數RData()讀取這100個銷售記錄并存入結構數組sell中。請編寫函數SortDat(),其功能要求:按金額從大到小進行排列,若金額相同,則按產品代碼從大到小進行排列,最終排列結果仍存入結構數組sell中。最后調用函數WData(),把結果輸出到OUT.dat文件中。
    【答案】
    void SortDat()
    { int i, j;
    PRO temp;
    for (i=0; i《99; i++)
    for (j=i+1; j《100; j++)
    if (sell[i].sum 《 sell[j].sum)
    { temp = sell[i];
    sell[i] = sell[j];
    sell[j] = temp;
    }
    else if (sell[i].sum == sell[j].sum)
    { if (strcmp(sell[i].code, sell[j].code) 《 0)
    { temp = sell[i];
    sell[i] = sell[j];
    sell[j] = temp;
    }
    }
    }