C语言简单编程题.doc
《C语言简单编程题.doc》由会员分享,可在线阅读,更多相关《C语言简单编程题.doc(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流C语言简单编程题【精品文档】第 14 页题目: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
2、; 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
3、, &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); f
4、or(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) /选择
5、排序 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
6、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 st
7、h *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; ch
8、ar 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;
9、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 =
10、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); p
11、1 = 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 st
12、udent *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,
13、 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
14、= (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 = head
15、b; 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 s
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 简单 编程
限制150内