2016年計算機(jī)三級數(shù)據(jù)庫技術(shù)考試備考試題(7)

字號:


    1.已知數(shù)據(jù)文件IN39.DAT中存有300個4位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請編制一函數(shù)jsValue( ),其功能是:求出這些4位數(shù)是素數(shù)的個數(shù)cnt,再把所有滿足此條件的4位數(shù)依次存入數(shù)組b中,然后對數(shù)組b的4位數(shù)按從小到大的順序進(jìn)行排序,最后調(diào)用函數(shù)writeDat()把數(shù)組b中的數(shù)輸出到OUT39.DAT文件中。
    例如:5591是素數(shù),則該數(shù)滿足條件,存入數(shù)組b中,且個數(shù)cnt=cnt+1。
    9812是非素數(shù),則該數(shù)不滿足條件,忽略。
    注意:部分源程序已給出。
    程序中已定義數(shù)組:a[300],b[300],已定義變量:cnt。
    請勿改動主函數(shù)main()、讀函數(shù)readDat()和寫函數(shù)writeDat()的內(nèi)容。
    #include
    int a[300],b[300],cnt=0;
    void readDat();
    void writeDat();
    int isP(int m)
    {
    int i;
    for(i=2;i
    if(m%i==0) return 0;
    return 1;
    }
    void jsValue()
    {
    int i,j; /*定義循環(huán)控制變量*/
    int temp; /*定義數(shù)據(jù)交換是的暫存變量*/
    for(i=0;i<300;i++) /*逐個取4位數(shù)*/
    if(isP(a[i])) /*如果該數(shù)為素數(shù),則將該數(shù)存入數(shù)組b中*/
    {
    b[cnt]=a[i];
    cnt++; /*并統(tǒng)計滿足條件的數(shù)的個數(shù)*/
    }
    for(i=0;i
    for(j=i+1;j
    if(b[i]>b[j])
    {
    temp=b[i];
    b[i]=b[j];
    b[j]=temp;
    }
    }
    void main()
    {
    int i;
    readDat();
    jsValue();
    writeDat();
    printf("cnt=%d\n",cnt);
    for(i=0;i
    printf("b[%d]=%d\n",i,b[i]);
    }
    void readDat()
    {
    FILE *fp;
    int i;
    fp=fopen("IN39.DAT","r");
    for(i=0;i<300;i++)
    fscanf(fp,"%d,",&a[i]);
    fclose(fp);
    }
    void writeDat()
    {
    FILE *fp;
    int i;
    fp=fopen("OUT39.DAT","w");
    fprintf(fp,"%d\n",cnt);
    for(i=0;i
    fprintf(fp, "%d\n",b[i]);
    fclose(fp);
    }
    2.已知數(shù)據(jù)文件IN40.DAT中存有300個4位數(shù),并已調(diào)用函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請編制一函數(shù)jsValue(),其功能是:求出這些4位數(shù)是素數(shù)的個數(shù)cnt,再求出所有滿足此條件的4位數(shù)的平均值pjz1,以及所有不滿足此條件的4位數(shù)的平均值pjz2,最后調(diào)用函數(shù)writeDat()把結(jié)果cnt,pjz1,pjz2,輸出到out40.dat文件中。
    例如:5591是素數(shù),則該數(shù)滿足條件,計算平均值pjz1,且個數(shù)cnt=cnt+1。
    9812是非素數(shù),則該數(shù)不滿足條件,計算平均值pjz2。
    注意:部分源程序已給出。
    程序中已定義數(shù)組:a[300],b[300],已定義變量:cnt,pjz1,pjz2。
    請勿改動主函數(shù)main()、讀函數(shù)readDat()和寫函數(shù)writeDat()的內(nèi)容。
    #include
    int a[300], cnt=0;
    double pjz1=0.0,pjz2=0.0;
    void readDat();
    void writeDat();
    int isP(int m)
    {
    int i;
    for(i=2;i
    if(m%i==0) return 0;
    return 1;
    }
    void jsValue()
    {
    int i,n=0; /*定義循環(huán)控制變量和計數(shù)器變量*/
    for(i=0;i<300;i++) /*逐個取4位數(shù)*/
    if(isP(a[i])) /*如果該數(shù)為素數(shù)*/
    {
    pjz1+=a[i]; /*將滿足條件的數(shù)求和*/
    cnt++; /*統(tǒng)計滿足條件的數(shù)的個數(shù)*/
    }
    else
    {
    pjz2+=a[i]; /*將不滿足條件的數(shù)求和*/
    n++; /*統(tǒng)計不滿足條件的數(shù)的個數(shù)*/
    }
    pjz1/=cnt; /*求滿足條件的數(shù)的平均值*/
    pjz2/=n; /*求不滿足條件的數(shù)的平均值*/
    }
    void main()
    {
    readDat();
    jsValue();
    writeDat();
    printf("cnt=%d\n滿足條件的平均值pjz1=%7.2lf\n不滿足條件的平均值pjz2=%7.2lf\n",cnt,pjz1,pjz2);
    }
    void readDat()
    {
    FILE *fp;
    int i;
    fp=fopen( "in40.dat","r");
    for(i=0;i<300;i++)
    fscanf(fp,"%d,",&a[i]);
    fclose(fp);
    }
    void writeDat()
    {
    FILE *fp;
    fp=fopen("out40.dat","w");
    fprintf(fp,"%d\n%7.2lf\n%7.2lf\n",cnt ,pjz1,pjz2);
    fclose(fp);
    }