查看: 3690| 回复: 10
跳转到指定楼层
上一主题 下一主题
收起左侧

[入门|算法|数据结构] 求助:coursera斯坦福的那个算法课week3的作业

全局:
公开课
学校名称: 斯坦福
Unit号: 6
开课时间: 2013-07-24
课程全名: algorithm
平台: Coursera

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
用c++实现文件操作,读取文件并且制作邻接表

1 2 3
2 4 1
3 1
4 2

文件形式如上所示。最左边的一列代表顶点(datatype为int),右边的几列都是相邻的顶点。现在想把这个东西用c++读进来并且创建一个邻接表,请问应该怎么操作??谢谢!

上一篇:[Udacity] Introduction to Programming (Week 4)
下一篇:Stanford Algorithms: Design and Analysis I Week#3 自由讨论
🔗
mirage 2013-7-24 23:55:23 | 只看该作者
回复

使用道具 举报

🔗
lvchaoshuai 2013-7-26 03:24:39 | 只看该作者
全局:
freopen("in.txt", "r", stdin);
getline字符串后用sstream处理
回复

使用道具 举报

🔗
浅浅 2013-7-29 00:12:47 | 只看该作者
全局:
AdjList.h
  1. #ifndef _ADJLIST_H_
  2. #define _ADJLIST_H_
  3. #include <vector>

  4. class AdjList {
  5. private:
  6.   std::vector<int>* myAdj;
  7.   int myVertexNum;
  8.   void destory();
  9.   void copyAll(const AdjList &that);

  10. public:
  11.   AdjList(const int vertexNum);
  12.   AdjList(const AdjList &that);
  13.   const AdjList & operator=(const AdjList &that);
  14.   ~AdjList();
  15.   void addEdge(const int v, const int w);
  16.   void getAdj(const int v, std::vector<int> &adj) const;
  17.   void print() const;
  18. };

  19. #endif
复制代码
AdjList.cpp
  1. #include <iostream>
  2. #include <cassert>
  3. #include <vector>
  4. #include "AdjList.h"

  5. void AdjList::destory() {
  6.     delete [] myAdj;
  7.     myAdj = 0;
  8.     myVertexNum = 0;
  9. }

  10. void AdjList::copyAll(const AdjList &that) {
  11.     myVertexNum = that.myVertexNum;
  12.     myAdj = new std::vector<int> [myVertexNum];
  13.     for (int i = 0; i < myVertexNum; ++i) {
  14.         myAdj[i] = that.myAdj[i];
  15.     }
  16. }

  17. AdjList::AdjList(const int vertexNum) : myAdj(0), myVertexNum(vertexNum) {
  18.     myAdj = new std::vector<int> [myVertexNum];
  19. }

  20. AdjList::AdjList(const AdjList &that) : myAdj(0), myVertexNum(0) {
  21.     copyAll(that);
  22. }

  23. const AdjList & AdjList::operator=(const AdjList &that) {
  24.     if (this != &that) {
  25.         destory();
  26.         copyAll(that);
  27.     }
  28.     return *this;
  29. }

  30. AdjList::~AdjList() {
  31.     destory();
  32. }

  33. void AdjList::addEdge(const int v, const int w) {
  34.     assert(v >= 0 && v < myVertexNum && w >= 0 && w < myVertexNum);
  35.     for (unsigned i = 0; i < myAdj[v].size(); ++i) {
  36.         if (myAdj[v][i] == w) {
  37.             return;
  38.         }
  39.     }
  40.     myAdj[v].push_back(w);
  41.     myAdj[w].push_back(v);
  42. }

  43. void AdjList::getAdj(const int v, std::vector<int> &adj) const {
  44.     assert(v >= 0 && v < myVertexNum);
  45.     adj = myAdj[v];
  46. }

  47. void AdjList::print() const {
  48.     for (int i = 0; i < myVertexNum; ++i) {
  49.         std::cout << "No." << i << ": ";
  50.         for (unsigned j = 0; j < myAdj[i].size(); ++j) {
  51.             std::cout << myAdj[i][j] << " ";
  52.         }
  53.         std::cout << std::endl;
  54.     }
  55. }
复制代码
helperFunction.h
  1. #ifndef _FUN_H_
  2. #define _FUN_H_
  3. #include <string>

  4. int power(const int base, const int exp);

  5. int str2int(const std::string &str);

  6. #endif
