如何提高编程能力?(下)
- 2019 年 10 月 5 日
- 筆記
22.栈及栈应用
22.1 入栈出栈及判空实现
/* 栈 */ #include<stdio.h> #define N 10 typedef struct Stack { int top; int data[N]; }stack; stack s = { -1,{0} }; int isempty() { if (s.top == -1) return 1; else return 0; } void setEmpty() { s.top = -1; } int push(int data) { if (s.top + 1 <= N - 1) { s.data[++s.top] = data; return 1; } else { return 0; } } int pop() { if (isempty()) return -1; else return s.data[s.top--]; } void tenTobinary(int n) { if (n == 0) return; else { int m = n % 2; push(m); tenTobinary(n/2); } } int main() { int a[10]; int i; for (i = 0; i < 10; i++) { a[i] = i + 1; } for (i = 0; i < 10; i++) push(a[i]); while (!isempty()) { printf("%dn",pop()); } tenTobinary(100); while (!isempty()) { printf("%d", pop()); } printf("n"); return 0; }
22.2 栈实现括号匹配
#include<stdio.h> #include<string.h> #define MAX 999 int judge(char p[]) { int len = strlen(p); int top = -1; char s[MAX]; int i; for (i = 0; i < len; i++) { switch(p[i]) { case '(': case '[': case '{': s[++top] = p[i]; break; case ')': if (s[top] == '(') top--; else return 0; break; case ']': if (s[top] == '[') top--; else return 0; break; case '}': if (s[top] == '{') top--; else return 0; break; } } if (top == -1) return 1; else return 0; }
23. 二叉树
23.1 二叉树所有操作
- 前序遍历
- 中序遍历
- 后序遍历
- 节点总数
- 各层节点
- 节点访问
- 二叉树深度
#include<stdio.h> #include<malloc.h> #define MAXSIZE 100 typedef char dataType; typedef struct bnode { dataType data; struct bnode *lChild, *rChild; }Bnode,*BTree; typedef struct { BTree data[MAXSIZE]; int front, rear; }SeQuence,*PSeqQueue; BTree createTree() { BTree tree; dataType str; str = getchar(); if (str == '#') tree = NULL; else { tree = (BTree)malloc(sizeof(Bnode)); tree->data = str; tree->lChild = createTree(); tree->rChild = createTree(); } return tree; } void visit(char ch) { printf("%c t",ch); } void preOrder(BTree tree) { BTree p = tree; if (tree == NULL) return; visit(p->data); preOrder(p->lChild); preOrder(p->rChild); } void inOrder(BTree tree) { BTree p = tree; if (tree == NULL) return; inOrder(p->lChild); visit(p->data); inOrder(p->rChild); } void postOrder(BTree tree) { BTree p = tree; if (tree == NULL) return; postOrder(p->lChild); postOrder(p->rChild); visit(p->data); } PSeqQueue initSeqQueue() { PSeqQueue queue; queue = (PSeqQueue)malloc(sizeof(SeQuence)); if (queue) { queue->front = queue->rear = 0; } return queue; } int emptyQueue(PSeqQueue queue) { if (queue&&queue->front == queue->rear) return 1; else return 0; } int pushQueue(PSeqQueue queue, Bnode *node) { if ((queue->rear + 1) % MAXSIZE == queue->front) return -1; else { queue->rear = (queue->rear + 1) % MAXSIZE; queue->data[queue->rear] = node; return 1; } } int popQueue(PSeqQueue queue, BTree *node) { if (emptyQueue(queue)) { return -1; } else { queue->front = (queue->front + 1) % MAXSIZE; *node = queue->data[queue->front]; return 1; } } void destryQueue(PSeqQueue *queue) { if (*queue) { free(*queue); *queue = NULL; } } void levelOrder(BTree tree) { BTree p = tree; PSeqQueue queue = initSeqQueue(); if (p) { pushQueue(queue,p); while (!emptyQueue(queue)) { popQueue(queue,&p); visit(p->data); if(p->lChild) pushQueue(queue, p->lChild); if (p->rChild) pushQueue(queue, p->rChild); } } } int height(BTree tree) { int h1, h2; if (tree == NULL) return 0; else { h1 = height(tree->lChild); h2 = height(tree->rChild); if (h1 > h2) return h1 + 1; else return h2 + 1; } } void levelCount(BTree tree,int num[],int l) { if (tree) { num[l]++; levelCount(tree->lChild, num, l + 1); levelCount(tree->rChild, num, l + 1); } } int countTree(BTree tree) { int lCount, rCount; if (tree == NULL) return 0; lCount = countTree(tree->lChild); rCount = countTree(tree->rChild); return lCount + rCount + 1; } int main() { BTree tree = createTree(); int i = 0; int countNum[10] = { 0,0,0,0,0,0,0,0,0,0 }, l = 1, treeHeight, treeCount;//记录每层的节点数,l从1开始,树的深度 treeHeight = height(tree); printf("n此二叉树的深度为: %dn", treeHeight); treeCount = countTree(tree); printf("此二叉树的节点总数为: %dn", treeCount); levelCount(tree, countNum, l); printf("此二叉树各层的节点数为: "); for (i = 1; i <= treeHeight; i++) { printf("第%d层数目: %d, ", i, countNum[i]); } printf("nn"); printf("先序遍历此二叉树: "); preOrder(tree); printf("n"); printf("中序遍历此二叉树: "); inOrder(tree); printf("n"); printf("后序遍历此二叉树: "); postOrder(tree); printf("n"); printf("层次遍历此二叉树: "); levelOrder(tree); printf("n"); return 0; }
24.必会小点
24.1 四舍五入
- 通过float类型转变为int类型即可实现
- float a; 假设a=5.6;则四舍五入操作为:
int b=(int)(a+0.5);
24.2 逗号表达式
逗号表达式的要领:
- 1.从左到右逐个计算;
- 2.逗号表达式作为一个整体,它的值为最后一个表达式的值;
- 3.逗号表达式的优先级别在所有运算符中最低。
例如:
x=y=1; z=x++,y++,++y; printf("x=%d,y=%d,z=%d",x,y,z); 由于赋值运算符优先级大于逗号表达式优先级,所以z=1; x=2;y=3 若z=(x++,y++,++y) 则z=3; x=2;y=3; 若z=(x++,++y,y++) 则z=2; x=2;y=3;
同时也说明了一个问题对于z=(x++,y++,++y);中,y++执行完后,再去执行++y,即y++后,y变为2,++y变为3.而不是先y++,只按照赋值,后面+1未操作,这样是不对的。 带括号记住逗号表达式中取最后一个为逗号表达式的值,不带括号,要注意赋值运算或者其他运算,自左向右进行。
24.3 void 类型指针
可以指向任何类型的数据,也可以用任意数据类型的指针对void指针赋值。 int *pint; void *pvoid; pvoid = pint; //right; pint = pvoid; //error; pint = (int *)pvoid;//right,强制转化可以
24.4 内存分配
1.静态分配
int a[50]; int *p; p=a;
2.动态分配
int *p=(int *)malloc(sizeof(int));
24.5 质数分解
#include<stdio.h> int main() { int i, j, val; printf("请输入一个正整数:"); scanf("%d",&val); printf("将该正整数分解质因数输出为:"); printf("%d=",val); for (i = 2; i <val; i++) { if (val%i == 0) { printf("%d*", i); val /= i; } } printf("%d",val); printf("n"); return 0; }
24.6 大小写转化
if (p[i] >= 'a'&&p[i] <= 'z') //小写转大写 //s[i] = p[i] - 32; //s[i]=p[i]-'a'+'A'; if (p[i] >= 'A'&&p[i] <= 'Z') //大写转小写 s[i] = p[i] + 32; //s[i] = p[i] - 'A' + 'a';
24.7 字符数字转化为整型数字
'1'–> 1
#include<stdio.h> int main() { char ch; ch=getchar(); int a = ch - '0'; //int a = ch - 48; printf("%dn",a); //数字转字符 int b = 98; char ch1 = (char)b; putchar(ch1); return 0; }
25.常考小点
25.1 完数
例如:6=1+2+3,即所有因子之和
int fun(int n, int a[], int *k) { int t = n; int i, j=0; for (i = 1; i < n; i++) { if (n%i == 0) { a[j++] = i; t -= i; } } *k = j; if (t == 0) return 1; else return 0; }
25.2 闰年
leap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
25.3 统计输入的数是几位数
int fun(int n) { int bits = 1; while (n / 10) { bits++; n /= 10; } return bits; }
25.4 注意事项
1.gets/gets_s函数只能传数组名,而不能传指针 2.求int类型数组长度 a[]={1,6,8,3,6,0,10}; int len=sizeof(a)/sizeof(int);
25.5 字符串比较
int compare(char *s1, char *s2) { while (*s1&&*s2&&*s1 == *s2) { s1++; s2++; } if (*s1 == '