7-hafta mustaqil ta’lim topshiriq javoblari


Download 159.68 Kb.
bet2/2
Sana14.01.2023
Hajmi159.68 Kb.
#1093075
1   2
Bog'liq
7-vazifa

Natijasi:


  1. (vector sinfini implementatsiyasi) C++ dagi vector sinfini o’zingiz mustaqil ravishda implementatsiya qiling. Standart vector sinfida ko’plab funksiyalar mavjud. Siz 12.2 chizmada UML diagrammada berilgan funksiyalarni implementatsiya qiling.


#include
#include
using namespace std;

int main()


{
vector Vektor;
cout << Vektor.empty() << endl;

vector Vektor1(4);


Vektor1.push_back(4);
Vektor1.push_back(5);
Vektor1.push_back(6);
Vektor1.push_back(7);
for(unsigned i = 0; i < Vektor1.size(); i++)
cout << Vektor1[i] << " ";
cout << endl;

vector Vektor2(3, 1);


Vektor2.push_back(1);
Vektor2.push_back(2);
Vektor2.push_back(9);
Vektor2.pop_back();
for(unsigned i = 0; i < Vektor1.size(); i++)
cout << Vektor1[i] << " ";
cout << endl;
Vektor1.at(3) = 123;
for(unsigned i = 0; i < Vektor1.size(); i++)
cout << Vektor1[i] << " ";
cout << endl;

Vektor1.clear();


for(unsigned i = 0; i < Vektor1.size(); i++)
cout << Vektor1[i] << " ";
cout << endl;
Vektor.swap(Vektor2);
for(unsigned i = 0; i < Vektor.size(); i++)
cout << Vektor[i] << " ";
cout << endl;

return 0;


}



  1. (stek sinfini vektordan foydalangan holda qayta implementatsiya qiling) 12.4 listingda GenericStack sinfi massivdan foydalanilgan holda implementatsiya qilingan. Uni vektordan foydalangan holda implementatsiya qiling.

Javob:
Improvedsatck.h
#ifndef IMPROVEDSTACK_H
#define IMPROVEDSTACK_H

template


class Stack
{
public:
Stack();
Stack(const Stack&);
~Stack();
bool empty() const;
T peek() const;
void push(T value);
T pop();
int getSize() const;

private:
T* elements;


int size;
int capacity;
void ensureCapacity();
};

template


Stack::Stack(): size(0), capacity(16)
{
elements = new T[capacity];
}

template


Stack::Stack(const Stack& stack)
{
elements = new T[stack.capacity];
size = stack.size;
capacity = stack.capacity;
for (int i = 0; i < size; i++)
{
elements[i] = stack.elements[i];
}
}

template


Stack::~Stack()
{
delete [] elements;
}

template


bool Stack::empty() const
{
return size == 0;
}

template


T Stack::peek() const
{
return elements[size - 1];
}

template


void Stack::push(T value)
{
ensureCapacity();
elements[size++] = value;
}

template


void Stack::ensureCapacity()
{
if (size >= capacity)
{
T* old = elements;
capacity = 2 * size;
elements = new T[size * 2];

for (int i = 0; i < size; i++)


elements[i] = old[i];

delete [] old;


}
}

template


T Stack::pop()
{
return elements[--size];
}

template


int Stack::getSize() const
{
return size;
}

#endif


Main.cpp
#include
#include
#include
#include
#include
#include "ImprovedStack.h"

using namespace std;


vector split(const string& expression);


double evaluateExpression(const string& expression);


void processAnOperator(Stack& operandStack, Stack& operatorStack);


int main()


{
string expression;
cout << "Ifodani kiriting: ";
getline(cin, expression);

cout << expression << " = "


<< evaluateExpression(expression) << endl;

return 0;


}

vector split(const string& expression)


{
vector v;
string numberString;

for (unsigned i = 0; i < expression.length(); i++)


{
if (isdigit(expression[i]))
numberString.append(1, expression[i]);
else
{
if (numberString.size() > 0)
{
v.push_back(numberString);
numberString.erase();
}

if (!isspace(expression[i]))


{
string s;
s.append(1, expression[i]);
v.push_back(s);
}
}
}

if (numberString.size() > 0)


v.push_back(numberString);

return v;


}

double evaluateExpression(const string& expression)


{

Stack operandStack;


Stack operatorStack;


vector tokens = split(expression);


for (unsigned i = 0; i < tokens.size(); i++)


{
if (tokens[i][0] == '+' || tokens[i][0] == '-')
{
while (!operatorStack.empty() && (operatorStack.peek() == '+'
|| operatorStack.peek() == '-' || operatorStack.peek() == '*'
|| operatorStack.peek() == '/'))
{
processAnOperator(operandStack, operatorStack);
}
operatorStack.push(tokens[i][0]);
}
else if (tokens[i][0] == '*' || tokens[i][0] == '/')
{

while (!operatorStack.empty() && (operatorStack.peek() == '*'


|| operatorStack.peek() == '/'))
{
processAnOperator(operandStack, operatorStack);
}

operatorStack.push(tokens[i][0]);


}
else if (tokens[i][0] == '(')
{
operatorStack.push('(');
}
else if (tokens[i][0] == ')')
{
while (operatorStack.peek() != '(')
{
processAnOperator(operandStack, operatorStack);
}

operatorStack.pop();


}
else
{
operandStack.push(atoi(tokens[i].c_str()));
}
}

while (!operatorStack.empty())


{
processAnOperator(operandStack, operatorStack);
}

return operandStack.pop();


}

void processAnOperator(


Stack& operandStack, Stack& operatorStack)
{
char op = operatorStack.pop();
double op1 = operandStack.pop();
double op2 = operandStack.pop();
if (op == '+')
operandStack.push(op2 + op1);
else if (op == '-')
operandStack.push(op2 - op1);
else if (op == '*')
operandStack.push(op2 * op1);
else if (op == '/')
operandStack.push(op2 / op1);
}


Download 159.68 Kb.

Do'stlaringiz bilan baham:
1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling