2016年計算機三級考試網(wǎng)絡技術精選試題及答案(3)

字號:


    1.下列程序的功能是:利用以下所示的簡單迭代方法求方程:
    cos (x) -x=0的一個實根。
    xn+1=cos(xn )
    迭代步驟如下:
    (1)取x1初值為0.0。
    (2)x0=x1,把x1的值賦給x0。
    (3)x1=cos(x0),求出一個新的x1。
    (4)若x0-x1的絕對值小于0.000001,執(zhí)行步驟(5),否則執(zhí)行步驟(2)。
    (5)所求x1就是方程cos(x)-x=0的一個實根,作為函數(shù)值返回。
    請編寫函數(shù)countValue ( )實現(xiàn)程序要求,最后調用函數(shù)writeDAT( )把結果輸出到文件out9.dat中。
    注意:部分源程序已給出。
    請勿改動主函數(shù)main()和寫函數(shù)writeDAT()的內(nèi)容。
    #include
    #include
    #include
    void writeDAT();
    float countValue( )
    {
    float x0,x1=0.0; /*定義兩個浮點型變量進行迭代*/
    while(1) /*無條件循環(huán)*/
    {
    x0=x1; /*將x1賦值給x0*/
    x1=cos(x0); /*求出新的x1*/
    if(fabs(x0-x1)<1e-6) break; /*若x0-x1的絕對值小于0.000001,則結束循環(huán)*/
    }
    return x1; /*返回 x1的值*/
    }
    void main( )
    {
    system("CLS");
    printf("實根=%f\n",countValue( ));
    printf("%f\n",cos(countValue( ))-countValue( ));
    writeDAT( );
    }
    void writeDAT( )
    {
    FILE *wf;
    wf=fopen("out9.dat","w");
    fprintf(wf,"%f\n",countValue( ));
    fclose(wf);
    }
    2.請編寫函數(shù)void countValue(int *a,int *n),它的功能是:求出1到1000之內(nèi)能被7或11整除但不能同時被7和11整除的所有整數(shù)并存放在數(shù)組a中,并通過n返回這些數(shù)的個數(shù)。
    注意:部分源程序已給出。
    請勿改動主函數(shù)main()和寫函數(shù)writeDAT()的內(nèi)容。
    #include
    #include
    void writeDAT();
    void countValue(int *a,int *n)
    {
    int i; /*定義循環(huán)控制變量*/
    *n=0 ; /*初始化計數(shù)器變量*/
    for(i=1;i<=1000;i++) /*在這個范圍內(nèi)尋找符合條件的數(shù)*/
    if((i%7==0 && i%11!=0)||(i%7!=0 && i%11==0))
    /*如果當前的數(shù)可以被7整除而不可以被11整除,或者可以被11整除而不可以被7整除*/
    {
    *a=i; /*保存符合條件的數(shù)*/
    *n=*n+1; /*統(tǒng)計個數(shù)*/
    a++;
    }
    }
    void main()
    {
    int aa[1000],n,k;
    system("CLS");
    countValue(aa,&n);
    for(k=0;k
    if((k+1) %10 ==0)
    {
    printf("%5d",aa[k]);
    printf("\n");
    }
    else printf("%5d",aa[k]);
    writeDAT();
    }
    void writeDAT()
    {
    int aa[1000],n,k;
    FILE *fp;
    fp=fopen("out10.dat","w");
    countValue(aa,&n);
    for(k=0;k
    if((k+1)%10==0)
    {
    fprintf(fp,"%5d",aa[k]);
    fprintf(fp,"\n");
    }
    else fprintf(fp,"%5d",aa[k]);
    fclose(fp);
    }
    3.已知在文件IN11.DAT中存有若干個(個數(shù)<200)4位數(shù)字的正整數(shù),函數(shù)ReadDat() 的功能是讀取這若干個正整數(shù)并存入數(shù)組xx中。請編制函數(shù)CalValue(),其功能要求:(1)求出該文件中共有多少個正整數(shù)totNum;(2)求這些數(shù)右移1位后,產(chǎn)生的新數(shù)是偶數(shù)的數(shù)的個數(shù)totCnt,以及滿足此條件的這些數(shù)(右移前的值)的算術平均值totPjz,最后調用函數(shù)WriteDat()把所求的結果輸出到文件OUT11.DAT中。
    注意:部分源程序已給出。
    請勿改動主函數(shù)main()、讀函數(shù)ReadDat()和寫函數(shù)WriteDat()的內(nèi)容。
    #include
    #include
    #define MAXNUM 200
    int xx[MAXNUM] ;
    int totNum = 0 ; /* 文件IN11.DAT中共有多少個正整數(shù) */
    int totCnt = 0 ; /* 符合條件的正整數(shù)的個數(shù) */
    double totPjz = 0.0 ; /* 平均值 */
    int ReadDat(void) ;
    void Writedat(void) ;
    void CalValue(void)
    {
    int i; /*定義循環(huán)控制變量*/
    int data; /*用于保存處理后產(chǎn)生的新數(shù)*/
    for(i=0;i<200;i++) /*逐個取數(shù)組xx中的數(shù)進行統(tǒng)計*/
    if(xx[i]>0) /*判斷是否正整數(shù)*/
    {
    totNum++; /*統(tǒng)計正整數(shù)的個數(shù)*/
    data=xx[i]>>1; /*將數(shù)右移一位*/
    if(data%2==0) /*如果產(chǎn)生的新數(shù)是偶數(shù)*/
    {
    totCnt++; /*統(tǒng)計這些數(shù)的個數(shù)*/
    totPjz+=xx[i]; /*并將滿足條件的原數(shù)求和*/
    }
    }
    totPjz/=totCnt; /*求滿足條件的這些數(shù)(右移前的值)的算術平均值*/
    }
    void main()
    {
    int i ;
    system("CLS");
    for(i = 0 ; i < MAXNUM ; i++)
    xx[i] = 0 ;
    if (ReadDat ())
    {
    printf("數(shù)據(jù)文件IN11.DAT不能打開!\007\n");
    return ;
    }
    CalValue() ;
    printf("文件IN11.DAT中共有正整數(shù)= %d 個\n", totNum);
    printf("符合條件的正整數(shù)的個數(shù)= %d 個\n", totCnt);
    printf("平均值=%.2lf\n", totPjz);
    Writedat() ;
    }
    int ReadDat(void)
    {
    FILE *fp;
    int i = 0 ;
    if((fp = fopen ("IN11.DAT", "r")) == NULL)
    return 1 ;
    while(! feof(fp))
    {
    fscanf(fp, "%d,", &xx[i++]) ;
    }
    fclose(fp) ;
    return 0 ;
    }
    void Writedat(void)
    {
    FILE *fp;
    fp = fopen("OUT11.DAT", "w") ;
    fprintf(fp, "%d\n%d\n%.2lf\n", totNum, totCnt, totPjz) ;
    fclose(fp) ;
    }
    4.已知數(shù)據(jù)文件IN12.DAT中存有300個4位數(shù),并已調用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中。請編制函數(shù)jsValue(),其功能是:求出千位數(shù)上的數(shù)加個位數(shù)上的數(shù)等于百位數(shù)上的數(shù)加十位數(shù)上的數(shù)的個數(shù)cnt,再把所有滿足此條件的4位數(shù)依次存入數(shù)組b中,然后對數(shù)組b的4位數(shù)按從小到大的順序進行排序,最后調用寫函數(shù)writeDat()把數(shù)組b中的數(shù)輸出到OUT12.DAT文件中。
    例如:6712,6+2=7+1,則該數(shù)滿足條件,存入數(shù)組b中,且個數(shù)cnt=cnt+1。
    8129,8+9≠1+2,則該數(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();
    void jsValue()
    {
    int i,j; /*定義循環(huán)控制變量*/
    int a1,a2,a3,a4; /*定義變量保存4位數(shù)的每位數(shù)字*/
    int temp; /*定義數(shù)據(jù)交換時的暫存變量*/
    for(i=0;i<300;i++) /*逐個取每一個4位數(shù)*/
    {
    a4=a[i]/1000; /*求4位數(shù)的千位數(shù)字*/
    a3=a[i]%1000/100; /*求4位數(shù)的百位數(shù)字*/
    a2=a[i]%100/10; /*求4位數(shù)的十位數(shù)字*/
    a1=a[i]%10; /*求4位數(shù)的個位數(shù)字*/
    if(a4+a1==a3+a2) /*如果千位數(shù)加個位數(shù)等于百位數(shù)加十位數(shù)*/
    {
    b[cnt]=a[i]; /*將滿足條件的數(shù)存入數(shù)組b中*/
    cnt++; /*統(tǒng)計滿足條件的數(shù)的個數(shù)cnt*/
    }
    }
    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("IN12.DAT", "r");
    for(i=0; i<300; i++)
    fscanf(fp, "%d,", &a[i]);
    fclose(fp);
    }
    void writeDat()
    {
    FILE *fp;
    int i;
    fp = fopen("OUT12.DAT", "w");
    fprintf (fp, "%d\n",cnt);
    for(i=0; i
    fprintf(fp, "%d,\n", b[i]);
    fclose(fp);
    }