hello i have wrote a program that converts a statement to sth like the example below please help me:
(9*(1+(3*8)))+(1+(4*5))
into:
9 1 3 8 * + * 1 4 5 * + +

while((c=getch())!='\0'){   /*i dont know whether it is true or not*/

	if(c=='('){
	co[k]=1;
   	k++;
   	break;
		}

	if(c==')'){
	while(k>=0&&k<=20){
   	co[k]=0;
      	k--;
         }
      	break;
		}

	if(c==" "){
		break;
		}  /*this ode may be useful for converting a usual statemnt to poland model*/

	if(c==n){
 	while(n>=1){
   	rep[i+n]=rep[i+n-1];
      	n--;
   		}
	rep[i]=c;
   	i++;
   	n=counter;
 		}

	if(c==o){
	if(co[k]=0){
   	rep[i+n]=c;
      	i=i+n;
      	n=1;
   		}
   	else{
       	while(n>=1){
       	rep[i+n]=rep[i+n-1];
         	n--;
       		}
   	rep[i]=c;
   	n=counter;
   	n++;
   	counter=n;
   	}

}

please tell me where is the problem.

Recommended Answers

All 7 Replies

while((c=getch())!='\0') {

The getch() get a single character from keyboard. There is no NULL key button in our keyboard, so the condition is always true.

Your code is poorly indented.
The variables are undeclared.
The variable names are meaningless.

> if(c==" ")
This is meaningless in a C program, you can't compare arrays of chars like this.

> /*this ode may be useful for converting a usual statemnt to poland model*/
Huh?
It's the only comment, and it just adds to the confusion.

I assume by "poland", you mean Reverse Polish Notation (RPN).
There is an algorithm, which I believe is called the "shunting yard algorithm", for converting infix expressions into RPN expressions.

Member Avatar for iamthwee

>please tell me where is the problem
You need to go back to basics I think.

hello i have changed my previous code about converting a statement to reverse polish notation but still i think it has serious problems .can any one help me solve it? i have just a little time!

#include <stdio.h>
#include <conio.h>

main(){

int k=0;
int co[20];
int n=0;
char c;
char rep[256]={0};
int i=0;

while((c=getchar())!='\n'){  

	if(c=='('){
		co[k]=1;
   	k++;
   	break;
		}

	if(c==')'){
		while(k>=0&&k<=20){
   		co[k]=0;
      	k--;
         }
      	break;
		}

	if(c==' '){
		break;
		}  



	 if(c=='*'||c=='-'||c=='+'||c=='/'||c=='s'||c=='c'||c=='t'||c=='C'){
		if(co[k]=0){
   		rep[i+n]=c;
         //printf("%c",rep[0]);
         //getch();
      	i=i+n;
      	n=1;
   		}
   	else{
       	while(n>=1){
       		rep[i+n]=rep[i+n-1];
         	n--;
       		}
   	if(rep[i]!='s'||'c'||'t'||'C') rep[i]=c;
   	n++;
   	}
  }
     	 if(c>47&&c<58){
 			while(n>=1){

   		rep[i+n]=rep[i+n-1];
      	n--;
   		}
		rep[i]=c;
   	i++;
 	
}
}
Member Avatar for iamthwee

There is no point creating a separate thread again. It just wastes our time.

As I said before I think you need to go back to basics, i.e figuring out basic i/o basic parsing etc.
Have you considered how you are going to represent the stack. Are you going to use a linked list la-la-la-de-da.

hello i have changed my previous code about converting a statement to reverse polish notation but still i think it has serious problems .can any one help me solve it? i have just a little time!

You think you have a problem? What makes you think so? Have you read the post Read Me: Read This Before Posting yet? The title says it all.

Also, you need to format your code. See this...

Let me interpret how is your code really works.

#include <stdio.h>
#include <conio.h>

// A bad main() function because it doesn't have
// the return type. The standard one is int main()
// Next time learn how to indent your code, because
// I am too lazy to help you indent your code.
main()
{
   // Learn how to name your variable because
   // I couldn't understand what each variable
   // is used for. A nice comment for each
   // variable would be a very good habit
   int k=0;
   int co[20];
   int n=0;
   char c;
   char rep[256]={0};
   int i=0;

   // Waiting the user to press any key and
   // assign it to variable c. If the Enter
   // key were pressed, skip the loop.
   while((c=getchar())!='\n'){  

      if(c=='('){ // If the key was '(' then
         co[k]=1; // Assign 1 to co[k]
         k++; // Increase k by 1
         break; // Skip the whole loop and end the program
      }

      if(c==')') { // If the key was ')' then
         while(k>=0&&k<=20) { // Loop while k value is in 0-20
            co[k]=0; // Assign 0 to co[k]
            k--; // Decrease the k by 1
         }
         break; // Skip the whole loop and end the program
      }

      if(c==' ') { // If the input was a space
         break; // Skip the whole loop and end the program
      }  

      // If the input was *, -, /, s, c, t or C, then
      if(c=='*'||c=='-'||c=='+'||c=='/'||c=='s'
          ||c=='c'||c=='t'||c=='C'){
         // Assign 0 to co[k], which make this condtion 
         // always false
         if(co[k]=0) {
            rep[i+n]=c;
            i=i+n;
            n=1;
         // Because the first condition always false
         // It always proccess here
         } else {
            while(n>=1) { // Loop until n is smaller than 1
               // Assign the rep[i+n] with the rep[i+n-1]
               rep[i+n]=rep[i+n-1];
               n--; // Decrease n by 1
            }
            // If the rep[i] is sometimes besides s, c, t, and C
            if(rep[i]!='s'||'c'||'t'||'C') 
               rep[i]=c; // Assign the input to rep[i]
            n++; // Increase n by 1
         }
      }

      // If c is in between 48-57 (which is a number)
      if(c>47&&c<58) {
         while(n>=1) { // Loop until the n smaller than 1
            // Assign the rep[i+n] with the rep[i+n-1]
            rep[i+n]=rep[i+n-1]; 
            n--; // Decrease n by 1
         }
      // Missing close brace here
      rep[i]=c; // Assign the input to rep[i]
      i++; // Increase i by 1
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.