Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Adjacency of cities
- // Created on Aug 28, 2016 - [last updated: Sep 7, 2016]
- // Author: fueanta
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- class nodes
- {
- string name, adjacent[10];
- int num,cost[10];
- public:
- void setValues(string n, string *ad, int *cos, int num)
- {
- this->num= num;
- name=n;
- for (int i=0; i<(this->num) ;i++) {
- adjacent[i]= ad[i];
- cost[i]= cos[i];
- }
- }
- void setNodes(string n, int cos) {
- num++;
- adjacent[num-1]= n;
- cost[num-1]= cos;
- }
- void setName(string n) {
- name= n; num= 0;
- }
- void disp()
- {
- cout << "City Name: " << name << endl;
- for (int i=0; i<(11+name.size());i++) {
- cout << "*";
- }
- cout << endl;
- if (num != 0)
- cout << name << " has " << num << " adjacent ";
- if (num > 1)
- cout << "cities." << endl;
- else if (num == 1)
- cout << "city." << endl;
- for (int i=0; i<(this->num) ; i++) {
- cout << "\nAdjacent " << i+1 << ": " << adjacent[i] << endl;
- cout << "Cost of connection for " << adjacent[i] << ": " << cost[i] << endl;
- }
- if (num == 0)
- cout << name << " doesn't have any adjacent city." << endl;
- cout << endl;
- }
- string get_name() {
- return name;
- }
- friend void add_nodes(string, nodes&);
- };
- void add_nodes(string n_node, int cos, nodes& C) {
- C.setNodes(n_node,cos);
- }
- int main()
- {
- cout << "How many cities are there?" << endl << "Number of cities: "; int l=0;
- cin >> l;
- nodes cities[l];
- int i=l-1,k=0;
- while(i>=0)
- {
- cout << "\nName for city " << l-i << ": ";
- string name,approval; int ads;
- cin.ignore(); getline(cin,name);
- cout << "Does this city have any adjacent city? (y/n) " << endl;
- cin >> approval; if(approval=="y" || approval=="Y") {
- cout << "How many? " << endl << "Number of adjacent cities: "; cin>> ads;
- string ad_names[ads]; int cost[ads];
- for(int j=0; j<ads; j++) {
- cout << "\nAdjacent City no." << j+1 << ": "; cin.ignore(); getline(cin,ad_names[j]);
- cout << "Cost of connection for this city? Cost: "; cin >> cost[j];
- }
- cities[k].setValues(name,ad_names,cost,ads);
- } else {
- cities[k].setName(name); k++;
- i--;continue;
- }
- i--;k++;
- }
- cout << "\n\n";
- for(int i=0; i<l ;i++) {
- cities[i].disp();
- }
- go:
- string lm;
- cout << "Do you want to add a node with " << cities[l-1].get_name() << "? ('y' for yes) ";
- cout << "\n or,Press x/X for termination, key:";
- cin.ignore(); getline(cin, lm); if(lm=="y" || lm=="Y") {
- cout << "\nNode Name: "; cin.sync(); getline(cin, lm);
- cout << "Cost for connection: "; cin >> i;
- add_nodes(lm,i,cities[l-1]);
- } else if (lm=="x" || lm=="X") exit(0);
- cout << "\n\n";
- cout << "Details with/without updated info:\n";
- for(int i=0; i<l ;i++) {
- cities[i].disp();
- }
- goto go;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement