C++ 136

C++로 쉽게 풀어쓴 자료구조 프로그래밍 프로젝트 2(2)

[SparsePoly.h]/*[심화문제] 희소 다항식을 위한 클래스 SparseMatrix를 구현하라 (1) 각 항을 나타내는 Term 클래스를 설계하고 구현해라(2) SparseMatrix 클래스를 설계하고 다음 멤버함수를 구현하라 i) void read(); //희소 다항식 입력 함수 ii) void add(SparsePoly a, SparsePoly b); //c.add(a, b)는 c=a+b iii)void display(char *str="SPoly = "); //희소다항식 출력 함수*/#include using namespace std; #define MAX_TERMS 20 struct Term //하나의 항을 표현하는 구조체{ int expn; //항의 지수 float coef; //항의 계..

C++/C++로 쉽게 풀어쓴 자료구조 프로그래밍 프로젝트 2(1)

[Polynomial.h]/*다항식 클래스의 C++ 구현프로그램 2.7의 닿항식 클래스를 다음과 같이 확장ㅎ하라.(1) 두 다항식 a와 b의 뺄셈을 구하는 멤버 함수 sub를 구현하라(2) 두 닿항식의 곱셈을 구하는 멤버 함수 mult를 구현하라(3) 다항식의 연산결과 최고차항의 계수가 0으로 변할 수 있다. 현재 다항식의 계수를 분석해 최고차항의 계수가 0이 아닌 값이 나오도록 다항식의 속성 값들을 변경하는 멤버 함수 trim을 구현하라(4) 다항식의 출력 함수 display를 수정하여 계수가 0인 항은 출력되지 않도록 변경하라. 또한 계수가 1인 경우는 게수 1을 출력하지 않도록 변경하라*/#include using namespace std; #define MAX_DEGREE 80 //다항식의 처리 ..

C++로 쉽게 풀어쓴 자료구조 2장 연습문제

[3번]/*크기가 10인 배열 two[]를 선언하고 여기에 2의 제곱 값들을 저장해보자.즉 배열의 첫 번째 요소에는 2^0을 저장하고 두 번째 요소에는 2^1값을 저장한다.마지막 요소에는 2^9값을 저장한다.for 루프를 이용하여 two[] 배열의 전체 요소의 값을 출력하는 프로그램을 작성한다*/#include using namespace std; void Initialize(int two[10]); //초기화void Display(int two[10]); //출력 int main(void){ int two[10]; Initialize(two); Display(two); return 0;} void Initialize(int two[10]){ for (int i = 0; i < 10; i++) { int..

C++ Fundamentals of Data Structures(C++ 자료구조론) 6.3 Kruskal's Algorithm 예제

/*Kruskal's Algorithm using disjoint set집합을 이용한 크루스칼 알고리즘*/#include using namespace std; int Find(int v2, int parent[]) //최종부모 찾기{ while (parent[v2] != v2) { v2 = parent[v2]; } return v2;} void Union(int i, int j, int parent[]) //집합{ if (i < j) parent[j] = i; //j의 부모는 i else parent[i] = j; //i의 부모는 j} class Kruskal{private: typedef struct _graph { int v1; //정점 1 int v2; //정점 2 int weight; //가중치 ..

C++로 쉽게 풀어쓴 자료구조 프로그래밍 프로젝트 1

/*1부터 n까지의 합을 구하는 방법은 다음과 같이 3가지가 있다 알고리즘 A: sum=n(n+1)/2 공식 사용알고리즘 B: sum=1+2+...+n알고리즘 C: sum=(0)+(1)+(1+1)+(1+1+1)+...+(1+1+...+1) 각 알고리즘을 함수로 구현하라. n을 매개변수로 전달받고 결과를 반환한다n에 대해 각 함수를 호출하여 세 알고리즘의 계산결과가 동일함을 확인하라.*/#include #include #include using namespace std; int sumAlgorithmA(int n);int sumAlgorithmB(int n);int sumAlgorithmC(int n); int main(void){ clock_t start, end; double duration; //걸린..

C++ Fundamentals of Data Structures(C++ 자료구조론) 6.2 Elementary Graph Operations(기본적인 그래프 연산들) 예제

/*Elementary Graph Operations, 기본적인 그래프 연산들(DFS, BFS, Components, DfnLow)*/#include #include //BFS 위해using namespace std; class Chain;class ChainIterator; class ChainNode{private: int data; ChainNode *link;public: ChainNode(int num) :data(num) { link = NULL; } friend Chain; friend ChainIterator;}; class Chain{private: ChainNode *first;public: Chain() { first = NULL; } void Insert(int num) { Chai..

C++ Fundamentals of Data Structures(C++ 자료구조론) 6.1 AdjacencyList Graph(인접리스트 그래프) 예제

/*간단한 인접리스트 그래프, Simple AdjacencyList Graph*/#include using namespace std; class ChainNode{private: int data; ChainNode *link;public: ChainNode(int element = 0, ChainNode *next = NULL) :data(element), link(next) { } int getData() { return data; } ChainNode *Link() { return link; } friend class AdjList; friend class LinkedGraph; friend ostream &operator

C++ Fundamentals of Data Structures(C++ 자료구조론) 5.10 DisjointSets(집합) 예제

/*DisjointSets, 집합수정날짜:9/8*/#include using namespace std; class Sets{private: int *parent; int n; //집합의 요소 수public: Sets(int size = 10) { n = size; parent = new int[size + 1]; //[0]은 사용하지 않는다 for (int i = 1; i parent[j]) //i가 더 적은 노드를 갖고 있을 때(음수이므로 이렇게 계산) { parent[i] = j; parent[j] = temp; } else { parent[j] = i; parent[j] = temp; } } int CollapsingFind(int i) //i를 포함하는 집합의 루트를 찾는다(SimpleFind의 ..

C++ Fundamentals of Data Structures(C++ 자료구조론) 5.8 WinnerTree(승자 트리) 예제

/*토너먼트 트리(Winner Tree A.K.A Tournament Tree)*/#include using namespace std; struct player{ int id, key; //id: 몇번째 플레이어, key: 값 operator int() const { return key; }}; templateclass winnerTree{public: virtual ~winnerTree() {} virtual void initialize(T *thePlayer, int theNumberOfPlayers) = 0; // theNumberOfPlayers만큼의 노드를 가진 승자트리 생성 virtual int winner() const = 0; // 승자노드의 인덱스 반환 virtual void rePlay..

C++ Fundamentals of Data Structures(C++ 자료구조론) 5.7 BinarySearchTree(이진탐색트리) 예제

/*이진탐색트리, Binary Search Tree*/#include #include #include using namespace std; template class Dictionary{public: virtual bool IsEmpty() const = 0; virtual pair *Get(const K&) const = 0; //특정 키를 반환 virtual void Insert(const pair&) = 0; virtual void Delete(const K&) = 0;}; template class TreeNode{public: T data; TreeNode *leftChild; TreeNode *rightChild; TreeNode(T temp) :data(temp), leftChild(NULL..