C/C++
文章平均质量分 56
czjuttsw
Learning, staying up to date, and working on the latest and greatest in languages and APIs is what keeps me excited.
展开
-
BMP文件右旋90度[c语言]
#include #include #include typedef unsigned short WORD;typedef unsigned int DWORD;typedef unsigned int LONG;#pragma pack(2)typedef struct tagBITMAPFILEHEADER{ WORD bfType; DWORD bfSiz原创 2013-12-19 17:15:39 · 6663 阅读 · 3 评论 -
Java 调用 C程序 之HelloWorld
在开始之前,请先下载一个C编译器(tcc)并将其添加到系统路径中。下载地址: http://mirror.veriportal.com/savannah//tinycc/tcc-0.9.25-win32-bin.zip 1. 编写Java源程序并编译: Java代码 public class Ja原创 2013-01-07 23:09:19 · 1205 阅读 · 0 评论 -
#include的两种形式
通常我们在使用include指令的时候,有两种形式:#include "file"#include 比如我们想导入I/0流头文件,我们是这样做的:#include 如果我们有一个Person.h头文件// Person.hclass Person{ char * name; int age ; //原创 2012-12-21 14:33:57 · 1764 阅读 · 0 评论 -
函数指针的用法
指针不仅可以用来指向基本类型的数据,还可以指向函数。如果我们有了函数指针,这样就可以将其作为参数传递给其它函数。其应用之一是回调函数。函数指针的声明原型(Declaration Prototype)typename (*pointer_name)(parameters...)既然指针是指向一个具体函数的,那么假设有一个简单的加法函数:int add(int a, i原创 2012-12-22 15:26:38 · 1917 阅读 · 1 评论 -
C语言之局部变量和全局变量
通俗的说,C语言是由声明和函数组成的。在函数之外声明的变量称之为全局变量(Global Variable),在函数内声明的变量称之为局部变量(Local Variable),还有一种情况,在函数内部如果存在嵌套花括号,则花括号里的变量也为局部变量。在源文件中,所有的函数都可以访问到全局变量。而局部变量只能在某个函数内使用(或者某个块)看几个简单的例子:首先是全局变量#i原创 2012-12-22 13:14:23 · 1321 阅读 · 0 评论 -
Union 共用体
共用体(Union)和结构体(strut)在结构上很相似:union union_type{ short s; int i; double d; char c[8];}my_union;不同的是共用体的成员占据同一块物理地址空间,因此成员之间不是独立的,其中一个成员的值改变可能会影响到其它成员的值。由于所有的成员占据同一块内存,因此一个共用体原创 2012-12-20 15:47:06 · 898 阅读 · 0 评论 -
C++ 动态内存分配
通常,我们在声明一个数组时,数组的元素个数是确定的,即是一个常数。在编译的时候,编译器会根据数组的大小分配好内存。如果我们需要在运行时决定数组的大小,比如接受用户的输入。此时,如果你试图用变量来代替常量,编译器则不允许通过。在C++中,提供了两个专有的操作符,new deletenew基本格式是:new data_type;new data_type[array_elemen原创 2012-12-20 14:05:50 · 1201 阅读 · 0 评论 -
i++,++i的区别
我们知道,i=i+1,可以写成i++或者++i。(类似地,-也适用于此),但是它们有什么区别呢?下面给出一个简单的例子:int main(){ int i = 1; int j = 1; i++; ++j; cout<<"i="<<i<<",j="<<j;}i=2,j=2请按任意键继续. . .该例子说明i和j确实自增了1下面再原创 2012-12-19 16:39:14 · 923 阅读 · 0 评论 -
goto关键字
在Java中,goto是保留字,但是并没有使用。Although reserved as a keyword in Java, goto is not used and has no function在C/C++中,goto的使用:int main(){ int countDown = 10; loop: cout<<countDown<<endl; --coun原创 2012-12-18 21:23:37 · 1508 阅读 · 0 评论 -
FIFO、LRU、OPT这三种置换算法的缺页次数
转载自:http://yinzhezq.blog.163.com/blog/static/1648628902010112961039187/考虑下述页面走向: 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6 当内存块数量分别为3时,试问FIFO、LRU、OPT这三种置换算法的缺页次数各是多少?转载 2012-12-26 23:45:21 · 14681 阅读 · 2 评论 -
Swap函数
一天,小明去一家IT公司应聘,面试官给他出了一个很简单的题目:请分别用C++,C#和Java实现一个参数为原始类型的交换函数。小明想了想,这还不简单,于是在草稿纸上画了起来:首先是C++:templatevoid swap(T* a,T* b){ T temp; temp = *a; *a = *b; *b = temp;}然后是C原创 2012-12-27 22:22:35 · 1768 阅读 · 0 评论 -
C++之钻石问题和解决方案(菱形继承问题)
在C++中,什么叫做钻石问题(也可以叫菱形继承问题),怎么避免它?下面的图表可以用来解释钻石问题。假设我们有类B和类C,它们都继承了相同的类A。另外我们还有类D,类D通过多重继承机制继承了类B和类C。因为上述图表的形状类似于钻石(或者菱形),因此这个问题被形象地称为钻石问题(菱形继承问题)。现在,我们将上面的图表翻译成具体的代码:翻译 2012-12-27 14:17:10 · 31958 阅读 · 5 评论 -
二维数组和二维指针的关系
对于一个二维数组a[m][n],虽然在内存中是线性排列的,但是我们可以想象成一个m行n列的二维矩阵。假设我们声明并初始化了一个整形二维数组a[4][3] int a[3][4] = {{1,2,3,4},{2,3,4,5},{4,5,6,7}};我们通过取值运算符得到每个元素的地址(&a[i][j]) for(int i=0; i<3;i++) { for(int j=0;j原创 2012-12-25 21:04:44 · 1299 阅读 · 0 评论 -
递归函数
递归函数(recursive function),说到底就是自己调用自己:void recurse(){ recurse(); //递归}该recurse函数的函数体中调用了自己,因此如果在main函数中调用该函数,该程序将会一直循环下去(有点类似于Loop),直到栈溢出(StackOverflow)。学过数据结构的同学都知道栈(Stack)是一种后进先出(LIFO)的数据结构,原创 2012-12-23 13:04:54 · 1837 阅读 · 0 评论 -
创建和遍历单向线性链表(C)
链表(Linked List)是由节点组成的(node)。而节点实质上是一个数据结构(struct or class)。链表和数组(Array)的区别在于链表中的节点在内存中的位置不一定是连续的,并且节点个数无需在编译时确定。节点包含两种信息,一种是数据(data),一种是指向另一个节点的指针。(指针实质上存储的是节点的物理位置)考虑一个单向线性链表(节点只有一个指针,原创 2012-12-23 15:17:20 · 2989 阅读 · 0 评论 -
BMP文件图像上下对称[C]
/** * Reverse bmp file * Author: Benson */#include "stdafx.h"#include #include void Symmetric(unsigned char* img,unsigned char* data,int lineSize,int h) { // 上下对称算法 for(int i =0; i < h; i++原创 2013-12-17 15:24:27 · 2682 阅读 · 0 评论