【3.46】輸入N個(gè)整數(shù),儲(chǔ)存輸入的數(shù)及對(duì)應(yīng)的序號(hào),并將輸入的數(shù)按從小到大的順序進(jìn)行排列。要求:當(dāng)兩個(gè)整數(shù)相等時(shí),整數(shù)的排列順序由輸入的先后次序決定。例如:輸入的第3個(gè)整數(shù)為5,第7個(gè)整數(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),按輸入時(shí)的逆序建立一個(gè)鏈接式的結(jié)點(diǎn)序列,即先輸入的位于鏈表尾(如下圖),然后再按輸入的相反順序輸出,并釋放全部結(jié)點(diǎn)。 #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】下面程序的功能是從鍵盤(pán)輸入一個(gè)字符串,然后反序輸出輸入的字符串。 #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】下面程序的功能是從鍵盤(pán)上順序輸入整數(shù),直到輸入的整數(shù)小于0時(shí)才停止輸入。然后反序輸出這些整數(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)建一個(gè)帶有頭結(jié)點(diǎn)的鏈表,將頭結(jié)點(diǎn)返回給主調(diào)函數(shù)。鏈表用于儲(chǔ)存學(xué)生的學(xué)號(hào)和成績(jī)。新產(chǎn)生的結(jié)點(diǎn)總是位于鏈表的尾部。 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ù)的功能是建立一個(gè)帶頭結(jié)點(diǎn)的單向鏈表,新產(chǎn)生的結(jié)點(diǎn)總是插入在鏈表的末尾。單向鏈表的頭指針作為函數(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)計(jì)文件中的字符的個(gè)數(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】下面程序的功能是把從鍵盤(pán)輸入的文件(用 @ 作為文件結(jié)束標(biāo)志)復(fù)制到一個(gè)名為second.txt的新文件中。 #include FILE *fp; main() { char ch; if((fp=fopen( ① ))==NULL) exit(0); while((ch=getchar())!='@') fputc(ch,fp); ② ; } 【3.55】下面程序的功能是將磁盤(pán)上的一個(gè)文件復(fù)制到另一個(gè)文件中,兩個(gè)文件名在命令行中給出(假定給定的文件名無(wú)誤)。 #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ù)分別實(shí)現(xiàn)一個(gè)正整數(shù)的累加或階乘。例如:如果可執(zhí)行文件的文件名是sm,則執(zhí)行該程序時(shí)輸入:"sm + 10",可以實(shí)現(xiàn)10的累加;輸入:"sm - 10",可以實(shí)現(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】下面程序的功能是鍵盤(pán)上輸入一個(gè)字符串,把該字符串中的小寫(xiě)字母轉(zhuǎn)換為大寫(xiě)字母,輸出到文件test.txt中,然后從該文件讀出字符串并顯示出來(lái)。 #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個(gè)整數(shù)以二進(jìn)制方式寫(xiě)入名為"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】以字符流形式讀入一個(gè)文件,從文件中檢索出六種C語(yǔ)言的關(guān)鍵字,并統(tǒng)計(jì)、 輸出每種關(guān)鍵字在文件中出現(xiàn)的次數(shù)。本程序中規(guī)定:?jiǎn)卧~是一個(gè)以空格或'\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】下面程序的功能是從鍵盤(pán)接受姓名(例如:輸入"ZHANG SAN"),在文件"try.dat"中查找,若文件中已經(jīng)存入了剛輸入的姓名,則顯示提示信息;若文件中沒(méi)有剛輸入的姓名,則將該姓名存入文件。要求:⑴若磁盤(pán)文件"try.dat",已存在,則要保留文件中原來(lái)的信息;若文件"try.dat"不存在,則在磁盤(pán)上建立一個(gè)新文件;⑵當(dāng)輸入的姓名為空時(shí)(長(zhǎ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); }