简体   繁体   English

如何修复我的 topological.cpp 输出错误?

[英]How to fix my topological.cpp outputting error?

i have been provided middleearth.h/cpp and was asked to make a makefile, doxyfile (which i did correctly) and a topological.cpp that works but has a small mistake in the output and i need help with that please.ill provide all three files and the text we use to test and the error.我已经获得了 middleearth.h/cpp 并被要求制作一个 makefile、doxyfile(我做对了)和一个 topological.cpp 可以工作但在输出中有一个小错误,我需要帮助。请提供所有三个文件和我们用来测试的文本和错误。

cs2110 cs2150
cs2102 cs2150
cs1110 cs2110
cs3330 cs4414
cs2150 cs4414
cs2110 cs3330
cs1110 cs2102
0 0

在此处输入图片说明

middleearth.h中土世界.h

#ifndef MIDDLEEARTH_H
#define MIDDLEEARTH_H

#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <random>

using namespace std;

// see the comments in the lab11 write-up, or in middleearth.cpp

class MiddleEarth {
public:
    MiddleEarth(int xsize, int ysize, int num_cities, int seed);
    void print();
    void printTable();
    float getDistance(const string& city1, const string& city2);
    vector<string> getItinerary(unsigned int length);

private:
    int num_city_names, xsize, ysize;
    unordered_map<string, float> xpos, ypos;
    vector<string> cities;
    unordered_map<string, unordered_map<string, float>> distances;

    mt19937 gen; // Mersenne-Twister random number engine
};

#endif

middleearth.cpp中土.cpp

#include "middleearth.h"

#include <algorithm>
#include <array>
#include <cstdlib>
#include <cmath>

// New shuffle method that uses the Mersenne Twister engine
void shuffle (vector<string>::iterator first, vector<string>::iterator last, mt19937& g) {
    for (auto i=(last-first)-1; i>0; --i) {
        unsigned int n = (g() / (double) g.max())*distance(first,last);
        swap (first[i], first[n]);
    }
}

// The list of all the place names that we'll be using
const array<string, 40> all_city_names{
    // human towns, cities and strongholds
    "Bree",             // a human and hobbit town between the Shire and Rivendell
    "Isengard",         // the tower fortress where Saruman resided; Gandalf was imprisoned there.
    "Minas Tirith",     // capital of Gondor, the "white city"; home to Boromir, Denethor, and later, Aragorn
    "Osgiliath",        // city on the river Anduin; is at the other end of Pelennor Fields from M. Tirith
    "Edoras",           // the capital city of Rohan, where King Theoden resides
    "Helm's Deep",      // fortress of Rohan, it is where the people of Edoras fled to from the orc invasion
    "Dunharrow",        // a refuge of Rohan, it is where Elrond presents the sword to Aragorn in the movie
    // dwarf cities
    "Moria",            // the enormous dwarven underground complex that the Fellowship traveled through
    // elvish cities
    "Lothlorien",       // the elvish tree-city, home of Lady Galadriel and Lord Celeborn
    "Rivendell",        // the elvish city that is home to Lord Elrond
    "The Grey Havens",  // the port city on the western coast from which the elves travel westward
    // hobbit villages
    "Bucklebury",       // a Shire village, it has a ferry across the Brandywine River that the Hobbits use
    "Bywater",          // a Shire village, it is the site of the Battle of Bywater (removed from the movie)
    "Hobbiton",         // a Shire village, it is home to Bilbo and, later, Frodo
    "Michel Delving",   // a Shire village, it is the chief town of the Shire
    // Mordor places
    "Orodruin",         // Mount Doom in Mordor, it is where the Ring was made, and later, unmade
    "Barad-Dur",        // Sauron's fortress that was part castle, part mountain
    "Minas Morgul",     // formerly the Gondorian city of Minas Ithil; renamed when Sauron took it over
    "Cirith Ungol",     // the mountianous pass that Sam & Frodo went through; home of Shelob
    "Gorgoroth",        // the plains in Mordor that Frodo & Sam had to cross to reach Mount Doom
    // places that are not cities
    "Emyn Muil",        // the rocky region that Sam & Frodo climb through after leaving the Fellowship
    "Fangorn Forest",   // the forest where Treebeard (and the other Ents) live
    "Dagorlad",         // great plain/swamp between Emyn Muil & Mordor where a great battle was fought long ago
    "Weathertop",       // the tower between Bree and Rivendell where Aragorn and the Hobbits take refuge
    "Gladden Fields",   // this is where the Ring is lost in the River Anduin, after Isildur is ambushed and killed by Orcs
    "Entwash River",    // a river through Rohan, which flows through Fangorn Forest
    "River Isen",       // river through the Gap of Rohan; Theoden's son was slain in a battle here.
    "The Black Gate",   // huge gate to Mordor that Aragorn and company attack as the ring is destroyed
    "The Old Forest",   // a forest to the west of the Shire (adventures there were removed from the movie)
    "Trollshaws",       // area to the west of Rivendell that was home to the trolls that Bilbo met
    "Pelennor Fields",  // great plain between M. Tirith and Osgiliath; site of the Battle of M. Tirith
    "Hollin",           // the empty plains that the Fellowship crosses between Rivendell and Moria
    "Mirkwood",         // Legolas' forest home; Bilbo travels there in 'The Hobbit'.
    "Misty Mountains",  // the north-south moutain range that runs through Middle-earth
    "Prancing Pony",    // an inn in Bree where the hobbits tried to meet Gandalf, but meet Aragorn instead
    // places from the Hobbit book and movies
    "Laketown",         // also called Esgaorth, it is the town of men on the Long Lake near Erebor
    "Dale",             // the town of men outside Erebor, destroyed by Smaug long before the Hobbit story
    "Erebor",           // the Elvish name for the Lonely Mountain, where the dwarves had their fortress
    "Beorn's House",    // Beorn is the shape-shifter who shelters the dwarf party
    "Dol Guldur",       // fortress in Mirkwood where Sauron, as the Necromancer, hid during most of the Hobbit
};

