Stack Data structures (infix to postfix) - data-structures

This is a program to convert infix to postfix in stack data structures.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int F(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '^':
case '$':return 5;
case '(':return 0;
case '#':return -1;
default:return 8;
}
}
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '^':
case '$':return 6;
case '(':return 9;
case ')':return 0;
default:return 7;
}
}
void infixtopostfix(char infix[],char postfix[])
{
int top,i,j=0;
top = -1;
char s[30],symbol;
s[++top]= '#';
for(i=0;i<strlen(infix);i++)
{
symbol = infix[i];
while(F(s[top]) > G(symbol))
{
postfix[j]=s[top--];
j++;
}
if(F(s[top]) != G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top] != '#')
{
postfix[j++]=s[top--];
}
postfix[j] = '\0';
}
int main()
{
char infix[20],postfix[20];
printf("Enter the infix expression:\n");
scanf("%s",infix);
infixtopostfix(infix,postfix);
printf("Postfix Expression is %s",postfix);
return 0;
}
In this code, what is going on with the following lines?
if(F(s[top]) != G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top] != '#')
{
postfix[j++]=s[top--];
}
I don’t understand how f(s[top]) != g(symbol) is different than f(s[top]) > g(symbol), because if it is greater, it means automatically it is not equal. And what is f(s[top]) and g(symbol)?

The conditions
f(s[top]) != g(symbol)
and
f(s[top]) > g(symbol)
are both different.
The first one gives false if f(s[top]) and g(symbol) are equal. But in the second condition if f(s[top]) is less than g(symbol) then it generates false. But according to the first condition, true should be generated.

Related

Why is my code printing symbols instead of letters?

