存档

文章标签 ‘C语言’

Devcpp(Dev C++)使用说明及技巧

2009年1月13日 Slyar 9 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

本文仅适合初学程序设计的人学习,菜鸟以上级别请飘过...

Dev-C++是一个Windows下的C和C++程序的集成开发环境。它使用MingW32/GCC编译器,遵循C/C++标准。开发环境包括多页面窗口、工程编辑器以及调试器等,在工程编辑器中集合了编辑器、编译器、连接程序和执行程序,提供高亮度语法显示的,以减少编辑错误,还有完善的调试功能,能够适合初学者与编程高手的不同需求,是学习C或C++的首选开发工具!多国语言版中包含简繁体中文语言界面及技巧提示,还有英语、俄语、法语、德语、意大利语等二十多个国家和地区语言提供选择。

阅读全文...

C语言标准库函数 qsort 详解

2008年12月31日 Slyar 2 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

qsort包含在<stdlib.h>头文件中,此函数根据你给的比较条件进行快速排序,通过指针移动实现排序。排序之后的结果仍然放在原数组中。使用qsort函数必须自己写一个比较函数。

函数原型:

void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

用法以及参数说明:

Sorts the num elements of the array pointed by base, each element size bytes long, using the comparator function to determine the order.

The sorting algorithm used by this function compares pairs of values by calling the specified comparator function with two pointers to elements of the array.

The function does not return any value, but modifies the content of the array pointed by base reordering its elements to the newly sorted order.

base Pointer to the first element of the array to be sorted.(数组起始地址)
num Number of elements in the array pointed by base.(数组元素个数)
size Size in bytes of each element in the array.(每一个元素的大小)
comparator Function that compares two elements.(函数指针,指向比较函数)
1、The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.
2、The return value of this function should represent whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.
Return Value none (无返回值)
阅读全文...

分类: 编程相关 标签: ,

格雷码(Gray Code) C语言 DFS版

2008年12月30日 Slyar 1 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

Description

Gray code is an interesting code sequence and has many applications in computer science. No matter you have known it before or not, here are some introductions about its features:
(1) Gray code has 2^n unique elements;
(2) Each element contains n digits of 0 or 1;
(3) Each pair of adjacent elements has exactly one different digit.
For example, when n=2, one of the gray code sequence is: 00,01,11,10.
Now, the task is quite simple, given a positive integer n, generate the corresponding Gray code sequence.

Input

Input may contain multiple test cases. Each test case consists of one positive integer n(n<=16), Input is terminated by a case with n=0, which should not be processed.

Output

For each test case, output the corresponding Gray code sequence, one element per line. There may be multiple answers, any of them will be accepted. Please output a blank line after each test case.

Tips:注释很重要,很久以前的程序不加注释很容易看不懂...

输出N位前2^N个格雷码,因为每次只改变相邻的一个数,可以使用深度优先搜索,每次改变节点就好了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
 
int n;
char s[17];
 
void dfs(int i)
{
    if(i==n) printf("%s\n",s); //到达底部就输出
    else
    {
        dfs(i+1); //深度优先搜索
        if(s[i]=='0') s[i]='1';
        else s[i]='0'; //把当前位逆转
        dfs(i+1); //继续向下搜索
    }
}
 
int main()
{
    int i;
    while(1)
    {
        scanf("%d",&n);
        if(!n) break;
        for(i=0;i<17;i++) s[i]='0';
        s[n]='\0';
        dfs(0);
        printf("\n");
    }
    system("pause");
    return 0;
}
分类: 编程相关 标签: ,

求N个正整数的最小公倍数 C语言版

2008年12月17日 Slyar 6 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

Description

有N个正整数,它们的最小公倍数是多少?

Input

数据包含两行,第一行是一个整数N (1<=N<=10),第二行包含n个不超过10^9正整数。

Output

输出N个数的最小公倍数。

Tips:想了一下午才想到一个“相对比较高效”的算法,核心还是这个公式:两数乘积=两数最大公约数*两数最小公倍数。

只不过这次用到的是 两数最小公倍数=两数乘积/两数最大公约数。每两个数个一组依次向后求就可以了,嘿嘿~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
 
int slyar(int x,int y) //计算两个数的最大公约数
{
    while(x>y?(x%=y):(y%=x));
    return x+y;
}
 
int main()
{
    int i,n,a[100],sum;
    scanf("%d",&n);
    for(i=0;i<n;i++) scanf("%d",&a[i]);
    sum=a[0];
    for(i=1;i<n;i++) sum=sum*(a[i]/slyar(a[i],sum));
    printf("%d\n",sum);
    return 0;
}
分类: 编程相关 标签: ,

C语言 单引号和双引号的区别

2008年12月16日 Slyar 2 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

最近的C语言课在教字符串,貌似N多同学搞不清楚单引号和双引号的区别,有人还以为在C语言里用哪个都可以...其实C语言中的单引号和双引号含义是一点也不一样滴...

1、含义不同。

用单引号引起的一个字符实际上代表一个整数,整数值对应于该字符在编译器采用的字符集中的序列值。而一般我们的编译器采用的都是ASCII字符集。因此's'的含义其实和十进制数115的含义是一致的。

而用双引号引起的字符串,代表的是一个指向无名数组起始字符的指针。

2、大小不同。

