Você está na página 1de 5

1) A representation of a directed graph with n vertices using an n × n matrix, where the entry at

(i,j) is 1 if there is an edge from vertex i to vertex j; otherwise the entry is 0. A weighted graph
may be represented using the weight as the entry. An undirected graph may be represented
using the same entry in both (i,j) and (j,i) or using an upper triangular matrix.

#include <list>
#include <vector>

template <typename DataType>


class Digraph
{
public:
Graph(int vertexCount) {
            this->vertexCount = vertexCount;
            adjacencyMatrix = new bool*[vertexCount];
            for (int i = 0; i < vertexCount; i++) {
                  adjacencyMatrix[i] = new bool[vertexCount];
                  for (int j = 0; j < vertexCount; j++)
                        adjacencyMatrix[i][j] = false;
            }
      }

private:
bool** adjacencyMatrix;
      int vertexCount;
void addEdge(int i, int j) {
            if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) {
                  adjacencyMatrix[i][j] = true;
                  adjacencyMatrix[j][i] = true;
            }
      }
 
      void removeEdge(int i, int j) {
            if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) {
                  adjacencyMatrix[i][j] = false;
                  adjacencyMatrix[j][i] = false;
            }
      }
 
      bool isEdge(int i, int j) {
            if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
                  return adjacencyMatrix[i][j];
            else
                  return false;
      }
 
      ~Graph() {
            for (int i = 0; i < vertexCount; i++)
                  delete[] adjacencyMatrix[i];
            delete[] adjacencyMatrix;
      }
};
2) Total sum=1.0
1.0
/ \
0.60 0.40
/ \ / \
0.30 0.30 0.20 0.20
/ \
0.0.5 0.15
Codes

Int 00
Main 01
While 110
If 11
For 111

3)Public: A member (either data member or member function) declared in a private section of a
class can only be accessed by member functions and friends of that class

Private:A member (either data member or member function) declared in a protected section of a class
can only be accessed by member functions and friends of that class, and by member functions and
friends of derived classes

Protected:A member (either data member or member function) declared in a public section of a
class can be accessed by anyone

#include <iostream>
using namespace std;

class Enemy
{
public:
    Enemy(): m_Damage(10) {} 
    
    void Attack() const
    { cout << "Attack inflicts " << m_Damage << " damage points!\n"; }  

protected:
    int m_Damage;
};

class Boss : public Enemy
{
public:
    Boss(): m_DamageMultiplier(3) {}
        
    void SpecialAttack() const
    { cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); 
      cout << " damage points!\n"; }

private:
    int m_DamageMultiplier;
};

int main()

    Enemy enemy1;
    enemy1.Attack();

    Boss boss1;
    boss1.Attack();
    boss1.SpecialAttack();

    return 0;
}

4)
  

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str = "Anatoliy";
char *ary = new char[str.length()+1];

// strcpy ( ary, str ); that is wrong way


strcpy ( ary, str.c_str() ); // that is correct

cout << ary << endl;

cout << "str is: " << str << endl;

cout << "Length of str is : "


<< str.length() << endl;

int pos1, pos2; // size_t or size_type


// work not correct

// search for first string "best" inside of str


// default position is 0
pos1 = str.find ("best");
cout << "Word best is found on position " << pos1+1
<< endl;

// if pattern is not found - return -1


pos2 = str.find ("best",pos1+1);
cout << "Word best is found on position " << pos2+1
<< endl;

// search for first occurrence of character


pos1 = str.find('g');
cout << "First character 'g' found on position "
<< pos1
<< endl;
// search for first occurrence of string
string s = "is";
pos1 = str.find (s);
cou
t << "Word 'is' is found on position " << pos1+1
<< endl;
String s=string.concat()
Cout<<”str1 += str2”>>;
Cout<<”str = str1 + str2”>>;
}
String s=str.recur();
ret_str(char* s)
{
if (*s != '\0')

ret_str(s+1);
cout<<*(s);
}
}
}

Você também pode gostar