The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Please look at this code...
Discuss Please look at this code... in the C Programming forum on Dev Shed. Please look at this code... C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 8th, 2002, 09:27 AM
|
|
Registered User
|
|
Join Date: Sep 2002
Posts: 21
Time spent in forums: 1 h 52 m 59 sec
Reputation Power: 0
|
|
|
Please look at this code...(New Code)
tjanks to Scopion4ever...I finished my validation funtion. But I got all the errors after i inserted that valid function. I hope someone can help me and fix this code if there're any errors.
Thank you
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define STACKSIZE 10
#define LINESIZE 60
struct node
{
int number;
struct node * next;
};
struct stack
{
struct node *top;
int size;
};
/*function prototypes for stack operations*/
struct stack * clear_stack(void);
int empty (struct stack *sp);
int full (struct stack *sp);
void push (struct stack *sp, int k);
int pop (struct stack *sp);
void print_stack (struct stack *sp);
int isoperator ( char c);
int valid(char expression[], int length);
int compute(int a, char op, int b);
int main()
{ struct stack *pointer;
char expression[LINESIZE], c, op;
int N1, N2, N, current_ch, S, size;
do
{ clrscr();
clear_stack(); /*call clear_stack function*/
if ( empty(pointer) )
{
printf("Stack is empty.\n\n");
}
printf("The Start of the program\n\n");
printf("Please enter a postfix expression:\n");
gets(expression);
N = strlen(expression);
if (!valid (expression, N))
{
printf ("Invalid characters");
exit(0);
}
for (current_ch = 0; current_ch > 0 && expression[current_ch] !='\0'; current_ch++) /*for each character in the valid expression*/
{
if (expression[current_ch] >= '0' || expression[current_ch] <= '9')
{
if (!full(expression[current_ch]) ) /*if stack is not full*/
{
N1 = expression[current_ch]-48;
op = expression[++current_ch];
N2 = expression[++current_ch]-48;
push(pointer, N1);
push(pointer, op); /*push the character numerical value into the stack*/
push(pointer, N2);
}
else
{
printf("push failed\n"); /*print stack overflow message and exit the program*/
exit(1);
}
}
else
{
if (expression[++current_ch]==isoperator(current_ch)) /*if current_ch is one of the operators*/
{
if (size < 2) /*if stack has less than 2 numbers in it*/
{
printf("Error! Stack has less than 2 numbers in it");
exit(1);
}
else
{
N1 =pop(pointer); /* pop 2 numbers from stack*/
N2 =pop(pointer);
S = compute(N1,current_ch,N2); /*apply the operator to the numbers*/
push (pointer, S); /*push result back into stack*/
}
}
}
}
printf("Stack is to be popped.\n"); /*on exit from the above FOR loop*/
if (empty(pointer) || size> 1) /*if stack is empty OR has more than number in it*/
{
printf("pop failed\n"); /*print error message*/
exit(1);
}
else
{
printf("%d is popped from stack\n", pop(pointer) ); /*pop final result from stack*/
}
printf("More Expressions (y/n)? ");
scanf(" %c", &c);
print_stack(pointer); /*printf final result*/
if (c!='y' && c!='Y')
printf("End of the program\n\n");
}while(c=='y' || c=='Y');
return (0);
}
/* Use the following functions suitably in the main() part */
/* Functions for stack operations defined */
struct stack * clear_stack(void)
{
struct stack * sp;
sp= (struct stack *) malloc( sizeof(struct stack) );
if (sp !=NULL)
{
sp->top = NULL;
sp->size = 0;
}
return (sp);
}
int empty(struct stack *sp)
{
return (sp->top == NULL);
}
int full(struct stack *sp)
{
return (sp->size == STACKSIZE);
}
void push(struct stack *sp, int k)
{
struct node *newndp;
if (full(sp) )
{
printf("\nERROR: stack is full.\n");
}
else
{
newndp = (struct node *) malloc(sizeof(struct node));
newndp->number = k;
newndp->next = sp->top;
sp->top = newndp;
sp->size = sp->size+1;
}
}
int pop(struct stack *sp)
{
struct node *oldtop;
int k;
if ( empty(sp) )
{
printf("ERROR: Stack is empty.\n");
return (0);
}
oldtop = sp->top;
k = oldtop->number;
sp->top = oldtop->next;
sp->size = sp->size - 1;
free (oldtop);
return (k);
}
void print_stack(struct stack *sp)
{
struct node *np;
if (empty(sp) )
{
printf("Stack is empty.\n");
return;
}
printf("Contents of Stack: ");
np = sp->top;
while ( np != NULL)
{
printf("%i ", np->number);
np = np->next;
}
printf("\n");
}
int isoperator (char ch) /*returns true if ch is one of the operators*/
{
return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%');
}
int valid(char line[], int k)
{ int i; /*check array contaning k charaters*/
char c; /*return true if array has only spaces, digits, or operators*/
if (k == 0)
return 0;
for (i = 0; i < k; i++)
{
c = line[i];
if ((!isdigit(c)) && (c != ' ') && (c != '+') && (c != '-') && (c != '*') && (c != '%') && (c !='/'))
{
return 0;
}
else
{
printf("Invalid Characters");
exit(1);
}
}
return 1;
}
int compute(int n1, char op, int n2)
{
int result;
op = getchar();
switch (op)
{
case '+': if (op=='+')
result= (n1 + n2);
break;
case '-': if (op=='-')
{ if (n1<n2)
{
result= (n2 - n1);
}
else
{
result= (n1 - n2);
}
}
break;
case '*': if (op=='*')
result= (n1 * n2);
break;
case '/': if (op=='/')
{ if (n2==0)
{
printf("Error!!!Invalid value to divide\n");
}
else
{
result = (n1 / n2);
}
}
break;
case '%': if (op=='%')
result= (n1 % n2);
break;
default: printf("Error! Invalid Operator\n");
}
return (result);
}
|

