Javob: Quyidagi ikki o’lchovli massivni vektorda tasvirlang: Javob
Download 74.22 Kb.
|
Obidov Abdulaziz=2
- Bu sahifa navigatsiya:
- Javob: Quyidagi ikki o’lchovli massivni vektorda tasvirlang: Javob
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 Double turidagi qiymat vektorga quyidagicha qo’shiladi: Vektor.push_back(3); Double turidagi qiymat vektordan quyidagicha o’chiriladi: Vektor.pop_back(3); 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. Quyidagi massivni vektorda tasvirlang: Javob: Quyidagi ikki o’lchovli massivni vektorda tasvirlang: Javob: #include #include Using namespace std; int main() { vector {9,10,11,12},{13,14,15,16}}; for(vector for(int &col : row){ cout << col << “ ”; } cout << endl; } return 0; } Misollar: (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 cout << Vektor.empty() << endl; vector 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.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:
int size; int capacity; void ensureCapacity(); }; template Stack { elements = new T[capacity]; } template 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 { delete [] elements; } template bool Stack { return size == 0; } template T Stack { return elements[size - 1]; } template void Stack { ensureCapacity(); elements[size++] = value; } template void Stack { 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 { return elements[--size]; } template int Stack { return size; } #endif
Main.cpp #include #include #include #include #include #include "ImprovedStack.h" using namespace std; vector double evaluateExpression(const string& expression); void processAnOperator(Stack int main() { string expression; cout << "Ifodani kiriting: "; getline(cin, expression); cout << expression << " = " << evaluateExpression(expression) << endl; return 0; } vector { vector 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 Stack vector 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 { 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); } (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: |
ma'muriyatiga murojaat qiling