I am supposed to write a program with three files (mysource.c, myMain.c, and mysource.h) to create a randomly generated string of characters. The length of the string is decided by the user. After the string is generated, the program will bump all letters in the string to the next letter in the alphabet to create a new offset string. I have most of the code sorted out, but my output is printing "╠╠╠╠". It prints the correct amount of characters but it is only printing those symbols. What do I need to do so that the characters print as actual letters rather than these symbols?
Here is my header file:
void generateChars(char *myarr, int len);
void offsetChars(char *myarr, int len);
void printChars(char *myarr, int len);
Here is my source code:
#include <stdio.h>
#include <stdlib.h>
#include "mysource.h"
void generateChars(char* myarr, int len)
{
int i = 0;
char letters[26] ={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'
,'p','q','r','s','t','u','v','w','x','y','z' };
for (i = 0; i < len; i++);
{
myarr[i] = letters[rand() % 26];
}
}
//end generate function
void offsetChars(char *myarr, int len)
{
char i;
int j;
for (j = 0; j < len; j++)
{
for (i = 'a'; i <= 'z'; i++)
{
if (myarr[j] == i)
{
myarr[j] = i + 1;
break;
}
if (myarr[j] == 'z')
{
myarr[j] = 'a';
break;
}
}
}
}
//end offset function
void printChars(char *myarr, int len)
{
int i;
for (i = 0; i < len; i++)
{
printf("%c",myarr[i]);
}
}//end of print function
Here is my main code:
#include <stdio.h>
#include <stdlib.h>
#include "mysource.h"
int main()
{
int n;
printf("How many random characters do you want to
generate?: ");
scanf_s("%i", &n);
char myarr[1024];
printf("\nOriginal Combo:\n");
generateChars(&myarr, n);
printChars(&myarr, n);
printf("\nOffset Combo:\n");
offsetChars(&myarr, n);
printChars(&myarr, n);
return 0;
}
Here is the output I get:
I don't have enough reputation so this is the picture of the output
Yes there are two source codes, the objective is to make this assignment work with both source codes. Any help is appreciated!

creating compare string function

I created a function that compares strings, and as I was frustrated about it always missing the last character in the second string and always returning "identical strings" as a result, I noticed that I was messing around and used gets() instead of fgets() for the second string. I changed that and the function works as expected.
My question is, why does the gets() function subtract that last character? Shouldn't it subtract the null and leave it at that?
Does that mean that as a newcomer to C, I should avoid using gets() and focus on fgets() instead? I'm starting to think of them in the same way I think of strcmp() vs strncmp()
Thanks for your time everyone!
Note: I'm aware that I don't really need the (i==j) at the end, I just left it there (extra security, maybe?).
bool compare_string(const char *string1, const char *string2) {
int i = 0, j = 0, result = 0;
while (string1[i] != '\0') {
i++;
}
while (string2[j] != '\0') {
j++;
}
i = 0;
j = 0;
while ((string1[i] != '\0') && (string2[j] != '\0')) {
if (string1[i] < string2[j]) {
result = -1;
break;
} else if (string1[i] > string2[j]) {
result = 1;
break;
} else if (string1[i] == string2[j]) {
result = 0;
}
i++;
j++;
}
if ((result == 0) && (i==j)) {
printf("identical strings \n");
} else if (result == -1) {
printf("not identical, -1 \n");
} else if (result == 1) {
printf("not identical, 1 \n");
}
}
//in main
char str_compare1[STRING_LIMIT];
char str_compare2[STRING_LIMIT];
printf("enter 1st string to compare, (100) characters or less: \n");
fgets(str_compare1, STRING_LIMIT, stdin);
printf("enter 2nd string to compare, (100) characters or less \n");
fgets(str_compare2, STRING_LIMIT, stdin);
result = compare_string(str_compare1, str_compare2);

Spoj question ONP Transform the expression giving signal abort

I am trying to solve ONP - Transform the Expression in spoj. The question is to transform infix expression into postfix expression. I have used std::stack as my data structure and shunting-yard algorithm for solving it. The code runs fine on my computer using g++. But on spoj, it gives SIGABRT error. Even on ideone, it gives run time error free() invalid pointer.
I have tried several test cases. At first, I thought that my program was taking too much memory, but upon testing with time -v (ubuntu), I found that the maximum space taken was in KB.
// ------------------- infix to postfix conversion ---------------
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
#include <utility>
using std::stack;
using std::pair;
using std::cout;
using std::cin;
using std::endl;
using std::string;
stack< pair<char, short> > op_st; // operator stack
short op_precedence(char op) {
// return operator precedence
// input: operator; output: its precedence value
switch (op) {
case '+': return 0;
case '-': return 1;
case '*': return 2;
case '/': return 3;
case '^': return 4;
case '(': return 6;
}
}
inline bool is_operator(char sym) {
// is sym an operator?
return (sym == '+' || sym == '-' || sym == '*' || sym == '/' || sym == '^' || sym == '(');
}
inline bool is_operand(char sym) {
// is sym an operand?
return (sym >= 'a' && sym <= 'z');
}
void in_to_post(string & expr) {
// infix to postfix converter
// input: infix expression
for (int i = 0; i < expr.length(); ++i) {
if (is_operator(expr[i])) { // operator
// pop op_stack until the
// top of the stack has less precedence
// than curr operator or stack is empty
while(1) {
if (op_st.empty()) { // stack is empty; straight away push
op_st.push(std::make_pair(expr[i], op_precedence(expr[i])));
break;
}
pair <char, short> & top_op = op_st.top();
if (op_precedence(top_op.second) >= op_precedence(expr[i])) {
cout << top_op.first;
op_st.pop();
}
else {
op_st.push(std::make_pair(expr[i], op_precedence(expr[i])));
break;
}
}
}
else if (is_operand(expr[i])) { // operand; push it to output queue immediately
cout << expr[i];
}
else if (expr[i] == ')') { // right paranthesis
while (1) {
if (op_st.empty()) { // invalid expression; ')' reached before matching '('
//cout << "No matching '(' found\n";
abort();
}
pair <char, short> & top_op = op_st.top();
if (top_op.first == '(') { // matching '(' found; stop
op_st.pop();
break;
}
else {
cout << top_op.first;
op_st.pop();
}
}
}
}
// pop out the whole op_st (if any)
while (!(op_st.empty())) {
pair <char, short> & top_op = op_st.top();
cout << top_op.first;
op_st.pop();
}
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
string expr;
cin >> expr;
//cout << expr.length() << endl;
in_to_post(expr);
cout << "\n";
}
return 0;
}
Input to program given on my system:
((a+b)-c*(d+e))^((a+b)-c*(d+e))+((a+b)-c*(d+e))-((a+b)-c*(d+e))+((a+b)-c*(d+e))^((a+b)-c*(d+e))+((a+b)-c*(d+e))-((a+b)-c*(d+e))-((a+b)-c*(d+e))^((a+b)-c*(d+e))+((a+b)-c*(d+e))-((a+b)-c*(d+e))*((a+b)-c*(d+e))^((a+b)-c*(d+e))+((a+b)-c*(d+e))-((a+b)-c*(d+e))^((a+b)-c*(d+e))^((a+b)-c*(d+e))+((a+b)-c*(d+e))-((a+b)-c*(d+e))+((a+b)-c*(d+e))^((a+b)-c*(d+e))+((a+b)-c*(d+e))-((a+b)-c*(d+e))^((a+b)-c*(d+e))^((a+b)-c*(d+e))+((a+b)-c*(d+e))-((a+b)-c*(d+e))
Successfully gives the output:
ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*-ab+cde+*--+^^-+^+-+^^-+^*-+^--+^+-+^.
But, the same code gives free() invalid pointer error in ideone. Why is that?
op_precedence(top_op.second) calls op_precedence with the number returned by earlier op_precedence call - not with the operator character.
When op_precedence is passed an argument that doesn't match one of the recognized operators, the program exhibits undefined behavior, by way of reaching the end of a non-void function without encountering a return statement.
So, after Igor Tandetnik kindly pointed the mistake to me, in line 58, I changed op_precedence(top_op.second) to top_op.second. I also added default case to op_precedence function to correct the warning. After compiling and running, this code did actually abort() on line 75 for simple input ((a+b)). It turns out, that my implementation of the algorithm was wrong. My code didn't take into consideration associativity of operator '('. According to the question, we don't need to take into consideration associativity for other operators but for '(' we need its precedence to be higher than all other operators when outside of the stack, and lower than all other operators when inside of the stack. This is to ensure that when any operator other than '(' is the input and the top of the stack is '(', we could push the input operator into the stack. The corrected code is below:
// ------------------- infix to postfix conversion ---------------
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
#include <utility>
using std::stack;
using std::pair;
using std::cout;
using std::cin;
using std::endl;
using std::string;
short op_out_precedence(char op) {
// return operator precedence (when outside of stack)
// in and out precedence is to take care of associativity
// Here we don't require the associativity of any other operator except '('
// input: operator; output: its precedence value
switch (op) {
case '+': return 1;
case '-': return 2;
case '*': return 3;
case '/': return 4;
case '^': return 5;
case '(': return 6;
default : abort(); // not supposed to happen
// operator can't be other than the mentioned cases
}
}
short op_in_precedence(char op) {
// return operator precedence (when inside of stack)
// in and out precedence is to take care of associativity
// Here we don't require the associativity of any other operator except '('
// input: operator; output: its precedence value
switch (op) {
case '+': return 1;
case '-': return 2;
case '*': return 3;
case '/': return 4;
case '^': return 5;
case '(': return 0;
default : abort(); // not supposed to happen
// operator can't be other than the mentioned cases
}
}
inline bool is_operator(char sym) {
// is sym an operator?
return (sym == '+' || sym == '-' || sym == '*' || sym == '/' || sym == '^' || sym == '(');
}
inline bool is_operand(char sym) {
// is sym an operand?
return (sym >= 'a' && sym <= 'z');
}
void in_to_post(string & expr) {
// infix to postfix converter
// input: infix expression
stack< pair<char, short> > op_st; // operator stack
int len = expr.length();
for (int i = 0; i < len; ++i) {
if (is_operator(expr[i])) { // operator
// pop op_stack until the
// top of the stack has less or equal precedence
// than curr operator or stack is empty
while(1) {
if (op_st.empty()) { // stack is empty; straight away push
op_st.push(std::make_pair(expr[i], op_in_precedence(expr[i])));
break;
}
pair <char, short> & top_op = op_st.top();
if (top_op.second > op_out_precedence(expr[i])) {
cout << top_op.first;
op_st.pop();
}
else {
op_st.push(std::make_pair(expr[i], op_in_precedence(expr[i])));
break;
}
}
}
else if (is_operand(expr[i])) { // operand; push it to output queue immediately
cout << expr[i];
}
else if (expr[i] == ')') { // right paranthesis
while (1) {
if (op_st.empty()) { // invalid expression; ')' reached before matching '('
cout << "No matching '(' found\n";
abort();
}
pair <char, short> & top_op = op_st.top();
if (top_op.first == '(') { // matching '(' found; stop
op_st.pop();
break;
}
else {
cout << top_op.first;
op_st.pop();
}
}
}
}
// pop out the whole op_st (if any)
while (!(op_st.empty())) {
pair <char, short> & top_op = op_st.top();
cout << top_op.first;
op_st.pop();
}
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
string expr;
cin >> expr;
//cout << expr.length() << endl;
in_to_post(expr);
cout << "\n";
}
return 0;
}