September 8th, 2002, 10:54 AM
|
 |
Contributing User
|
|
Join Date: Dec 2001
Location: USA
Posts: 286

Time spent in forums: 7 m 23 sec
Reputation Power: 12
|
|
|
Re: Please look at this code...
Some of your function definitions have a semicolon after them, like so:
Code:
void myfunc(/* arguments here */); // <-- Semicolon not needed here
{
// function contents
}
I have marked the places where you did that in the code below.
Quote: Originally posted by Tozilla
Code:
int isoperator (char ch); // <---- Remove semicolon
/*returns true if ch is one of the operators*/
{
return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%');
}
int valid(char line[], int k);// <---- Remove semicolon
{ int i; /*check array contaning k charaters*/
char c; /*return true if array has only spaces, digits, or operators*/
if (k == 0)
return 0;
for (i = 0; i < k; i++)
{
c = line[i];
if ((!isdigit(c)) && (c != ' ') && (c != '+') && (c != '-') && (c != '*') && (c != '%'))
{
return 0;
}
else
{
printf("Invalid Characters");
exit(1);
}
}
return 1;
}
float compute(int n1, char op, int n2);// <---- Remove semicolon
{
float result;
op = getchar();
switch (op)
{
case '+': if (op=='+')
result= (n1 + n2);
break;
case '-': if (op=='-')
{ if (n1<n2)
{
result= (n2 - n1);
}
else
{
result= (n1 - n2);
}
}
break;
case '*': if (op=='*')
result= (n1 * n2);
break;
case '/': if (op=='/')
{ if (n2==0)
{
printf("Error!!!Invalid value to divide\n");
}
else
{
return = (n1 / n2);
}
}
break;
case '%': if (op=='%')
result= (n1 % n2);
break;
default: printf("Error! Invalid Operator\n");
}
return result;
}
|
__________________
Jon Sagara
"Me fail English? That's unpossible!"
|

September 8th, 2002, 11:40 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
You also have logic errors in your valid() function. It's not the same as the valid() function that was described in the previous thread you made. I commented out the code that I think is erronous in your function. Also, I noticed that you wrote an isoperator() function, but you're not calling it from the valid() function?.
Code:
int valid(char line[], int k);
{ int i; /*check array contaning k charaters*/
char c; /*return true if array has only spaces, digits, or operators*/
if (k == 0)
return 0;
for (i = 0; i < k; i++)
{
c = line[i];
if ((!isdigit(c)) && (c != ' ') && (c != '+') && (c != '-') && (c != '*') && (c != '%'))
{
return 0;
}
/* YOU HAVE A BUG HERE. This section of code will be
reached if the char IS a digit OR a space OR an
operator. You've made the program terminate
itself here. I think you want the program to continue
looping if it is a digit, space or operator
else
{
printf("Invalid Characters");
exit(1);
}
*/
}
return 1;
}
|

