版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、改错. 1.程序D8.c的功能是打开文件d:te.c用于读并判断打开是否成功【此部分在计算中心已公布的题库中,未标明正确答案,同学们自己做】#include<stdio.h>int main(void) FILE *fp; /*Found*/ char fileName = "d:te.c" /*Found*/ fp = fopen(fileName, "r"); /*Found*/ if (fp = NULL) puts("File Open Error!"); return 1; putchar(fgetc(fp);
2、fclose(fp);return 0;2.程序D9.c的功能是申请100个字节的内存空间,显示其首地址,然后释放申请到的内存空间#include<stdio.h>#include<stdlib.h>int main(void) /*Found*/ char *pch2; /*Found*/ if (pch = (char *)malloc(100) = NULL) printf("malloc memory fail!n"); return 1; printf("%pn", pch); /*Found*/ free(pch);r
3、eturn 0;3.程序D10.c的功能是将字符串str2显示在屏幕上#include<stdio.h>/*Found*/#define START /*Found*/#define NEXT int main(void) const int i = 0; for (printf("n"); str2i; ) START putchar(str2i); /*Found*/ p+;NEXTreturn 0;4.程序D16.c的功能是worker2的信息使用结构体存储,从键盘读入其各项信息并显示。#include<stdio.h>int main(voi
4、d) struct WKER long ID; long int num; char name20; char sex; /*Found*/ worker2 ; worker2.ID = 1L; /*Found*/ scanf("%ld%s %c", &worker2.num, , &worker2.sex); /*Found*/ printf("worker's info: num=%ld name=%s sex=%cn", worker2.num, , worker2.sex);
5、return 0;程序D19.c的功能是将inBuf中字符串拆分成一个个的单词。程序说明:(1)单词之间的分隔符由串divChar,程序中定义为“;?!,.>/”。 (2)函数getStrFromBuf的功能是从Buf的开始处寻找第一个单词,将找到的单词作为一个字符串复制到Str处,divStr指明单词间的分隔符。GetStrFromBuf的返回值指向已找到单词的下一个字符。(3)函数charInStr的功能是判断字符t是否出现在串Str中。(4)主程序的功能是将inBuf中的所有单词在屏幕上显示出来。#include<stdio.h&
6、gt;#define Yes 1#define No 0char *getStrFromBuf(char *Buf, char *Str, char *divStr);int charInStr(char *Str, char t);int main(void) char inBuf100 = ">how old are/ you?", *point, oneWord20; char divChar = " ;?!,.>/" point = inBuf; while (*point) /*Found*/P=getStrFromBuf(poin
7、t, oneWord, divChar); if (*oneWord) puts(oneWord); return 0;int charInStr(char *Str, char t) for (; *Str; Str+) /*Found*/ if (*Str = *t)(去掉*) /*Found*/ return Yes; /*Found*/ return No;char *getStrFromBuf(char *Buf, char *Str, char *divStr) for (; *Buf; Buf+) if (charInStr(divStr, *Buf) = No) break;
8、for (; *Buf; Buf+) if (charInStr(divStr, *Buf) = Yes) break; /*Found*/ *Str = *Buf; Str+; /*Found*/ *Str = '0' /*Found*/ return Buf;填空题【附带正确答案】一:#define M 10 main() int sub_max(); int i,aM,*ptr=a; int max; for(i=0;i<=9;i+) scanf("%d",a+i); max=sub_max(ptr,10); printf("max=%
9、dn",max); int sub_max(int *b,int i) int temp,j; temp=*b; for(j=1;j<=i-1;j+)if(temp<*(b+j) temp=*(b+j); return temp; 二、 求数组中值最大的元素值及其位置main()int a34,*ptr,i,j,max,maxi,maxj; for(i=0;i<3;i+) for(j=0;j<4;j+) scanf("%d",&aij); ptr=a0; max_arr(ptr,&max,&maxi,12); max
10、j=maxi%4; maxi=maxi/4; printf("max=%d,maxi=%d,maxj=%d",max,maxi,maxj); int max_arr(b,p1,p2,n)int *b,*p1,*p2,n; int i; *p1=b0; *p2=0; for(i=1;i<n;i+) if (bi>*p1) *p1=bi; *p2=i; 三、求二维数组中每行元素的和 #define M 3#define N 4main() void fun(); float aMN; float score1=0,score2=0,score3=0, *pa=a0;
11、int i,j; for(i=0;i<M;i+) for(j=0;j<N; j+)/*二维数组的数据输入*/ scanf("%f",&aij); fun(pa,&score1,&score2,&score3); printf("%.2f,%.2f,%.2fn",score1,score2,score3); void fun(b,p1,p2,p3)float b N,*p1,*p2,*p3;int i,j; *p1=*p2=*p3=0; for(i=0;i< M;i+) for(j=0;j< N;j+
12、) if(i=0) *p1=*p1+bij;/*第0行的数据相加*/ if(i=1) *p2=*p2+bij;/*第1行的数据相加*/ if(i=2) *p3=*p3+bij;/*第2行的数据相加*/ 四、输出二维数组任意行任意列元素的值#include "stdio.h"main() int a34=1,3,5,7,9,11,13,15,17,21,23,56; int (*p)4,i,j; p=a; scanf("%d%d",&i,&j); printf("a%d,%d=%dn",i,j,*(*(p+i)+j);
13、/ 或*(pi+j) 或Pij五、有一个班,3个学生,各学4门课,计算总平均分数,以及第n 个学生的成绩。mian()void average(float *p,int n); void serch(float (*p)4,int);float score34=65,67,70,60,80,87,90,81, 90,99,100,98; average(*score,12): serch(score,2); 六、动态数组的实现。/*程序功能:实现动态数组*/#include “alloc.h”#include “stdlib.h”main() int *array=NULL, num, i;p
14、rintf(“Input the number of element: ”); scanf(“%d”, &num); /*申请动态数组array=(int *) malloc( sizeof(int) * num ); if ( array=NULL ) printf(“out of memory, press any key to quit”); exit(0); printf(“Input %d elements: ”, num); for (i=0; i<num; i+) scanf(“%d”, &arrayi); printf(“%d elements are:
15、”, num); for (i=0; i<num; i+) printf(“%d,”, arrayi); printf(“b ”); free(array);七、用函数调用方式,实现字符串的复制。void string_copy(char *str_from, char *str_to) int i=0; for(; (*(str_from+i)!=0; i+) *(str_to+i)=*(str_from+i) *(str_to+i)=0;main() char array_str120=”I am a teacher.”; char array_str220; string_copy
16、(array_str1, array_str2); printf(“array_str2=%sn”,array_str2); 八、用指针变量实现将字符串a复制为字符串bmain()char a =“I am a boy.”, b20, *p1, *p2; int i; p1=a; p2=b; for( ; *p1!=0; p1+,p2+) *p2=*p1; *p2=0; printf(“string a is:%sn”,a); printf(“string b is:”); for(i=0; bi!=0; i+) printf(“%c”,bi); printf(“n”); 九、找出十个数中的最
17、大值main() int *max(); int n,a10,*s,i; for(i=0;i<10;i+) scanf("%d",a+i); s=max(a,10); printf("max=%d,index=%dn",*s , s-a); int *max(a,n) int *a,n; int *p,*t; for(p=a,t=a;p-a<n;p+) if(*p>*t) t=p; return t; 十、用指针数组的各指针指向字符串数组、指向一维整型数组、指向二维整型数组。 #include "stdlib.h"m
18、ain()char *ptr14="china","chengdu","sichuang", "chongqin" int i, *ptr23;int a3=1,2,3, b32=1,2,3,4,5,6;for (i=0;i<4;i+) printf("n%s",ptr1i); printf("n");for(i=0;i<3;i+) ptr2i=&ai; /*将整型一维数组a的3个元素的地址传递给指针数组ptr2的三个元素*/ for(i=0;i<3
19、;i+) /*输出3个整型变量的值*/ printf("%4d",*ptr2i); printf("n");for(i=0;i<3;i+) ptr2i=bi; for(i=0;i<3;i+) printf("%4d%4dn",*ptr2i,*(ptr2i+1);十一。用冒泡排序法对4个字符串数组按字典排序。#include "stdlib.h"#include "string.h"main() void sort(char *ptr1 .int n); void print(char
20、 *ptr1 ,int n); char *ptr14, str420; int i,j,n=4; for (i=0;i<n;i+) gets(stri); /*输入4个字符串*/ for(i=0;i<n;i+) ptr1i=stri; sort(ptr1,n); print(ptr1,n); void sort(char *ptr1 ,int n) int i,j,temp20; for(i=0;i<n-1;i+) /*冒泡排序*/ for( j=0;j<n-i-1;j+) if(strcmp(ptr1j,ptr1j+1)>0) strcpy(temp,ptr1
21、j); strcpy(ptr1j,ptr1j+1); strcpy(ptr1j+1,temp); void print(char *ptr1 ,int n) int i; for( i=0;i<n;i+) printf("%sn" ,ptr1i); 十二.利用 malloc()函数为指针分配存储空间,实现字符串的排序 #include "stdlib.h“ #include "string.h"main()void sort(char *ptr1 ,int n); void print(char *ptr1 ,int n); char *
22、ptr14; int i,j;for (i=0;i<4;i+) ptr1i=malloc(20); /*让指针数组各指针指向动态分配的20字节的存储空间*/ if (ptr1i=NULL ) /*内存申请失败: printf(“out of memory, press any key to quit”); exit(0); for(i=0;i<4;i+) gets(ptr1i); /*输入字符串*/sort(ptr1,4);print(ptr1,4); for(i=0;i<4;i+) free(ptr1i);void sort(char *ptr2 ,int n) char
23、*temp; int i,j; for(i=0;i<n-1;i+) for(j=0;j<n-i-1;j+) if(strcmp(ptr2j,ptr2j+1)>0) temp=ptr2j; ptr2j=ptr2j+1; ptr2j+1=temp; void print(char *ptr1 ,int n) int i; for( i=0;i<n;i+) printf("%sn" ,ptr1i);十三、用指向指针的指针变量访问一维和二维数组。main()int a10, b34, *p1, *p2, *p3, i, j; /*p3是指向指针的指针变量*/
24、 for(i=0; i<10; i+) scanf(“%d”, &ai); for (i=0;i<3;i+) for(j=0;j<4;j+) scanf("%d",&bij); for (p1=a;p1-a<10;p1+) /* 用指向指针的指针变量输出一维数组*/ p3=&p1; printf("%4d",*p3); printf("n"); for(i=0;i<3;i+) /*用指向指针的指针变 p2=bi; 量输出二维数组*/ for(; p2-bi<4; p2+) p
25、3=&p2; printf("%4d",*p3); printf("n"); 十四、输入一个结构体类型变量的成员,并输出。#include "stdlib.h" /*使用malloc()需要*/ struct data int day,month,year; ;struct stu char name20; long num; struct data birthday; ;main() struct stu *student; student=(struct stu*)malloc(sizeof(struct stu); pr
26、intf("Inputname,number,year,month,day:n"); scanf("%s",student->name); scanf("%ld",&student->num); scanf("%d%d%d",&student->birthday.year,&student->birthday.month,&student->birthday.day);printf("outputname,number,year,month,d
27、ayn" );printf("%20s%10ld%10d/%d/%dn",student->name,student->num,student->birthday.year,student->birthday.month,student->birthday.day); 十五struct data int day, month, year; struct stu char name20; long num; struct data birthday; ;m
28、ain() int i; struct stu *p, student4="liying",1,1978,5,23, "wangping",2,1979,3,14,"libo",3,1980,5,6, "xuyan",4,1980,4,21; p=student; printf("n1-Output name,number,year,month,dayn" ); for(i=0;i<4;i+) printf("%20s%10ld%10d/%d/%dn", (p+i)-&
29、gt;name,(p+i)->num,(p+i)->birthday.year,(p+i)->birthday.month,(p+i)->birthday.day); 十六、编写一个creat( )函数,按照规定的节点结构体,创建一个单链表(链表中的节点个数不限,以输入学号0作结束)。#include<stdio.h>#include<stdlib.h>#define LEN sizeof(struct student)#define NULL 0 struct student long num; int score; struct studen
30、t *next; ; int n; struct student *creat() struct student *head=NULL,*tail,*newp;n=0;newp= (struct student *) malloc(LEN);printf("input Number and Scoren");scanf(“%ld,%d”,&newp->num,&newp->score);tail=newp;while(newp->num!=0) n+;if(n=1) head=newp; else tail->next=newp;ta
31、il=newp;newp=(struct student *)malloc(LEN); printf("input Number and Scoren");scanf(“%ld,%d”,&newp->num,&newp->score); tail->next=NULL; return(head); main()struct student *head; printf("Please creat the listn"); head=creat();十七、输出链表的某节点数据typedef struct student ch
32、ar name10; int score; struct student *next;NODE;void print_list(NODE *head) NODE *p=head; while (p!=NULL) printf("n%s %d",p->name,p->score); p=p->next; 十八。该子函数的功能是统计链表节点的个数typedef struct student char name10; int score; struct student *next;NODE;int count_list(NODE *head) NODE *p=h
33、ead; int count=0;while (p!=NULL) count+; p=p->next; return(count);十九,该子函数的功能是删除链表中的某节点struct node int num; char str20; struct node *next; ; struct node *delet(struct node * head, char * pstr) /*以head为头指针,删除pstr所在节点*/ struct node *temp,*p; temp=head; if (head=NULL) printf("nList is null!n&quo
34、t;); else temp=head; while(strcmp(temp->str,pstr)!=0 && temp->next!=NULL) p=temp;temp=temp->next; /*跟踪链表的节点,即指针后移*/ if(strcmp(temp->str,pstr)= =0 ) if (temp=head) printf("deletestring :%sn",temp->str); head=head->next;free(temp); else /*表中节点或最后节点*/ p->next=temp
35、->next; printf("delete string :%sn",temp->str); free(temp);else printf("nno find string!n"); /*没找到要删除的字符串*/ return(head); /*返回表头指针*/ 二十、以学号为顺序建立链表,插入某节点struct node *insert(struct node * head, char * pstr, int n) /*插入学号为n、姓名为pstr的节点*/ struct node *p1,*p2,*p3;p1=(struct node*
36、)malloc(sizeof(struct node); strcpy(p1->str,pstr); /*写入节点的姓名字串*/ p1->num=n; p2=head; if(head=NULL) head=p1; p1->next=NULL; /*新节点插入表头*/else /*非空表*/ while(n>p2->num&&p2->next!=NULL) /*输入的学号小于节点的学号,并且未到表尾*/ p3=p2; p2=p2->next; /*跟踪链表增长*/if (n<=p2->num) /*找到插入位置*/ if (
37、head=p2) head=p1;p1->next=p2; /*插入位置在表头*/ else p3->next=p1; p1->next=p2; /*插入位置在表中*/else p2->next=p1; p1->next=NULL; /*插入位置在表尾*/ return(head); /*返回链表的头指针*/二十二、打开d盘根下的myfile.c文件,验证文件能否正确打开.include <stdio.h>void main() FILE *fa; if(fa=fopen(“d:myfile.c”,“r”)=NULL) printf(“n Cannot
38、 open the file!”); exit(0); else printf(“ n Open! ”); fclose(fa); 二十三【编程题】、将键盘上输入的一个字符串(以#作为结束字符),存储到一个文件中。#include "stdio.h” main( ) char ch,filename10; printf(“Enter the file name:”); scanf("%s",filename); if (fp=fopen(filename,“w”) = = NULL) printf("can not open this filen&quo
39、t;); exit(0); ch=getchar( );/*输入字符并存储到文件中*/ while(ch!=#) fputc(ch,fp); ch=getchar( ); fclose(fp); 二十四、【编程题】将一个磁盘文件的信息复制到另一个文件中并显示在屏幕上。#include "stdio.h“ main( ) FILE *in,*out; char ch, infile20, outfile20; printf("Enter the infile name:"); scanf("%s",infile); printf("En
40、ter the outfile name:"); scanf("%s",outfile); if (in=fopen(infile,"r")=NULL) printf("can not open infilen"); exit(0); if (out=fopen(outfile,"w")=NULL) printf("can not open outfilen"); exit(0); while (!feof(in) ch fgetc(in);fputc(ch,out); putchar
41、(ch); fclose(in); fclose(out); 二十五、编写一个程序,实现DOS中的type命令的功能。命令格式如下: c:>type d:file1.cpp#include <stdio.h> #include <stdlib.h> void main(int argc,char *argv) FILE *fp; char c; if (argc<2)printf(“n NO file name”); exit(0); if (fp=fopen(argv1, “r” )=NULL) printf(“n File not exist!”);ex
42、it(0); while(c=fgetc(fp)!=EOF) putchar( c );fclose(fp);二十六、从键盘读入字符串存入文件file1.txt ,再将文本文件file1.txt中的内容全部读出并显示在屏幕上。#include<stdio.h>main() FILE *fp; char string81; if(fp=fopen("file1.txt","w")=NULL) printf("cann't open file");exit(0); while(strlen(gets(string)&g
43、t;0) fputs(string,fp); fputs("n",fp); fclose(fp);if(fp=fopen("file1.txt","r")=NULL) printf("cann't open file");exit(0); while(fgets(string,81,fp)!=NULL) fputs(string,stdout);fclose(fp);二十七、【编程题】把文件1的内容追加到文件2#include<stdio.h>#include<stdlib.h>#d
44、efine BUFSIZE 256void main(int argc,char *argv) int i; char buffBUFSIZE; FILE *fp1,*fp2; if(argc=1) printf("Rsage: fcat filename linke_fnamelink_fname."); printf("n");exit(0); if(fp1=fopen (argv1,"a")=NULL) printf("file %s cannot opened.n",argv1); exit(1);for(
45、i=2;i<argc;i+) if(fp2=fopen(argvi,"r")=NULL) printf("file %s cannot opened.n"); exit(1); while(fgets(buff,BUFSIZE,fp2)!=NULL) fputs(buff,fp1); fclose(fp2); fclose(fp1);二十八。从键盘输入4个学生的有关数据,然后把它们转存到一个写入磁盘文件student.dat中。#include "stdio.h” #define SIZE 4typedef struct int num;
46、 char name10; stutype;stutype studentSIZE;void save( ) FILE *fp; int i; if (fp=fopen(“student.dat","wb")=NULL) printf(“Can not open this filen"); return; for (i=0;i<SIZE;i+) if (fwrite(&studenti,sizeof(stutype),1,fp)!=1) printf("file write errorn"); fclose(fp); m
47、ain( ) int i; printf("Enter %d students No. and name:n“,SIZE); for (i=0;i<SIZE;i+)scanf("%d%s",&studenti.num,); save( );二十九、【编程题】从文件中读入数据,然后在屏幕上输出。#include "stdio.h” #define SIZE 4typedef struct int num; char name10; stutype;stutype studentSIZE;main( ) int i;
48、FILE *fp; fp=fopen(“student.dat","rb"); printf("%d students No. and name:n",SIZE); for (i=0;i<SIZE;i+) fread(&studenti,sizeof(stutype),1,fp);printf("%d%10sn",studenti.num,); fclose(fp);三十、按指定的格式,将学生信息写入到文件stud. dat中#include<stdio.h>struct studentchar name10; int num; int age; char addr15;boy2,*pp;void main()FILE *fp; char ch, filename20; int i; gets(filename); pp=boy;if(fp=fopen(filename,"w")=NULL) printf("Cannot open file!"); getch(); exit(1); printf ("ninput datan");for(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医学课件-口腔组织病理学之含牙囊肿
- 医疗信息化建设中的数据共享问题
- sCD40L、IL-10与DM2患者视网膜病变的相关性分析
- 医疗废物-分类、放置与处理
- 个性化医疗与生物信息学应用
- AFP病例又称为急性弛缓性麻痹(AFP),是指将所有15岁以下急性发生的肢体
- 下肢深静脉血栓溶栓护理(“静脉”文档)共26张
- 血液细胞形态学检查在血常规检验中的重要性分析
- 医用呼吸机技术升级与市场布局
- 2026年湖北黄石市中考二模考试化学试题附答案
- 《耳科学》全册配套教学课件3
- 文言文二则之《曹冲称象》教案(2课时)-2026-2027学年统编版六年级语文上册
- 2026南开大学校友工作办公室招聘劳务派遣人员1人考前冲刺试卷附参考答案详解【达标题】
- 2026秋译林版八年级英语上册【Unit1-8】全册语法专项练习(含参考答案)
- 2026年甘肃省酒泉矿业投资集团有限公司招聘考试备考试题及答案详解
- 初高中语文衔接第一课
- 高中常见不规则动词的过去式及过去分词背诵版
- GJB3206B-2022技术状态管理
- 德育为先 立德树人
- 宝马工程师及系列软件一些地址
- GB/T 17193-1997电气安装用超重荷型刚性钢导管
评论
0/150
提交评论