2012c语言考试题.docx_第1页
2012c语言考试题.docx_第2页
2012c语言考试题.docx_第3页
2012c语言考试题.docx_第4页
2012c语言考试题.docx_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

清华大学本科生考试试题专用纸考试课程 计算机语言与程序设计 ( 卷) 2013 年 1 月 10 日姓名 陈广义 班级 自21 学号 2012011521 大题题号一二三四总分分数30分30分20分20分成绩试卷提交说明1. 在考卷上填写你的姓名、学号;2. 按要求编程,调试运行,将每道题的结果考屏,粘贴在考卷对应的题后;3. 交卷时,提交一个压缩文件包,包括:a) 文件包名学号+姓名,如:2012123456张三.rarb) 文件包内容 每道题的c工程文件夹; 考卷。第一题:字符串映射。#include#include#include#include#includevoid input(char *p);void reinput(char *q,char *p);void yingshe(char *q,char *p,char *v);void main()char ch=0;char a30,b30,c30,*p,*q,*v;p=a;q=b;v=c;while(ch!=)input(p);reinput(q,p);yingshe(q,p,v);printf(转换后为%s,c);printf(是否继续程序,若退出请输入n);ch=getch();void input(char *p)printf(请输入字符串:n);gets(p);void reinput(char *q,char *p)int t,r=1,k,i,j;t=strlen(p);char temp,c30;doprintf(请输入字符串2(由0到%d):n,t-1);cinq;r=0;k=strlen(q);if(k=t)strcpy(c,q);for (i=0;it-1;i+)for(j=0;jcj+1)temp=cj;cj=cj+1;cj+1=temp;for(i=0;it;i+)if(ci!=i+48)r+;elser=1;while(r!=0);void yingshe(char *q,char *p,char *v)int t,k;t=strlen(p);for(int i=0;it;i+)k=(int)qi-48;vi=pk;vt=0;第二题:动态矩阵处理。编制程序,实现如下功能:1. 函数input() (10分)首先从键盘输入整数矩阵的行数m与列数n,根据输入的m与n,动态生成整数矩阵a,并依次输入矩阵a的各元素值,将整数矩阵a以及m与n的值传回给主函数。2. 函数RowSort() (6分)对于矩阵a的每一行内的各个元素按照从大到小的顺序进行降序排序。3. 函数MatrixSort() (6分)该函数逐行调用函数RowSort(),并将矩阵每一行的位置,按照行内所有元素的和的数值,从大到小重排矩阵,最后将排序处理后的矩阵返回给主函数。4. 函数print() (4分)按行依次输出m*n的矩阵a的各个元素。5. 编写主函数main() (4分)按照程序示例,主函数循环运行,当且仅当输入时,程序结束运行。#include#include#include#include#includeint *input(int *m,int *n);/矩阵生成void RowSort(int *p,int a,int b);/每行排序void MatrixSort(int *p,int a,int b);/按平均值的大小进行列排序void print(int *p,int a,int b);/打出矩阵void main()int *p;int a,b,*m,*n;m=&a;n=&b;char c;dop=input(m,n);MatrixSort(p,a,b);printf(result:n);print(p,a,b);printf(quit input n);/判断是否退出c=getch();while(c=);int *input(int *m,int *n)int *p;int i,j;printf(input mn);cin*m;printf(input nn);cin*n;p=(int *)malloc(*m*sizeof(int *);for(i=0;i*m;i+)pi=(int *)malloc(*n*sizeof(int);for(i=0;i*m;i+)for(j=0;jpij;return(p);void RowSort(int *p,int a,int b)int i,j,t;for(i=0;ib-1;i+)for(j=0;jpaj+1)t=paj;paj=paj+1;paj+1=t;void MatrixSort(int *p,int a,int b)int k,i,j,sum=0,t;int *q;int *temp;q=(int *)malloc(a*sizeof(int);for(k=0;ka;k+)RowSort(p,k,b);for(k=0;ka;k+)sum=0;for(i=0;ib;i+)sum=sum+pki;qk=sum;for(i=0;ia-1;i+)for(j=i+1;jqj)temp=pi;pi=pj;pj=temp;t=qi;qi=qj;qj=t;void print(int *p,int a,int b)int i,j;for(i=0;ia;i+)for(j=0;jb;j+)printf(%dt,pij);printf(n);第三题:函数编程。按照要求,编写完成函数程序。一段可运行的程序如下:其中,函数create()生成一个长度随机的环链表如下图所示(链表中每一结点数据域也是随机值),该链表最后一个节点的next指针随机的指向了链表内的某一节点。请编写函数(不允许修改create()函数和主函数等已有的程序代码)实现以下功能:1. 解环函数openloop()(10分)形参是环链表头指针head,openloop函数将链表解环,使之恢复成为一个正常的单链表如下图所示。2. 输出链表函数print()(10分)输出解环后的单向链表到屏幕。给定程序:#include #include #include #include struct nodeint num;struct node *next;struct node *create();void openloop(struct node *);void print(struct node *);void main()struct node *head;srand(time(NULL);head = create();openloop(head);print(head);struct node *create()int n = 0;struct node *p1, *p2, *head;int i;int randomIndex ; head=NULL;while (n=0) /环链的长度随机n=(int)rand() % 20; for( i=0; inext = NULL;p1-num = (int)rand(); /每节点内容随机if(i = 0)head = p1;elsep2-next = p1;p2 = p1; if (head!=NULL) /当链表不为空时,随机成环randomIndex = (int)rand() % n; /随机成环的节点p2 = head;for(i=0; inext;p1-next = p2;return head;/以上原始代码不允许修改#include #include #include #include struct nodeint num;struct node *next;struct node *create();void openloop(struct node *);void print(struct node *);void main()struct node *head;srand(time(NULL);head = create(); openloop(head);print(head);void openloop(struct node *head) struct node *p1=head; struct node *p2=head; struct node *p0=head; do p1=p1-next; p2=head; while(p1-next!=p2) p0=p2;p2=p2-next; while(p0=p1); p1-next=0;void print(struct node *head)struct node *p=head;while(p!=0)printf(%d-,p-num);p=p-next;printf(endn);struct node *create()int n = 0;struct node *p1, *p2, *head;int i;int randomIndex ; head=NULL;while (n=0) n=(int)rand() % 20; for( i=0; inext = NULL;p1-num = (int)rand(); if(i = 0)head = p1;elsep2-next = p1;p2 = p1; if (head!=NULL) randomIndex = (int)rand() % n; p2 = head;for(i=0; inext;p1-next = p2;return head;第四题:字符串匹配。请按照要求编写程序,实现字符串模式匹配。字符串应用的一个重要方面是模式匹配,也就是判断包含通配符(仅考虑*的情况)的字符串str2是否匹配输入的字符串str1,假设字符串str2中包含的通配符仅有*,且通配符不会连续出现,其中通配符*可以匹配0个或多个任意字符。例如,str1为“hello, world”,str2为“hello, *ld”,则str1与str2是匹配的,其中*匹配了str2中的“wor”。按照要求,编写程序。1. 函数input()(10分)键盘输入两个字符串str1与str2(假设长度均不超过40),str1中不包含通配符,str2中包含通配符*,并返回给主函数2. 递归函数PatternMatch()(5分)使用递归的方法,判断str1与str2是否匹配,如果匹配成功,则返回1,否则返回0。3. 主函数main()(5分)主函数循环运行。调用input()输入字符串str1、str2;若输入的字符串str1为“quit”时,程序执行结束;否则调用递归函数PatternMatch(),判断str1、str2的匹配情况,并输出“匹配成功”或“匹配失败”的信息。#include#include#include#include#include#includevoid input(char *p,char *q);void PatternMatch(char *p,char *q,int *i);void main() char a10=p,b10;int *i,t=0;char *p,*q;p=a;q=b;i=&t;while(strcmp(a,quit)!=0)printf(第一个字符串是quit时结束n);input(p,q); PatternMatch(p,q,i);if(t=0)printf(匹配成功!n);elseprintf(匹配失败!n);printf(程序结束!n);void input(char *p,char *q)int n,i,r;dor=0;printf(input the firstn);gets(p);n=strlen(p);for(i=0;in;i+)if(pi122)&(pi!=44&pi!=32)r+;while(r!=0); dor=0;printf(input the secondn); gets(q);n=strlen(q);for(i=0;in;i+)if(qi122)&(qi!=42&q

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论