Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. // Node Class Test 01.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <stdio.h>
  9. #include <sstream>
  10.  
  11.  
  12. void parseFile(std::string);
  13.  
  14.  
  15. // node class
  16. class Node {
  17.     int amount;
  18. public:
  19.     std::string name;
  20.     bool usable;
  21.     Node();
  22.     Node(std::string, int);
  23.     std::vector<Node*> link;
  24.     void checkUsability(); // will test to see if the node is available
  25.     void connect(Node*);
  26. };
  27.  
  28.  
  29. // default (has no arguments)
  30. Node::Node() {
  31.     name = "this node doesn't have a name";
  32.     amount = 0;
  33.     usable = false;
  34. }
  35.  
  36.  
  37. // actual constructor
  38. Node::Node(std::string _name, int _amount) {
  39.     name = _name;
  40.     amount = _amount;
  41.     usable = false;
  42. }
  43.  
  44.  
  45. void Node::checkUsability() {
  46.     usable = true;
  47.     // add: check dependencies and see if this is actually usable
  48.  
  49.     std::cout << "checking requirements for " << name << "\n";
  50.  
  51.     // iterate over all the vector's elements
  52.  
  53.     bool result = true;
  54.     for (std::vector<Node*>::iterator i = link.begin(); i != link.end(); ++i) {
  55.         // check each
  56.         if ((*i)->amount > 0) { // it's ok
  57.             std::cout << (*i)->amount << " " << (*i)->name << " available - ok so far\n";
  58.         }
  59.         else { // if there are no available resources of this type
  60.             std::cout << "0 " << (*i)->name << " available\n";
  61.             result = false;
  62.         }
  63.     }
  64.     usable = result;
  65. }
  66.  
  67.  
  68. void Node::connect(Node* target) {
  69.     // add this new node as a thing we're dependent on
  70.     link.push_back(target);
  71. }
  72.  
  73.  
  74.  
  75.  
  76. // global
  77. std::vector<Node*> nodes;
  78.  
  79.  
  80.  
  81. // main
  82.  
  83. using namespace std;
  84.  
  85. int main()
  86. {
  87.  
  88.     cout << "class creation test\n";
  89.  
  90.     // create a single node
  91.  
  92.  
  93.     Node* dog = new Node("dog", 1);
  94.     nodes.push_back(dog); // add to central list
  95.  
  96.     cout << "class created (usable should be false)\n";
  97.  
  98.  
  99.  
  100.     // make a second node
  101.  
  102.     Node* hug = new Node("hug", 1);
  103.     nodes.push_back(hug);
  104.     // try connecting two nodes
  105.     dog->connect(hug); // hug is required for dog to be usable
  106.     dog->checkUsability();
  107.  
  108.     // test usability
  109.  
  110.     cout << dog->name << " is usable: " << dog->usable;
  111.  
  112.  
  113.     // test file parsing
  114.     std:string filename = "input.txt";
  115.     parseFile(filename);
  116.  
  117.     return 0;
  118. }
  119.  
  120.  
  121.  
  122. // text parsing
  123. void parseFile(std::string filename) {
  124.     char str[512];
  125.     FILE * stream;
  126.  
  127.     errno_t error; // we use this variable for file reading errors
  128.                    // if there's an error it will set this to some informative value
  129.  
  130.     error = fopen_s(&stream, "input.txt", "r+");
  131.  
  132.     if (error == 0) // if there are no errors, go ahead
  133.     {
  134.         printf("successfully opened the file\n");
  135.  
  136.         char line[100];
  137.         while (fgets(line, sizeof(line), stream)) {
  138.             puts(line);
  139.  
  140.             // convert to actual string
  141.             std::string word;
  142.             std::istringstream iss(line);
  143.             int i = 0;
  144.             while (std::getline(iss, word, ' ')) {
  145.                 // note: trim off any trailing new line characters
  146.  
  147.                 printf("'%s'\n", word.c_str());
  148.  
  149.                 // at this point we're looking at individual words within the line
  150.  
  151.                 if (i == 0) {
  152.                     Node* newNode = new Node(word, 1);
  153.                     nodes.push_back(newNode);
  154.                     // this is the first word- it's the node name
  155.                     // make node
  156.                 }
  157.                 if (i == 1) {
  158.                     // add a dependency to the node
  159.                     // look through all nodes which exist
  160.                     // search for the specified name
  161.                     // if it exists, grab that node
  162.                     for (std::vector<Node*>::iterator iter = nodes.begin(); iter != nodes.end(); ++iter) {
  163.                         // check each
  164.                         printf("checking to see if existing node %s matches value", word.c_str());
  165.                         if ((*iter)->name == word.c_str()) { // see if this has the same name
  166.                             printf("node matches!");
  167.                         }
  168.                     }
  169.                     // if not, make the node
  170.                     // now link to the retrieved (/ created) node
  171.                 }
  172.                 i++;
  173.             }
  174.         }
  175.  
  176.         //printf("current word is %s \n", str);
  177.  
  178.         /*
  179.         //old version
  180.         while (fscanf(stream, "%s", str) != EOF)
  181.         {
  182.         // contents are in str at this point- put them in the array
  183.         // do further processing here
  184.         printf("current word is %s \n", str);
  185.         }
  186.         */
  187.     }
  188.     else { // we have some sort of error
  189.         printf("error - the file could not be opened");
  190.     }
  191.  
  192.     // now close the file if it's open
  193.     if (stream)
  194.     {
  195.         fclose(stream);
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement