高级农民
- 积分
- 1241
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-12-28
- 最后登录
- 1970-1-1
|
AdjList.h- #ifndef _ADJLIST_H_
- #define _ADJLIST_H_
- #include <vector>
- class AdjList {
- private:
- std::vector<int>* myAdj;
- int myVertexNum;
- void destory();
- void copyAll(const AdjList &that);
- public:
- AdjList(const int vertexNum);
- AdjList(const AdjList &that);
- const AdjList & operator=(const AdjList &that);
- ~AdjList();
- void addEdge(const int v, const int w);
- void getAdj(const int v, std::vector<int> &adj) const;
- void print() const;
- };
- #endif
复制代码 AdjList.cpp- #include <iostream>
- #include <cassert>
- #include <vector>
- #include "AdjList.h"
- void AdjList::destory() {
- delete [] myAdj;
- myAdj = 0;
- myVertexNum = 0;
- }
- void AdjList::copyAll(const AdjList &that) {
- myVertexNum = that.myVertexNum;
- myAdj = new std::vector<int> [myVertexNum];
- for (int i = 0; i < myVertexNum; ++i) {
- myAdj[i] = that.myAdj[i];
- }
- }
- AdjList::AdjList(const int vertexNum) : myAdj(0), myVertexNum(vertexNum) {
- myAdj = new std::vector<int> [myVertexNum];
- }
- AdjList::AdjList(const AdjList &that) : myAdj(0), myVertexNum(0) {
- copyAll(that);
- }
- const AdjList & AdjList::operator=(const AdjList &that) {
- if (this != &that) {
- destory();
- copyAll(that);
- }
- return *this;
- }
- AdjList::~AdjList() {
- destory();
- }
- void AdjList::addEdge(const int v, const int w) {
- assert(v >= 0 && v < myVertexNum && w >= 0 && w < myVertexNum);
- for (unsigned i = 0; i < myAdj[v].size(); ++i) {
- if (myAdj[v][i] == w) {
- return;
- }
- }
- myAdj[v].push_back(w);
- myAdj[w].push_back(v);
- }
- void AdjList::getAdj(const int v, std::vector<int> &adj) const {
- assert(v >= 0 && v < myVertexNum);
- adj = myAdj[v];
- }
- void AdjList::print() const {
- for (int i = 0; i < myVertexNum; ++i) {
- std::cout << "No." << i << ": ";
- for (unsigned j = 0; j < myAdj[i].size(); ++j) {
- std::cout << myAdj[i][j] << " ";
- }
- std::cout << std::endl;
- }
- }
复制代码 helperFunction.h- #ifndef _FUN_H_
- #define _FUN_H_
- #include <string>
- int power(const int base, const int exp);
- int str2int(const std::string &str);
- #endif
复制代码 helperFunction.cpp- #include <string>
- #include "fun.h"
- int power(const int base, const int exp) {
- int result = 1;
- for (int i = 0; i < exp; ++i) {
- result *= base;
- }
- return result;
- }
- int str2int(const std::string &str) {
- int size = str.size();
- int result = 0;
- for (int i = size - 1; i >= 0; --i) {
- result += (str[i] - '0') * power(10, size - 1 - i);
- }
- return result;
- }
复制代码 main.cpp- #include <iostream>
- #include <stdio.h>
- #include <fstream>
- #include <vector>
- #include "AdjList.h"
- #include "fun.h"
- using namespace std;
- int main()
- {
- ifstream infile("input.txt");
- int vertexNum = 0;
- string line;
- while (getline(infile, line)) {
- ++vertexNum;
- line.clear();
- }
- infile.close();
- AdjList adjList(vertexNum);
- infile.open("input.txt");
- while (getline(infile, line)) {
- unsigned pos1 = -1, pos2 = -1;
- pos1 = line.find_first_of(' ');
- if (pos1 == string::npos) {
- line.clear();
- continue;
- }
- string vName(line, 0, pos1);
- int v = str2int(vName) - 1;
- while (pos1 != string::npos) {
- pos2 = line.find_first_of(' ', pos1 + 1);
- if (pos2 == string::npos) {
- break;
- }
- string wName(line, pos1 + 1, pos2 - pos1 - 1);
- int w = str2int(wName) - 1;
- adjList.addEdge(v, w);
- pos1 = line.find_first_of(' ', pos2 + 1);
- }
- pos1 = line.find_last_of(' ');
- string wName(line, pos1 + 1, line.size() - 1 - pos1);
- int w = str2int(wName) - 1;
- adjList.addEdge(v, w);
- line.clear();
- }
- infile.close();
- adjList.print();
- return 0;
- }
复制代码 |
|