C/C++
文章平均质量分 61
shunshierweilk
这个作者很懒,什么都没留下…
展开
-
回顾一些小函数
1.实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。 #define _CRT_SECURE_NO_WARNINGS 1 #include void print_kjb(int line) { int i = 0; for (i = 1; i <=line; i++) { int j = 0; for (原创 2016-06-09 22:02:45 · 216 阅读 · 0 评论 -
面向对象封装
#include using namespace std; class Calc { private://声明私有成员,外部类不能访问 int sum; int diff; int pro; int que; public://声明公有方法,所有类都可以调用 void add(int num1, int num2) { sum =num1 + num2; printf("%d=原创 2016-06-09 22:01:57 · 177 阅读 · 0 评论 -
模板实现双向链表
#include #include using namespace std; template struct Node { public: Node(const T& d) :_next(NULL) , _prev(NULL) , _data(d) {} T _data; Node* _next; Node* _prev; }; template class DList {原创 2016-06-09 22:01:54 · 349 阅读 · 0 评论 -
模板实现顺序表
#include using namespace std; template class Seqlist { public: Seqlist() :_data(NULL) , _size(0) , _capacity(0) { CheckCapacity(); } ~Seqlist() { if (_data != NULL) {原创 2016-06-09 22:01:51 · 260 阅读 · 0 评论 -
候选人得票统计程序
#include using namespace std; struct Person { char name[20]; int count; }; int main() { Person leader[3] = { "Li", 0, "Zhang", 0, "Sun", 0 }; int i, j; char leader_name[20]; for (i = 0; i< 10; i++) {原创 2016-06-09 22:01:48 · 3088 阅读 · 1 评论 -
双向链表的几种实现
#pragma once #include using namespace std; typedef int DataType; //结构体的定义 struct Node { Node(const DataType& d) :_data(d) , _prev(NULL) , _next(NULL) {} Node* _next; Node* _prev; DataType _dat原创 2016-06-09 22:01:45 · 281 阅读 · 0 评论 -
单链表的相关实现
头文件SList.h #define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; #pragma once typedef int DataType; struct Node { Node(const DataType& d) :_data(d) , _next(NULL) {} DataType _原创 2016-06-09 22:01:42 · 185 阅读 · 0 评论 -
两个节点的最近公共节点
二叉树中 找两个结点的最近的公共祖先结点#pragma once #include using namespace std; struct Node { Node * left; Node * right; int value; Node( int v )原创 2016-08-22 21:20:01 · 677 阅读 · 0 评论 -
函数的递归与嵌套
有4个人,丁比丙大4岁,丙比乙大4岁,乙比甲大4岁,甲说他的年龄是16岁,求丙的年龄?#define _CRT_SECURE_NO_WARNINGS 1 #include using namespace std; int Get_age(int n); const int interval = 4; void main() { int age = Get_age(4); cout << "丁的年龄原创 2016-06-09 22:02:11 · 681 阅读 · 0 评论 -
保护继承
#define _CRT_SECURE_NO_WARNINGS 1 #include using namespace std; class CBase1//定义基类 { public://定义公有型成员 void SetXY(int a, int b) { x1 = a; y1 = b; num1 = 10; } void Show() { cout << "在基类CBase1中,公有型变量y1的原创 2016-06-09 22:02:00 · 208 阅读 · 0 评论 -
栈的应用 逆波兰式
#define _CRT_SECURE_NO_WARNINGS 1 #pragma once #include #include #include using namespace std; enum Type { OP_NUM, OP_SYMBOL, }; enum SYMBOL { ADD, SUB, MUL, DIV, }; struct Cell { Type _type原创 2016-06-09 22:02:05 · 278 阅读 · 0 评论 -
多态性与虚函数
多态性:向不同的对象发送同一个消息,不同的对象在接受时会产生不同的行为(即方法) #include using namespace std; class B { public: void print() { cout << "B::print()" << endl; } virtual void show(); }; void B::show() { cout << "B::show()"原创 2016-06-09 22:02:39 · 408 阅读 · 0 评论 -
类和对象
三种访问限定符 public成员可从类外部直接访问,private/protected成员不能从类外部直接访问。每个限定符在类体中可使用多次,它的作用域是从该限定符出现开始到下个限定符 之前或类体结束前。类体中如果没有定义限定符,则默认为私有的。类的访问限定符体现了向对象的封装性。 #define _CRT_SECURE_NO_WARNINGS 1 #include using namespace原创 2016-06-09 22:02:34 · 196 阅读 · 0 评论 -
结构体内存对齐
1.原因:为什么需要内存对齐.1、平台原因(移植原因):不是所有的硬件平台都能访问任意地址上的任意数据的;某些硬件平台只能在某些地址处取某些特定类型的数据,否则抛出硬件异常。2、性能原因:数据结构(尤其是栈)应该尽可能地在自然边界上对齐。原因在于,为了访问未对齐的内存,处理器需要作两次内存访问;而对齐的内存访问仅需要一次访问。2.内存对齐的规则和范例讲述内存对齐之前先看下各种类型的大小,和编译器以原创 2016-06-09 22:02:31 · 304 阅读 · 0 评论 -
“打擂台'找最大数
遇到比较多的数据时,我们通常采用"打擂台"的方法处理#define _CRT_SECURE_NO_WARNINGS 1 #include int main() { int a, b, c, d, max; scanf("%d%d%d%d", &a, &b, &c, &d); max = a; if (b > max) max = b; if (c > max) max = c; if (d > m原创 2016-06-09 22:02:28 · 2721 阅读 · 0 评论 -
广义表
广义表是非线性的结构,是线性表的一种扩展,是有n个元素组成有限序列。 广义表的定义是递归的,因为在表的描述中又得到了表,允许表中有表#define _CRT_SECURE_NO_WARNINGS 1 #include using namespace std; #include enum Type { HEAD, //头节点 VALUE, //值节点 SUB, //子表节点 };原创 2016-06-09 22:02:25 · 357 阅读 · 0 评论 -
类和对象的简单应用
#define _CRT_SECURE_NO_WARNINGS 1 #include using namespace std; class Time { public: int hour; int minute; int sec; }; void set_time(Time& t) //定义函数set_time,形参t是引用变量 { cin >> t.hour; //输入原创 2016-06-09 22:02:22 · 263 阅读 · 0 评论 -
利用枚举类型实现统计
口袋中有5种颜色的小球,每次从口袋中任意取3个,问得到3种不同颜色的小球的可能取法?#define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; int main() { enum color{red,yellow,blue,white,black};//声明枚举类型color color pri; //定义原创 2016-06-09 22:02:14 · 252 阅读 · 0 评论 -
指向结构体变量的指针引用结构体变量的成员
#define _CRT_SECURE_NO_WARNINGS 1 #include #include using namespace std; struct Student { int num; string name; char sex; float score; }; int main() { Student stu; Student *p = &stu; stu.num =原创 2016-06-09 22:02:08 · 1359 阅读 · 0 评论 -
C语言顺序表的几种实现
#include #include #include using namespace std; typedef int DataType; #define DEFAULT_INC 9 #define DEFAULT_CAPACITY 7 class SeqList { public: friend ostream& operator<<(ostream& os, const SeqList&原创 2016-06-09 22:01:39 · 321 阅读 · 0 评论