Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
// infix.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<stdlib.h>
#include<ctype.h>
using namespace std;

class expression
{
private:
 char infix[100];
 char stack[200];
 int top;
 int r;
 char postfix[100];
public:
 void convert();
 int input_p(char);
 int stack_p(char);
 int rank(char);
};

int expression::input_p(char c)
{
 if( c== '+' || c=='-' )
  return 1;
 else if( c=='*' || c=='/' )
  return 3;
 else if( c=='^')
  return 6;
 else if( isalpha(c)!=0)
  return 7;
 else if( c=='(' )
  return 9;
 else if( c==')' )
  return 0;
 else
 {
  cout<<"Invalid expression ::input error\n";
  exit(0);
 }
}

int expression::stack_p(char c)
{
 if(c=='+' || c=='-')
  return 2;
 else if(c=='*' || c=='/')
  return 4;
 else if(c=='^' )
  return 5;
 else if(isalpha(c)!=0)
  return 8;
 else if(c== '(' )
  return 0;
 else
 {
  cout<<"Invalid expression  ::stack error\n";
  exit(0);
 }
}

int expression::rank(char c)
{
 if(c=='+' || c=='-')
  return -1;
 else if(c=='*' || c=='/')
  return -1;
 else if(c=='^')
  return -1;
 else if(isalpha(c)!=0)
  return 1;
 else
 {
  cout<<"Invalid expression ::in rank\n";
  exit(0);
 }
}

void expression::convert()
{
 cout<<"\n*************************************************\n"
  <<"This program converts the given infix expression\n"
  <<"in to postfix form"
                <<"\n*************************************************\n";
 cout<<"Enter an infix expression ::\n";
 cin>>infix;
 int l=strlen(infix);

 infix[l]=')';
 infix[l+1]='';

 //Convertion starts
 top=1;
 stack[top]='(';

 r=0;
 int x=-1;

 int i=0;
 char next=infix[i];

 while(next!='')
 {
  //Pop all the elements to outputin stack which have higher precedence
  while( input_p(next) < stack_p(stack[top]) )
  {
   if(top<1)
   {
    cout<<"invalid expression ::stack error\n";
    exit(0);
   }

   postfix[++x]=stack[top];
   top–-;

   r=r+rank(postfix[x]);
   
   if(r<1)
   {
    cout<<"Invalid expression  ::r<1\n";
    exit(0);
   }
  }

  if(input_p( next ) != stack_p( stack[top]))
   stack[++top]=next;
  else
   top–-;

  i++;
  next=infix[i];
 }
 postfix[++x]='';

 if(r!=1 || top!=0)
 {
  cout<<"Invalid expression ::error in rank or stack\n";
  exit(0);
 }

 cout<<"\n\nThe corresponding postfix expression is ::\n";
 cout<<postfix<<endl;
}

int main()
{
 expression obj;
 obj.convert();
 return 0;
}


1>------ Build started: Project: infix, Configuration: Debug Win32 ------
1>Compiling...
1>infix.cpp
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(94) : error C2137: empty character constant
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(106) : error C2137: empty character constant
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(118) : error C2065: 'top–' : undeclared identifier
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(118) : error C2059: syntax error : ';'
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(132) : error C2065: 'top–' : undeclared identifier
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(132) : error C2059: syntax error : ';'
1>z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(137) : error C2137: empty character constant
1>Build log was saved at "file://\\scidc01\bsprof$\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\Debug\BuildLog.htm"
1>infix - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


it is an infix to postfix program:confused:
Posted
Updated 7-Feb-11 2:55am
v3
Comments
OriginalGriff 7-Feb-11 3:00am    
I'm sorry, but we need more information: we have even less than you do!

Is this a compilation, problem? A runtime error? Or a logic bug? What is it doing that it shouldn't, or not doing that it should?

Edit you question, and give us more information so we can help you!

