Here I am converting an infix expression to a prefix expression.
For some test cases my result is perfect.
But for certain test cases I get an output which is correct according to some sites but wrong according to other sites. So I am in a dilemma as to whether my code is correct or not.
For example, if infix expression is:
A+B*C+D
then output according to my code is:
+A+*BCD
But some site mentions the output as:
++A*BCD
for the same input.
#include<stdio.h>
#include<string.h>
int main()
{
int top=-1,k=0,i,l,top2=-1,j,a,t,m,t1;
char s[100] ,s2[100],n, main[100];
char q[100],p[100];
printf("\nenter string :");
gets(main);
for(i=strlen(main)-1;i>=0;i--)
{
if(main[i]=='(')
{
q[k]=')';
}
else if(main[i]==')')
{
q[k]='(';
}
else
{
q[k]=main[i];
}
q[k+1]='\0';
k++;
}
k=0;
strcat(q,")");
s[++top]='(';
for(i=0;i<strlen(q);i++)
{
if(q[i]>='a' && q[i]<='z' || q[i]>='A' && q[i]<='Z' || q[i]>='0' && q[i]<='9')
{
p[k]=q[i];
p[k+1]='\0';
k++;
}
else if (q[i]=='(')
{
s[++top]=q[i];
}
else if (q[i]==')')
{
for(j=top;j>=0;j--)
{
if(s[j]!='(')
{
s[top--];
p[k]=s2[top2];
p[k+1]='\0';
s2[top2--];
k++;
}
else
break;
}
s[top--];
}
else if(q[i]=='+' || q[i]=='-' || q[i]=='*' || q[i]=='/' || q[i]=='^' || q[i]=='%')
{
if(q[i]=='+' || q[i]=='-')
{
n='1';
}
else if(q[i]=='*' || q[i]=='/' || q[i]=='%')
{
n='2';
}
else if (q[i]=='^')
{
n='3';
}
t=n-48;
if(top2!=-1)
{
for(j=top;j>=0;j--)
{
t1=s[j]-48;
if(t<=t1 )
{
s[top--];
p[k]=s2[top2];
p[k+1]='\0';
k++;
s2[top2--];
}
else if(s[j]=='(')
break;
}
}
s[++top]=n;
s2[++top2]=q[i];
}
}
k=0;
for(i=strlen(p)-1;i>=0;i--)
{
main[k]=p[i];
main[k+1]='\0';
k++;
}
printf("\nFINAL ANSWER :\n");
printf("\n%s",main);
}
Related
**My task is: The user should input numbers until -1 is pressed. The array consisting of those elements should be formed. I need to check is the array is descending up to the local minimum and ascending after it. If this condition is satisfied 'Yes' should be printed, 'No' in the opposite case.
I don't know what is wrong with my code, so I hope you could help. I am beginner.
int main() {
int a[100],n=0,b,i,j=0,c=0,h;
for (i=0;i<100;i++){
if (b==-1)
break;
else {scanf("%d", &b);
a[i]=b;
n++;
}
}
if (n<3)
printf("Not enough elements");
else{
for (i=0;i<n;i++){
for(j=0;j<n;j++)
a[j]=a[i];
}
for (i=0;i<n;i++){
if (a[i]>a[i+1] && i!=n-1)
continue;
else if (a[i]<a[i+1]){
c=1;
h=i;
break;
}
}
if (c==1 && h==i){
for(j=i;j<n;i++){
if(a[j]<a[j+1])
continue;
else if(a[j]>a[j+1] || a[j+1]==a[j]){
break;
c=0;
}
}
if(j==n-1 && c==1)
printf("Yes");
else if (c==0)
printf("No");
}
}
}
This code can help you :
#include <stdio.h>
int main() {
int a[100],n=0,b,i,j=0,c=0,h;
do
{
printf("Give me a number :");
scanf("%d", &b);
a[j]=b;
j++;
}while(b!=-1);
i=0;
j--;
if (j<3)
{
printf("Not enough elements");
}
else
{
i=0;
while(i<j-1 && a[i]>a[i+1])
{
i++;
}
if(i==j-1)
{
printf("No");
return 0;
}
else
{
c=i;
while(a[c+1]>a[c]&&c<j-1)
{
c++;
}
}
}
if(c==j-1)
{
printf("Yes");
}
else
{
printf("No");
}
}
I could not find specific student_Id info from a rage of given info.suppose, I am taking input from 1 to 100 all student info. now I want to find only 50th number student_ID all info.i could not do it. how it possible.here show my code. what's wrong with my code and how fixed it.thanks
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
struct student
{
char student_id[100];
char name[10];
int m[50],credit[100],sub[100];
int total,sumCredit;
float GP[100];
char result[5];
char grade[100][10];
double sumCreditxGP;
}*p,*s;
float gradesToGP(float marks);
char *markToG(float gp);
void lines(void);
void main()
{
int i,j,l,n,sub,k;
// clrscr();
printf("Enter the no. of students : ");
scanf("%d",&n);
p=(struct student*) malloc(n*sizeof(struct student));
//printf("%d",p);
//exit(0);
s=p;
for(i=0; i<n; i++)
{
printf("Enter a student id : ");
scanf("%s",&p->student_id);
printf("Enter a name : ");
scanf("%s",&p->name);
printf("Enter the no. of subject : ");
scanf("%d",&p->sub[i]);
p-> total=0;
p-> sumCredit=0;
p-> sumCreditxGP=0;
l=0;
for(j=0; j<p->sub[i]; j++)
{
one:
printf("Enter Marks of %d Subject\t%d : ",j+1,p->sub[i]);
scanf("%d",&p->m[j]);
printf("Enter Credit Hour: ");
scanf("%d",&p->credit[j]);
p->GP[j] = gradesToGP((float)p->m[j]);
strcpy(p->grade[j],markToG(p->m[j]));
if((p->m[j])>100)
{
printf("---------------Wrong Value Entered-----------\n");
goto one;
}
p->total+=p->m[j];
p->sumCredit+=p->credit[j];
p->sumCreditxGP+=p->credit[j]*p->GP[j];
if(p->m[j]<40)
l=1;
}
if(l==0)
strcpy(p->result,"PASS");
else
strcpy(p->result,"FAIL");
p++;
}
char search_id[10];
printf("Enter id to find specific student ");
scanf("%s",search_id);
//PROBLEM START HERE
for(i=0; i<n; i++)
{
if(p->student_id==search_id){
printf("found");
printf("%s",s->student_id);
}else{
printf("Not found");
}
s++;
}
getch();
}
float gradesToGP(float marks)
{
if (marks>=80 && marks<=100)
{
return(float)4.00;
}
else if (marks>=75 && marks<=79)
{
return(float)3.67;
}
else if (marks>=70 && marks<=74)
{
return(float)3.33;
}
else if (marks>=65 && marks<=69)
{
return(float)3.00;
}
else if (marks>=60 && marks<=64)
{
return(float)2.67;
}
else if (marks>=55 && marks<=59)
{
return(float)2.33;
}
else
{
return(float)5.00;
}
}
char *markToG(float marks)
{
if (marks>=80 && marks<=100)
{
return "A";
}
else if (marks>=75 && marks<=79)
{
return "A-";
}
else if (marks>=70 && marks<=74)
{
return "B+";
}
else if (marks>=65 && marks<=69)
{
return "B";
}
else if (marks>=60 && marks<=64)
{
return "B-";
}
else if (marks>=55 && marks<=59)
{
return "C+";
}
else
{
return "null";
}
}
void lines(void)
{ printf("**********************************************************************");
}
Please tell me how can I fixed it.thanks in advanced.
if(p->student_id==search_id){
printf("found");
Now, that's not how you compare strings in C. Use the strcmp() function for string comparison. You may read about strcmp() here.
The issue is your equality check: if(p->student_id==search_id)
Because both student_id and search_id are char arrays, the types will decay to pointers (char *) and this will never work as you expect. Instead, you need to use strcmp (or better, strncmp).
if(strncmp(p->student_id, search_id, 10) == 0) { /* equality */ }
While solving the NEXT PALINDROME question on SPOJ. I have considered for every possible case and wrote the code. But it is showing WA(Wrong Answer). I am not getting my mistake or any possible case where my code won't work.
Below is my code.
Please help me out.
Thank You.
#include<stdio.h>
#include<string.h>
char K[10000000];
int a[10000000];
int main()
{
int t,length,i,count,j,rem,carry;
scanf("%d",&t);
while(t--)
{
scanf("%s",K);
length=strlen(K);
count=0;
for(i=0;i<length;i++)
{
a[i]=K[i]-'0';
if(a[i]==9)
count++;
}
if(length==2 && count!=length)
{
if(a[0]<=a[1])
{
a[1]=++a[0];
}
else
{
a[1]=a[0];
}
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
continue;
}
if(count==length)
{
if(length==1)
{
printf("11\n");
continue;
}
memset(a,0,sizeof(a));
a[0]=10;
a[length-1]=1;
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
continue;
}
i=0;
j=length-1;
count=0;
while(j-i!=1 && j-i!=0)
{
if(a[i]>a[j])
{
a[j--]=a[i++];
count++;
}
else
{
if(a[i]<a[j])
count--;
a[j--]=a[i++];
}
}
if(count==0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
while(carry!=0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
if(i<0&&j>length-1)
break;
}
}
else
{
if(a[i]<a[j] || count<0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
while(carry!=0)
{
rem=a[i]+1;
a[i]=rem%10;
a[j++]=a[i--];
carry=rem/10;
if(i<0&&j>length-1)
break;
}
}
else
a[j]=a[i];
}
for(i=0;i<length;i++)
{
printf("%d",a[i]);
}
printf("\n");
}
return 0;
}
In this program i have encoded
Using the numerical value of each letter (A=1, B=2, ... Z= 26) the rules are as follows:
A–E Multiply its numerical value by 2
F–J Divide its numerical value by 3. Multiply the integer remainder by 5
K–O Divide its numerical value by 4. Multiply the integer remainder by 8.
P–T Add 10.
U- Z Find the largest integer factor of its numerical value less than the value itself. Multiply it by 12.
As an example if the letter to encode is a B, the B has a numerical value of 2 and encodes to a 4 and becomes a D, the 4th letter of the alphabet.
The G has a numerical value of 7. It encodes to a 5 and becomes an E.
The numerical value of Z is 26. Its largest factor is 13. You must count 156 (13*12) letters. This has the effect of wrapping around the alphabet 6 complete times and ending at Z. If a numerical value of zero is evaluated print a # symbol.
Problem with the code
- I am not able to print #.
- whenever i am entering Y i am getting J why???????
Here is my code:
#include<stdio.h>
void main()
{
char c;
int n,i,max=0,j,y;
int a[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};
scanf("%c",&c);
n=c;
for(i=0;i<5;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[((i+1)*2)-1]);
break;
}
}
for(i=5;i<10;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[(((i+1)%3)*5)-1]);
break;
}
}
for(i=10;i<15;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[(((i+1)%4)*8)-1]);
break;
}
}
for(i=15;i<20;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
}
if(flag==1)
{
printf("%c",a[((i+1)+10)-1]);
break;
}
}
for(i=15;i<26;i++)
{
int flag=0;
if(a[i]==n )
{
flag=1;
for(j=1;j<=(i+1)/2;j++)
{
if((i+1)%j==0)
{
if(max<j)
{
max=j;
}
}
}
}
if(flag==1)
{
y=(max*12)/6;
if(y>=0)
{
printf("%c",a[y-1]);
}
else
{
printf("#");
}
break;
}
}
}
Help me out guy's.
You have a variety of rules for encoding the characters, but at the end of the day, each letter of the alphabet maps to a single output character. So you can simply create a table that contains the outputs that correspond to each letter. Thus the code reduces to the following
int main( void )
{
char table[] = "BDFHJ#EJ#EX#HPXZABCDFBLJHZ";
char c;
if ( scanf("%c",&c) == 1 && c >= 'A' && c <= 'Z' )
printf( "%c\n", table[c - 'A'] );
}
Note that the table in this code attempts to follow the rules that you specified. Those rules don’t create a 1-to-1 mapping of input to output, but of course it’s trivial to change the table to achieve any mapping you like.
#include<stdio.h>
int large(int n);
int main()
{
char c;
int n,y;
scanf("%c",&c);
n=c;
if(n>=65 && n<70)
{
int val;
val=(((n-65)+1)*2)+64;
printf("%c\n",val);
}
else if(n>=70 && n<75)
{
int val=(n-65+1);
if((val)%3!=0)
{
printf("%c\n",(((val)%3)*5)+64);
}
else printf("#\n");
}
else if(n>=75 && n<80)
{
int val=(n-65+1);
if((val)%4!=0)
{
printf("%c\n",(((val)%4)*8)+64);
}
else
printf("#\n");
}
else if(n>=80 && n<85)
{
int val=n-65+1;
if(val>16)
{
val=val-26;
printf("%c",val+10+64);
}
else
printf("%c\n",(val+10)+64);
}
if(n>=85 && n<91)
{
int d,b=0,m;
int val=n-65+1;
d=large(val);
y=(d*12);
while(b<y)
{
b=b+26;
}
b=b-26;
m=y-b;
printf("%c\n",m+64);
}
return 0;
}
int large(int n)
{
int j,max=0;
for(j=1;j<=n/2;j++)
{
if(n%j==0)
{
if(max<j)
{
max=j;
}
}
}
return max;
}
Try this code:
#include<stdio.h>
int maxFac(int n)
{
int i,max=0;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
if(max<i)
max=i;
}
}
return max;
}
int main()
{
char c;
scanf("%c",&c);
if((int)c>=65 && (int)c<=69)
{
c=(((int)c-64)*2)+64;
}
else if ((int)c>=70 && (int)c<=74)
{
c= (((int)c-64)%3)*5;
if(c==0)
c='#';
else
c=(int)c+64;
}
else if((int)c>=75 && (int)c<=79)
{
c= (((int)c-64)%4)*8;
if(c==0)
c='#';
else
c=(int)c+64;
}
else if((int)c>=80 && (int)c<=84)
{
c= c+10;
if(c>90)
c=c-26;
}
else if((int)c>=85 && (int)c<=90)
{
int z;
c=c-64;
z=maxFac(c);
c=z;
c=(z*12)%26 + 64;
if(c=='#')
c=c+26;
}
printf("%c",c);
}
I need to convert infix to postfix and evaluate postfix expression. When reading from a file, I should be able to evaluate multiple expressions at once. But when I run it and it encounters an expression that is not well-formed in the sense that there are more closed parentheses than open ones, it evaluates that expression 2 or 3 times.
Code:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000
typedef struct stack
{
int data[MAX];
int top;
} stack;
int priority(char);
void init(stack*);
int empty(stack*);
int full(stack*);
char pop(stack*);
void push(stack*, char);
char top(stack*);
void add(char*, char, int);
void keyboard();
void read_file();
int change(char);
void pushYO(double);
double popYO();
stack s;
char x;
int i, token, topYO = -1;
double result[MAX];
char filepath[MAX];
int main()
{
char choice;
init(&s);
system("CLS");
printf("[1] Keyboard \n[2] Read from text file \n[3] Exit \n");
scanf("%c", &choice);
if(choice != '1' && choice != '2' && choice != '3')
{
main();
}
else if(choice == '1')
{
keyboard();
}
else if(choice == '2')
{
read_file();
}
else if(choice == '3')
{
printf("\nThank you for using Kei Shirabe's \ninfix to postfix converter and evaluator! :)\n");
exit(0);
}
}
void keyboard() //the keyboard input version. this works fine
{
//code here
}
void read_file()
{
int z, count, form, paren;
double one, two, three = 1;
char infx[MAX];
char pofx[MAX];
char choice;
FILE *text = fopen("text.txt", "a");
printf("Enter path of file:");
scanf("%s",&filepath);
FILE *textYO = fopen(filepath, "r");
if((textYO) == NULL)
{
printf("\nError! Unable to open %s\n\n", filepath);
}
else
{
while((fgets(infx, MAX, textYO))!= NULL)
{
form = -1, paren = 0, count = 0, z = 0;
infx[
strlen(infx)-1] = '\0';
for (i=0; i<strlen(infx); i++)
{
if((token = infx[i]) != '\n')
{
if(isalnum(token))
{
form++;
}
else
{
if(token == '(')
{
paren++;
}
else if(token == ')')
{
paren--;
}
else
{
form--;
}
}
if (paren < 0)
{
printf("%s", infx);
fprintf(text, "%s", infx);
printf("\n\nError! Not well formed :( \n-----------------\n");
fprintf(text, "\n\nError! Not well formed :( \n-----------------\n");
}
else
if(isalnum(token))
{
add(pofx, token, count);
count++;
}
else
if(token == '(')
{
push(&s, '(');
}
else
{
if(token == ')')
while((x = pop(&s)) != '(')
{
add(pofx, x, count);
count++;
}
else
if(token == '^')
{
push(&s, token);
}
else
{
while(priority(token) <= priority(top(&s)) && !empty(&s))
{
x = pop(&s);
add(pofx, x, count);
count++;
}
push(&s, token);
}
}
}
else
{
while(!empty(&s))
{
x = pop(&s);
add(pofx, x, count);
count++;
}
}
}
if(form != 0 || paren != 0)
{
printf("%s", infx);
fprintf(text, "%s", infx);
printf("\n\nError! Not well formed :( \n-----------------\n");
fprintf(text, "\n\nError! Not well formed :( \n-----------------\n");
}
else
{
form = -1, paren = 0;
printf("%s", infx);
fprintf(text, "%s", infx);
printf("\n\nPostfix: %s \n\n", pofx);
fprintf(text, "\n\nPostfix: %s\n\n", pofx);
while((token = pofx[z++]) != '\0')
{
three = 1;
if(!isdigit(token) && !isalpha(token))
{
two = popYO();
one = popYO();
switch(token)
{
case '+':
pushYO(one+two); break;
case '-':
pushYO(one-two); break;
case '*':
pushYO(one*two); break;
case '/':
pushYO(one/two); break;
case '^':
if (two > 0)
{
for(i=0;i<two;i++)
{
three = three * one;
}
pushYO(three);
three = 1; break;
}
else
{
for(i=0;i<(two-(2*two));i++)
{
three = three * one;
}
pushYO((1/three));
three = 1; break;
}
}
}
else if(isalpha(token))
{
if(isupper(token))
{
pushYO(token - '#');
}
if(islower(token))
{
pushYO(token - change(token));
}
}
else
{
pushYO(token - '0');
}
}
printf("Result: %lf\n-----------------\n", result[topYO]);
fprintf(text, "Result: %lf\n-----------------\n", result[topYO]);
}
}
}
fclose(text);
fclose(textYO);
printf("\nRun again? \n[1] Yes \n[any other key] No\n");
scanf("%c", &choice);
scanf("%c", &choice);
if(choice == '1')
{
main();
}
else
{
printf("\nThank you for using Kei Shirabe's \ninfix to postfix converter and evaluator! :)\n");
exit(0);
}
}
//other functions down here, not important
Sample text file (and yes there is an extra space at the end):
(1+2))
(1+2*3)
For this text file, the expression (1+2)) is evaluated three times, each result being "Not well formed". The last expression works as expected.
Any help please?