the same result when i run the program

#include <iostream>
using namespace std;
int main()
{
bool result;
char text[1000];
cin>>text;
int len=sizeof(text);
for(int i = 0 ;i<len; ++i)
{
if(text[i]=='t' && text[i+1]=='r' && text[i+2]=='u' && text[i+3]=='e')
result = true;
else if(text[i]=='f' && text[i+1]=='a' && text[i+2]=='l' && text[i+3]=='s' && text[i+4]=='e')
result = false;
}
for(int i = 0 ;i<len; ++i)
{
if(text[i]=='n' && text[i+1]=='o' && text[i+2]=='t')
result = !result;// i think here is the problem
}
if(result == true)
cout<<"true"<<endl;
else if(result == false)
cout<<"false"<<endl;
return 0;
the exercise:
A boolean value can be either True or False. Given a string with less than 1000 characters with a number of space-separated not directives terminated by a True or False value, evaluate the boolean expression.
but when i run the program the result is always true.
please can you tell me where is the problem
Why don't you just use what is already there?
#include <iostream>
#include <iterator>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
int main()
{
bool result;
// Read the line
std::string line;
std::getline(std::cin, line);
// Split the line at spaces (https://stackoverflow.com/a/237280/1944004)
std::istringstream iss(line);
std::vector<std::string> tokens{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
// Convert last element to bool
if (tokens.back() == "true") result = true;
else if (tokens.back() == "false") result = false;
else throw std::invalid_argument("The last argument is not a boolean!");
// Remove the last element
tokens.pop_back();
// Loop over the nots
for (auto const& t : tokens)
{
if (t == "not") result = !result;
else throw std::invalid_argument("Negation has to be indicated by 'not'!");
}
// Output the result
std::cout << std::boolalpha << result << '\n';
}
Live example

parentheses balanced in C language [closed]

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 6 years ago.
Improve this question
This is a parentheses balanced code. I submitted and got comment from my professor.
"Your stack is supposed to grow and shrink dynamically to accommodate any number of characters. DO NOT USE scanf for %s, this is risky behavior and Dr. Adams does not approve. Write a helper function to read in characters until newline."
Can you help me to fix this problem?
Your professor is correct and he gave you the solution: do not read a line into a buffer with scanf("%s",...): an arbitrary long line will cause a buffer overflow. You do not need to read a full line, just make check_balanced read one character at a time:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
#define TRUE 1
#define FALSE 0
int check_matching(void);
int main(int argc, char *argv[]) {
int n, i, c;
/* get the number of cases */
if (scanf("%d", &n) != 1) {
printf("invalid number\n");
exit(1);
}
/* consume the rest of the line */
while ((c = getchar()) != EOF && c != '\n')
continue;
for (i = 0; i < n; i++) {
if (check_matching()) {
printf("yes\n");
} else {
printf("no\n");
}
}
return 0;
}
int check_matching(void) {
int ret = TRUE, symbol, checkSymbol;
LinkedStack *pStack;
StackNode *pNode;
StackNode node;
pStack = createLinkedStack();
if (pStack == NULL) {
printf("createLinkedStack failed\n");
exit(1);
}
/* read a full line, one byte at a time */
while ((symbol = getchar()) != EOF && symbol != '\n') {
if (ret == FALSE)
continue;
switch (symbol) {
case '(':
case '[':
case '{':
node.data = symbol;
pushLS(pStack, node);
break;
case ')':
case ']':
case '}':
pNode = popLS(pStack);
if (pNode == NULL) {
ret = FALSE;
break;
} else {
checkSymbol = pNode->data;
if ((symbol == ')' && checkSymbol == '(')
|| (symbol == ']' && checkSymbol == '[')
|| (symbol == '}' && checkSymbol == '{')) {
// Right case. do nothing.
} else {
ret = FALSE;
}
free(pNode);
}
break;
}
}
if (isLinkedStackEmpty(pStack) == FALSE) {
ret = FALSE;
}
deleteLinkedStack(pStack);
return ret;
}

Resources