Thanks for the update:
z:\math\arianfar\mydocs\visual studio 2008\projects\infix\infix\infix.cpp(94) : error C2137: empty character constant
Look at line 94 of infix.cpp - from the "infix.cpp(94)"
infix[l+1]='';
Two quote characters together are not allowed: they are not a character.
I suspect that you mean '\0' as a null terminator character instead. The same will apply to line 106.
Just work though the list looking at each error message in relation to the line itself!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Feb-11 3:34am    
As easy as that, a 5;
--SA
Nish Nishant 7-Feb-11 9:07am    
Good response, voted 5, proposed as answer.
#realJSOP 7-Feb-11 9:09am    
Proposed as answer
For the "empty character" constants, I don't think '' is valid. If you want the null character, you need to use '\0' or ""

On line 118/132, one of your minus signs in top-- is not a minus sign but some other character, causing both errors for those lines.
 
Share this answer
 
v2
Comments
Emilio Garavaglia 7-Feb-11 7:32am    
- you need to use '\0' or "" -
They are completely different beasts!
jswolf19 7-Feb-11 8:51am    
Very true... ^^;
Member 11669873 11-May-15 10:44am    
error C2137: empty character constant win32

#include <iostream>
#include <windows.h>
using namespace std;

int main(int argc, const char **argv)
{
wcout << "Connecting to pipe..." << endl;

// Open the named pipe
// Most of these parameters aren't very relevant for pipes.
HANDLE pipe = CreateFile(
L"\\\\.\\pipe\\my_pipe",
GENERIC_READ, // only need read access
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);

if (pipe == INVALID_HANDLE_VALUE) {
wcout << "Failed to connect to pipe." << endl;
// look up error code here using GetLastError()
system("pause");
return 1;
}

wcout << "Reading data from pipe..." << endl;

// The read operation will block until there is data to read
wchar_t buffer[128];
DWORD numBytesRead = 0;
BOOL result = ReadFile(
pipe,
buffer, // the data from the pipe will be put here
127 * sizeof(wchar_t), // number of bytes allocated
&numBytesRead, // this will store number of bytes actually read
NULL // not using overlapped IO
);

if (result) {
buffer[numBytesRead / sizeof(wchar_t)] =''; // null terminate the string
wcout << "Number of bytes read: " << numBytesRead << endl;
wcout << "Message: " << buffer << endl;
} else {
wcout << "Failed to read data from the pipe." << endl;
}

// Close our pipe handle
CloseHandle(pipe);

wcout << "Done." << endl;

system("pause");
return 0;
}
Member 11669873 11-May-15 10:45am    
email id : suresh.bonappagaru01@gmail.com


error C2137: empty character constant win32


#include <iostream>
#include <windows.h>
using namespace std;

int main(int argc, const char **argv)
{
wcout << "Connecting to pipe..." << endl;

// Open the named pipe
// Most of these parameters aren't very relevant for pipes.
HANDLE pipe = CreateFile(
L"\\\\.\\pipe\\my_pipe",
GENERIC_READ, // only need read access
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);

if (pipe == INVALID_HANDLE_VALUE) {
wcout << "Failed to connect to pipe." << endl;
// look up error code here using GetLastError()
system("pause");
return 1;
}

wcout << "Reading data from pipe..." << endl;

// The read operation will block until there is data to read
wchar_t buffer[128];
DWORD numBytesRead = 0;
BOOL result = ReadFile(
pipe,
buffer, // the data from the pipe will be put here
127 * sizeof(wchar_t), // number of bytes allocated
&numBytesRead, // this will store number of bytes actually read
NULL // not using overlapped IO
);

if (result) {
buffer[numBytesRead / sizeof(wchar_t)] =''; // null terminate the string
wcout << "Number of bytes read: " << numBytesRead << endl;
wcout << "Message: " << buffer << endl;
} else {
wcout << "Failed to read data from the pipe." << endl;
}

// Close our pipe handle
CloseHandle(pipe);

wcout << "Done." << endl;

system("pause");
return 0;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900