复制代码
helperFunction.cpp
  1. #include <string>
  2. #include "fun.h"

  3. int power(const int base, const int exp) {
  4.     int result = 1;
  5.     for (int i = 0; i < exp; ++i) {
  6.         result *= base;
  7.     }
  8.     return result;
  9. }

  10. int str2int(const std::string &str) {
  11.     int size = str.size();
  12.     int result = 0;
  13.     for (int i = size - 1; i >= 0; --i) {
  14.         result += (str[i] - '0') * power(10, size - 1 - i);
  15.     }
  16.     return result;
  17. }
复制代码
main.cpp
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <fstream>
  4. #include <vector>
  5. #include "AdjList.h"
  6. #include "fun.h"

  7. using namespace std;

  8. int main()
  9. {
  10.     ifstream infile("input.txt");
  11.     int vertexNum = 0;
  12.     string line;
  13.     while (getline(infile, line)) {
  14.         ++vertexNum;
  15.         line.clear();
  16.     }
  17.     infile.close();
  18.     AdjList adjList(vertexNum);
  19.     infile.open("input.txt");
  20.     while (getline(infile, line)) {
  21.         unsigned pos1 = -1, pos2 = -1;
  22.         pos1 = line.find_first_of(' ');
  23.         if (pos1 == string::npos) {
  24.             line.clear();
  25.             continue;
  26.         }
  27.         string vName(line, 0, pos1);
  28.         int v = str2int(vName) - 1;
  29.         while (pos1 != string::npos) {
  30.             pos2 = line.find_first_of(' ', pos1 + 1);
  31.             if (pos2 == string::npos) {
  32.                 break;
  33.             }
  34.             string wName(line, pos1 + 1, pos2 - pos1 - 1);
  35.             int w = str2int(wName) - 1;
  36.             adjList.addEdge(v, w);
  37.             pos1 = line.find_first_of(' ', pos2 + 1);
  38.         }
  39.         pos1 = line.find_last_of(' ');
  40.         string wName(line, pos1 + 1, line.size() - 1 - pos1);
  41.         int w = str2int(wName) - 1;
  42.         adjList.addEdge(v, w);
  43.         line.clear();
  44.     }
  45.     infile.close();
  46.     adjList.print();
  47.     return 0;
  48. }
复制代码

评分

参与人数 2大米 +7 收起 理由
西米猪 + 2 很有用的信息!
zhujl1991 + 5 回答的很好!

查看全部评分

回复

使用道具 举报

🔗
 楼主| zhujl1991 2013-7-29 18:27:31 | 只看该作者
全局:
lvchaoshuai 发表于 2013-7-26 03:24
freopen("in.txt", "r", stdin);
getline字符串后用sstream处理

嗯。谢谢~我之后就是这么干的~
回复

使用道具 举报

🔗
 楼主| zhujl1991 2013-7-29 18:28:27 | 只看该作者
全局:
浅浅 发表于 2013-7-29 00:12
AdjList.hAdjList.cpphelperFunction.hhelperFunction.cppmain.cpp

谢谢~我还没有啥把程序分成头文件的意识~看你写的程序真规整。向你学习!
回复

使用道具 举报

🔗
gnijuohz 2013-7-29 20:22:09 | 只看该作者
全局:
我用的是python。。。感觉python写起来很方便。
回复

使用道具 举报

🔗
kelvinzhong 2013-7-29 20:48:39 | 只看该作者
全局:
gnijuohz 发表于 2013-7-29 20:22
我用的是python。。。感觉python写起来很方便。

我也用习惯python..但是感觉用惯了python..对底层算法的理解并不有利
回复

使用道具 举报

🔗
浅浅 2013-7-29 21:39:33 | 只看该作者
全局:
zhujl1991 发表于 2013-7-29 18:28
谢谢~我还没有啥把程序分成头文件的意识~看你写的程序真规整。向你学习!

哈哈不必客气,互相学习吧;能否帮我加点分,分太低看不了别人帖子

评分

参与人数 1大米 +2 收起 理由
西米猪 + 2 欢迎来介绍你知道的情况

查看全部评分

回复

使用道具 举报

🔗
西米猪 2013-7-29 21:44:00 | 只看该作者
全局:
楼上厉害,写的真规整
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表