Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Node Class Test 01.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <string>
- #include <stdio.h>
- #include <sstream>
- void parseFile(std::string);
- // node class
- class Node {
- int amount;
- public:
- std::string name;
- bool usable;
- Node();
- Node(std::string, int);
- std::vector<Node*> link;
- void checkUsability(); // will test to see if the node is available
- void connect(Node*);
- };
- // default (has no arguments)
- Node::Node() {
- name = "this node doesn't have a name";
- amount = 0;
- usable = false;
- }
- // actual constructor
- Node::Node(std::string _name, int _amount) {
- name = _name;
- amount = _amount;
- usable = false;
- }
- void Node::checkUsability() {
- usable = true;
- // add: check dependencies and see if this is actually usable
- std::cout << "checking requirements for " << name << "\n";
- // iterate over all the vector's elements
- bool result = true;
- for (std::vector<Node*>::iterator i = link.begin(); i != link.end(); ++i) {
- // check each
- if ((*i)->amount > 0) { // it's ok
- std::cout << (*i)->amount << " " << (*i)->name << " available - ok so far\n";
- }
- else { // if there are no available resources of this type
- std::cout << "0 " << (*i)->name << " available\n";
- result = false;
- }
- }
- usable = result;
- }
- void Node::connect(Node* target) {
- // add this new node as a thing we're dependent on
- link.push_back(target);
- }
- // global
- std::vector<Node*> nodes;
- // main
- using namespace std;
- int main()
- {
- cout << "class creation test\n";
- // create a single node
- Node* dog = new Node("dog", 1);
- nodes.push_back(dog); // add to central list
- cout << "class created (usable should be false)\n";
- // make a second node
- Node* hug = new Node("hug", 1);
- nodes.push_back(hug);
- // try connecting two nodes
- dog->connect(hug); // hug is required for dog to be usable
- dog->checkUsability();
- // test usability
- cout << dog->name << " is usable: " << dog->usable;
- // test file parsing
- std:string filename = "input.txt";
- parseFile(filename);
- return 0;
- }
- // text parsing
- void parseFile(std::string filename) {
- char str[512];
- FILE * stream;
- errno_t error; // we use this variable for file reading errors
- // if there's an error it will set this to some informative value
- error = fopen_s(&stream, "input.txt", "r+");
- if (error == 0) // if there are no errors, go ahead
- {
- printf("successfully opened the file\n");
- char line[100];
- while (fgets(line, sizeof(line), stream)) {
- puts(line);
- // convert to actual string
- std::string word;
- std::istringstream iss(line);
- int i = 0;
- while (std::getline(iss, word, ' ')) {
- // note: trim off any trailing new line characters
- printf("'%s'\n", word.c_str());
- // at this point we're looking at individual words within the line
- if (i == 0) {
- Node* newNode = new Node(word, 1);
- nodes.push_back(newNode);
- // this is the first word- it's the node name
- // make node
- }
- if (i == 1) {
- // add a dependency to the node
- // look through all nodes which exist
- // search for the specified name
- // if it exists, grab that node
- for (std::vector<Node*>::iterator iter = nodes.begin(); iter != nodes.end(); ++iter) {
- // check each
- printf("checking to see if existing node %s matches value", word.c_str());
- if ((*iter)->name == word.c_str()) { // see if this has the same name
- printf("node matches!");
- }
- }
- // if not, make the node
- // now link to the retrieved (/ created) node
- }
- i++;
- }
- }
- //printf("current word is %s \n", str);
- /*
- //old version
- while (fscanf(stream, "%s", str) != EOF)
- {
- // contents are in str at this point- put them in the array
- // do further processing here
- printf("current word is %s \n", str);
- }
- */
- }
- else { // we have some sort of error
- printf("error - the file could not be opened");
- }
- // now close the file if it's open
- if (stream)
- {
- fclose(stream);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement