随笔分类 -  C/C++

转载两个C程序
摘要:代码非原创,先记下来:1、播放影音(VC6下通过)#include <windows.h> #include <stdio.h> #include <mmsystem.h> #include <shellapi.h> #include <AFXCOM_.H> #pragma comment(lib,"winmm.lib") void main() { char str[128]={0}; int i = 0; char buf[128]={0}; MCI_OPEN_PARMS mciOpen; MCIERROR m 阅读全文

posted @ 2012-05-18 21:14 c语言源码 阅读(294) 评论(0) 推荐(0)

acm 血泪教训
摘要:http://icpc.ahu.edu.cn/OJ/Problem.aspx?id=231#include <iostream> #include <algorithm> using namespace std; int arr[10005]; unsigned long long Fun(int N) { sort(arr, arr + N); unsigned long long ans = 0; for (int i = 0; i < N - 1; i++) { ans += (arr[i + 1] - arr[i]) * (i + 1) * (N - i 阅读全文

posted @ 2012-05-13 19:11 c语言源码 阅读(223) 评论(0) 推荐(0)

priority_queue POJ 3253 Fence Repair
摘要:#include <iostream> #include <queue> using namespace std; int main(void) { priority_queue<double> q; q.push(66.6); q.push(22.2); q.push(44.4); cout << q.top() << ' '; q.pop(); cout << q.top() << endl; q.pop(); q.push(11.1); q.push(55.5); q.push(33.3) 阅读全文

posted @ 2012-05-12 22:54 c语言源码 阅读(184) 评论(0) 推荐(0)

C++内存管理
摘要:[导语]内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不在,内存泄漏几乎在每个C++程序中都会发生,因此要想成为C++高手,内存管理一关是必须要过的,除非放弃C++,转到Java或者.NET,他们的内存管理基本是自动的,当然你也放弃了自由和对内存的支配权,还放弃了C++超绝的性能。本期专题将从内存管理、内存泄漏、内存回收这三个方面来探讨C++内存管理问题。1内存管理伟大的Bill Gates曾经失言: 640K ought to be enough f. 阅读全文

posted @ 2012-05-05 00:14 c语言源码 阅读(506) 评论(0) 推荐(0)

Big Three
摘要:定义一个有序数组类来说明这个问题:#include <iostream> using namespace std; #define MAXSIZE 20 class A { public: A(void); // 构造函数 A (const A &); // 复制构造函数 A & operator= (const A &); // 重载赋值运算符 ~A() { // 析构函数 cout << "析构函数" << endl; } void Display(void); int Size(void); bool IsE 阅读全文

posted @ 2012-05-01 18:01 c语言源码 阅读(246) 评论(0) 推荐(0)

动态分配的标准写法(new, delete; malloc, free)
摘要:1、可能有的人涉及到动态分配只用到下面的这句:#include <iostream> using namespace std; int main(void) { int *p; p = new int(1); // 1 cout << *p << endl; int *q; q = (int *)malloc(sizeof(int)); *q = 1; cout << *q << endl; return 0; }漏洞百出,不想多说。(补充一句,上面用的是new的plain new用法)2、自己总结了一下动态分配的写法(当然还有其他写 阅读全文

posted @ 2012-04-30 12:15 c语言源码 阅读(207) 评论(0) 推荐(0)

计算个税(定义一个薪水类)
摘要:(起征点800):#include <iostream> using namespace std; #define TAX_THRESHOLD 800 struct Tax { double standard; double tax_rate; }; class Salary { double income; public: static Tax tax_array[]; Salary(int m = 0) { income = m; } void operator - (int payout) { income -= payout; cout << "工资余 阅读全文

posted @ 2012-04-24 19:39 c语言源码 阅读(318) 评论(0) 推荐(0)

const 的用法(持续更新)
摘要:先上一段代码:#include <iostream> using namespace std; int main(void) { int a = 1; const int *p = NULL; p = &a; int *q = p; // 错误 // error C2440: 'initializing' : cannot convert from 'const int *' to 'int *' // Conversion loses qualifiers q = &a; // (1) q = (int *)p; / 阅读全文

posted @ 2012-04-22 22:10 c语言源码 阅读(189) 评论(0) 推荐(0)

函数指针
摘要:1、常见的用法#include <stdio.h> typedef int (*PFUN)(int, int); // PFUN 是函数指针类型 int fun(int a, int b) { return a + b; } int main(void) { PFUN pf = fun; // 或 PFUN pf = &fun; printf("%d\n", pf(1, 2)); printf("%d\n", (*pf)(1, 2)); return 0; }2、第二种用法#include <stdio.h> typede 阅读全文

posted @ 2012-04-14 14:50 c语言源码 阅读(202) 评论(0) 推荐(0)

文字常量区,字符串常量
摘要:可以:#include <stdio.h> int main(void) { char str[8] = {0}; str[0] = *"jiang"; printf("%s\n", str); return 0; }2012/5/16 更新补充一个例子(正确):#include <iostream> using namespace std; int main(void) { cout << "0123456789"[5] << endl; return 0; }输出结果是5 阅读全文

posted @ 2012-03-31 00:50 c语言源码 阅读(196) 评论(0) 推荐(0)

为什么 C++不叫作++C? o(∩_∩)o
摘要:【答】 C++之名是 Rick Mascitti 在 1983 年夏天定名的(参见 The C++ Programming Language(Special Edition) 1.4节) ,C说明它本质上是从 C语言演化而来的,“++”是 C语言的自增操作符。C++语言是 C 语言的超集,是在 C 语言基础上进行的扩展(引入了 new、delete 等 C语言中没有的操作符,增加了对面向对象程序设计的直接支持,等等) ,是先有C 语言,再进行++。根据自增操作符前、后置形式的差别(参见习题 5.15 的解答) ,C++表示对 C语言进行扩展之后,还可以使用 C语言的内容;而写成++C则表示无法 阅读全文

posted @ 2012-03-30 18:45 c语言源码 阅读(291) 评论(0) 推荐(0)

indent 用法
摘要:indent是一个很有用的c源代码对齐工具。一般大家有自己喜欢的风格,可以根据需要来设定indent的风格。Linux Style是:-nbad -bap -nbc -bbo -hnl -br -brs -c33 -cd33 -ncdb -ce -ci4-cli0 -d0 -di1 -nfc1 -i8 -ip0 -l80 -lp -npcs -nprs -npsl -sai -saf -saw-ncs -nsc -sob -nfca -cp33 -ss -ts8 -il1我采用的是类Linux Style:indent -bad -bap -nbc -bbo -hnl -br -brs -c3 阅读全文

posted @ 2012-03-10 23:51 c语言源码 阅读(649) 评论(0) 推荐(0)

sizeof 那点破事
摘要:2012年Google的一道面试题:运行下图中的C++代码,输出是什么?int SizeOf(char pString[]) { return sizeof(pString); } int _tmain(int argc, _TCHAR* argv[]) { char* pString1 = "google"; int size1 = sizeof(pString1); int size2 = sizeof(*pString1); char pString2[100] = "google"; int si... 阅读全文

posted @ 2012-02-26 00:35 c语言源码 阅读(502) 评论(0) 推荐(0)

等比数列的和
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=3411http://hi.baidu.com/aekdycoin/blog/item/63f633ec11c822d8b21cb168.html 阅读全文

posted @ 2011-05-17 00:33 c语言源码 阅读(344) 评论(0) 推荐(0)

导航