Advertisement
fueanta

Adjacency of Cities/Nodes

Sep 6th, 2016
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. // Adjacency of cities
  2. // Created on Aug 28, 2016 - [last updated: Sep 7, 2016]
  3. // Author: fueanta
  4.  
  5. #include <iostream>
  6. #include <stdlib.h>
  7. using namespace std;
  8.  
  9. class nodes
  10. {
  11.     string name, adjacent[10];
  12.     int num,cost[10];
  13. public:
  14.     void setValues(string n, string *ad, int *cos, int num)
  15.     {
  16.         this->num= num;
  17.         name=n;
  18.         for (int i=0; i<(this->num) ;i++) {
  19.             adjacent[i]= ad[i];
  20.             cost[i]= cos[i];
  21.         }
  22.     }
  23.     void setNodes(string n, int cos) {
  24.         num++;
  25.         adjacent[num-1]= n;
  26.         cost[num-1]= cos;
  27.     }
  28.     void setName(string n) {
  29.         name= n; num= 0;
  30.     }
  31.     void disp()
  32.     {
  33.         cout << "City Name: " << name << endl;
  34.         for (int i=0; i<(11+name.size());i++) {
  35.             cout << "*";
  36.         }
  37.         cout << endl;
  38.         if (num != 0)
  39.             cout << name << " has " << num << " adjacent ";
  40.         if (num > 1)
  41.             cout << "cities." << endl;
  42.         else if (num == 1)
  43.             cout << "city." << endl;
  44.         for (int i=0; i<(this->num) ; i++) {
  45.             cout << "\nAdjacent " << i+1 << ": " << adjacent[i] << endl;
  46.             cout << "Cost of connection for " << adjacent[i] << ": " << cost[i] << endl;
  47.         }
  48.         if (num == 0)
  49.             cout << name << " doesn't have any adjacent city." << endl;
  50.         cout << endl;
  51.     }
  52.     string get_name() {
  53.         return name;
  54.     }
  55.     friend void add_nodes(string, nodes&);
  56. };
  57.  
  58. void add_nodes(string n_node, int cos, nodes& C) {
  59.     C.setNodes(n_node,cos);
  60. }
  61.  
  62. int main()
  63. {
  64.     cout << "How many cities are there?" << endl << "Number of cities: "; int l=0;
  65.     cin >> l;
  66.     nodes cities[l];
  67.     int i=l-1,k=0;
  68.     while(i>=0)
  69.     {
  70.         cout << "\nName for city " << l-i << ": ";
  71.         string name,approval; int ads;
  72.         cin.ignore(); getline(cin,name);
  73.         cout << "Does this city have any adjacent city? (y/n) " << endl;
  74.         cin >> approval; if(approval=="y" || approval=="Y") {
  75.             cout << "How many? " << endl << "Number of adjacent cities: "; cin>> ads;
  76.             string ad_names[ads]; int cost[ads];
  77.             for(int j=0; j<ads; j++) {
  78.                 cout << "\nAdjacent City no." << j+1 << ": "; cin.ignore(); getline(cin,ad_names[j]);
  79.                 cout << "Cost of connection for this city? Cost: "; cin >> cost[j];
  80.             }
  81.             cities[k].setValues(name,ad_names,cost,ads);
  82.         } else {
  83.             cities[k].setName(name); k++;
  84.             i--;continue;
  85.           }
  86.         i--;k++;
  87.     }
  88.     cout << "\n\n";
  89.     for(int i=0; i<l ;i++) {
  90.         cities[i].disp();
  91.     }
  92.     go:
  93.     string lm;
  94.     cout << "Do you want to add a node with " << cities[l-1].get_name() << "? ('y' for yes) ";
  95.     cout << "\n or,Press x/X for termination, key:";
  96.     cin.ignore(); getline(cin, lm); if(lm=="y" || lm=="Y") {
  97.         cout << "\nNode Name: "; cin.sync(); getline(cin, lm);
  98.         cout << "Cost for connection: "; cin >> i;
  99.         add_nodes(lm,i,cities[l-1]);
  100.     } else if (lm=="x" || lm=="X") exit(0);
  101.     cout << "\n\n";
  102.     cout << "Details with/without updated info:\n";
  103.     for(int i=0; i<l ;i++) {
  104.         cities[i].disp();
  105.     }
  106.     goto go;
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement