Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here is my code for the problem id-4 in spoj, it is running perfectly in ideone.com but in spoj its showing segmentation fault, I am unable to find out the bug.Please help. I have used stacks to implement it.Thanks in advance.
exp is the input string, and out is the output string, int l keeps track of the index of output string
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//declarations
int top = 0;
void push(char ch);
char pop();
int prec(char c);
char stack[20];
char out[20];
int l = 0;
int main() {
char exp[20];
int x, t;
char temp;
int i = 0;
scanf("%d", &t); //no. of test cases
while (t--) {
l=0;
scanf("%s", exp);
stack[0] = '('; //initially pushing '('
x = strlen(exp);
exp[x] = ')'; //for completion of statement
for (i = 0; i < x + 1; i++) {
if (exp[i] == '+' || exp[i] == '-' ||
exp[i] == '/' || exp[i] == '*' || exp[i] == '^') { //operators
while (prec(exp[i]) < prec(stack[top])) { //checking precedence
out[l] = pop();
l++;
}
push(exp[i]);
} else
if (exp[i] == '(') {
push(exp[i]);
} else
if (exp[i] == ')') {
while (stack[top] != '(') {
out[l] = pop();
l++;
}
temp = pop(); //to throw out '('
} else {
out[l] = exp[i]; //variables
l++;
}
}
for (i = 0; i < l; i++) {
printf("%c", out[i]); //output
}
}
return 0;
}
void push(char c) { //push operation on stack
if (top >= 19) {
} else {
top++;
stack[top] = c;
}
return;
}
char pop() { //pop operation on stack
char t;
if (top <= -1) {
return 0;
} else {
t = stack[top];
top--;
return t;
}
}
int prec(char c) { //precedence check
if (c == 94) { return 5; }
else if (c == 47) { return 4; }
else if (c == 42) { return 3; }
else if (c == 43) { return 2; }
else if (c == 45) { return 1; }
else { return 0; }
}
The buffer to read the expression is very small: char exp[20]; and you do not protect scanf for buffer overflow.
Sphere Online Judge specifies:
Input
t [the number of expressions <= 100]
expression [length <= 400]
[other expressions]
You should use a larger stack, at least 100, a larger buffer and use:
char exp[402];
...
scanf("%400s", exp);
Also test the return value from scanf("%d", &t), just in case they give you purposely erroneous input.
You have to check if top is greater or equal 0:
while( top >= 0 && prec(exp[i])<prec(stack[top])){
// ^^^^^^^^
....
while( top >= 0 && stack[top]!='('){
// ^^^^^^^^
The segmentation fault occurs, because you access to stack[top] and top is less than 0. Apart form this I recommend to increase the size of arrays exp and out coherently.
Related
I tried to make simple calculator supporting brackets and classic operators +,-,*,/ and I also need to eliminate division by zero in it. I may not be able to cope with it, I need to check if there is division by zero in the expression and if so function return 0. Now it returns Floating point exception. Can someone help?
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 1024
int insert_operand(int *operand, int * top_num, int num) /* data is pushed into the data stack*/
{
(*top_num) ++;
operand[*top_num] = num; /*save data*/
return 0; /*Exit normally*/
}
int insert_oper (char * oper , int *top_oper , char ch)
{
(*top_oper)++;
oper[*top_oper] = ch; /*save operator*/
return 0; /*Exit normally*/
}
int compare(char *oper , int *top_oper , char ch) /* compare the priority of the operating server*/
{
if((oper[*top_oper] == '-' || oper[*top_oper] == '+') /*Determine whether the current priority is higher than the priority of the operator at the top of the stack*/
&& (ch == '*' || ch == '/'))
{
return 0;
}
else if(*top_oper == -1 || ch == '('
|| (oper[*top_oper] == '(' && ch != ')')) /*Determine whether the operator stack is empty; whether the top operator is '('*/
{
return 0;
}
else if (oper[*top_oper] =='(' && ch == ')')
{
(*top_oper)--;
return 1;
}
else
{
return -1; /*Operate the operator*/
}
}
int deal_date(int *operand ,char *oper ,int *top_num, int *top_oper) /*perform data operation*/
{
int num_1 = operand[*top_num]; /*Take out two data from the data stack*/
int num_2 = operand[*top_num - 1];
int value = 0;
if(oper[*top_oper] == '+')
{
value = num_1 + num_2;
}
else if(oper[*top_oper] == '-')
{
value = num_2 - num_1;
}
else if(oper[*top_oper] == '*')
{
value = num_2 * num_1;
}
else if(oper[*top_oper] == '/')
{
value = num_2 / num_1;
}
(*top_num) --; /*Move the top of the data stack down one bit*/
operand[*top_num] = value; /*Push the obtained value into the data stack*/
(*top_oper) --; /*Move the top of the operator stack down one bit*/
return value;
}
void compress(char *stx) /* The additional function */
{
char work[101];
int i = strlen(stx);
strcpy(work, stx);
for (int j = 0; j < i; j++)
{
stx[j] = 0;
}
i = 0;
for (int j = 0; j < (int)strlen(work); j++)
{
if (work[j] != ' ')
{
stx[i] = work[j];
i++;
}
}
}
int main()
{
int operand[MAX_SIZE] = {0}; /*data stack, initialize*/
int top_num = -1;
char oper[MAX_SIZE] = {0}; /*operator stack, initialize*/
int top_oper = -1;
char *str = (char *) malloc (sizeof(char) * 100); /*get expression (without =)*/
//scanf("%s", str);
scanf("%[^\n]", str); /* Refined the scanf call to receive all characters prior to the newline/return character */
compress(str); /* Added this function to remove spaces */
char* temp;
char dest[MAX_SIZE];
int num = 0;
int i = 0;
while(*str != '\0')
{
temp = dest;
while(*str >= '0' && *str <= '9') /*judging whether it is data*/
{
*temp = *str;
str++;
temp++;
} /*Encounter a symbol to exit*/
if(*str != '(' && *(temp - 1) != '\0') /*Determine whether the symbol is '('*/
{
*temp = '\0';
num = atoi(dest); /*convert string to number*/
insert_operand(operand, &top_num,num); /*Push data into the data stack*/
}
while(1)
{
i = compare(oper,&top_oper,*str); /*judgment operator priority*/
if(i == 0)
{
insert_oper(oper,&top_oper,*str); /*press operator*/
break;
}
else if(i == 1) /*judging whether the expression in brackets ends*/
{
str++;
}
else if(i == -1) /* data processing */
{
deal_date(operand,oper,&top_num,&top_oper);
}
}
str ++; /* point to the next character of the expression */
}
printf("%d\n",operand[0]); /*output result*/
return 0;
}
Thanks for all answers
I would detect if the divisor is zero along these lines:
else if(oper[*top_oper] == '/')
{
value = num_1 ? num_2 / num_1 : 0;
}
or perhaps more verbose:
else if(oper[*top_oper] == '/')
{
if(num_1)
value = num_2 / num_1;
else
value = 0;
}
and example session:
1 / 2
0
I have spent the last two hours trying to debug my code that is supposed to check if the input consists of well-formed brackets. What I mean by well formed is that ()()[] or ([()]) are acceptable but ((((() is not.
I'm not allowed to use any header file apart from <stdio.h>
#include <stdio.h>
void cross(char str[], int i, int j) {
str[i] = 'X';
str[j] = 'X';
}
int iscrossed(char str[]) {
int i = 0;
while (str[i] != '\0') {
if (str[i] != 'X')
return 0;
i++;
}
return 1;
}
int check(char str[]) {
int i = 1, j;
while (str[i] != '\0') {
if (str[i] == ')') {
for (j = i - 1; j >= 0; j--) {
if (str[j] == '(') {
cross(str, str[i], str[j]);
}
break;
}
} else
if (str[i] == ']') {
for (j = i - 1; j >= 0; j--) {
if (str[j] == '[') {
cross(str, str[i], str[j]);
}
break;
}
}
i++;
}
if (iscrossed(str) == 1)
return 1;
else
return 0;
}
int main() {
char str[20];
scanf("%s", str);
printf("%d\n", check(str));
}
For certain inputs the program prints a zero followed by a segmentation fault and for the others it just prints a zero.
Please keep in mind that I'm a beginner programmer so please don't include too much heavy stuff in your hints. I'd be grateful for any help on this.
Edit: It would be wonderful if your answer tells me the errors in my code, because that was my question in the first place.
Here a simple recursive solution:
#include <stdio.h>
int brace(const char **s, char cc)
{
while(1) {
if(**s == cc) { return 0; }
switch(**s) {
case '\0': return -1;
case '[': ++(*s); if(brace(s, ']')) { return -1; } ++(*s); break;
case '{': ++(*s); if(brace(s, '}')) { return -1; } ++(*s); break;
case '(': ++(*s); if(brace(s, ')')) { return -1; } ++(*s); break;
case ']':
case '}':
case ')': return -1;
default: ++(*s);
}
}
}
int check_brace(const char *s)
{
return brace(&s, '\0');
}
int main()
{
printf("%d\n", check_brace(" hekl(l o{ asdf } te)ts()({})"));
}
Returns -1 when somethings wrong. Otherwise 0.
There are multiple problems in your code:
you call cross(str, str[i], str[j]); instead of cross(str, i, j); when you find matches for parentheses and brackets.
the break statement should be moved inside the if block.
your method does not allow detection of nesting errors
your method would have undefined behavior if str is an empty string (which you cannot input via scanf())
Here is a modified version:
#include <stdio.h>
void cross(char str[], int i, int j) {
str[i] = str[j] = 'X';
}
int iscrossed(char str[]) {
int i = 0;
while (str[i] != '\0') {
if (str[i] != 'X')
return 0;
i++;
}
return 1;
}
int check(char str[]) {
int i = 0, j;
while (str[i] != '\0') {
if (str[i] == ')') {
for (j = i - 1; j >= 0; j--) {
if (str[j] == '(') {
cross(str, i, j);
break;
}
}
} else
if (str[i] == ']') {
for (j = i - 1; j >= 0; j--) {
if (str[j] == '[') {
cross(str, i, j);
break;
}
}
}
i++;
}
return iscrossed(str);
}
int main() {
char str[80];
if (scanf("%79s", str) == 1) {
printf("%d\n", check(str));
}
return 0;
}
Here is a simpler alternative:
#include <stdio.h>
const char *check(const char str[], int endc) {
while (str) {
int c = *str++;
switch (c) {
case '(': str = check(str, ')'); break;
case '[': str = check(str, ']'); break;
case '{': str = check(str, '}'); break;
case ')':
case ']':
case '}':
case '\0': return c == endc ? str : NULL;
}
}
return NULL;
}
int main() {
char str[80];
if (fgets(str, sizeof str, stdin)) {
printf("%d\n", check(str, '\0') != NULL);
}
return 0;
}
Pseudocode of a possible answer:
initialize char[] unclosed
int latest_unclosed_index = -1
for each char in string {
if char == opening_bracket {
latest_unclosed_index += 1
unclosed[latest_unclosed_index] = char
} else if char == closing_bracket {
if latest_unclosed_index < 0 {
return false
} else if char == closing_of(unclosed[latest_unclosed_index]) {
unclosed[latest_unclosed_index] = null
latest_unclosed_index -= 1
} else {
return false
}
}
}
if latest_unclosed_index == -1 {
return true
} else {
return false
}
This works by keeping an array of all unclosed opening brackets in the order that you encounter them in, and removing them whenever you encounter a closing bracket, as a sort of stack.
This solution has a complexity of O(n).
A problem with this implementation is that there is an unknown amount of brackets in string, which may cause the array to overflow if it isn't large enough.
Solution:
To be sure that this solution doesn't overflow, the size of the array should be at least half the size of the input string, and you'll have to check at each character if there are enough characters left in the input string to be able to completely close all brackets.
Use a list implementation (or write your own) instead of an array for unclosed.
If its ok for you to use stdlib.h then,
#include <stdio.h>
#include <stdlib.h>
#define bool int
// structure of a stack node
struct sNode {
char data;
struct sNode* next;
};
// Function to push an item to stack
void push(struct sNode** top_ref, int new_data);
// Function to pop an item from stack
int pop(struct sNode** top_ref);
// Returns 1 if character1 and character2 are matching left
// and right Brackets
bool isMatchingPair(char character1, char character2)
{
if (character1 == '(' && character2 == ')')
return 1;
else if (character1 == '{' && character2 == '}')
return 1;
else if (character1 == '[' && character2 == ']')
return 1;
else
return 0;
}
// Return 1 if expression has balanced Brackets
bool areBracketsBalanced(char exp[])
{
int i = 0;
// Declare an empty character stack
struct sNode* stack = NULL;
// Traverse the given expression to check matching
// brackets
while (exp[i])
{
// If the exp[i] is a starting bracket then push
// it
if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')
push(&stack, exp[i]);
// If exp[i] is an ending bracket then pop from
// stack and check if the popped bracket is a
// matching pair*/
if (exp[i] == '}' || exp[i] == ')'
|| exp[i] == ']') {
// If we see an ending bracket without a pair
// then return false
if (stack == NULL)
return 0;
// Pop the top element from stack, if it is not
// a pair bracket of character then there is a
// mismatch.
// his happens for expressions like {(})
else if (!isMatchingPair(pop(&stack), exp[i]))
return 0;
}
i++;
}
// If there is something left in expression then there
// is a starting bracket without a closing
// bracket
if (stack == NULL)
return 1; // balanced
else
return 0; // not balanced
}
// Driver code
int main()
{
char exp[100] = "{()}[]";
// Function call
if (areBracketsBalanced(exp))
printf("Balanced \n");
else
printf("Not Balanced \n");
return 0;
}
// Function to push an item to stack
void push(struct sNode** top_ref, int new_data)
{
// allocate node
struct sNode* new_node
= (struct sNode*)malloc(sizeof(struct sNode));
if (new_node == NULL) {
printf("Stack overflow n");
getchar();
exit(0);
}
// put in the data
new_node->data = new_data;
// link the old list off the new node
new_node->next = (*top_ref);
// move the head to point to the new node
(*top_ref) = new_node;
}
// Function to pop an item from stack
int pop(struct sNode** top_ref)
{
char res;
struct sNode* top;
// If stack is empty then error
if (*top_ref == NULL) {
printf("Stack overflow n");
getchar();
exit(0);
}
else {
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
}
Features
Support nested parantheses like (())
Support bad nestings such as ([)]
Commented but not by me (see the below spoiler)
Note: i feel guilty that i have copied code from here
The algorithm
Pseudocode Like Block code!
If it's not ok for you to use stdlib.h,then someone may edit this code and remove the errors that occur when we remove that line(#include <stdlib.h>),I am not a c guy and i don't know to edit,i just copy pasted !
First attempt didn't worked with bad nesting, as MOehm wrote in the comments.
Storing the opened braces that not have been closed yet helps to recognize bad nesting. The last opened brace will determine which closing brace is need.
#include <stdio.h>
int check(char str[], int size)
{
char opened[size/2];
char close;
int i = 0;
int pos = 0;
int error = 0;
while((i < size) || (pos < size/2))
{
if((str[i] == '(') || (str[i] == '['))
{
opened[pos] = str[i];
pos++;
}
else if((str[i] == ')') || (str[i] == ']'))
{
if(str[i] == close)
{
opened[pos-1] = 0;
pos--;
}
else
{
error = 1;
break;
}
}
printf("%s\n", opened);
if(pos > 0)
{
switch(opened[pos-1])
{
case '(':
close = ')';
break;
case '[':
close = ']';
break;
}
}
else
close = 0;
i++;
}
return error;
}
int main() {
char str[20];
printf("%d\n", check(str, sizeof(str)));
return 0;
}
What does it mean by item=infix_exp[i++]; in the following C code? Line no 21. It is for infix to postfix conversion. As far as I know, here i is array index. But why is it incrementing without any loop?
This is the Code
#include<stdio.h>
#include<conio.h>
#define SIZE 100
int top = -1;
char stack[SIZE];
void push(char item);
char pop();
int is_operator(char symbol);
int precedence(char symbol);
void main()
{
int i;
int j;
char infix_exp[SIZE], postfix_exp[SIZE];
char item;
char x;
printf("\nEnter Infix expression in parentheses: \n");
gets(infix_exp);
i=0;
j=0;
item=infix_exp[i++]; /* HERE */
while(item != '\0')
{
if(item == '(')
{
push(item);
}
else if((item >= 'A' && item <= 'Z') ||
(item >= 'a' && item <= 'z'))
{
postfix_exp[j++] = item;
}
else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 && precedence(x)
>= precedence(item))
{
postfix_exp[j++] = x;
x = pop();
}
push(x);
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j++] = x;
x = pop();
}
}
else
{
printf("\nInvalid Arithmetic Expression.\n");
getch();
}
item = infix_exp[i++];
}
postfix_exp[j++] = '\0';
printf("\nArithmetic expression in Postfix notation: ");
puts(postfix_exp);
getch();
}
void push(char item)
{
if(top >= SIZE-1)
{
printf("\nStack Overflow. Push not possible.\n");
}
else
{
top = top+1;
stack[top] = item;
}
}
char pop()
{
char item = NULL;
if(top <= -1)
{
printf("\nStack Underflow. Pop not possible.\n");
}
else
{
item = stack[top];
stack[top] = NULL;
top = top-1;
}
return(item);
}
int is_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol == '/' ||
symbol == '+' || symbol == '-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if(symbol == '^')
{
return(3);
}
else if(symbol == '*' || symbol == '/')
{
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}
item=infix_exp[i++]; means to fetch ith element of the array to item, then increment i by 1.
It seems the line is there because the author of the code prefered to write item=infix_exp[i++]; twice (the another one is in line 59) to using while((item=infix_exp[i++]) != '\0').
item=infix_exp[i++]; is equivalent to
item=infix_exp[i];
i++;
except that latter has one more sequence point.
It is just the same as
item=infix_exp[i];
i = i + 1;
The statement where you are getting confused is
item=infix_exp[i++]
Before this line of statement if the value of i is 0 then in this line the value of i is also 0 but the value of i in the next line is 1.
This statement is not in loop but the value of i is used in a loop. Each time the loop iterates the value of i is increased by one. Besides, i++ has no relation with loop. If you want to make the effect of increment in the very next line then you can do so.
I've searched the site to see if there are any similar questions to mine, but I can't seem to find one which offers a working solution. Below is the code to a fully functional infix to postfix converter for arithmetic syntax.
The issue is that if the user enters a string such as 23+89, the infix output is 2389+. This is of course correct, however I wish to add separators between the operands to distinguish between 23 and 89. How can this be implemented with the array I am using? Do I need to add a whitespace character between the operands in the array? How can this be achieved?
Any insight will be greatly appreciated.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#define MAX 100 // maximum number of input characters
int precedence(char x);
struct stack
{
char my_stack[MAX];
int pointer;
};
int precedence(char x)
{
if(x == '(')
return(0);
if(x == '+' || x == '-')
return(1);
if(x == '*' || x == '/')
return(2);
return(3);
}
int main(void)
{
struct stack S;
S.pointer = 0;
char c;
char output[MAX]; // stores output
int out_count = 0; // stores no. of characters of output
char data; // stores data being popped off the stack
printf("Enter an expression to check:\n");
fflush(stdout);
while(('\n' != (c=getchar())))
{
if(isdigit(c)) // if an operand is found print to screen
{
output[out_count] = c;
out_count++;
}
else if(c == '(') // if ( is found push on stack
{
S.pointer = S.pointer +1;
S.my_stack[S.pointer] = c;
}
else if(c == ')') // if ) is found pop stack until ( is found
{
while(!(S.my_stack[S.pointer] == '('))
{
data = S.my_stack[S.pointer];
output[out_count] = data;
out_count++;
S.pointer = S.pointer -1;
}
S.pointer = S.pointer -1; // pop the extra (
}
else if(c == '*' || c == '/' || c == '+' || c == '-')
{
if((S.pointer) == 0)
{
S.pointer = S.pointer +1;
S.my_stack[S.pointer] = c;
}
else
{
if(precedence(S.my_stack[S.pointer]) > precedence(c))
{
// pop stack
int i;
for(i=(S.pointer); i >= 0; i--)
{
if(c == '(' || c == ')')
continue;
output[out_count] = c;
out_count++;
}
S.pointer = 0;
}
else
{
// push c on stack
S.pointer = S.pointer +1;
S.my_stack[S.pointer] = c;
}
}
}
}
int j = S.pointer;
while(j != 0)
{
if(S.my_stack[j] == '(')
continue;
output[out_count] = S.my_stack[j];
out_count++;
j--;
}
output[out_count+1] = '\0';
int k=0;
while(output[k] != '\0') // go through output array and print
{
printf("%c", output[k]);
k++;
}
return 0;
}
23+89, the infix output is 2389+. This is of course correct,
Well, no. 2389 is not the same as 23 and 89.
You could either add whitespace, or an explicit "push" operator. HP calculators often use an up arrow ↑ symbol to show that operation.
This is the code for reverse polish notation. When I run it on unix it works fine but codechef says wrong answer. Please help.
#include<stdio.h>
#include<string.h>
void push(char);
void pop();
char stack[400];
unsigned long top=0;
int main()
{
unsigned long len, test_case,i=0,j=0;
char expr[400];
scanf("%u\n",&test_case);
for(;j<test_case;j++)
{
scanf("%s\n",expr);
len=strlen(expr);
for(;i<len;i++)
{
if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/'||expr[i]=='^')
push(expr[i]);
else if(expr[i]==')')
{
pop();
}
else if(expr[i]=='(')
continue;
else
printf("%c",expr[i]);
}
}
return 0;
}
void pop()
{
if(top==-1)
return;
else
{
printf("%c",stack[top]);
top=top-1;
}
}
void push(char x)
{
if(top==400)
return;
else
{
stack[++top]=x;
}
}
Change this line
for(;i<len;i++)
to
for(i=0;i<len;i++)
and then it will work fine. In codechef there are many testcases, so in your program it will work fine for 1st testcase but it will fail after that because the value of 'i' from the 2nd testcase will start from where it ends in the 1st case. So,you have to initialize the value of i in every testcase.
I went through the same situation and this is my solution to that problem:
#include<stdio.h>
#include<string.h>
char stack[400];
unsigned long top = 0;
void pop() {
if (top == -1) {
return;
} else {
printf("%c",stack[top]);
top = top - 1;
}
}
void push(char x) {
if (top == 400) {
return;
} else {
stack[++top] = x;
}
}
int main() {
unsigned long t, n, i = 0, j = 0;
char a[400];
scanf("%lu", &t);
while(t--) {
scanf("%s", &a);
n = strlen(a);
for (i = 0; i < n; i++) {
if (a[i] == '+' || a[i] == '-' || a[i] == '*' || a[i] == '/' || a[i] == '^') {
push(a[i]);
} else if (a[i] == ')') {
pop();
} else if (a[i] == '(') {
continue;
} else {
printf("%c", a[i]);
}
}
printf("\n");
}
return 0;
}
You missed writing some sections and there is another notation along with +, -, * and / i.e. ^ which was also required for the evaluation of the answer.
Only place that I noticed for the time being
`scanf("%u\n",&test_case);` //"%u" for unsigned not unsigned long
`scanf("%s\n",expr);` //%s : newline to not input are separated by white space
you have to initialize unsigned long i = 0 ; inside for loop as if you take two or more test cases i will initiate from where it left in last case for every next case .
Add
top=0;
stack[0]=0;
before ending for loop in function main and change
for(;i<len;i++) to for(i=0;i<len;i++).