Міністерство освіти та науки України
Національний університет «Львівська політехніка»
ЗВІТ
З лабораторної роботи №6,7,8,9,10
З дисципліни: «Програмування ч.4»
Львів 2010
Мета:
6) Видалити з рядка коментарі, якщо коментарі частина рядка коментарі не видаляти.
7) Реалізувати чергу на двох стеках.
8) Підрахувати кількість листків в дереві.
9) Визначити мінімальну вартість перевезення між будь-якими точками і та j
10) З’ясувати чи присутнє слово «abcd» у введеному рядку
Блок-схема:
6)
7)
8)
9)
10)
Лістинг програм:
case 6:
cout << "Object-oriented method" << endl;
cout << "Umova: vudalutu komentari z rjadka, jakscho komentar chastuna rjadka komentar ne vudaljatu." << endl;
cout << "Enter string with comments:" << endl;
cin >> szText;
text = new CText(szText);
text->Delete_coments();
system("PAUSE");
system("cls");
cout << "Procedure-oriented method" << endl;
cout << "Enter string with comments:" << endl;
cin >> szText;
Del_comn(szText);
system("PAUSE");
break;
case 7:
cout << "Object-oriented method" << endl;
cout << "Umova: realizyvatu chergy na dvoh stekah" << endl;
cout << "n = ";
cin >> iN;
que = new CQueue(iN);
que->queue();
system("PAUSE");
system("cls");
cout << "Procedure-oriented method" << endl;
cout << "n = ";
cin >> iN;
Cherga(iN);
system("PAUSE");
break;
case 9:
setlocale(2,"");
cout << "Object-oriented method" << endl;
cout << "Умова: Вивести всi найкоротшi шляхи мiж пунктами i->j" << endl;
cout << "n = ";
cin >> iN;
graf = new CGraf(iN);
graf->Seek();
system("PAUSE");
system("cls");
cout << "Procedure-oriented method" << endl;
cout << "n = ";
cin >> iN;
Graf(iN);
system("PAUSE");
break;
case 8:
cout << "Object-oriented method" << endl;
cout << "Umova: porahyvatu kilkist lustkiv dereva" << endl;
cout << "n = ";
cin >> iN;
tree = new CTree(iN);
tree->show_tree();
iLeafs = tree->Seek_Leafs(tree->Get_root());
cout <<"Kilkist lustkiv dereva: " << iLeafs << endl;
system("PAUSE");
system("cls");
cout << "Procedure-oriented method" << endl;
int* piTree[3];
cout << "n = ";
cin >> iN;
iRoot = 0;
for (int i=0;i<3;i++)
{
piTree[i] = new int[iN];
}
Enter_tree(piTree,iN, &iRoot);
show_tree(piTree,iN);
iLeafs = Seek_Leafs(piTree,iRoot);
cout <<"Kilkist lustkiv dereva: " << iLeafs << endl;
system("PAUSE");
break;
case 0:
cout << "Object-oriented method" << endl;
cout << "Umova: perevirutu chu v rjadky e pidrjadok \"abcd\"" << endl;
cout << "Enter text:" << endl;
cin >> szText;
text = new CText(szText);
if (text->Seek_sub())
{
cout << "Yes \"abcd\"" << endl;
}
else
{
cout << "No \"abcd\"" << endl;
}
system("PAUSE");
system("cls");
cout << "Procedure-oriented method" << endl;
cout << "Enter text:" << endl;
cin >> szText;
if (Seek_sub(szText))
{
cout << "Yes \"abcd\"" << endl;
}
else
{
cout << "No \"abcd\"" << endl;
}
system("PAUSE");
/*=============[ LABA #6 ]===================*/
/****************************************************************\
METHOD........: CText
DESCRIPTION...: Initializing variables
ATTRIBUTES....: Public
ARGUMENTS.....:
czText - text string with coments
RETURNS.......: None
\****************************************************************/
CText::CText(string szText)
{
m_szText = szText;
m_iStart = m_szText.find('{');
m_iFinish = m_szText.find('}');
}
/****************************************************************\
METHOD........: Delete_coments
DESCRIPTION...: Delete coments
ATTRIBUTES....: Public
ARGUMENTS.....: none
RETURNS.......: void
\****************************************************************/
void CText::Delete_coments()
{
m_iStartStr = m_szText.find('"');
m_iFinishStr = m_szText.rfind('"');
m_iStart = m_szText.find('{');
m_iFinish = m_szText.find('}');
if (m_szText.find('{')!=-1)
{
if (m_iStartStr==-1 && m_iFinishStr==-1)
{
m_szText[m_iStart]=' ';
m_szText.erase(m_iStart+1,m_iFinish-m_iStart);
}
else
{
if (m_iStartStr > m_iStart && m_iFinishStr < m_iFinish)
{
m_szText[m_iStart]=' ';
m_szText.erase(m_iStart+1,m_iFinish-m_iStart);
}
}
}
cout << m_szText << endl;
}
/****************************************************************\
FUNCTION......: Del_comn
DESCRIPTION...: Delete coments
ATTRIBUTES....: Public
ARGUMENTS.....:
czText - text string with coments
RETURNS.......: void
\****************************************************************/
void Del_comn(string szText)
{
int iStart, iFinish, iStartStr, iFinishStr;
iStartStr = szText.find('"');
iFinishStr = szText.rfind('"');
iStart = szText.find('{');
iFinish = szText.find('}');
if (szText.find('{')!=-1)
{
if (iStartStr==-1 && iFinishStr==-1)
{
szText[iStart]=' ';
szText.erase(iStart+1,iFinish-iStart);
}
else
{
if (iStartStr > iStart && iFinishStr < iFinish)
{
szText[iStart]=' ';
szText.erase(iStart+1,iFinish-iStart);
}
}
}
cout << szText << endl;
}
/*=============[End Laba 6]=================*/
/*=============[Laba 7]=================*/
/****************************************************************\
METHOD........: CQueue
DESCRIPTION...: Initializing variables
ATTRIBUTES....: Public
ARGUMENTS.....:
iN - size of Queue
RETURNS.......: None
\****************************************************************/
CQueue::CQueue(int iN)
{
m_iN = iN;
m_iStekSt = new int[iN];
m_iStekFn = new int[iN];
m_iIndexSt = 0;
m_iIndexFn = 0;
m_iTemp;
for (int i=0;i<iN;i++)
{
m_iStekSt[i]=0;
m_iStekFn[i]=0;
}
}
/****************************************************************\
METHOD........: CQueue
DESCRIPTION...: Show_queue
ATTRIBUTES....: Public
ARGUMENTS.....:
iArr - Queue
RETURNS.......: void
\****************************************************************/
void CQueue::Show_queue(int* iArr)
{
for (int i=0;i<m_iN;i++)
{
cout << iArr[i] << " ";
}
cout << endl;
}
/****************************************************************\
METHOD........: queue
DESCRIPTION...: Demo queue
ATTRIBUTES....: Public
ARGUMENTS.....: none
RETURNS.......: void
\****************************************************************/
void CQueue::queue()
{
int oper;
do
{
cout << "1 - add to queue" << endl;
cout << "2 - remove to queue" << endl;
cout << "0 - exit" << endl;
cin >> oper;
if (oper==1)
{
if (m_iIndexFn<m_iN)
{
cout << "Enter Element: ";
cin >> m_iStekFn[m_iIndexFn];
m_iIndexFn++;
Show_queue(m_iStekFn);
Show_queue(m_iStekSt);
}
else
{
cout << "chaerga povna" << endl;
}
}
if (oper==2)
{
if(m_iIndexFn>0)
{
m_iIndexFn--;
while (m_iIndexFn>-1)
{
m_iTemp = m_iStekFn[m_iIndexFn];
m_iStekFn[m_iIndexFn] = 0;
m_iIndexFn--;
m_iStekSt[m_iIndexSt] = m_iTemp;
m_iIndexSt++;
}
m_iIndexFn++;
m_iIndexSt--;
m_iStekSt[m_iIndexSt]=0;
Show_queue(m_iStekFn);
Show_queue(m_iStekSt);
if (m_iIndexSt>0)
{
while (m_iIndexSt>-1)
{
m_iTemp = m_iStekSt[m_iIndexSt];
m_iStekSt[m_iIndexSt] = 0;
m_iIndexSt--;
m_iStekFn[m_iIndexFn] = m_iTemp;
m_iIndexFn++;
}
Show_queue(m_iStekFn);
Show_queue(m_iStekSt);
m_iIndexSt++;
}
}
else
{
cout << "chaerga porojnja" << endl;
}
}
}while(oper!=0);
}
/****************************************************************\
FUNCTION......: Show_queue
DESCRIPTION...: Show Queue
ATTRIBUTES....: Public
ARGUMENTS.....:
iArr - Queue
iN - size of Queue
RETURNS.......: void
\****************************************************************/
void Show_queue(int* iArr, int iN)
{
for (int i=0;i<iN;i++)
{
cout << iArr[i] << " ";
}
cout << endl;
}
/****************************************************************\
METHOD........: Cherga
DESCRIPTION...: Demo queue
ATTRIBUTES....: Public
ARGUMENTS.....:
in - size of queue
RETURNS.......: void
\****************************************************************/
void Cherga (int iN)
{
int* iStekSt = new int[iN];
int* iStekFn = new int[iN];
int iIndexSt = 0;
int iIndexFn = 0;
int iTemp;
for (int i=0;i<iN;i++)
{
iStekSt[i]=0;
iStekFn[i]=0;
}
int oper;
do
{
cout << "1 - add to queue" << endl;
cout << "2 - remove to queue" << endl;
cout << "0 - exit" << endl;
cin >> oper;
if (oper==1)
{
if (iIndexFn<iN)
{
cout << "Enter Element: ";
cin >> iStekFn[iIndexFn];
iIndexFn++;
Show_queue(iStekFn, iN);
Show_queue(iStekSt, iN);
}
else
{
cout << "chaerga povna" << endl;
}
}
if (oper==2)
{
if(iIndexFn>0)
{
iIndexFn--;
while (iIndexFn>-1)
{
iTemp = iStekFn[iIndexFn];
iStekFn[iIndexFn] = 0;
iIndexFn--;
iStekSt[iIndexSt] = iTemp;
iIndexSt++;
}
iIndexFn++;
iIndexSt--;
iStekSt[iIndexSt]=0;
Show_queue(iStekFn, iN);
Show_queue(iStekSt, iN);
if (iIndexSt>0)
{
while (iIndexSt>-1)
{
iTemp = iStekSt[iIndexSt];
iStekSt[iIndexSt] = 0;
iIndexSt--;
iStekFn[iIndexFn] = iTemp;
iIndexFn++;
}
Show_queue(iStekFn, iN);
Show_queue(iStekSt, iN);
iIndexSt++;
}
}
else
{
cout << "chaerga porojnja" << endl;
}
}
}while(oper!=0);
}
/*=============[End Laba 7]=================*/
/*=============[LABA 8]=====================*/
/****************************************************************\
METHOD........: CTree
DESCRIPTION...: Initializing variables
ATTRIBUTES....: Public
ARGUMENTS.....:
iN - size of tree
RETURNS.......: None
\****************************************************************/
CTree::CTree(int iN)
{
m_iCount = 0;
m_iN = iN;
m_root = 0;
for (int i=0;i<3;i++)
{
m_piTree[i] = new int[iN];
}
m_bNext = true;
for(int i=0;i<3;i++)
{
for(int j=0;j<iN;j++)
{
m_piTree[i][j]=0;
}
}
for(int j=0,iTemp;j<iN;j++)
{
if (m_bNext)
for(int i=0;i<3;i++)
{
if (j == 0)
{
m_piTree[i][j] = 0;
continue;
}
if (i == 0)
{
m_piTree[i][j] = j;
}
if (j==1 && i==0)
{
cout << "Vvedit koin dereva: ";
cin >> iTemp;
if (iTemp)
{
m_piTree[i][j]=iTemp;
m_root = iTemp;
continue;
}
else
{
m_bNext = false;
continue;
}
}
else
{
if (i==1)
{
cout << "Livuu naschadok enementa " << j << ": ";
cin >> iTemp;
if (iTemp==-1)
{
m_piTree[i][j]=0;
m_bNext=false;
break;
}
else
{
m_piTree[i][j]=iTemp;
}
}
if (i==2)
{
cout << "Pravuu naschadok elementa " << j <<": ";
cin >> iTemp;
if (iTemp==-1)
{
m_piTree[i][j]=0;
m_bNext=false;
break;
}
else
{
m_piTree[i][j]=iTemp;
}
}
}
}
}
}
/****************************************************************\
METHOD........: show_tree
DESCRIPTION...: Show array of tree
ATTRIBUTES....: Public
ARGUMENTS.....: None
RETURNS.......: void
\****************************************************************/
void CTree::show_tree()
{
for(int i=0;i<3;i++)
{
for(int j=0, iNum;j<m_iN;j++)
{
cout << m_piTree[i][j] << " ";
}
cout << endl;
}
}
/****************************************************************\
METHOD........: Seek_Leafs
DESCRIPTION...: Comput the numbers of Leafs
ATTRIBUTES....: Public
ARGUMENTS.....:
iNext - next element of tree
RETURNS.......: int
\****************************************************************/
int CTree::Seek_Leafs(int iNext)
{
if (m_piTree[0][iNext] == 0)
{
return 0;
}
else
{
if (m_piTree[1][iNext]==0 && m_piTree[2][iNext]==0)
{
m_iCount++;
return 1;
}
else
{
m_iCount++;
return Seek_Leafs(m_piTree[1][iNext]) + Seek_Leafs(m_piTree[2][iNext]);
}
}
}
/****************************************************************\
METHOD........: Get_root
DESCRIPTION...: return root
ATTRIBUTES....: Public
ARGUMENTS.....: none
RETURNS.......: int
\****************************************************************/
int CTree::Get_root()
{
return m_root;
}
/****************************************************************\
FUNCTION......: Seek_Leafs
DESCRIPTION...: Comput the numbers of Leafs
ATTRIBUTES....: Public
ARGUMENTS.....:
ppiTree - tree
iNext - next element of tree
iCount - count of Leafs
RETURNS.......: int
\****************************************************************/
int Seek_Leafs(int **ppiTree, int iNext)
{
if (ppiTree[0][iNext] == 0)
{
return 0;
}
else
{
if (ppiTree[1][iNext]==0 && ppiTree[2][iNext]==0)
{
return 1;
}
else
{
return Seek_Leafs(ppiTree,ppiTree[1][iNext]) + Seek_Leafs(ppiTree,ppiTree[2][iNext]);
}
}
}
/****************************************************************\
FUNCTION......: show_tree
DESCRIPTION...: Show array of tree
ATTRIBUTES....: Public
ARGUMENTS.....:
ppiTree - tree
iN - size of tree
RETURNS.......: int
\****************************************************************/
void show_tree(int** ppiTree,int iN)
{
for(int i=0;i<3;i++)
{
for(int j=0, iNum;j<iN;j++)
{
cout << ppiTree[i][j] << " ";
}
cout << endl;
}
}
/****************************************************************\
FUNCTION......: Enter_tree
DESCRIPTION...: Enter elements of tree
ATTRIBUTES....: Public
ARGUMENTS.....:
ppiTree - tree
iN - size of tree
root - root of tree
RETURNS.......: int
\****************************************************************/
void Enter_tree(int** ppiTree, int iN, int* root)
{
bool bNext = true;
for(int i=0;i<3;i++)
{
for(int j=0, iNum;j<iN;j++)
{
ppiTree[i][j]=0;
}
}
for(int j=0,iTemp;j<iN;j++)
{
if (bNext)
for(int i=0;i<3;i++)
{
if (j == 0)
{
ppiTree[i][j] = 0;
continue;
}
if (i == 0)
{
ppiTree[i][j] = j;
}
if (j==1 && i==0)
{
cout << "Vvedit korin dereva: ";
cin >> iTemp;
if (iTemp)
{
ppiTree[i][j]=iTemp;
*root = iTemp;
continue;
}
else
{
bNext = false;
continue;
}
}
else
{
if (i==1)
{
cout << "Livuu naschadok elementa " << j << ": ";
cin >> iTemp;
if (iTemp==-1)
{
ppiTree[i][j]=0;
bNext=false;
break;
}
else
{
ppiTree[i][j]=iTemp;
}
}
if (i==2)
{
cout << "Pravuu naschadok elementa " << j <<": ";
cin >> iTemp;
if (iTemp==-1)
{
ppiTree[i][j]=0;
bNext=false;
break;
}
else
{
ppiTree[i][j]=iTemp;
}
}
}
}
}
}
/*=============[End Laba 8]=================*/
/*=================[Laba 9]=================*/
/****************************************************************\
METHOD........: CGraf
DESCRIPTION...: Initializing variables
ATTRIBUTES....: Public
ARGUMENTS.....:
iN - size of graf
RETURNS.......: None
\****************************************************************/
CGraf::CGraf(int iN)
{
m_iN = iN;
}
/****************************************************************\
METOD.........: Seek
DESCRIPTION...: Seek Negative way
ATTRIBUTES....: Public
ARGUMENTS.....: none
RETURNS.......: bool
\****************************************************************/
bool CGraf::Seek()
{
vector < vector<int> > G (m_iN, vector<int> (m_iN));
for (int i=0; i<m_iN; i++)
for (int j=0; j<m_iN; j++)
{
int iTemp;
cout << i+1 << " -> " <<j+1 <<" = ";
cin >> iTemp;
G[i][j] = iTemp ? iTemp : g_iINF;
}
vector<vector<int> > D (m_iN), D2;
D2 = G;
for (int i=0; i<m_iN; i++)
D[i].resize (m_iN+1);
for (int k=0; k<m_iN; k++)
{
for (int i=0; i<m_iN; i++)
for (int j=0; j<m_iN; j++)
D[i][j] = min (D2[i][j], D2[i][k]+D2[k][j]);
D.swap (D2);
}
D.swap (D2);
for (int i=0; i<m_iN; i++)
{
for (int j=0; j<m_iN; j++)
{
cout << "Min way " << i << " -> " << j << " = " << D[i][j] << endl;
}
}
return false;
}
/****************************************************************\
FUNCTION......: Graf
DESCRIPTION...: Seek Negative way
ATTRIBUTES....: Public
ARGUMENTS.....:
iN - size of graf
RETURNS.......: bool
\****************************************************************/
bool Graf (int iN)
{
vector < vector<int> > G (iN, vector<int> (iN));
for (int i=0; i<iN; i++)
for (int j=0; j<iN; j++)
{
int iTemp;
cout << i+1 << " -> " <<j+1 <<" = ";
cin >> iTemp;
G[i][j] = iTemp ? iTemp : g_iINF;
}
vector<vector<int