亚洲欧洲国产欧美一区精品,激情五月亚洲色五月,最新精品国偷自产在线婷婷,欧美婷婷丁香五月天社区

      自考

      各地資訊
      當(dāng)前位置:華課網(wǎng)校 >> 自考 >> 模擬試題 >> 工學(xué)類 >> C語言程序設(shè)計 >> 文章內(nèi)容

      排行熱點

      • 歷年真題
      • 模擬試題
      • 自考自答

      自學(xué)考試《C語言程序設(shè)計》練習(xí)題及答案_第4頁

      來源:華課網(wǎng)校  [2017年1月31日]  【

        【3.46】輸入N個整數(shù),儲存輸入的數(shù)及對應(yīng)的序號,并將輸入的數(shù)按從小到大的順序進行排列。要求:當(dāng)兩個整數(shù)相等時,整數(shù)的排列順序由輸入的先后次序決定。例如:輸入的第3個整數(shù)為5,第7個整數(shù)也為5,則將先輸入的整數(shù)5排在后輸入的整數(shù)5的前面。程序如下:

        #include "stdio.h"

        #define N 10

        struct

        { int no;

        int num;

        } array[N];

        main( )

        { int i,j,num;

        for( i=0;i

        { printf("enter No. %d:",i);

        scanf("%d",&num);

        for( ① ;j>=0&&array[j].num ② num; ③ )

        array[j+1]=array[j];

        array[ ④ ].num=num;

        array[ ⑤ ].no=i;

        }

        for( i=0;i

        printf("%d=%d,%d\n",i,array[i].num,array[i].no);

        }

        【3.47】以下程序的功能是:讀入一行字符(如:a、...y、z),按輸入時的逆序建立一個鏈接式的結(jié)點序列,即先輸入的位于鏈表尾(如下圖),然后再按輸入的相反順序輸出,并釋放全部結(jié)點。

        #include

        main( )

        { struct node

        { char info;

        struct node *link;

        } *top,*p;

        char c;

        top=NULL;

        while((c= getchar( )) ① )

        { p=(struct node *)malloc(sizeof(struct node));

        p->info=c;

        p->link=top;

        top=p;

        }

        while( top )

        { ② ;

        top=top->link;

        putchar(p->info);

        free(p);

        }

        }

        【3.48】下面函數(shù)將指針p2所指向的線性鏈表,串接到p1所指向的鏈表的末端。假定p1所指向的鏈表非空。

        #define NULL 0

        struct link

        { float a;

        struct link *next;

        };

        concatenate ( p1,p2 )

        struct list *p1,*p2;

        { if( p1->next==NULL )

        p1->next=p2;

        else

        concatenate( ① ,p2);

        }

        【3.49】下面程序的功能是從鍵盤輸入一個字符串,然后反序輸出輸入的字符串。

        #include

        struct node

        { char data;

        struct node *link;

        }*head;

        main()

        { char ch;

        struct node *p;

        head = NULL;

        while(( ch=getchar())!='\n' )

        { p = (struct node *)malloc(sizeof(struct node));

        p->data = ch;

        p->link = ① ;

        head = ② ;

        }

       、 ;

        while( p!=NULL )

        { printf("%c ", p->data);

        p = p->link;

        }

        }

        【3.50】下面程序的功能是從鍵盤上順序輸入整數(shù),直到輸入的整數(shù)小于0時才停止輸入。然后反序輸出這些整數(shù)。

        #include

        struct data

        { int x;

        struct data *link;

        }*p;

        input()

        { int num;

        struct data *q;

        printf("Enter data:");

        scanf("%d", &num);

        if( num<0 )

       、 ;

        q = ② ;

        q->x = num;

        q->link = p;

        p=q;

       、 ;

        }

        main()

        { printf("Enter data until data<0:\n");

        p=NULL;

        input();

        printf("Output:");

        while( ④ )

        { printf("%d\n", p->x);

       、 ;

        }

        }

        【3.51】下面函數(shù)的功能是創(chuàng)建一個帶有頭結(jié)點的鏈表,將頭結(jié)點返回給主調(diào)函數(shù)。鏈表用于儲存學(xué)生的學(xué)號和成績。新產(chǎn)生的結(jié)點總是位于鏈表的尾部。

        struct student

        { long num;

        int score;

        struct student *next;

        };

        struct student *creat()

        { struct student *head=NULL,*tail;

        long num; int a;

        tail= ① malloc(LEN);

        do

        { scanf("%ld,%d",&num,&a);

        if(num!=0)

        { if(head==NULL) head=tail;

        else ② ;

        tail->num=num; tail->score=a;

        tail->next=(struct student *)malloc(LEN);

        }

        else tail->next=NULL;

        }while(num!=0);

        return( ③ );

        }

        【3.52】下面create函數(shù)的功能是建立一個帶頭結(jié)點的單向鏈表,新產(chǎn)生的結(jié)點總是插入在鏈表的末尾。單向鏈表的頭指針作為函數(shù)值返回。

        #include

        #define LEN sizeof(struct student)

        struct student

        { long num;

        int score;

        struct student *next;

        };

        struct student *creat()

        { struct student *head=NULL,*tail;

        long num;

        int a;

        tail=( ① )malloc(LEN);

        do

        { scanf("%ld,%d",&num,&a);

        if(num!=0)

        { if(head==NULL) head=tail;

        else tail=tail->next;

        tail->num=num;

        tail->score=a;

        tail->next=( ② )malloc(LEN);

        }

        else tail->next=NULL;

        }while(num!=0);

        ③ ;

        }

        【3.53】下面程序的功能是統(tǒng)計文件中的字符的個數(shù)。

        #include

        main()

        { long num=0;

       、 *fp;

        if((fp=fopen("fname.dat", "r"))==NULL)

        { printf("Can't open the file! ");

        exit(0);

        }

        while( ② )

        { fgetc(fp);

        num++;

        }

        printf("num=%d\n",num);

        fclose(fp);

        }

        【3.54】下面程序的功能是把從鍵盤輸入的文件(用 @ 作為文件結(jié)束標志)復(fù)制到一個名為second.txt的新文件中。

        #include

        FILE *fp;

        main()

        { char ch;

        if((fp=fopen( ① ))==NULL)

        exit(0);

        while((ch=getchar())!='@')

        fputc(ch,fp);

       、 ;

        }

        【3.55】下面程序的功能是將磁盤上的一個文件復(fù)制到另一個文件中,兩個文件名在命令行中給出(假定給定的文件名無誤)。

        #include

        main(int argc,char *argv[])

        { FILE &f1,*f2;

        if(argc< ① )

        { printf("The command line error! ");

        exit(0);

        }

        f1=fopen(argv[1], "r");

        f2=fopen(arhv[2], "w");

        while( ② )

        fputs(fgetc(f1), ③ );

        ④ ;

       、 ;

        }

        【3.56】下面程序的功能是根據(jù)命令行參數(shù)分別實現(xiàn)一個正整數(shù)的累加或階乘。例如:如果可執(zhí)行文件的文件名是sm,則執(zhí)行該程序時輸入:"sm + 10",可以實現(xiàn)10的累加;輸入:"sm - 10",可以實現(xiàn)求10的階乘。

        #include

        #include

        main (int argc,char *argv[])

        { int n;

        void sum(),mult();

        void (*funcp)();

        n=atoi(argv[2]);

        if(argc!=3 || n<=0)

        dispform( );

        switch ( ① )

        { case '+': funcp=sum;

        break;

        case '-': funcp=mult;

        break;

        default: dispform( );

        }

       、 ;

        }

        void sum(int m)

        { int i,s=0;

        for(i=1;i

        ③ ;

        printf("sum=%d\n",s);

        }

        void mult(int m)

        { long int i, s=1;

        for(i=1;i<=m;i++ )

        s *= i;

        printf("mult= %ld\n";s);

        }

        dispform( )

        { printf ("usage:sm n(+/!) (n>0)\n");

        exit (0);

        }

        【3.57】下面程序的功能是鍵盤上輸入一個字符串,把該字符串中的小寫字母轉(zhuǎn)換為大寫字母,輸出到文件test.txt中,然后從該文件讀出字符串并顯示出來。

        #include

        main()

        { char str[100];

        int i=0;

        FILE *fp;

        if((fp=fopen("test.txt", ① ))==NULL)

        { printf("Can't open the file.\n");

        exit(0);

        }

        printf("Input a string:\n");

        gets(str);

        while(str[i])

        { if(str[i]>= 'a'&&str[i]<= 'z')

        str[i]= ② ;

        fputc(str[i],fp);

        i++;

        }

        fclose(fp);

        fp=fopen("test.txt", ③ );

        fgets(str,strlen(str)+1,fp);

        printf("%s\n",str);

        fclose(fp);

        }

        【3.58】下面程序的功能是將從終端上讀入的10個整數(shù)以二進制方式寫入名為"bi.dat"的新文件中。

        #include

        FILE *fp;

        main()

        { int i, j;

        if(( fp=fopen( ① , "wb" )) == NULL )

        exit (0);

        for( i=0;i<10;i++ )

        { scanf("%d", &j );

        fwrite( ② , sizeof(int), 1, ③ );

        }

        fclose( fp);

        }

        【3.59】以字符流形式讀入一個文件,從文件中檢索出六種C語言的關(guān)鍵字,并統(tǒng)計、 輸出每種關(guān)鍵字在文件中出現(xiàn)的次數(shù)。本程序中規(guī)定:單詞是一個以空格或'\t'、 '\n'結(jié)束的字符串。

        #include

        #include

        FILE *cp;

        char fname[20], buf[100];

        int num;

        struct key

        { char word[10];

        int count;

        }keyword[]={ "if", 0, "char", 0, "int", 0,

        "else", 0, "while", 0, "return", 0};

        char *getword (FILE *fp)

        { int i=0;

        char c;

        while((c=getc(fp)) != EOF && (c==' '||c=='\t'||c=='\n')) ;

        if( c==EOF ) return (NULL) ;

        else buf[i++]=c;

        while((c = ① && c!= ' ' && c!= '\t' && c!= '\n' )

        buf[i++] = c;

        buf[i]= '\0';

        return(buf);

        }

        lookup(char *p)

        { int i;

        char *q, *s;

        for(i=0;i

        { q = ② ;

        s=p;

        while( *s && (*s==*q) )

        { ③

        }

        if( ④ )

        { keyword[i].count++;

        break;

        }

        }

        return;

        }

        main()

        { int i;

        char *word;

        printf("Input file name:");

        scanf("%s", fname);

        if((cp=fopen(fname, "r")) ==NULL )

        { printf("File open error: %s\n", fname);

        exit(0);

        }

        num = sizeof(keyword) / sizeof(struct key);

        while( ⑤ )

        lookup(word);

        fclose(cp);

        for(i=0;i

        printf("keyword:%-20scount=%d\n",keyword[i].word,keyword[i].count);

        }

        【3.60】下面程序的功能是從鍵盤接受姓名(例如:輸入"ZHANG SAN"),在文件"try.dat"中查找,若文件中已經(jīng)存入了剛輸入的姓名,則顯示提示信息;若文件中沒有剛輸入的姓名,則將該姓名存入文件。要求:⑴若磁盤文件"try.dat",已存在,則要保留文件中原來的信息;若文件"try.dat"不存在,則在磁盤上建立一個新文件;⑵當(dāng)輸入的姓名為空時(長度為0),結(jié)束程序。

        #include

        main()

        { FILE *fp;

        int flag;

        char name[30], data[30];

        if((fp=fopen("try.dat",  ① ))==NULL )

        { printf("Open file error\n");

        exit(0);

        }

        do

        { printf("Enter name:");

        gets(name);

        if( strlen(name)==0 )

        break;

        strcat(name, "\n");

       、凇;

        flag=1;

        while( flag && (fgets(data, 30, fp)、邸) )

        if( strcmp(data, name) == 0 )

        ④ ;

        if( flag )

        fputs(name, fp);

        else

        printf("\tData enter error !\n");

        } while( 、荨 );

        fclose(fp);

        }

      責(zé)編:zhangjing0102