// Iluvatar, the creator of Middle-Earth
MiddleEarth::MiddleEarth(int xsize, int ysize, int num_cities, int seed) {
    this->xsize = xsize;
    this->ysize = ysize;

    // set up the random number generator
    gen.seed(seed == -1 ? random_device{}() : seed);

    // count the number of cities in the array
    this->num_city_names = all_city_names.size();

    if (num_cities > num_city_names) {
        cout << "There are only " << num_city_names << " city names, so "
             << num_cities << " cities cannot be created." << endl;
        cout << "Exiting." << endl;
        exit(0);
    }

    if (num_cities < 5) {
        num_cities = 5;
    }

    // copy all the cities into a mutable vector
    this->cities = vector<string>(all_city_names.begin(), all_city_names.end());

    shuffle(cities.begin(), cities.end(), gen); // shuffle all the cities
    cities.erase(cities.begin() + num_cities, cities.end()); // then remove the ones we won't be using

    // compute random city positions
    for (auto city : cities) {
        xpos.emplace(city, (gen() / (double) gen.max()) * xsize);
        ypos.emplace(city, (gen() / (double) gen.max()) * ysize);
    }

    // compute the 2-d distance array
    // we assume that num_cities < xsize * ysize
    for (auto city1 : cities) {
        for (auto city2 : cities) {
            distances[city1].emplace(city2, sqrt((xpos[city2] - xpos[city1]) * (xpos[city2] - xpos[city1]) +
                                                 (ypos[city2] - ypos[city1]) * (ypos[city2] - ypos[city1])));
        }
    }
}

// The Mouth of Sauron!
// Prints out info on the created 'world'
void MiddleEarth::print() {
    cout << "there are " << num_city_names
         << " locations to choose from; we are using " << cities.size() << endl;
    cout << "they are: " << endl;
    for (auto city : cities) {
        cout << "\t" << city << " @ (" << xpos[city] << ", " << ypos[city]
             << ")" << endl;
    }
}

// Prints a tab-separated table of the distances,
// which can be loaded into Excel or similar
void MiddleEarth::printTable() {
    cout << "Table: " << endl << endl << "Location\txpos\typos\t";
    for (auto city : cities) {
        cout << city << "\t";
    }
    cout << endl;

    for (auto city1 : cities) {
        cout << city1 << "\t" << xpos[city1] << "\t" << ypos[city1] << "\t";
        for (auto city2 : cities) {
            cout << distances[city1][city2] << "\t";
        }
        cout << endl;
    }
}

// This method returns the distance between the two passed cities.
// If we assume that the hash table (i.e. the map) is O(1),
// then this method call is also O(1)
float MiddleEarth::getDistance(const string& city1, const string& city2) {
    return distances[city1][city2];
}

