View Full Version : نمونه تمرینهایc++
a121212
دوشنبه 03 بهمن 1390, 11:07 صبح
تبدیل این فیکس به پرفیکس
djsaeedkhan
دوشنبه 03 بهمن 1390, 11:56 صبح
سلام
احتمالا اینجا فرم php نیست
#include <iostream>
#include <cstdlib>
#include <string.h>
#include <stack>
using namespace std;
int priority(char op);
int main()
{
char ar[80];
stack<char> s_1, s_2;
cin >> ar;
int length = strlen(ar);
for(int i = length - 1; i >= 0; i--)
{
switch(ar[i])
{
case ')':
s_1.push(ar[i]);
break;
case '(':
{
while(s_1.top() != ')')
{
s_2.push(s_1.top());
s_1.pop();
}
s_1.pop();
break;
}
case '+': case '-': case '*': case '/':
{
while(priority(s_1.top()) > priority(ar[i]))
{
s_2.push(s_1.top());
s_1.pop();
}
s_1.push(ar[i]);
break;
}
default:
{
s_2.push(ar[i]);
break;
}
}
}
while(!s_1.empty())
{
s_2.push(s_1.top());
s_1.pop();
}
while(!s_2.empty())
{
cout << s_2.top();
s_2.pop();
}
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
int priority(char op)
{
int p;
switch (op)
{
case '+' :
case '-' :
p = 1;
break;
case '*' :
case '/' :
p = 2;
break;
default:
p = 0;
break;
}
return p;
}
djsaeedkhan
دوشنبه 03 بهمن 1390, 11:57 صبح
#include "stdafx.h"
#include <iostream>
#include "conio.h"
#include <string.h>
#define MAX 20
using namespace std;
char stack[MAX];
int top = -1;
//Push Function
void push(char item){
top++;
stack[top]=item;
}
//Pop Function
char pop(){
char a;
a=stack[top];
top--;
return a;
}
//Function to analyze the precidence of operators
int prcd(char symbol){
switch(symbol) {
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '(':
case ')':
case '#':
return 1;
}
}
//Function to sort operators from other data
int isoperator(char symbol){
switch(symbol) {
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
return 1;
default:
return 0;
}
}
//Function to invert infix to prefix
void convertip(char infix[],char prefix[]){
int i,symbol,j=0;
char test[MAX];
infix=strrev(infix); //Used 'strrev' to reverse string
stack[++top]='#';
for(i=0;i<strlen(infix);i++){
symbol=infix[i];
if(isoperator(symbol)==0){
prefix[j]=symbol;
j++;
}
else{
if(symbol==')'){
push(symbol);
}
else if(symbol=='('){
while(stack[top]!=')'){
prefix[j]=pop();
j++;
}
pop();//pop out (.
}
else{
if(prcd(symbol)>prcd(stack[top])) {
push(symbol);
}
else{
while(prcd(symbol)<=prcd(stack[top])) {
prefix[j]=pop();
j++;
}
push(symbol);
}//end of else.
}//end of else.
}//end of else.
}//end of for.
while(stack[top]!='#'){
prefix[j]=pop();
j++;
}
prefix[j]='\0';//null terminate string.
prefix=strrev(prefix);
}
int main() {
char infix[20],prefix[20];
cout << "Enter the valid infix string: " << endl;
gets(infix);
convertip(infix,prefix);
cout << "The corresponding prefix string is: " << endl;
puts(prefix);
getch();
return 0;c
}
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.