测试一些C特性
This commit is contained in:
+42
-1
@@ -142,12 +142,53 @@ n个节点的完全二叉树,其查找,删除的复杂度都是O(logN),但
|
|||||||
## R树
|
## R树
|
||||||
|
|
||||||
|
|
||||||
## 赫夫曼编码
|
## 赫夫曼编码 Huffman
|
||||||
|
|
||||||
|
这是一个经典的压缩算法。通过`字符出现的频率`,`优先级`,`二叉树`进行的压缩算法。
|
||||||
|
|
||||||
|
对一个字符串,计算每个字符出现的次数,把这些字符放到优先队列(priority queue)
|
||||||
|
这这个priority queue转出二叉树
|
||||||
|
|
||||||
|
需要一个字符编码表来解码,通过二叉树建立huffman编码和解码的字典表
|
||||||
|
|
||||||
|
|
||||||
|
原始串:
|
||||||
|
二级制编码:
|
||||||
|
huffman编码:
|
||||||
|
|
||||||
|
|
||||||
|
###存储结构和基本操作
|
||||||
|
|
||||||
|
struct node{
|
||||||
|
char *huffCode; // 叶子节点的huff编码
|
||||||
|
int weight;
|
||||||
|
struct node *left,right;
|
||||||
|
}
|
||||||
|
|
||||||
|
*构建赫夫曼树*
|
||||||
|
|
||||||
|
原则:出现频率越多的会在越上层,编码也越短,出现频率越少的在越下层,编码也越长。
|
||||||
|
不存在某一个编码是另一个编码的前缀,字符都在叶节点上,所以不会存在一个编码是另一个编码的前缀
|
||||||
|
二叉树每个节点要么是叶子节点,要么是双分支节点(且左分支编码为0,右分支编码为1)
|
||||||
|
|
||||||
|
|
||||||
|
*压缩*
|
||||||
|
|
||||||
|
1. 扫描输入文件,统计各个字符出现的次数,对结构排序 (hash统计每个字符出现的次数)
|
||||||
|
2. 根据排序结构,构建赫夫曼树 (贪心策略,每次选频率值最低的2个节点合并,需要优先队列帮组(priority queue,又叫最小堆))
|
||||||
|
3. 对树进行遍历(左分支编码为0,右分支编码为1),得到各个字符的huffman编码,存到hash表中(这个就是编解码表,也可直接存储到节点中,如上面的char *huffCode)
|
||||||
|
4. 重新对文件扫描,根据hash表进行压缩
|
||||||
|
|
||||||
|
压缩的文件为了能够解压缩,需要一个文件头,用来重建赫夫曼树,包括:
|
||||||
|
被编码的文本长度 unsigned int size
|
||||||
|
字符频率表 unsigned char freqs[NUM_CHARS]
|
||||||
|
|
||||||
|
*解压缩*
|
||||||
|
|
||||||
|
1. 读取文件头
|
||||||
|
2. 遍历编码后的bits,从赫夫曼树的根节点出发,遇到0,进入左子树,遇到1进入右子树,直到叶节点
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 字典树trie(前缀树,单词查找树)
|
## 字典树trie(前缀树,单词查找树)
|
||||||
|
|
||||||
|
|||||||
+40
-46
@@ -2,19 +2,18 @@
|
|||||||
|
|
||||||
把二元查找树转变成排序的双向链表
|
把二元查找树转变成排序的双向链表
|
||||||
|
|
||||||
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
|
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求`不能创建任何新的结点`,只调整指针的指向。
|
||||||
要求不能创建任何新的结点,只调整指针的指向。
|
|
||||||
|
|
||||||
10
|
10
|
||||||
/ /
|
/ \
|
||||||
6 14
|
6 14
|
||||||
/ / / /
|
/\ / \
|
||||||
4 8 12 16
|
4 8 12 16
|
||||||
|
|
||||||
转换成双向链表
|
|
||||||
4=6=8=10=12=14=16。
|
转换成双向链表 4=6=8=10=12=14=16。
|
||||||
|
|
||||||
首先我们定义的二元查找树 节点的数据结构如下:
|
首先我们定义的二元查找树 节点的数据结构如下:
|
||||||
struct BSTreeNode
|
struct BSTreeNode
|
||||||
{
|
{
|
||||||
int m_nValue; // value of node
|
int m_nValue; // value of node
|
||||||
@@ -22,24 +21,25 @@
|
|||||||
BSTreeNode *m_pRight; // right child of node
|
BSTreeNode *m_pRight; // right child of node
|
||||||
};
|
};
|
||||||
|
|
||||||
|
分析:题目要求不能创建任何节点,也就是只能调整各个节点的指向
|
||||||
|
思路一:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
4.在二元树中找出和为某一值的所有路径
|
在二元树中找出和为某一值的所有路径
|
||||||
|
|
||||||
题目:输入一个整数和一棵二元树。
|
题目:输入一个整数和一棵二元树。
|
||||||
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
|
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
|
||||||
打印出和与输入整数相等的所有路径。
|
打印出和与输入整数相等的所有路径。
|
||||||
例如 输入整数22和如下二元树
|
例如 输入整数22和如下二元树
|
||||||
10
|
10
|
||||||
/ /
|
/ \
|
||||||
5 12
|
5 12
|
||||||
/ /
|
/\
|
||||||
4 7
|
4 7
|
||||||
则打印出两条路径:10, 12和10, 5, 7。
|
则打印出两条路径:10, 12和10, 5, 7。
|
||||||
|
|
||||||
二元树节点的数据结构定义为:
|
二元树节点的数据结构定义为:
|
||||||
|
|
||||||
struct BinaryTreeNode // a node in the binary tree
|
struct BinaryTreeNode // a node in the binary tree
|
||||||
{
|
{
|
||||||
int m_nValue; // value of node
|
int m_nValue; // value of node
|
||||||
@@ -47,16 +47,19 @@ BinaryTreeNode *m_pLeft; // left child of node
|
|||||||
BinaryTreeNode *m_pRight; // right child of node
|
BinaryTreeNode *m_pRight; // right child of node
|
||||||
};
|
};
|
||||||
|
|
||||||
|
1. 累加当前节点,
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
判断整数序列是不是二元查找树的后序遍历结果
|
判断整数序列是不是二元查找树的后序遍历结果
|
||||||
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。
|
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。
|
||||||
如果是返回true,否则返回false。
|
如果是返回true,否则返回false。
|
||||||
例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:
|
例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:
|
||||||
8
|
8
|
||||||
/ /
|
/ \
|
||||||
6 10
|
6 10
|
||||||
/ / / /
|
/\ / \
|
||||||
5 7 9 11
|
5 7 9 11
|
||||||
因此返回true。
|
因此返回true。
|
||||||
如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。
|
如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。
|
||||||
@@ -65,32 +68,27 @@ BinaryTreeNode *m_pRight; // right child of node
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
设计一个算法,找出二叉树上任意两个结点的最近共同父结点。
|
|
||||||
复杂度如果是O(n2)则不得分。
|
|
||||||
|
|
||||||
|
一棵排序二叉树,令 f=(最大值+最小值)/2,设计一个算法,找出距离f值最近、大于f值的结点。
|
||||||
一棵排序二叉树,令 f=(最大值+最小值)/2,
|
|
||||||
设计一个算法,找出距离f值最近、大于f值的结点。
|
|
||||||
复杂度如果是O(n2)则不得分。
|
复杂度如果是O(n2)则不得分。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
题目:输入一颗二元查找树,将该树转换为它的镜像,
|
题目:输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。用递归和循环两种方法完成树的镜像转换。
|
||||||
即在转换后的二元查找树中,左子树的结点都大于右子树的结点。
|
|
||||||
用递归和循环两种方法完成树的镜像转换。
|
|
||||||
例如输入:
|
例如输入:
|
||||||
8
|
8
|
||||||
/ /
|
/ \
|
||||||
6 10
|
6 10
|
||||||
// //
|
/\ / \
|
||||||
5 7 9 11
|
5 7 9 11
|
||||||
|
|
||||||
输出:
|
输出:
|
||||||
8
|
|
||||||
/ /
|
8
|
||||||
10 6
|
/ \
|
||||||
// //
|
10 6
|
||||||
|
/\ /\
|
||||||
11 9 7 5
|
11 9 7 5
|
||||||
|
|
||||||
定义二元查找树的结点为:
|
定义二元查找树的结点为:
|
||||||
@@ -104,13 +102,12 @@ struct BSTreeNode // a node in the binary search tree (BST)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
题目(微软):
|
|
||||||
输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。
|
输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。
|
||||||
例如输入
|
例如输入
|
||||||
8
|
8
|
||||||
/ /
|
/\
|
||||||
6 10
|
6 10
|
||||||
/ / / /
|
/ \ /\
|
||||||
5 7 9 11
|
5 7 9 11
|
||||||
|
|
||||||
输出8 6 10 5 7 9 11。
|
输出8 6 10 5 7 9 11。
|
||||||
@@ -122,16 +119,13 @@ struct BSTreeNode // a node in the binary search tree (BST)
|
|||||||
|
|
||||||
二元树的深度。
|
二元树的深度。
|
||||||
|
|
||||||
题目:输入一棵二元树的根结点,求该树的深度。
|
题目:输入一棵二元树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
|
||||||
|
|
||||||
从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
|
|
||||||
|
|
||||||
例如:输入二元树:
|
例如:输入二元树:
|
||||||
|
|
||||||
10
|
10
|
||||||
/ /
|
/ \
|
||||||
6 14
|
6 14
|
||||||
/ / /
|
/ / \
|
||||||
4 12 16
|
4 12 16
|
||||||
|
|
||||||
输出该树的深度3。
|
输出该树的深度3。
|
||||||
@@ -154,7 +148,9 @@ struct SBinaryTreeNode // a node of the binary tree
|
|||||||
|
|
||||||
|
|
||||||
怎样编写一个程序,把一个有序整数数组放到二叉树中?
|
怎样编写一个程序,把一个有序整数数组放到二叉树中?
|
||||||
|
|
||||||
分析:本题考察二叉搜索树的建树方法,简单的递归结构。
|
分析:本题考察二叉搜索树的建树方法,简单的递归结构。
|
||||||
|
|
||||||
关于树的算法设计一定要联想到递归,因为树本身就是递归的定义。
|
关于树的算法设计一定要联想到递归,因为树本身就是递归的定义。
|
||||||
而,学会把递归改称非递归也是一种必要的技术。
|
而,学会把递归改称非递归也是一种必要的技术。
|
||||||
毕竟,递归会造成栈溢出,关于系统底层的程序中不到非不得以最好不要用。
|
毕竟,递归会造成栈溢出,关于系统底层的程序中不到非不得以最好不要用。
|
||||||
@@ -166,17 +162,15 @@ struct SBinaryTreeNode // a node of the binary tree
|
|||||||
递归和非递归俩种方法实现二叉树的前序遍历。
|
递归和非递归俩种方法实现二叉树的前序遍历。
|
||||||
|
|
||||||
|
|
||||||
怎样编写一个程序,把一个有序整数数组放到二叉树中?
|
|
||||||
|
|
||||||
怎样从顶部开始逐层打印二叉树结点数据?请编程。
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
有一棵树(树上结点为字符串或者整数),请写代码将树的结构和数据写到一个文件中,并能通过读取该文件恢复树结构 。
|
有一棵树(树上结点为字符串或者整数),请写代码将树的结构和数据写到一个文件中,并能通过读取该文件恢复树结构。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
设计一个算法,找出二叉树上任意两个结点的最近共同父结点。复杂度如果是O(n2)则不得分
|
||||||
|
|
||||||
二叉树两个结点的最低共同父结点
|
二叉树两个结点的最低共同父结点
|
||||||
题目:二叉树的结点定义如下:
|
题目:二叉树的结点定义如下:
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int y = 100;
|
||||||
|
int *p;
|
||||||
|
p = malloc(sizeof(int));
|
||||||
|
*p = 10;
|
||||||
|
y = y / *p;
|
||||||
|
PrintInt(y);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int day,month,year;
|
||||||
|
printf("Enter the date (dd-mm-yyyy) format including -'s:");
|
||||||
|
scanf("%d-%d-%d",&day,&month,&year);
|
||||||
|
printf("The date you have entered is %d-%d-%d\n",day,month,year);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
printf("Enter a number:\n");
|
||||||
|
scanf("%d",&n); // &
|
||||||
|
|
||||||
|
printf("You entered %d \n",n);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int cnt = 5, a;
|
||||||
|
|
||||||
|
do {
|
||||||
|
a /= cnt;
|
||||||
|
} while (cnt --);
|
||||||
|
|
||||||
|
printf ("%d\n", a);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// cnt==1 时,会发生除0错误
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i = 6;
|
||||||
|
if( ((++i < 7) && ( i++/6)) || (++i <= 9))
|
||||||
|
;
|
||||||
|
printf("%d\n",i);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// && 优先级大于 ||
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#define SIZE 15
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int *a, i;
|
||||||
|
|
||||||
|
a = malloc(SIZE*sizeof(int));
|
||||||
|
|
||||||
|
for (i=0; i<SIZE; i++)
|
||||||
|
*(a + i) = i * i;
|
||||||
|
for (i=0; i<SIZE; i++)
|
||||||
|
printf("%d\n", *a++);
|
||||||
|
free(a);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 指针已经移动,free错误
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// FROM:http://www.gowrikumar.com/c/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char dummy[80];
|
||||||
|
printf("Enter a string:\n");
|
||||||
|
scanf("%[^a]",dummy);
|
||||||
|
printf("%s\n",dummy);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a=3, b = 5;
|
||||||
|
|
||||||
|
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
|
||||||
|
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
|
||||||
|
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int* ptr1,*ptr2;
|
||||||
|
ptr1 = malloc(sizeof(int));
|
||||||
|
ptr2 = ptr1;
|
||||||
|
*ptr2 = 10;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
|
||||||
|
/*
|
||||||
|
二叉查找树变成双向链表
|
||||||
|
*/
|
||||||
|
|
||||||
|
strcut BSTreeNode{
|
||||||
|
|
||||||
|
int m_nValue;
|
||||||
|
BSTreeNode *m_pLeft,m_pRight;
|
||||||
|
|
||||||
|
}BSTree;
|
||||||
|
|
||||||
|
void convertDoubleLinks(BSTree *root){
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char const *argv[])
|
||||||
|
{
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
// FROM:http://www.gowrikumar.com/c/
|
||||||
|
|
||||||
|
#include<stdio.h>
|
||||||
|
|
||||||
|
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
|
||||||
|
int array[] = {23,34,12,17,204,99,16};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int d;
|
||||||
|
int n = TOTAL_ELEMENTS-2;
|
||||||
|
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
|
||||||
|
printf("%d\n",array[d+1]);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
d 是有符号数,sizeof()计算为无符号数,比较时d会转为无符号数,变成一个大数
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i=43;
|
||||||
|
printf("%d\n",printf("%d",printf("%d",i)));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// printf 返回值是打印数据的长度 4321
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
void foobar1(void)
|
||||||
|
{
|
||||||
|
printf("In foobar1\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void foobar2()
|
||||||
|
{
|
||||||
|
printf("In foobar2\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char ch = 'a';
|
||||||
|
foobar1(33, ch);
|
||||||
|
foobar2();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// compile error
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
void foobar1(void)
|
||||||
|
{
|
||||||
|
printf("In foobar1\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void foobar2()
|
||||||
|
{
|
||||||
|
printf("In foobar2\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char ch = 'a';
|
||||||
|
foobar1();
|
||||||
|
foobar2(33, ch);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
// FROM:http://www.gowrikumar.com/c/
|
||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
float a = 12.5;
|
||||||
|
printf("%d\n", a);
|
||||||
|
//printf("%d\n", *(int *)&a);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 浮点数 转 整形 ,用强制转换(int)a
|
||||||
|
|
||||||
|
/*
|
||||||
|
浮点数的存储
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
int arr[80];
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#include "c13-1.c"
|
||||||
|
|
||||||
|
extern int *arr;
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
arr[1] = 100;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
类型并不匹配,所以导致这里的arr并没有指向c13-1.c中声明的arr[80],应该修改为 extern int arr[]
|
||||||
|
|
||||||
|
int *arr 和 int arr[80] 区别?
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a=1;
|
||||||
|
switch(a)
|
||||||
|
{ int b=20;
|
||||||
|
case 1: printf("b is %d\n",b);
|
||||||
|
break;
|
||||||
|
default:printf("b is %d\n",b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// In my computer ,output:b is 32767 switch中只会从case开始执行,b会随机输出
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#define SIZE 10
|
||||||
|
void size(int arr[SIZE])
|
||||||
|
{
|
||||||
|
printf("size of array is:%d\n",sizeof(arr));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int arr[SIZE];
|
||||||
|
size(arr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 32位机器 4, 64位机器 8
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
// FROM:http://www.gowrikumar.com/c/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
void Error(char* s)
|
||||||
|
{
|
||||||
|
printf(s);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int *p;
|
||||||
|
p = malloc(sizeof(int));
|
||||||
|
if(p == NULL)
|
||||||
|
{
|
||||||
|
Error("Could not allocate the memory\n");
|
||||||
|
Error("Quitting....\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/*some stuff to use p*/
|
||||||
|
Error("Could not allocate the memory\n");
|
||||||
|
Error("Quitting....\n");
|
||||||
|
Error(5);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
潜在的问题是:
|
||||||
|
|
||||||
|
Error函数中,如果输入的参数不是字符串,比如传一个整数5,5被转成一个内存地址,printf这时候访问就会出问题了。
|
||||||
|
$1 = 0x5 <error: Cannot access memory at address 0x5>
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
// FROM:http://www.gowrikumar.com/c/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char str[80];
|
||||||
|
scanf("%[^/n]",str);
|
||||||
|
printf("%s\n",str);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
char c;
|
||||||
|
scanf("%c",&c);
|
||||||
|
printf("%c\n",c);
|
||||||
|
|
||||||
|
scanf("%c",&c);
|
||||||
|
printf("%c\n",c);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
scanf("%c",&c) 和 scanf(" %c",&c) 区别
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
i = 10;
|
||||||
|
printf("i : %d\n",i);
|
||||||
|
printf("sizeof(i++) is: %d\n",sizeof(i++));
|
||||||
|
printf("i : %d\n",i);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// sizeof()不会对传入的表达式计算 ,所以后面的输入还是i:10
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||||
|
|
||||||
|
#define PrintInt(expr) printf("%s:%d\n",#expr,(expr))
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
/* The powers of 10 */
|
||||||
|
int pot[] = {
|
||||||
|
0001,
|
||||||
|
0010,
|
||||||
|
0100,
|
||||||
|
1000
|
||||||
|
};
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for(i=0;i<SIZEOF(pot);i++)
|
||||||
|
PrintInt(pot[i]);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以0大头的都是8进制数据
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
|
||||||
|
void OS_Solaris_print()
|
||||||
|
{
|
||||||
|
printf("Solaris - Sun Microsystems\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void OS_Windows_print()
|
||||||
|
{
|
||||||
|
printf("Windows - Microsoft\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
void OS_HP_UX_print()
|
||||||
|
{
|
||||||
|
printf("HP-UX - Hewlett Packard\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int num;
|
||||||
|
printf("Enter the number (1-3):\n");
|
||||||
|
scanf("%d",&num);
|
||||||
|
switch(num)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
OS_Solaris_print();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
OS_Windows_print();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
OS_HP_UX_print();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("Hmm! only 1-3 :-)\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
enum {false,true};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i=1;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
printf("%d\n",i);
|
||||||
|
i++;
|
||||||
|
if(i < 15)
|
||||||
|
continue;
|
||||||
|
}while(false);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
fprintf(stdout,"hello-out");
|
||||||
|
fprintf(stderr,"hello-err");
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
stdout 会缓冲输出
|
||||||
|
|
||||||
|
\n
|
||||||
|
fflush()
|
||||||
|
|
||||||
|
setbuf(stdout,NULL)
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
// FROM:http://www.gowrikumar.com/c/
|
||||||
|
#include <stdio.h>
|
||||||
|
#define f(a,b) a##b
|
||||||
|
#define g(a) #a
|
||||||
|
#define h(a) g(a)
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
printf("%s\n",h(f(1,2)));
|
||||||
|
//printf("%s\n",g(f(1,2)));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a=10;
|
||||||
|
switch(a)
|
||||||
|
{
|
||||||
|
case '1':
|
||||||
|
printf("ONE\n");
|
||||||
|
break;
|
||||||
|
case '2':
|
||||||
|
printf("TWO\n");
|
||||||
|
break;
|
||||||
|
a:
|
||||||
|
printf("NONE\n");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//什么也不输出, “default”这里随便什么都没有编译错误
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int* p;
|
||||||
|
p = (int*)malloc(sizeof(int));
|
||||||
|
*p = 10;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
float f=0.0f;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for(i=0;i<10;i++)
|
||||||
|
f = f + 0.1f;
|
||||||
|
|
||||||
|
if(f == 1.0f)
|
||||||
|
printf("f is 1.0 \n");
|
||||||
|
else
|
||||||
|
printf("f is NOT 1.0\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 浮点数判相等
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int a = (1,2);
|
||||||
|
printf("a : %d\n",a);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ,运算符优先级 最低 ,括号不能少
|
||||||
|
//
|
||||||
|
|
||||||
Reference in New Issue
Block a user