用单引号引起的一个字符大小就是一个字节。

而用双引号引起的字符串大小是字符的总大小+1,因为用双引号引起的字符串会在字符串末尾添加一个二进制为0的字符'\0'。

分类: 编程相关 标签:

求二叉树的后序遍历 C语言 数组实现

2008年12月15日 Slyar 没有评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

已知二叉树的前序遍历和后序遍历,求二叉树的后序遍历。算法很简单,由前序遍历的第一个元素可确定左、右子树的根节点,参照中序遍历又可进一步确定子树的左、右子树元素。如此递归地参照两个遍历序列,最终构造出二叉树。自己写了一段C语言的数组版,加了注释,应该很好理解。

由二叉树的前序遍历和中序遍历序列能唯一确定一棵二叉树。

前序遍历为:访问根节点、访问左子树、访问右子树;

中序遍历为:访问左子树、访问根节点、访问右子树。

后序遍历为:访问左子树、访问右子树、访问根节点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <string.h>
 
char preord[100],inord[100];
 
void rebuild(int preleft,int preright,int inleft,int inright)
{
    int root,leftsize,rightsize;
 
    if(preleft<=preright&&inleft<=inright)
    {
        //在中序遍历中寻找根节点
        for(root=inleft;root<=inright;root++)
        {
            if(preord[preleft]==inord[root]) break;
        }
 
        //计算左右子树大小
        leftsize=root-inleft;
        rightsize=inright-root;
 
        //如果有左子树则递归重建左子树
        if(leftsize>0)
        {
            rebuild(preleft+1,preleft+leftsize,inleft,root-1);
        }
 
        //如果有右子树则递归重建右子树
        if(rightsize>0)
        {
            rebuild(preleft+leftsize+1,preright,root+1,inright);
        }
 
        //如果无子树则打印根节点
        printf("%c",inord[root]);
    }
}
 
int main()
{
    int length;
    scanf("%s%s",preord,inord);
    length=strlen(preord)-1;
    rebuild(0,length,0,length);
    return 0;
}
分类: 编程相关 标签:

C语言中 NULL和NUL的区别

2008年12月14日 Slyar 没有评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

《C专家编程》里面提到了"The One 'l' nul and the Two 'l' null",网上查了一下,得到了一个更详细的区分。

NULL is a macro defined in several standard headers, 0 is an integer constant, '\0' is a character constant, and nul is the name of the character constant. All of these are not interchangeable:

NULL is to be used for pointers only since it may be defined as ((void *)0), this would cause problems with anything but pointers.

0 can be used anywhere, it is the generic symbol for each type's zero value and the compiler will sort things out.

'\0' should be used only in a character context.

nul is not defined in C or C++, it shouldn't be used unless you define it yourself in a suitable manner, like:

#define nul '\0'

翻译一下就是:

NULL被大量定义在标准头文件中,0是一个整型常量,'\0'是一个字符常量,而NUL是一个字符常量的名字。这几个术语都不可互换。

1、NULL用于表示什么也不指向,也就是空指针((void *)0)

2、0可以被用于任何地方,它是表示各种类型零值的符号并且编译器会挑出它

3、'\0'应该只被用于结束字符串

4、NUL没有被定义于C和C++,它不应该被使用除非你自己定义它,像:#define nul '\0'

分类: 编程相关 标签: ,

C语言 全局变量和局部变量的大小限制

2008年12月12日 Slyar 8 条评论

文章作者:Slyar 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

下午做最长公共子序列的时候遇到的问题,问了felix后恍然大悟...看代码

#include <stdio.h>
int main(){
int a[1000000];//局部变量
return 0;
}

编译运行后发现溢出错误。

#include <stdio.h>
int a[1000000];//全局变量
int main(){
return 0;
}

编译运行后正常。

在解释原因前我们先看一下一个由C/C++编译的程序占用的内存分为几个部分:

1、栈区(stack sagment):由编译器自动分配释放,存放函数的参数的值,局部变量的值等。在Windows下,栈是向低地址扩展的数据结构,是一块连续的内存的区域。这句话的意思是栈顶的地址和栈的最大容量是系统预先规定好的,在WINDOWS下,栈的大小是2M(也有的是1M,总之是一个编译时就确定的常数),如果申请的空间超过栈的剩余空间时,将提示overflow。因此,能从栈获得的空间较小。

2、堆区(heap sagment) : 一般由程序员分配释放,若程序员不释放,程序结束时可能由系统回收 。它与数据结构中的堆是两回事。堆是向高地址扩展的数据结构,是不连续的内存区域。这是由于系统是用链表来存储的空闲内存地址的,自然是不连续的,而链表的遍历方向是由低地址向高地址。堆的大小受限于计算机系统中有效的虚拟内存。由此可见,堆获得的空间比较灵活,也比较大。

3、全局区(静态区)(data sagment):全局变量和静态变量的存储区域是在一起的,程序结束后由系统释放。数据区的大小由系统限定,一般很大。

4、文字常量区:常量字符串就是放在这里的, 程序结束后由系统释放。

5、程序代码区:存放函数体的二进制代码。

综上所述,局部变量空间是很小的,我们开一个a[1000000]就会导致栈溢出;而全局变量空间在Win 32bit 下可以达到4GB,因此不会溢出。

分类: 编程相关 标签: ,
bnuep:0801010047