// Returns the list of cities to travel to.
// The first city is the original start point as well as the end point.
// The number of cities passed in does not include this start/end point
// (so there will be length+1 entries in the returned vector).
vector<string> MiddleEarth::getItinerary(unsigned int length) {
    // check parameter
    if (length >= cities.size()) {
        cout << "You have requested an itinerary of " << length
             << " cities; you cannot ask for an itinerary of more than length "
             << cities.size() - 1 << endl;
        exit(0);
    }

    length++; // to account for the start point

    // we need to make a deep copy of the cities vector
    vector<string> itinerary(cities.begin(), cities.end());

    // shuffle, erase unneeded ones, and return the itinerary
    shuffle(itinerary.begin(), itinerary.end(), gen);
    itinerary.erase(itinerary.begin() + length, itinerary.end());
    return itinerary;
}

topological.cpp拓扑文件

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <list>
#include <stack>
#include <string>
#include <map>
using namespace std;
/**
@date 11/16/2020
*/
/** @brief
*/
class Graph 
{
  
  public:

    Graph(int vert); 
    /** @brief adds an edge to the list
    *  @param string v 
    *  @param string w 
    *  @param map<string, int> m
    * 
    */

    void addEdge(string v, string w, map<string, int> m); 
  
    void printSort(); 
  
  private:
    int vertices; 
    list<int> *myList; 
  
    void sort(int v, stack<int> &myStack, bool visited[]); 
    map<int, string> myMap; 
  };

 
Graph::Graph(int vert) {
  vertices = vert;
  myList = new list<int>[vertices];
}


void Graph::addEdge(string v, string w, map<string, int> m) {
  int loc1 = m[v];
  int loc2 = m[w];
  
  myMap[loc1] = v;
  myMap[loc2] = w;
  
  myList[loc1].push_back(loc2); 
}


void Graph::sort(int v, stack<int> &myStack, bool visited[]) {
  visited[v] = true;
  list<int>::iterator i;
  for(i = myList[v].begin(); i != myList[v].end(); ++i) {
    if(!visited[*i]) {
      sort(*i, myStack, visited);
    }
  }
  myStack.push(v);
}



void Graph::printSort() {
  stack<int> myStack;
  bool *visited = new bool[vertices];
  
  for(int i = 0; i < vertices; i++) {
    visited[i] = false;
  }
  
  for(int i = 0; i < vertices; i++) {
    if(visited[i] == false)
      sort(i, myStack, visited);
  }
  
  while(myStack.empty() == false) {
    int x = myStack.top();
    string output = myMap[x];
    cout << output << " ";
    myStack.pop();
  }
}


int main (int argc, char **argv) {
    
    if ( argc != 2 ) {
        cout << "Must supply the input file name as the one and only parameter" << endl;
        return 1;
    }
    
    ifstream file(argv[1], ifstream::binary);
    ifstream file1(argv[1], ifstream::binary);
    
    if ( !file.is_open() ) {
        cout << "Unable to open file '" << argv[1] << "'." << endl;
        return 1;
    }
    
    string s1, s2;
    int count = 0;
    list <string> edges;
   while(!file.eof()) {
      file >> s1;
      file >> s2;
      if(s1 == "0" && s2 == "0") {
  break;
      }
      edges.push_back(s1);
      edges.push_back(s2);
   }
   file.close();
   
    edges.sort();
    edges.unique();
    int size = edges.size();
    map<string, int> myMap;
    
    Graph myGraph(size);
    list<string>::iterator i;
    for(i = edges.begin(); i != edges.end(); i++) {
      string s = *i;
      myMap[s] = count;
      count++;
    }
    
      while(!file1.eof()) {
      file1 >> s1;
      file1 >> s2;
    if(s1 == "0" && s2 == "0") {
  break;
  }
      myGraph.addEdge(s1, s2, myMap);
    }

      myGraph.printSort();
      cout<<endl;
    file1.close();
    return 0;
}

You are confusing yourself.你在迷惑自己。 You have your solution in edges .您在edges有您的解决方案。 There isn't a reason to read the data a second time.没有理由再次读取数据。 For example, you can simply output sorted/unique elements of edges , eg the modifications to your code are:例如,您可以简单地输出edges排序/唯一元素,例如对代码的修改是:

