2012年計算機三級數(shù)據(jù)庫考試經(jīng)典試題10

字號:

1.在文件IN.dat中有200組數(shù)據(jù),每組有3個數(shù),每個數(shù)均是三位數(shù)。函數(shù)Rdata()讀取這200組數(shù)據(jù)存放到結(jié)構(gòu)數(shù)組aa中,請編寫函數(shù)numSort(),其功能是:要求在200組數(shù)據(jù)中找出條件為每組中的第2個數(shù)大于第1個數(shù)加第3個數(shù)之和,其中滿足條件的組數(shù)作為函數(shù)numSort()的返回值,同時把滿足條件的數(shù)據(jù)存入結(jié)構(gòu)數(shù)組bb中,再對bb中的數(shù)據(jù)按照每組數(shù)據(jù)的第2個數(shù)加第3個數(shù)之和的大小進行降序排列(第2個數(shù)加第3個數(shù)的和均不相等),排序后的結(jié)果仍重新存入結(jié)構(gòu)數(shù)組bb中。最后調(diào)用函數(shù)Wdata(),把結(jié)果bb輸出到文件OUT.dat中。
    【答案】
    int numSort()
    { int i, cnt = 0, j;
    data ch;
    for (i=0; i《200; i++)
    if (aa[i].x2 》 aa[i].x1+aa[i].x3)
    { bb[cnt] = aa[i];
    cnt++;
    }
    for (i=0; i
    for (j=i+1; j
    if (bb[i].x2+bb[i].x3 《 bb[j].x2+bb[j].x3)
    { ch = bb[i];
    bb[i] = bb[j];
    bb[j] = ch;
    }
    return cnt;
    }