Міністерство освіти та науки України
Національний університет «Львівська політехніка»
ЗВІТ
З лабораторних робіт №6,7,8,10
З дисципліни: «Програмування ч.4»
Мета:
6) Визначити чи першi символи рядка задають десяткове число, якщо так вивести це 10-ве число
7) реалiзувати 2 стеки на одному масивi
8) Ввести дерево i вершину, визначити скiльки вершин є серед нащадкiв заданої вершини
10) Ввести рядок i визначити чи в цьому рядку є пiдрядок abcd
Блок-схеми:
6)
7)
8)
10)
Лістінги програм:
/****************************************************************\
FILE..........: main.cpp
AUTHOR........: Nazar
DESCRIPTION...: The header file contains programming_labs.
METHOD........: result, Similar_elements, get_Count, Generate, Create_arr, show_arr, mod_arr, Exit, Backtracking
FUNCTIONS.....: factorial, Similar_elements, Generate, Create_arr, show_arr, mod_arr, Exit, Backtracking
SWITCHES......: WIN32 - if defined, 32-bit version is
compiled, otherwise 16-bit edition is compiled.
COPYRIGHT.....: Copyright (c) 2010.
HISTORY.......: DATE COMMENT
-------- --------------------------------------
-
04-12-10 Created - Roman
04-13-10 Modified - Roman
\****************************************************************/
/*===============================[ PUBLIC DECLARATIONS ]========*/
#include <iostream>
#include <cstdlib>
#include <string>
#include <clocale>
#include <conio.h>
#include "main1.h"
using namespace std;
int main()
{
/*===============================[ OBJECT DECLARATIONS ]=======*/
string szText;
CText* text;
int iN;
CTree* tree; // pointer by CTree
int iCount = 0;
int iLeafs;
int* piTree[3];
int iRoot;
CStacks* stacks;
int iM;
/*===============================[ PRIVATE DECLARATIONS]=======*/
int labs;
setlocale(2,"");
while (1)
{
system("cls");
cout << "Enter number of labs: ";
labs = _getch() - 48;
cout << labs << endl;
switch(labs)
{
case 6:
cout << "Object-oriented method" << endl;
cout << "Визначити чи першi символи рядка задають десяткове число, якщо так вивести це 10-ве число" << endl;
cout << "Enter text: ";
cin >> szText;
text = new CText(szText);
text->Seek();
system("PAUSE");
system("cls");
cout << "Procedure-oriented method" << endl;
cout << "Enter text: ";
cin >> szText;
Seek_dec(szText);
system("PAUSE");
break;
case 7:
cout << "Object-oriented method" << endl;
cout << "Завдання: реалiзувати 2 стеки на одному масивi" << endl;
cout << "n = ";
cin >> iN;
stacks = new CStacks(iN);
stacks->Two_stacks();
system("PAUSE");
system("cls");
cout << "Procedure-oriented method" << endl;
cout << "n = ";
cin >> iN;
Two_stack(iN);
system("PAUSE");
break;
case 8:
cout << "Object-oriented method" << endl;
cout << "Ввести дерево i вершину, визначити скiльки вершин є серед нащадкiв заданої" << endl;
cout << "n = ";
cin >> iM;
iM++;
tree = new CTree(iM);
tree->show_tree();
iLeafs = tree->Seek_Leafs(tree->Get_root());
cout <<"Count: " << tree->Get_count()-iLeafs << endl;
system("PAUSE");
system("cls");
cout << "Procedure-oriented method" << endl;
cout << "n = ";
cin >> iM;
iM++;
for (int i=0;i<3;i++)
{
piTree[i] = new int[iM];
}
Enter_tree(piTree,iM);
cout << "Vershuna = ";
cin >> iRoot;
show_tree(piTree,iM);
iLeafs = Seek_Leafs(piTree,iRoot,&iCount);
cout <<"Count: " << iCount-iLeafs << endl;
system("PAUSE");
break;
case 9:
cout << "Object-oriented method" << endl;
system("PAUSE");
system("cls");
cout << "Procedure-oriented method" << endl;
system("PAUSE");
break;
case 0:
cout << "Object-oriented method" << endl;
cout << "Ввести рядок i визначити чи в цьому рядку є пiдрядок abcd" << endl;
cout << "Enter text:" << endl;
cin >> szText;
text = new CText(szText);
if (text->Seek_sub())
{
cout << "Is \"abcd\"" << endl;
}
else
{
cout << "Is not \"abcd\"" << endl;
}
system("PAUSE");
system("cls");
cout << "Procedure-oriented method" << endl;
cout << "Enter text:" << endl;
cin >> szText;
if (Seek_sub(szText))
{
cout << "Is \"abcd\"" << endl;
}
else
{
cout << "Is not \"abcd\"" << endl;
}
system("PAUSE");
break;
default:
system("pause");
return 0;
}
}
}
/****************************************************************\
METHOD........: CText
DESCRIPTION...: Inicialization
ATTRIBUTES....: Public
ARGUMENTS.....:
szText string text
RETURNS.......: void
\****************************************************************/
CText::CText(string szText)
{
m_szText = szText;
}
/****************************************************************\
METHOD........: Seek
DESCRIPTION...: Seek decimal
ATTRIBUTES....: Public
ARGUMENTS.....:
szText string text
RETURNS.......: void
\****************************************************************/
int CText::Seek()
{
if (m_szText[0]=='-'||m_szText[0]=='+')
{
if (0<=m_szText[1]-48&&9>=m_szText[1]-48)
{
cout << m_szText[0];
}
else
{
cout << endl;
return 1;
}
for(unsigned i = 1; i<m_szText.length();i++)
{
if ((0<=m_szText[i]-48 && 9>=m_szText[i]-48)||m_szText[i]=='.'||m_szText[i]==',')
{
cout << m_szText[i];
}
else
{
cout << endl;
return 1;
}
}
cout << endl;
}
else
{
for(unsigned i =0; i<m_szText.length();i++)
{
if ((0<=m_szText[i]-48 && 9>=m_szText[i]-48)||m_szText[i]=='.'||m_szText[i]==',')
{
cout << m_szText[i];
}
else
{
cout << endl;
return 1;
}
}
cout << endl;
}
return 0;
}
/****************************************************************\
FUNCTION......: Seek_dec
DESCRIPTION...: Seek decimal
ATTRIBUTES....: Public
ARGUMENTS.....:
szText - text string
RETURNS.......: void
\****************************************************************/
int Seek_dec(string szText)
{
if (szText[0]=='-'||szText[0]=='+')
{
if (0<=szText[1]-48&&9>=szText[1]-48)
{
cout << szText[0];
}
else
{
cout << endl;
return 1;
}
for(unsigned i = 1; i<szText.length();i++)
{
if ((0<=szText[i]-48&&9>=szText[i]-48)||szText[i]=='.'||szText[i]==',')
{
cout << szText[i];
}
else
{
cout << endl;
return 1;
}
}
cout << endl;
}
else
{
for(unsigned i = 0; i<szText.length();i++)
{
if ((0<=szText[i]-48&&9>=szText[i]-48)||szText[i]=='.'||szText[i]==',')
{
cout << szText[i];
}
else
{
cout << endl;
return 1;
}
}
cout << endl;
}
return 0;
}
/****************************************************************\
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 << "Enter root: ";
cin >> iTemp;
if (iTemp)
{
m_piTree[i][j]=iTemp;
continue;
}
else
{
m_bNext = false;
continue;
}
}
else
{
if (i==1)
{
cout << "Left sun " << 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 << "Right sun " << j <<": ";
cin >> iTemp;
if (iTemp==-1)
{
m_piTree[i][j]=0;
m_bNext=false;
break;
}
else
{
m_piTree[i][j]=iTemp;
}
}
}
}
}
cout << "Vershuna = ";
cin >> m_root;
}
/****************************************************************\
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;
}
/****************************************************************\
METHOD........: Get_count
DESCRIPTION...: return count
ATTRIBUTES....: Public
ARGUMENTS.....: none
RETURNS.......: int
\****************************************************************/
int CTree::Get_count()
{
return m_iCount;
}
/****************************************************************\
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, int* iCount)
{
if (ppiTree[0][iNext] == 0)
{
return 0;
}
else
{
if (ppiTree[1][iNext]==0 && ppiTree[2][iNext]==0)
{
*iCount=*iCount+1;
return 1;
}
else
{
*iCount=*iCount+1;
return Seek_Leafs(ppiTree,ppiTree[1][iNext],iCount) + Seek_Leafs(ppiTree,ppiTree[2][iNext],iCount);
}
}
}
/****************************************************************\
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)
{
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 << "Enter Root: ";
cin >> iTemp;
if (iTemp)
{
ppiTree[i][j]=iTemp;
continue;
}
else
{
bNext = false;
continue;
}
}
else
{
if (i==1)
{
cout << "Left Sun " << j << ": ";
cin >> iTemp;
if (iTemp==-1)
{
ppiTree[i][j]=0;
bNext=false;
break;
}
else
{
ppiTree[i][j]=iTemp;
}
}
if (i==2)
{
cout << "Right sun " << j <<": ";
cin >> iTemp;
if (iTemp==-1)
{
ppiTree[i][j]=0;
bNext=false;
break;
}
else
{
ppiTree[i][j]=iTemp;
}
}
}
}
}
}
CStacks::CStacks(int iN)
{
m_piStacks = new int[iN];
m_iN = iN;
for (int i=0;i<iN;i++)
{
m_piStacks[i]=0;
}
}
void CStacks::Two_stacks()
{
bool bExit = true;
int iTemp;
for (int i=0;i<m_iN;i++)
{
m_piStacks[i]=0;
}
while (bExit)
{
cout << endl;
for (int i=0;i<m_iN;i++)
{
cout << m_piStacks[i] << " ";
}
cout << endl;
cout << "1 - add to first stack" << endl;
cout << "2 - add to second stack" << endl;
cout << "3 - remove from first stack" << endl;
cout << "4 - remove from second stack" << endl;
cout << "Other - exit" << endl;
switch(_getch()-48)
{
case 1:
for (int i=0;i<m_iN;i++)
{
if(m_piStacks[i]==0)
{
cout << "elem = ";
cin >> iTemp;
m_piStacks[i] = iTemp;
break;
}
}
break;
case 2:
for (int i=m_iN-1;i>=0;i--)
{
if(m_piStacks[i]==0)
{
cout << "elem = ";
cin >> iTemp;
m_piStacks[i] = iTemp;
break;
}
}
break;
case 3:
for (int i=0;i<m_iN;i++)
{
if(m_piStacks[i]==0)
{
m_piStacks[i-1] = 0;
break;
}
}
break;
case 4:
for (int i=m_iN-1;i>=0;i--)
{
if(m_piStacks[i]==0)
{
m_piStacks[i+1] = 0;
break;
}
}
break;
default:
bExit = false;
}
}
}
void Two_stack(int iN)
{
int* piStacks = new int[iN];
bool bExit = true;
int iTemp;
for (int i=0;i<iN;i++)
{
piStacks[i]=0;
}
while (bExit)
{
cout << endl;
for (int i=0;i<iN;i++)
{
cout << piStacks[i] << " ";
}
cout << endl;
cout << "1 - add to first stack" << endl;
cout << "2 - add to second stack" << endl;
cout << "3 - remove from first stack" << endl;
cout << "4 - remove from second stack" << endl;
cout << "Other - exit" << endl;
switch(_getch()-48)
{
case 1:
for (int i=0;i<iN;i++)
{
if(piStacks[i]==0)
{
cout << "elem = ";
cin >> iTemp;
piStacks[i] = iTemp;
break;
}
}
break;
case 2:
for (int i=iN-1;i>=0;i--)
{
if(piStacks[i]==0)
{
cout << "elem = ";
cin >> iTemp;
piStacks[i] = iTemp;
break;
}
}
break;
case 3:
for (int i=0;i<iN;i++)
{
if(piStacks[i]==0)
{
piStacks[i-1] = 0;
break;
}
}
break;
case 4:
for (int i=iN-1;i>=0;i--)
{
if(piStacks[i]==0)
{
piStacks[i+1] = 0;
break;
}
}
break;
default:
bExit = false;
}
}
}
/****************************************************************\
METHOD........: Seek_sub
DESCRIPTION...: seek substring in the string
ATTRIBUTES....: Public
ARGUMENTS.....: none
RETURNS.......: bool
\****************************************************************/
bool CText::Seek_sub()
{
int iNum;
iNum = m_szText.find("abcd");
if (iNum==-1)
return false;
else
return true;
}
/****************************************************************\
FUNCTION......: Seek_sub
DESCRIPTION...: seek substring in the string
ATTRIBUTES....: Public
ARGUMENTS.....:
szText - text string
RETURNS.......: bool
\****************************************************************/
bool Seek_sub(string szText)
{
int iNum;
iNum = szText.find("abcd");
if (iNum==-1)
return false;
else
return true;
}
/** (END OF FILE : main.cpp)*********************************/
/****************************************************************\
FILE..........: main.cpp
AUTHOR........: Nazar
DESCRIPTION...: The header file contains programming_labs.
METHOD........: result, Similar_elements, get_Count, Generate, Create_arr, show_arr, mod_arr, Exit, Backtracking
FUNCTIONS.....: factorial, Similar_elements, Generate, Create_arr, show_arr, mod_arr, Exit, Backtracking
SWITCHES......: WIN32 - if defined, 32-bit version is
compiled, otherwise 16-bit edition is compiled.
COPYRIGHT.....: Copyright (c) 2010.