int main (int argc, char **argv) {
    
    if ( argc != 2 ) {
        cout << "Must supply the input file name as the one and only parameter" << endl;
        return 1;
    }
    
    ifstream file(argv[1], ifstream::binary);
//     ifstream file1(argv[1], ifstream::binary);
    
    if ( !file.is_open() ) {
        cout << "Unable to open file '" << argv[1] << "'." << endl;
        return 1;
    }
    
    string s1, s2;
//     int count = 0;
    list <string> edges;
    while(file >> s1 && file >> s2) {
        if(s1 == "0" && s2 == "0") {
        break;
    }
    edges.push_back(s1);
    edges.push_back(s2);
    }
    file.close();

    edges.sort();
    edges.unique();
//     int size = edges.size();
//     map<string, int> myMap;
    bool first = true;
    for (const auto& n : edges) {
        if (!first)
            cout.put(' ');
        cout << n;
        first = false;
    }
    cout.put('\n');
    
//     Graph myGraph(size);
//     list<string>::iterator i;
//     for(i = edges.begin(); i != edges.end(); i++) {
//       string s = *i;
//       myMap[s] = count;
//       count++;
//     }
//     
//     while(file1 >> s1 && file1 >> s2) {
//         if(s1 == "0" && s2 == "0") {
//     break;
//   }
//       myGraph.addEdge(s1, s2, myMap);
//     }
// 
//       myGraph.printSort();
//       cout<<endl;
//     file1.close();
    return 0;
}

( note: how while (!file.eof())) was replaced with while(file >> s1 && file >> s2) ) 注意: while (!file.eof()))是如何被替换为while(file >> s1 && file >> s2)

Example Use/Output示例使用/输出

With your sample data in dat/topological.txt , you would receive:使用dat/topological.txt示例数据,您将收到:

$ ./bin/topological dat/topological.txt
cs1110 cs2102 cs2110 cs2150 cs3330 cs4414

If you are having problems editing your code, then you can do things much easier with a std::set , eg如果您在编辑代码时遇到问题,那么您可以使用std::set更轻松地完成任务,例如

#include <iostream>
#include <fstream>
#include <string>
#include <set>

int main (int argc, char **argv) {
    
    if ( argc != 2 ) {
        std::cerr << "Must supply the input file name as the one and only parameter\n";
        return 1;
    }
    
    std::set<std::string> strset {};
    std::string s {};
    std::ifstream f (argv[1]);
    
    if (!f.good()) {
        std::cerr << "file open failed.\n";
        return 1;
    }
    
    while (f >> s && s != "0")
        strset.insert(s);
    
    bool first = true;
    for (const auto& unique : strset) {
        if (!first)
            std::cout.put(' ');
        std::cout << unique;
        first = false;
    }
    std::cout.put('\n');
}

(same answer) (同样的答案)

This is when i used the main you gave me: enter image description here这是我使用你给我的主要内容的时候:在此处输入图像描述

This is when i added (Yes. after edges.unique(); you can simply do. bool first = true; for (const auto& n : edges) { if (!first) cout.put(' '); cout << n; first = false; } cout.put('\\n');): enter image description here这是我添加 (是的。在edges.unique()之后;你可以简单地做。bool first = true; for (const auto& n :edges) { if (!first) cout.put(' '); cout << n; first = false; } cout.put('\\n');):在这里输入图片描述

the first line on output is correct, i dont want the line under it输出的第一行是正确的,我不想要它下面的行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复Android上的致命错误.cpp文件 - how to fix fatal error .cpp file on android 如何修复 Cpp 内部 While 循环错误? - How Can I Fix Cpp Internal While Loop Error? 如何修复 cpp 中没有可行的重载'=' - how to fix no viable overloaded '=' in cpp 如何解决或修复 C++98 中的 (dev cpp) [Error] 中的此错误 &#39;v&#39; 必须由构造函数初始化,而不是由 &#39;{...}&#39; - How to resolve or fix this error in (dev cpp ) [Error] in C++98 'v' must be initialized by constructor, not by '{...}' 如何修复 c++ 不输出输入 - How to fix c++ not outputting the input 如何修复 arduino 定序器始终输出 - How to fix arduino sequencer always outputting 如何修复错误? main.cpp|21|错误:无效类型 &#39;float [3] 第 21 行 - How do I fix error? main.cpp|21|error: invalid types 'float [3] Line 21 如何解决此错误cmdline.cpp:93:1:错误:从&#39;int&#39;到&#39;option_type&#39;的无效转换[-fpermissive] - how to fix this error cmdline.cpp:93:1: error: invalid conversion from 'int' to 'option_type' [-fpermissive] 配置PHP 7.0.1时,如何解决C ++预处理程序“ / lib / cpp”失败的健全性检查错误 - How to fix the C++ preprocessor “/lib/cpp” fails sanity check error when configure PHP 7.0.1 如何修复xychart.cpp中的“ index &lt;m_series-&gt; count()”错误? - How to fix “index < m_series->count()” error in xychart.cpp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM