7-hafta mustaqil ta’lim topshiriq javoblari
Download 159.68 Kb.
|
7-vazifa
Natijasi:
(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 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; } (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:
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); } Download 159.68 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling