I'm supposed to take user input in the form of a prefix expression, convert it to infix and postfix, and then print the results. So far, I've got both working, but I would like to add some parenthesis to the infix expression once its converted. At the moment, I can't think of a way to sneak it in there.

Here's my code so far.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static char s[50], i;
static char a[50];

void push(int data) {
	s[i++] = data;
}

char pop() {
	return s[--i];
}

// prefix -> infix
// prefix -> postfix
void prefix() {

	puts("Enter a prefix expression:");
	fgets(a, 50, stdin);
	int a_size = strlen(a);

	puts("Prefix -> Infix:");
	char *token, delim[] = {" \n"}, match[] = {"*+/-"};
	int j = 0, m_size = strlen(match);
	token = strtok(a, delim);
	while (token != NULL) {
		if (isdigit(*token)) {
			printf("%d ", atoi(token));
			if (i >= 0)	printf("%c ", pop());
		}
		else
			while (j < m_size) {
				if (*token == match[j]) {
					push(*token);
					break;
				}
				j++;
			}		
		token = strtok(NULL, delim);
		j = 0;
	}

	puts("");
	puts("Prefix -> Postfix:");
	int k = a_size;
	while (k >= 0) {
		if (a[k] == '\0' || a[k] == ' ') k--;
		printf("%c ", a[k--]);
	}
	puts("");
}

void infix() {
	
	puts("Enter an infix expression:");
	fgets(a, 50, stdin);


}

void postfix() {
}

void main(void) {

	int c;
	puts("1: Prefix");
	puts("2: Infix");
	puts("3: Postfix");
	scanf("%d", &c);
	fflush(stdin);
	
	prefix();
	/*
	switch(c) {
		case 1: prefix();
			break;
		case 2: infix();
			break;
		case 3: postfix();
			break;
	}
	*/
}

Recommended Answers

All 3 Replies

My first bit of advice would be to clean it up a little first.

PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software 1985-2006

--- Module: test.c (C)
test.c 10 [Info 734] Loss of precision (assignment) (31 bits to 7 bits)
test.c 22 [Warning 534] Ignoring return value of function 'fgets(char *, int, FILE *)'
test.c 23 [Info 713] Loss of precision (initialization) (unsigned int to int)
test.c 27 [Info 713] Loss of precision (initialization) (unsigned int to int)
test.c 51 [Warning 676] Possibly negative subscript (-1) in operator '
test.c 59 [Warning 534] Ignoring return value of function 'fgets(char *, int, FILE *)'
test.c 6 [Info 728] Symbol 'i' (line 6, file test.c) not explicitly initialized

--- Global Wrap-up
test.c 56 [Info 714] Symbol 'infix(void)' (line 56, file test.c) not referenced
test.c 64 [Info 714] Symbol 'postfix(void)' (line 64, file test.c) not referenced

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
test.c:
Warning W8071 test.c 10: Conversion may lose significant digits in function push
Error E2140 test.c 23: Declaration is not allowed here in function prefix
Error E2140 test.c 26: Declaration is not allowed here in function prefix
Error E2140 test.c 27: Declaration is not allowed here in function prefix
Warning W8065 test.c 32: Call to function 'pop' with no prototype in function prefix
Error E2140 test.c 48: Declaration is not allowed here in function prefix
Warning W8065 test.c 76: Call to function 'prefix' with no prototype in function main
*** 4 errors in Compile ***

The compiler errors are due to compiling as C++, but the others are worth a look. Some of the lint can be overlooked, but it's always worth looking into and finding out the reason behind the diagnostic.

And this always needs fixin':

scanf("%d", &c);
fflush(stdin);

User Input: Strings and Numbers [C]

Add in not using void main , and it might look like something I'd consider looking into. [I'm not trying to be offensive if it may sound so, I just hate rewriting the same stuff over and over and over...]

I hate these new quote tags. And what is it with the slider. It doesn't look nice at all. Plz bring back old ones.

Member Avatar for iamthwee

>So far, I've got both working

wouldn't it be better to have an infix to postfix converter?

>I would like to add some parenthesis to the infix expression once its converted. At the moment, I can't think of a way to sneak it in there.

This is not something you can just hope to sneak in. You have to plan it very carefully, from the onset of your program.

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.