《最新C语言简单编程题》由会员分享,可在线阅读,更多相关《最新C语言简单编程题(42页珍藏版)》请在金锄头文库上搜索。
1、精品资料C语言简单编程题.题目: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.功能描述:编写函数,实现对1
2、0个整数按由小到达排序,在主函数中调用此函数。*要 求:完成至少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,
3、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(%
4、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
5、; 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);
6、 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(str
7、uct 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
8、 = (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 = NU
9、LL; 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)pri