数据结构 第一章 绪论
2016-03-08 18:09
330 查看
程序构成说明
教材涉及的存储结构,c开头.h扩展名,c1-1.h是第1章第1种存储结构。存储结构的基本操作,bo开头.cpp扩展名。
调用基本操作的主程序,mian开头。
实现算法的程序,algo开头。
本章程序包含的文件说明
Header.h头文件包含OK,ERROR等函数结构状态代码,Status类型,常用头文件。C1-1.h头文件采用动态分配顺序存储结构。
Bo1-1.cpp是抽象数据类型Triplet和ElemType的8个基本操作函数,包含如下
1.InitTriplet():构建三元组。
2.DestoryTriplet():销毁三元组
3.Get():从三元组中得到第i个元素值。
4.Put():改变第i个元素值为e。
5.IsAscending():是否升序排列。
6.IsDescending():是否降序排列。
7.Max():返回最大值。
8.Min():返回最小值。
Main1-1.cpp是检验bo1-1.cpp各项操作是否正确的主函数。
编译需要注意的一点
当bo1-1.cpp加入到项目中,而且main文件中包含#include “bo1-1.cpp”,编译会在bo1-1.cpp中出错。解决方案:将bo1-1.cpp移除项目,即可以编译通过。因为项目中所有的.cpp文件都会编译一次,生成一个.obj文件,然后所有的.obj文件链接起来就生成了可执行的程序。bo1-1.cpp源程序不完整,故不能编译通过,即使加入在bo1-1.cpp中加入头文件使之完整,而在main.cpp中包含了bo1-1.cpp,会出现bo1-1.obj重定义错误。
调试
F9在需要运行到的行设置断点,F5是运行到断点位置,F10不进入函数单步逐过程,F11进入函数单步逐语句,SHIFT+F11跳出F11进入的函数块,CTRL+F10运行到光标位置。源码(4个)
1.header.h
/************************************ FileName: Header.h Author: Zhengjun Zhao Version: V1.0 Date: 2016/3/3 Description: This file include the common header file, the condition code of function results and the data type. Ohter: NULL History: 1.Date: Author: Modification: /************************************/ // common header file #include <string.h> #include <ctype.h> #include <malloc.h> // molloc() #include <limits.h> // INT_MAX #include <stdio.h> // EOF #include <stdlib.h> // atoi() #include <io.h> // eof() #include <math.h> // floor(), ceil(), abs() #include <process.h> // exit() #include <iostream> // cout, cin // the condition code of function results #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 // the data type typedef int Status; // Status is the function type, and the value of the function results // is the condition code typedef int Boolean; using namespace std;
2.c1-1.h
// c1-1.h typedef ElemType * Triplet; // InitTriplet allocate three data storage space这里写代码片
3.bo1-1.cpp
/************************************ FileName: bo1-1.cpp Author: Zhengjun Zhao Version: V1.0 Date: 2016/3/3 Description: This file is the example 1-7 in the book, include 8 operations Ohter: NULL History: 1.Date: Author: Modification: /************************************/ Status InitTriplet(Triplet &T, ElemType v1, ElemType v2, ElemType v3) { // create a triplet datastruct if (!(T = (ElemType *)malloc(3 * sizeof(ElemType)))) exit(OVERFLOW); T[0] = v1; T[1] = v2; T[2] = v3; return OK; } Status DestoryTriplet(Triplet &T) { // destory the triplet free(T); T = NULL; return OK; } Status Get(Triplet T, int i, ElemType &e) { // get the value of the ith from triplet to e if (i<1 || i>3) { return ERROR; } e = T[i - 1]; return OK; } Status Put(Triplet &T, int i, ElemType e) { // change the value of the ith from triplet into e if (i<1 || i>3) { return ERROR; } T[i - 1] = e; return OK; } Status IsAscending(Triplet T) { // if the triplet is ascending,return OK return (T[0] < T[1] && T[1] < T[2]); } Status IsDescending(Triplet T) { // if the triplet is descending,return OK return (T[0] > T[1] && T[1] > T[2]); } Status Max(Triplet T, ElemType &e) { // e equal the max of triplet e = (T[0] >= T[1] ? (T[0] >= T[2] ? T[0] : T[2]) : (T[1] >= T[2] ? T[1] : T[2])); return OK; } Status Min(Triplet T, ElemType &e) { // e equal the min of triplet e = (T[0] <= T[1] ? (T[0] <= T[2] ? T[0] : T[2]) : (T[1] <= T[2] ? T[1] : T[2])); return OK; }
4.main1-1.cpp
/************************************ FileName: main1-1.cpp Author: Zhengjun Zhao Version: V1.0 Date: 2016/3/5 Description: bo1-1.cpp is verified by correct this file Ohter: NULL History: 1.Date: Author: Modification: /************************************/ #include "Header.h" typedef int ElemType; #include "c1-1.h" #include "bo1-1.cpp" using namespace std; void main() { Triplet T; ElemType m; Status i; i = InitTriplet(T, 5, 7, 9); cout << "init success"; cout << " T[0] = " << T[0] << "\n T[1] = " << T[1] << "\n T[2] = " << T[2] << endl; Get(T, 2, i); cout << " T[1] = " << i << endl; Put(T, 1, 3); cout << " T[0] = " << T[0] << endl; cout << " T[0] = " << T[0] << "\n T[1] = " << T[1] << "\n T[2] = " << T[2] << endl; i = IsAscending(T); cout << i << endl; i = IsDescending(T); cout << i << endl; Max(T, i); cout << "max = " << i << endl; Min(T, i); cout << "min = " << i << endl; DestoryTriplet(T); }
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 数据结构之Treap详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)