Finish function to complete code.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define Max_Size 20
void push(char S[], int *p_top, char value);
char pop(char S[], int *p_top);
void printCurrentStack(char S[], int *p_top);
int validation(char infix[], char S[], int *p_top);
char *infix2postfix(char infix[], char postfix[], char S[], int
*p_top);
int precedence(char symbol);
int main()
{
// int choice;
int top1=0; //top for S1 stack
int top2=0; //top for S2 stack
int *p_top1=&top1;
int *p_top2=&top2;
char infix[]="(2+3)*(4-3)"; //Stores infix string
int n=strlen(infix); //length of the string
char S1[Max_Size]; //Stack used for checking balanced
expression
char S2[Max_Size]; //stack used for postfix conversion
char postfix[n]; //stores postfix string
char *p_postfix=postfix; //pointer to the string posfix (used for
ease in passing between function) pointer points to the first
element of postfix[]
if (validation(infix, S1, p_top1)==0){
printf("The expresion %s is NOT balanced \n", infix);
exit(1);
}
else
printf("The expresion %s is balanced\n", infix);
// printf("Infix Expression is %s\n", infix );
p_postfix=infix2postfix(infix, postfix, S2, p_top2);
// printf("Infix Expression is %s\n", infix );
printf("Postfix Expression is--->");
while (*p_postfix!='\0') {
printf("%c", *p_postfix++);
}
printf("\n" );
}
void push(char S[], int *p_top, char value) {
S[(*p_top)] = value;
(*p_top)++;
// printCurrentStack(s, p_top);
}
int isEmpty(char S[], int *p_top){
if (*p_top==0) {
return 1;
}
return 0;
}
char pop(char S[], int *p_top) {
if (isEmpty(S, p_top)==0) { //if not empty
(*p_top)--;
char x= S[(*p_top)];
// printCurrentStack(s, p_top);
return x;
}
}
char *infix2postfix(char infix[], char postfix[], char S2[], int *p_top2){
//write your code here
char * p_postfix=postfix;
return p_postfix;
}
void printCurrentStack(char s[], int *p_top) {
if ((*p_top) == 0) {
printf("The stack is empty.\n\n");
return;
}
printf("There are %d elements currently in the stack.\n", (*p_top));
for (int i = (*p_top) - 1; i >= 0; i--)
printf("%c \n", s[i]);
printf("\n");
}
int precedence (char symbol){
switch (symbol) {
case '(':
return 0; //this is special case as we do not pop it until we find
a ')' hence 0
break;
case '^':
return 3;
break;
case '/':
case '*' :
return 2;
break;
case '+':
case '-':
return 1;
default:
printf("Error : not an operator\n");
return 0;
break;
}
}
int validation(char infix[], char S1[], int *p_top1){
char y;
//int n=sizeof(s)/sizeof(s[0]);
int i=0;
//printf("%c, %c, %c, %c, %c\n",s[i], s[i+1], s[i+2], s[i+3],
s[i+9]);
while(infix[i]!='\0'){
switch (infix[i]) {
case '(':
case '{':
case '[':
push(S1, p_top1,infix[i] );
// printf("pushed %c\n",s[i] );
// printCurrentStack(s, p_top);
break;
case ')':
y=pop(S1, p_top1);
// printf("1. popped %c\n",y );
if(y=='(')
break;
else {
return 0;
}
break;
case '}':
y=pop(S1, p_top1);
//printf("2. popped %c\n",y );
if(y=='{')
break;
else {
return 0;
}
break;
case ']':
y=pop(S1, p_top1);
//printf("3. popped %c\n",y );
if(y=='[')
break;
else {
return 0;
}
break;
default :
break;
}
i++;
}
return 1;
}
Updated Code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define Max_Size 20
void push(char S[], int *p_top, char value);
char pop(char S[], int *p_top);
void printCurrentStack(char S[], int *p_top);
int validation(char infix[], char S[], int *p_top);
char *infix2postfix(char infix[], char postfix[], char S[], int
*p_top);
int precedence(char symbol);
int main()
{
// int choice;
int top1=0; //top for S1 stack
int top2=0; //top for S2 stack
int *p_top1=&top1;
int *p_top2=&top2;
char infix[]="(2+3)*(4-3)"; //Stores infix string
int n=strlen(infix); //length of the string
char S1[Max_Size]; //Stack used for checking balanced
expression
char S2[Max_Size]; //stack used for postfix conversion
char postfix[n]; //stores postfix string
char *p_postfix=postfix; //pointer to the string posfix (used for
ease in passing between function) pointer points to the first
element of postfix[]
if (validation(infix, S1, p_top1)==0){
printf("The expresion %s is NOT balanced \n", infix);
exit(1);
}
else
printf("The expresion %s is balanced\n", infix);
// printf("Infix Expression is %s\n", infix );
p_postfix=infix2postfix(infix, postfix, S2, p_top2);
// printf("Infix Expression is %s\n", infix );
printf("Postfix Expression is--->");
while (*p_postfix!='\0') {
printf("%c", *p_postfix++);
}
printf("\n" );
}
void push(char S[], int *p_top, char value) {
S[(*p_top)] = value;
(*p_top)++;
// printCurrentStack(s, p_top);
}
int isEmpty(char S[], int *p_top){
if (*p_top==0) {
return 1;
}
return 0;
}
char pop(char S[], int *p_top) {
if (isEmpty(S, p_top)==0) { //if not empty
(*p_top)--;
char x= S[(*p_top)];
// printCurrentStack(s, p_top);
return x;
}
}
char *infix2postfix(char infix[], char postfix[], char
S2[], int *p_top2)
{
// sz is used current
size of postfix[]
int sz=-1;
// Iterate over all the elements in infix
for (int i = 0; infix[i]; ++i)
{
// Here test for the type of character and based on the type of
character we perform task
// if the infix[i] is operand
if (infix[i] != '+' && infix[i] != '-' && infix[i]
!= '/' && infix[i] != '*' && infix[i] != '^'
&& infix[i]!='(' && infix[i]!=')')
postfix[++sz] = infix[i];
// If the infix[i] is ( then push it to stack s2
else if (infix[i] == '(')
push(S2, p_top2,infix[i] ) ;
// If the infix[i] is an ‘)’, pop and output from the s2
// until an ‘(‘ is encountered.
else if (infix[i] == ')')
{
while(!isEmpty(S2,p_top2) && S2[(*p_top2)-1] != '(')
postfix[++sz] = pop(S2,p_top2);
pop(S2,p_top2);
}
else // if infix i is operator
{
while (!isEmpty(S2,p_top2) && precedence(infix[i]) <=
precedence(S2[(*p_top2)-1]))
postfix[++sz] = pop(S2,p_top2);
push(S2, p_top2,infix[i] ) ;
}
}
// at the end of iterating over infix we will simply pop all
elements from stack which are remaining in stack
while (!isEmpty(S2,p_top2))
postfix[++sz] = pop(S2,p_top2);
// set the last character as null
postfix[++sz] = '\0';
char * p_postfix=postfix;
// return the pointet to postfix
return p_postfix;
}
void printCurrentStack(char s[], int *p_top) {
if ((*p_top) == 0) {
printf("The stack is empty.\n\n");
return;
}
printf("There are %d elements currently in the stack.\n", (*p_top));
for (int i = (*p_top) - 1; i >= 0; i--)
printf("%c \t", s[i]);
printf("\n");
}
int precedence (char symbol){
switch (symbol) {
case '(':
return 0; //this is special case as we do not pop it until we find
a ')' hence 0
break;
case '^':
return 3;
break;
case '/':
case '*' :
return 2;
break;
case '+':
case '-':
return 1;
default:
printf("Error : not an operator\n");
return 0;
break;
}
}
int validation(char infix[], char S1[], int *p_top1){
char y;
//int n=sizeof(s)/sizeof(s[0]);
int i=0;
//printf("%c, %c, %c, %c, %c\n",s[i], s[i+1], s[i+2], s[i+3],
s[i+9]);
while(infix[i]!='\0'){
switch (infix[i]) {
case '(':
case '{':
case '[':
push(S1, p_top1,infix[i] );
// printf("pushed %c\n",s[i] );
// printCurrentStack(s, p_top);
break;
case ')':
y=pop(S1, p_top1);
// printf("1. popped %c\n",y );
if(y=='(')
break;
else {
return 0;
}
break;
case '}':
y=pop(S1, p_top1);
//printf("2. popped %c\n",y );
if(y=='{')
break;
else {
return 0;
}
break;
case ']':
y=pop(S1, p_top1);
//printf("3. popped %c\n",y );
if(y=='[')
break;
else {
return 0;
}
break;
default :
break;
}
i++;
}
return 1;
}
Sample output:
The expresion (2+3)*(4-3)
is balanced
Postfix Expression is--->23+43-*
Note: If you have any
doubts regarding this solution please ask me in the comments
section and please do not forget to upvote the
answer.
Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...
This code in C converts infix to postfix and evaluates it. The problem is that it only evaluates one digit expressions. I need to fix it so that it can evaluate 2 digits expressions as well. #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> #define SIZE 100 char s[SIZE]; int top=-1; void infixToPostfix(char *infix, char *postfix); void postfixEvaluation(char *postfix); void push(char elem){ s[++top]=elem; } char pop(){ return(s[top--]); } int pr(char elem){ // Order of precedence switch (elem) { case '(':...
Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } } return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...
I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...
i want similar for this code to solve two questions : 1- Write a program to convert a postfix expression to infix expression 2-Write a program to convert an infix expression to prefix expression each question in separate code ( so will be two codes ) #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack { char s[SIZE]; int top; }; void push(Stack *st, char c) { st->top++; st->s[st->top] = c;...
Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...
#include <stdio.h> #include string.h #define C17_PRICE 12.80 #define F25_PRICE 4.29 #define DN3_PRICE 10.00 #define GG7_PRICE 35.00 #define MV4_PRICE 18.49 #define DISCOUNT 0.05 #define DISC_THRES 10 #define SAME_DAY_DELIVERY = 35.0 #define NEXT_DAY_DELIVERY = 20.0 typedef enum{ WRONG, C17, F25, DN3, GG7, MV4} part /*--- Function Prototypes ------------*/ part getPartType ( void ); int getquantity ( void ); int getDeliveryOption ( void ); float calcPriceOfParts( part orderedPart, int quantity ); float calcTotalCarges ( float orderPrice, char...
what's wrong with my code?????? #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXBINS 99 #define MAXCORS 26 #define MAXCUS 100 #define MAXPUR 10 /* datatype for a list of orders ----------------- */ typedef struct { int bin_order_number; char bin_order_char; }bin_order; typedef struct { int orderNo; bin_order orderbin; }order; typedef struct { int cusnumber; int n; /* number of the orders what the txt include */ order oo[MAXPUR+1]; }customer; typedef struct{ int n; /*number of the customers */ customer cc[MAXCUS+1];...
I NEED HELP FINDING THE BUG IN THIS CODE USING THE GDB #include #include <stdio.h> <string.h> Return the result of appending the characters in s2 to s1 Assumption: enough space has been allocated for sl to store the extra characters. char* append (char s1[ ], char s2[ ]) int sllenstrlen (s1); int s2lenstrlen (s2) int k; for (k=0; k<s21en; k++) { sl [kts11en] = s2 [k] ; return sl; int main () char strl [10]; char str2 [10]; while (1)...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str); int numUppltr(char *str); int numLwrltr(char *str); int punChar(char *str); char*readString(char *str); int main() { char givString[MAX_LEN]; puts("Enter your string(Max 255 characters):"); //readString(givString); gets(givString); printf("\nnumber of words :%d",numWords(givString)); printf("\nnumber of uppercase letters %d",numUppltr(givString)); printf("\nnumber of lowercase letters %d",numLwrltr(givString)); printf("\nnumber of punctuations %d\n",punChar(givString)); printf("\nnumber of digits:%d\n",numDigit(givString)); system("pause"); return 0; } char *readString(char *str) { int ch, i=0; while((ch=getchar())!=EOF && ch!='\n') { if(i) { str[i]=ch; i++; }...