ضرب مصفوفتين ?!
كود تحويل infix to prefix
- وكود حساب قيمة العملية prefix
هاالبرامج ولاعرفت كيف احلهم بواجه صعوبه فيهم رغم الفهم
كود تحويل infix to prefix
- وكود حساب قيمة العملية prefix
هاالبرامج ولاعرفت كيف احلهم بواجه صعوبه فيهم رغم الفهم
نصائح و استشارات برمجية
كود تحويل infix to prefix
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int getPrecedence(char op) {
if (op == '+' || op == '-') {
return 1;
} else if (op == '*' || op == '/') {
return 2;
}
return 0;
}
string infixToPrefix(string infix) {
stack<char> operators;
stack<string> operands;
reverse(infix.begin(), infix.end());
for (char& c : infix) {
if (isalnum(c)) {
operands.push(string(1, c));
} else if (c == ')') {
operators.push(c);
} else if (c == '(') {
while (!operators.empty() && operators.top() != ')') {
string operand1 = operands.top();
operands.pop();
string operand2 = operands.top();
operands.pop();
char op = operators.top();
operators.pop();
operands.push(op + operand2 + operand1);
}
operators.pop();
} else if (isOperator(c)) {
while (!operators.empty() && getPrecedence(operators.top()) >= getPrecedence(c)) {
string operand1 = operands.top();
operands.pop();
string operand2 = operands.top();
operands.pop();
char op = operators.top();
operators.pop();
operands.push(op + operand2 + operand1);
}
operators.push(c);
}
}
while (!operators.empty()) {
string operand1 = operands.top();
operands.pop();
string operand2 = operands.top();
operands.pop();
char op = operators.top();
operators.pop();
operands.push(op + operand2 + operand1);
}
return operands.top();
}
int evaluatePrefix(string prefix) {
stack<int> s;
reverse(prefix.begin(), prefix.end());
for (char& c : prefix) {
if (isdigit(c)) {
s.push(c - '0');
} else {
int operand1 = s.top();
s.pop();
int operand2 = s.top();
s.pop();
switch (c) {
case '+':
s.push(operand1 + operand2);
break;
case '-':
s.push(operand1 - operand2);
break;
case '*':
s.push(operand1 * operand2);
break;
case '/':
s.push(operand1 / operand2);
break;
}
}
}
return s.top();
}
int main() {
string infixExpression;
cout << "Enter infix expression: ";
cin >> infixExpression;
string prefixExpression = infixToPrefix(infixExpression);
cout << "Prefix expression: " << prefixExpression << endl;
int result = evaluatePrefix(prefixExpression);
cout << "Result: " << result << endl;
return 0;
}
اخى عند سؤال بتكتب ايه علشان الكود يلون عشان عندى مش ظابطه، بلغة سي بلس بلس
انا تقريبا خلصتها ولكن المطلوب هو إضافة افكار جديدة الى هذه الآلة الحاسبة
نصائح و استشارات برمجية
Photo
بكل بساطة تقدري تشوفي افكار جديدة من الآلات الحاسبة المتقدمة في عصرنا الحالي و طبعا في انواع كتيرة، زي ما في الصور
نصائح و استشارات برمجية
اي الغلط
1. ناسية قوس معقوف زي ما في الصورة ⬆️
2. والحالات في switch بتتكتب كدا ⬇️
2. والحالات في switch بتتكتب كدا ⬇️
✅️✅️
case 1:
❎️❎️
case (1):