C语言简单编程题.doc_第1页
C语言简单编程题.doc_第2页
C语言简单编程题.doc_第3页
C语言简单编程题.doc_第4页
C语言简单编程题.doc_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

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

文档简介

题目:1.编写一个程序,要求输入一个ASCII码值(比如66), 然后输出相应的字符(比如输入66,输出B)。#include#include main() int i, j; char c; printf(输入一个ASCII码值:n); scanf(%d, &i); while(1) if(i = 0) printf(输出相对应的字符:n); c=toascii(i); printf(%cn, c); printf(继续输入一个数:n); scanf(%d, &i); else printf(结束); break; return 0;2.功能描述:编写函数,实现对10个整数按由小到达排序,在主函数中调用此函数。*要 求:完成至少3个函数分别实现插入排序(Insertion Sort)、选择排序、冒泡排序()#include#define M 10void insert(int a, int n);void choice(int a, int n);void BubbleSort(int a, int n);void print(int a,int n); void main() int aM, i; printf(请输入%d个整数:n, M); for(i = 0; i M; i+) scanf(%d, &ai); printf(请输出%d个整数:n, M); for(i = 0; i M; i+) printf(%d , ai); printf(n); insert(a, M); /插入排序 print(a, M); printf(请输入%d个整数:n, M); for(i = 0; i M; i +) scanf(%d, &ai); printf(请输出%d个整数:n, M); for(i = 0; i M; i+) printf(%d , ai); printf(n); choice(a, M); /选择排序 print(a, M); printf(请输入%d个整数:n, M); for(i = 0; i M; i +) scanf(%d, &ai); printf(请输出%d个整数:n, M); for(i = 0; i M; i+) printf(%d , ai); printf(n); BubbleSort(a, M); /冒泡排序 print(a, M);void insert(int a,int n) /插入排序 int i, j, k, temp; for(i = 1; i 0; j-) if(aj aj-1) temp = aj; aj = aj-1; aj-1 = temp; else break; void choice(int a, int n) /选择排序 int i, j, temp = 0, t=0; for(i = 0; i n; i+) t = i; for(j = i + 1; j aj) t = j; temp = ai; ai = at; at = temp; void BubbleSort(int a, int n) int i, j, temp; for(i = 0; i n - 1; i+) for(j = 0; j aj+1) temp = aj; aj = aj+1; aj+1 = temp; void print(int a,int n) int i; printf(输出排序后的整数:n); for(i = 0; i 10; i +) printf(%d ,ai); printf(n);3. 已知head指向一个带头结点的单向链表,链表中每个结点包含数据域(data)和指针域(next)。 请编写函数实现如图所示链表逆置。要求: 不开辟任何额外的链表结点空间,实现链表的就地逆置考察: 对链表结构的基本操作#include#include#include#include#define LEN sizeof(struct sth)struct sth char a20; struct sth *next;struct sth *creat(void);struct sth *exchange(struct sth *creat);void print(struct sth *head);void free1(struct sth *head);void main(void) struct sth *head; printf(请输入链表: n); head = creat(); printf(请输出链表: n); print(head); exchange(head); printf(输出逆置的链表: n); print(head); free1(head); struct sth *creat(void) struct sth *head = NULL; struct sth *p1, *p2; char input10=0; / p1 = p2 = (struct sth*)malloc(LEN); if(p1 =(struct sth*)malloc(LEN)= NULL) printf(内存不足,请稍后再试!n); return 0; else head = p1; p2 = head; printf(请输入相关信息:n); while(gets(input) != 0 & input0 != 0) if(p1 = (struct sth*)malloc(LEN)= NULL) printf(内存不足,请稍后再试!n); return 0; else p2 - next = p1; p2 = p1; p1 - next = NULL; strcpy(p1 - a, input); printf(请输入数据(空行则退出链表的输入):n); return (head); struct sth *exchange(struct sth *head) struct sth *p1, *p2, *p3; p1=head-next; ; p2=p1-next; p3=p2-next; p2-next = p1; p1-next = NULL; p1 = p2; p2 = p3; while(p2!=NULL) p3 = p2 - next; p2 - next = p1; p1 = p2; p2 = p3; / p2-next = p1; head - next = p1; return head;void print(struct sth *head)struct sth *p1;p1 = head-next;if(head!= NULL)while(p1 != NULL)printf(%sn, p1-a);p1 = p1-next; void free1(struct sth *head) struct sth *p1, *p2; if(head!=NULL) p1 = head-next; if(p1!=NULL) p2 = p1-next; free(p1); p1 = p2; 功能描述:4.有两个链表a和b,设结点中包含学号和姓名。 从a链表中删去与b链表中相同学号的结点。#include#include#include#define LEN sizeof(struct student)struct student char num10; char name10; struct student *next;struct student*creat(void);void print(struct student *heada);void free1(struct student *heada);struct student*found(struct student *heada, struct student *headb);int main(void) struct student *head1, *head2; printf(请输入链表a:n); head1 = creat(); print(head1); printf(请输入链表b:n); head2 = creat(); print(head2); printf(输出删除后的链表a:n); found(head1, head2); free1(head1); free1(head2); return (0);struct student *creat(void) int n = 0, i; char num2 = 0; struct student *head; struct student *p1, *p2; head = NULL; p1 = p2 = (struct student *)malloc(LEN); printf(请分别输入学号和姓名:例:1204 王岩n); scanf(%s %s,p1-num, p1-name); while(p1-num != NULL & strcmp(q, p1-num) fflush(stdin); n = n + 1; if(n = 1) head = p1; else p2-next = p1; p2 = p1; p1 = (struct student *)malloc(LEN); printf(请分别输入学号和姓名:n); scanf(%s %s, p1-num, p1-name); p2-next = NULL; return (head);struct student*found(struct student *heada, struct student *headb) struct student *p1, *p2; struct student *p3, *p4; int flag;if(heada = NULL)printf(end);p1 = p2 = heada;p3 = p4 = headb; while(p1!= NULL) flag = 0; while(p3!= NULL) if(strcmp(p1-num, p3-num) = 0) flag = 1; if( p1 = heada) heada = p1-next; free(p1); p1 = heada; else p2-next = p1-next; free(p1); p1 = p2-next; break; else p3 = p3-next; if(flag = 0) p1 = p1-next; p3 = headb; print(heada); return (0);void print(struct student *heada)struct student *p1;p1 = heada;while(p1 != NULL)printf(%s, %sn, p1-num, p1-name);p1 = p1-next;void free1(struct student *heada) struct student*p1, *p2; p2 = p1 = heada; while(p1 != NULL) p2 = p1-next; free(p1); p1 = p2; 题目:5.请编写这样一个程序,要求反复输入您的年龄,然后显示该年龄已经过了多少秒?当数据溢出时或者输入非法时发出报警声,输入q则退出程序(注意对常数的定义,年按每年365天计算)。#include#include#define NUM_YEAR 365int main() int second, s, i, a; printf(输入年龄:n); scanf(%d,&a); while(1) if(a=1) & (a68) printf(a); else if(a=q) printf(out); break; /return; 将一个5*5的矩阵中最大的元素放在中心, 四个角分别放四个最小的元素 (顺序从左到右,从上到下顺序依次从小到大存放), 写一个函数实现之。 用main函数调用。#includevoid array(int a55);int main(void)int a55, i, j;printf(输入25个数:n);for(i = 0; i 5; i+)for(j = 0; j 5; j+)scanf(%d, &aij);printf(以矩阵的形式输出25个数:n);for(i = 0; i 5; i+)for(j = 0; j 5; j+)if(j + 1) % 5 = 0)printf(%d, aij);printf(n);break; printf(%d, aij);array(a);printf(输出交换后的矩阵:n);for(i = 0; i 5; i+)for(j = 0; j 5; j+)if(j + 1) % 5 = 0)printf(%d, aij);printf(n);break; printf(%d, aij);void array(int a55)int i, j, min1, min2, min3, min4, max, temp, mini = 0, minj = 0; int min_1 = 0, min_2 = 0, min_3 = 0, min_4 = 1, min_5 = 0, min_6 = 1, min_7 = 0, min_8 = 1;max = a00;min1 = a00;for(i = 0; i 5; i+)for(j = 0; j aij)min1 = aij;min_1 = i;min_2 = j;temp = a00;a00 = amin_1min_2;amin_1min_2 = temp;min2 = a01;for(i = 0; i 5; i+)for(j = 0; j aij & (i != 0 | j != 0)min2 = aij;min_3 = i;min_4 = j;temp = a04;a04 = amin_3min_4;amin_3min_4 = temp;min3 = a01;for(i = 0; i 5; i+)for(j = 0; j aij) & (i !=0 | j != 0) & (i != 0 | j != 4)min3 = aij;min_5 = i;min_6 = j;temp=a40;a40=amin_5min_6;amin_5min_6=temp;min4 = a01;for(i = 0; i 5; i+)for(j = 0; j aij) & (i != 0 | j != 0) & (i != 0 | j != 4) & (i != 4 | j != 0)min4 = aij;min_7 = i;min_8 = j;temp = a44;a44 = amin_7min_8;amin_7min_8 = temp;for(i = 0; i 5; i+)for(j = 0; j 5; j+)if(max aij)max = aij;mini = i; minj = j;temp = a22;a22 = aminiminj;aminiminj = temp;功能描述 9.(1)编写一段程序,实现统计一个或多个文件的行数、字数和字符数,程序中包括错误检查, 以确定参数数目是否正确和是否能打开文件。 如果不能打开文件,程序要报告这一情况然后继续处理下一个文件。要求说明: a)一个行由一个换行符限定,一个字由空格分隔(包括空白符、制表符和换行符), b)字符是指文件中的所有字符。要求程序设置3个任选的参数,让用户指定他所要的统计或操作。 它们是: 1 统计文件行数 w 统计文件字数 c 统计文件字符数 若用户未指定任选的参数,则默认是lwc都有。 c)运行本程序时的参数按以下格式给出: -l -w -c 文件1 文件2 . 文件n 其中,参数l、w、c的出现与否和出现顺序任意,或任意组合在一起出现, 如:-lwc,-cwl,-lw,-wl,-lc,-cl,cw等。程序依次打开每个文件, 然后报告每个文件中相应的统计信息。文件名和统计信息一起报告。考察: (1)程序的命令行参数提供 (2)文件的基本操作 (3)错误检测和程序运行控制。 (2)在(1)的基础上可以添加每个文件中统计特定字符(串)和替换功能。 a)程序参数运行如下: f 查找文件中的某个字符(串) r 替换文件中的某个字符(串)为其他 b)运行本程序时的参数按以下格式给出: -f 字符(串) 文件1 文件2 文件n 程序依次打开每个文件,然后报告每个文件中该字符(串)出现的次数。 文件名和字符(串)本身也与计数值一起报告。 -r 字符(串)1 字符(串)2 文件1 文件2 文件n 程序依次打开每个文件,然后依次将每个文件中字符(串)1全部替换为字符(串)2#include#includeint file1(char *p);int file2(char *p);int file3(char *p);int main(int argc, char *argv) int count, i, lflag = 0, wflag = 0, cflag = 0; char *t; printf(The command line has %d arguments: n, argc); if(argc = 2) if(*argv1 = -) for(count = 1; count argc; count+) for( t = argvcount; *t != 0; t+) if(*t = l) lflag = 1; if(*t = w) wflag = 1; if(*t = c) cflag = 1; for(count = 2; count argc; count+) if(*argvcount != -) if(lflag = 1) file1(argvcount); if(wflag = 1) file2(argvcount); if(cflag = 1) file3(argvcount); else printf(错误n); else if(*argv1 != -) for(count = 2; count argc; count+) if(*argvcount = -) printf(参数输入有误n); return 0; for(count = 1; count argc; count+) file1(argvcount); file2(argvcount); file3(argvcount); return 0;int file1(char *p) FILE *fp; char ch; int flag = 0, count = 0; if(fp = fopen(p, r) = NULL) printf(cannot open filen); return 0; ch = fgetc(fp); while(ch != EOF) if(ch = n) flag = 0; else if(flag = 0) count+; flag = 1; ch = fgetc(fp); printf(输出文件中的行数:n); printf(%dn, count); int file2(char *p) FILE *fp; char ch; int flag = 0, count = 0; if(fp = fopen(p, r) = NULL) printf(cannot open filen); return 0; ch = fgetc(fp); while(ch != EOF) if(ch = | ch = n | ch = t) flag = 0; else if(flag = 0) count+; flag = 1; ch = fgetc(fp); printf(输出文件中的字数:n); printf(%dn, count);int file3(char *p) FILE *fp; char ch; int flag = 0, count = 0; if(

温馨提示

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

评论

0/150

提交评论