2016計算機三級考試網(wǎng)絡技術(shù)精選練習題

字號:

1.已知數(shù)據(jù)文件IN1.DAT中存有200個4位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請編制一函數(shù)jsVal( ),其功能是:如果4位數(shù)各位上的數(shù)字均是奇數(shù),則統(tǒng)計出滿足此條件的個數(shù)cnt,并把這些4位數(shù)按從大到小的順序存入數(shù)組b中。最后調(diào)用函數(shù)writeDat()把結(jié)果cnt及數(shù)組b中符合條件的4位數(shù)輸出到OUT1.DAT文件。
    注意:部分源程序已給出。
    程序中已定義數(shù)組:a[200],b[200],已定義變量:cnt。
    請勿改動主函數(shù)main()、讀函數(shù)readDat()和寫函數(shù)writeDat()的內(nèi)容。
    #include
    #define MAX 200
    int a[MAX],b[MAX],cnt=0;
    void writeDat();
    void jsVal()
    {
    int i,j; /*定義循環(huán)控制變量*/
    int a1,a2,a3,a4; /*定義變量保存4位數(shù)的每位數(shù)字*/
    int temp; /*定義數(shù)據(jù)交換時的暫存變量*/
    for(i=0;i<200;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%2!=0 && a3%2!=0 && a2%2!=0 && a1%2!=0)
    /*如果4位數(shù)各位上的數(shù)字均是奇數(shù)*/
    {
    b[cnt]=a[i]; /*將滿足條件的數(shù)存入數(shù)組b中*/
    cnt++; /*統(tǒng)計滿足條件的數(shù)的個數(shù)*/
    }
    }
    for(i=0;i
    for(j=i+1;j
    if(b[i]
    {
    temp=b[i];
    b[i]=b[j];
    b[j]=temp;
    }
    }
    void readDat()
    {
    int i;
    FILE *fp;
    fp=fopen("IN1.DAT","r");
    for(i=0;i
    fscanf(fp,"%d",&a[i]);
    fclose(fp);
    }
    void main()
    {
    int i;
    readDat();
    jsVal();
    printf("滿足條件的數(shù)=%d\n",cnt);
    for(i=0;i
    printf("%d\n",b[i]);
    printf("\n");
    writeDat();
    }
    void writeDat()
    {
    FILE *fp;
    int i;
    fp=fopen("out1.dat","w");
    fprintf(fp,"%d\n",cnt);
    for(i=0;i
    fprintf(fp,"%d\n",b[i]);
    fclose(fp);
    }
    2.已知IN2.DAT中存有200個4位數(shù),并已調(diào)用讀函數(shù)readDat()把這些數(shù)存入數(shù)組a中,請編制一函數(shù) jsVal(),其功能是:依次從數(shù)組a中取出一個數(shù),如果該4位數(shù)連續(xù)大于該4位數(shù)以后的5個數(shù)且該數(shù)是奇數(shù),則統(tǒng)計出滿足此條件的數(shù)的個數(shù)cnt,并把這些4位數(shù)按從小到大的順序存入數(shù)組b中,最后調(diào)用寫函數(shù) writeDat() 把結(jié)果cnt及數(shù)組b中符合條件的4位數(shù)輸出到 OUT2.DAT文件中。
    注意:部分源程序已給出。
    程序中已定義數(shù)組:a[200],b[200],已定義變量:cnt。
    請勿改動主函數(shù)main()、讀函數(shù)readDat()和寫函數(shù)writeDat()的內(nèi)容。
    #include
    #define MAX 200
    int a[MAX], b[MAX], cnt = 0 ;
    void writeDat();
    void jsVal()
    {
    int i,j; /*定義循環(huán)控制變量*/
    int temp; /*定義數(shù)據(jù)交換是的暫存變量*/
    for(i=0;i
    if(a[i]%2!=0) /*如果當前數(shù)是奇數(shù)*/
    for(j=i+1;j<=i+5;j++) /*取該數(shù)后面的5個數(shù)進行比較*/
    {
    if(a[i]
    break; /*如果當前數(shù)不滿足比后面5個數(shù)都大的條件,則跳出循環(huán)*/
    else if(j==i+5) /*如果當前數(shù)比后面的5個數(shù)都大*/
    {
    b[cnt]=a[i]; /*將滿足條件的數(shù)存入數(shù)組b中*/
    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 readDat()
    {
    int i ;
    FILE *fp;
    fp = fopen("IN2.DAT", "r") ;
    for(i = 0 ; i < MAX ; i++) fscanf(fp, "%d", &a[i]) ;
    fclose(fp) ;
    }
    void main()
    {
    int i ;
    readDat() ;
    jsVal() ;
    printf("滿足條件的數(shù)=%d\n", cnt) ;
    for(i = 0 ; i < cnt ; i++) printf("%d ", b[i]) ;
    printf("\n") ;
    writeDat() ;
    }
    void writeDat()
    {
    FILE *fp;
    int i ;
    fp = fopen("OUT2.DAT", "w") ;
    fprintf(fp, "%d\n", cnt) ;
    for(i = 0 ; i < cnt ; i++) fprintf(fp, "%d\n", b[i]) ;
    fclose(fp) ;
    }
    3.已知在文件IN3.DAT中存有100個產(chǎn)品銷售記錄,每個產(chǎn)品銷售記錄由產(chǎn)品代碼dm(字符型4位)、產(chǎn)品名稱mc(字符型10位)、單價dj(整型)、數(shù)量sl(整型)、金額je(長整型)幾部分組成。其中:金額=單價×數(shù)量。函數(shù)ReadDat()的功能是讀取這100個銷售記錄并存入結(jié)構(gòu)數(shù)組sell中。請編制函數(shù)SortDat(),其功能要求:按產(chǎn)品名稱從小到大進行排列,若產(chǎn)品名稱相同,則按金額從小到大進行排列,最終排列結(jié)果仍存入結(jié)構(gòu)數(shù)組sell中,最后調(diào)用函數(shù)WriteDat()把結(jié)果輸出到文件OUT3.DAT中。
    注意:部分源程序已給出。
    請勿改動主函數(shù)main()、讀函數(shù)ReadDat()和寫函數(shù)WriteDat()的內(nèi)容。
    #include
    #include
    #include
    #include
    #define MAX 100
    typedef struct
    {
    char dm[5] ; /*產(chǎn)品代碼 */
    char mc[11] ;/* 產(chǎn)品名稱 */
    int dj ; /* 單價 */
    int sl ; /* 數(shù)量 */
    long je ; /* 金額*/
    } PRO ;
    PRO sell [MAX] ;
    void ReadDat() ;
    void WriteDat() ;
    void SortDat()
    {
    int i,j; /*定義循環(huán)控制變量*/
    PRO temp; /*定義數(shù)據(jù)交換時的暫存變量(這里是PRO類型的結(jié)構(gòu)體變量)*/
    for(i=0;i<99;i++) /*利用選擇法進行排序*/
    for(j=i+1;j<100;j++)
    if(strcmp(sell[i].mc,sell[j].mc)>0) /*按產(chǎn)品名稱從小到大進行排列*/
    {
    temp=sell[i];
    sell [i]=sell[j];
    sell[j]=temp;
    }
    else if(strcmp(sell[i].mc,sell[j].mc)==0) /*若產(chǎn)品名稱相同*/
    if(sell[i].je>sell[j].je) /*則按金額從小到大進行排列*/
    {
    temp=sell[i];
    sell[i]=sell[j];
    sell[j]=temp;
    }
    }
    void main()
    {
    memset(sell, 0, sizeof(sell)) ;
    ReadDat() ;
    SortDat() ;
    WriteDat() ;
    }
    void ReadDat()
    {
    FILE *fp ;
    char str[80], ch[11] ;
    int i ;
    fp = fopen("IN3.DAT", "r") ;
    for (i = 0 ; i < 100 ; i++)
    {
    fgets(str, 80, fp) ;
    memcpy(sell[i].dm, str, 4) ;
    memcpy(sell[i].mc, str + 4, 10) ;
    memcpy(ch, str + 14, 4) ; ch[4] = 0 ;
    sell[i].dj = atoi(ch) ;
    memcpy(ch, str +18, 5) ; ch[5] = 0 ;
    sell[i].sl = atoi(ch) ;
    sell[i].je = (long)sell[i].dj * sell[i].sl;
    }
    fclose(fp) ;
    }
    void WriteDat()  {
    FILE *fp;
    int i ;
    fp = fopen("OUT3.DAT", "w") ;
    for(i = 0 ; i < 100 ; i++)
    {
    fprintf(fp, "%s %s %4d %5d %10ld\n", sell[i].dm, sell[i].mc, sell[i].dj,sell[i].sl, sell[i].je) ;
    }
    fclose(fp) ;
    }
    4.函數(shù)ReadDat()的功能是實現(xiàn)從文件ENG4.IN中讀取一篇英文文章,存入到字符串數(shù)組xx中。請編制函數(shù)encryptChar(),按給定的替代關系對數(shù)組xx中的所有字符進行替代,結(jié)果仍存入數(shù)組xx對應的位置上,最后調(diào)用函數(shù)WriteDat()把結(jié)果xx輸出到文件PS4.DAT中。
    替代關系:f(p)=p*11 mod 256(p是數(shù)組xx中某一個字符的ASCII值,f(p)是計算后新字符的ASCII值),如果計算后f(p)的值小于等于32或大于130,則該字符不變,否則將f(p)所對應的字符進行替代。
    注意:部分源程序已給出。
    原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符。
    請勿改動主函數(shù)main()、讀函數(shù)ReadDat()和寫函數(shù)WriteDat()的內(nèi)容。
    #include
    #include
    #include
    #include
    unsigned char xx[50][80] ;
    int maxline = 0 ;/* 文章的總行數(shù) */
    int ReadDat(void) ;
    void WriteDat(void) ;
    void encryptChar()
    {
    int i,j; /*定義循環(huán)控制變量*/
    int str; /*存儲字符串的長度*/
    char ch; /*存儲當前取得的字符*/
    for(i=0;i
    {
    str=strlen(xx[i]); /*求得當前行的字符串長度*/
    for(j=0;j
    {
    ch=xx[i][j]*11%256;
    if(ch<=32 || ch>130)
    continue; /*如果計算后的值小于等于32或大于130,則該字符不變*/
    else
    xx[i][j]=ch; /*否則將所對應的字符進行替代*/
    }
    }
    }
    void main()
    {
    system("CLS");
    if(ReadDat())
    {
    printf("數(shù)據(jù)文件ENG4.IN不能打開!\n\007") ;
    return ;
    }
    encryptChar() ;
    WriteDat() ;
    }
    int ReadDat(void)
    {
    FILE *fp;
    int i = 0 ;
    unsigned char *p ;
    if((fp = fopen("ENG4.IN", "r")) ==NULL) return 1 ;
    while(fgets(xx[i], 80, fp) !=NULL)
    {
    p = strchr(xx[i], '\n') ;
    if(p) *p = 0;
    i++ ;
    }
    maxline = i ;
    fclose(fp) ;
    return 0 ;
    }
    void WriteDat(void)
    {
    FILE *fp ;
    int i ;
    fp = fopen("PS4.DAT", "w") ;
    for(i = 0 ; i < maxline ; i++)
    {
    printf("%s\n", xx[i]) ;
    fprintf(fp, "%s\n", xx[i]) ;
    }
    fclose(fp) ;
    }