Javob: Quyidagi ikki o’lchovli massivni vektorda tasvirlang: Javob


Download 74.22 Kb.
bet1/2
Sana22.10.2023
Hajmi74.22 Kb.
#1715869
  1   2
Bog'liq
Obidov Abdulaziz=2




  1. double turidagi qiymatlar bilan ishlovchi vektor qanday e’lon qilinadi? double turidagi qiymat vektorga qanday qo’shiladi? double turidagi qiymat vektordan qanday o’chiriladi?

Javob:
Double turidagi qiymatlar bilan ishlovchi vektor quyidagicha e’lon qilinadi:
vector Vektor;
Double turidagi qiymat vektorga quyidagicha qo’shiladi:
Vektor.push_back(3);
Double turidagi qiymat vektordan quyidagicha o’chiriladi:
Vektor.pop_back(3);

  1. Quyidagi kodda (a) va (b) holatdan qaysi biri to’g’ri? Noto’g’ri holat mavjud bo’lsa, sababini tushuntirib bering:


Javob:
Yuqoridagi b) holatdagi kod noto’g’ri, chunki, vektorning boshlang’ich o’lchami 5 qilib berilgan va v[0] = 4 qilib berilgan. Bu holatda vektorning boshlang’ich beshta qiymati 4 qilib ta’minlanadi. 6-elementdan boshlab esa yangi qiymat kiritish mumkin. Vektorni chaqirganda esa biz qiymatlarni noto’g’ri olib qo’yishimiz mumkin.
Lekin bu yerda compilyatsiya davomida xatolik yo’q, shunchaki vectorlarni e’lon qilishda xatolik bor.

  1. Quyidagi massivni vektorda tasvirlang:


Javob:




  1. Quyidagi ikki o’lchovli massivni vektorda tasvirlang:



Javob:
#include
#include
Using namespace std;

int main()


{
vector> v = {{1,2,3,4},{5,6,7,8},
{9,10,11,12},{13,14,15,16}};
for(vector & row:v){
for(int &col : row){
cout << col << “ ”;
}
cout << endl;
}
return 0;
}



Misollar:



  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.

Javob:


#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;


}



6.#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);
}


  1. (Sarala) Quyidagicha berilgan funksiyaning tanasini davom ettiring.

Funksiya massiv elementlarini saralashini tekshiring.
template
bool isSarala(const T list[], int size)
Funksiyani int, double va string turlaridagi qiymatlar bilan tekshiring.

Download 74.22 Kb.

Do'stlaringiz bilan baham:
  1   2




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