September 8th, 2002, 02:42 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
You also have a lot more logic/programming errors in your code. For example:
In function main(), you are calling valid() like this:
valid (expression, N);
Two errors here:
1. You are not checking the return value of the valid() function to see if the return was true or false. IMHO, it should possibly be written something like:
Code:
if (!valid (expression, N)) {
printf ("Invalid characters");
exit(0);
}
2. You have not initialized the value of N yet, before calling this function. So the value of N could be *ANYTHING* and your function will probably not work 99.99% of the time, because N may be any value.
You have a lot of similar errors all over the place:
i = current_ch;
Here i is int and current_ch is char and not initialized previously. This statement also doesn't do anything because you initialize i again in the very next statement.
for (i = 0; i < k; i++)
k is not initialized previously. So the value could be anything and your loop may or may not run.
if (i != isdigits)
isdigits is not defined anywhere, so your program won't compile
I could go on and on here. You've got a lot of corrections to do, so all I can say is "Enjoy!"
Heck, with all the errors you've got lying around, even if you manage to get the program to compile by fixing the syntax errors, it's not going to run unless you fix the errors in the program logic as well. You might consider reading your book a little more carefully or ask your professor for help before attempting to fix anything.
|

September 8th, 2002, 05:02 PM
|
|
Registered User
|
|
Join Date: Sep 2002
Posts: 21
Time spent in forums: 1 h 52 m 59 sec
Reputation Power: 0
|
|
|
how should I set the initialisation properly?
|

September 9th, 2002, 02:06 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
You have to initialize the variables by assigning meaningful values to them. All I'm pointing out is that you're accessing variables without assigning values to them first. For example, in your main() function, something like this would fix the first issue that I pointed out:
N = strlen(expression);
if (!valid(expression, N)) {
....
}
You've got several instances where you are comparing or passing variables without assigning values to those variables first. The code may compile because it is syntatically correct C, but that doesn't mean it will run correctly.
|

September 11th, 2002, 04:12 AM
|
|
Registered User
|
|
Join Date: Sep 2002
Posts: 21
Time spent in forums: 1 h 52 m 59 sec
Reputation Power: 0
|
|
Quote: Originally posted by Scorpions4ever
You have to initialize the variables by assigning meaningful values to them. All I'm pointing out is that you're accessing variables without assigning values to them first. For example, in your main() function, something like this would fix the first issue that I pointed out:
N = strlen(expression);
if (!valid(expression, N)) {
....
}
You've got several instances where you are comparing or passing variables without assigning values to those variables first. The code may compile because it is syntatically correct C, but that doesn't mean it will run correctly. |
Hi! thanks for helping me. I fixed those errors u mentioned before.
but I don't understand some of the warings I got. Could you explain it to me?
I got warnings like these:
Warning a:\num 41: Possible use of 'pointer' before definition in funtion main
Warning a:num 61: Non-portable pointer conversion in function main
warning a:\num 81: Possible use of 'size' before definition in function main
Also, When i enter a postfix expression like this: 3 4 5 * +
It only shows me the error message. does it that mean i set the data type wrong or some thing wrong with this code?
Code:
N = strlen(expression);
if (!valid (expression, N))
{
printf ("Invalid characters");
exit(0);
}
P.S. I put the new code
|

September 11th, 2002, 01:18 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
>> Warning a:\num 41: Possible use of 'pointer' before definition in funtion main
>> warning a:\num 81: Possible use of 'size' before definition in function main
Look at your code carefully -- I told you that you had several places in the code where you are using variables without initializing them first. This is why you are getting the warnings.
>> Warning a:num 61: Non-portable pointer conversion in function main
This is plainly a logic error. You're passing the wrong type of argument to the function. Check what you're passing on line 61 and what the function is expecting as an argument  . You clearly are passing the wrong argument to the function.
>> P.S. I put the new code
Not entirely. You missed out the bug I pointed out in your valid function() above. Remember I said that you hadn't transcribed the valid() function exactly the same as was discussed in an earlier thread?? If you look at one of the posts above in *this* thread, I commented out a large section of the code in the valid() function. There was a good reason for doing this -- you have a logic error in your valid() function, the way you chose to implement it. The original function I described did the following:
1. Check if string length = 0. If so, return false immediately
2. Loop thru each character in the array
---->If char is not a space, digit or operator, return false and exit he function immediately
3. If we finished the loop, that means that all the chars are either space, digit or operator, so we return true.
Now the way, you've implemented valid() does the following:
1. Check if string length = 0. If so, return false immediately
2. Loop thru each character in the array
---->If char is not a space, digit or operator, return false and exit
---->ELSE print error message and quit! (THIS IS WHERE YOUR ERROR IS, because if the char is a space, digit or operator, then your program quits).
3. If we finished the loop, that means that all the chars are either space, digit or operator, so we return true.
The extra code that you stuck in there is NOT necessary and is clearly a logic bug. That's why your code is quitting with that message.
Like I said before, you have some more logic errors in your code. Even if you fix these above problems, I don't think the program will actually work the way you want it to. Happy debugging!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|