i'm trying to make a small lexical analyser that recognizes numbers as it is in the following regular expression:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
char C[500];
bool temp;
bool Q0(char k[]);
bool Q1(char k[]);
bool Q2(char k[]);
bool Q3(char k[]);
void substr (char C[]);
char nextchar(char C[]);
int main()
{
printf("enter a string: ");
scanf("%s",C);
temp=Q0(C);
if (temp==true)
{
printf("it's number");
}
else
{
printf("false");
}
return 0;
}
bool Q0(char k[])
{
printf("%s\n",k);
printf("%c\n",k[0]);
if (k[0]=='+'||k[0]=='-')
{
return Q1(nextchar(k));
}
else if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q1(nextchar(k));
}
else
{
return false;
}
}
bool Q1(char k[])
{
if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q1(nextchar(k));
}
else if (k[0]=='.')
{
return Q2(nextchar(k));
}
else if (k[0]=='\0')
{
return true;
}
else
{
return false;
}
}
bool Q2(char k[])
{
if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q2(nextchar(k));
}
else if (k[0]=='e'||k[0]=='E')
{
return Q3(nextchar(k));
}
else
{
return false;
}
}
bool Q3(char k[])
{
if (k[0]=='+'||k[0]=='-')
{
return Q3(nextchar(k));
}
else if (k[0]=='0'||k[0]=='1'||k[0]=='2'||k[0]=='3'||k[0]=='4'||k[0]=='5'||k[0]=='6'||k[0]=='7'||k[0]=='8'||k[0]=='9')
{
return Q3(nextchar(k));
}
else if (k[0]=='\0')
{
return true;
}
else
{
return false;
}
}
void substr (char *C)
{
(C)++;
}
char nextchar(char *C){
substr(C);
return C[0];
}
why the code execute else section in the Q0 function ?
in the result k[0] equals 5 and it should execute the else if(k[0] == ...'5'...)
and go to the next state Q1
also i got lot of warnings about type casting and passing parameters but i can't understand why
PS: i printed the whole string and the first letter of it in order to make sure that there isn't a problem with the array
Related
I am trying to solve a problem of HackerRank. The problem wants me to find out if a string can be changed into a palindrome string or not with a certain number of changing characters of that string. That certain number will be given by input. I am giving the link of full description of the problem below.
In my code, I first checked if the string is already palindrome or not. Then I checked some cases under the first checking for the given number of changes to find out if that string can be changed into palindrome after changing the characters.
But I am getting different output for my code. Output that I found from code blocks is not the same as the output which I found in HackerRank.
Here is the link of the full problem statement
my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print(char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%c",str[i]);
}
}
void cpy(char qtr[],char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
qtr[i]=str[i];
}
}
void rev(char str[],int n)
{
int i;
char qtr[100000];
for(i=0;i<n;i++)
{
qtr[i]=str[n-1-i];
}
for(i=0;i<n;i++)
{
str[i]=qtr[i];
}
}
int main()
{
char str[100],str2[100000],qtr[100000],c;
int n,k,i;
scanf("%d",&n);
scanf("%d",&k);
fflush(stdin);
gets(str);
cpy(qtr,str,n);
rev(str,n);
cpy(str2,str,n);
int same=0;
if(strcmp(str2,qtr)==0)
{
same=1;
}
if(k==n)
{
c=qtr[i]+1;
for(i=0;i<k;i++)
{
if(qtr[i]==c)
{
c=c+1;
i=-1;
continue;
}
}
for(i=0;i<k;i++){qtr[i]=c;}
print(qtr,n);
return 0;
}
if(same==1)
{
if(n%2!=0)
{
if(qtr[n/2]!='9'){qtr[n/2]='9';}
else
{qtr[n/2]='1';}
k--;
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){printf(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
else
{
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){print(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
}
else
{
if(n%2==0)
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
else
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
}
}
I was practising some data structures problems that I did previously but this time I don't know what is going wrong in my code. I looked over a long time but I did not found the mistake. When I'm printing I'm just getting the first character and it looks like e is not being updated. But I've written e++.
#include<stdio.h>
#include "ctype.h"
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int priorityof(char x)
{
if(x=='(')
return 3;
else if(x=='+'|| x=='-')
return 1;
else if(x=='*'|| x=='/')
return 2;
}
int main()
{
char exp[20];
char *e;
e=exp;char x;
scanf("%c",exp);
while(*e!='\0')
{
if(isalnum(*e))
{
printf("%c", *e);
}
else if(*e=='(')
{
push(*e);
}
else if(*e==')')
{
while((x=pop())!='(')
printf("%c",x);
}
else {
while (priorityof(stack[top]) >= priorityof(*e)) {
printf("%c", pop());
push(*e);
}
}
e++;
}
while(top!=-1)
{
printf("%c",pop());
}
}
%c is for single character and reading your question it seems like you are giving more than one character so its a string, use %s.
#include<stdio.h>
#include "ctype.h"
int stack[20]; int top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() { return stack[top--]; }
int priorityof(char x) {
if(x=='(') return 3;
else if(x=='+'|| x=='-') return 1;
else if(x=='*'|| x=='/') return 2;
}
int main() {
char exp[20];
char *e;
e=exp;char x;
scanf("%s",exp);
while(*e!='\0') { if(isalnum(*e)) { printf("%c", *e); } else if(*e=='(') { push(*e); } else if(*e==')') { while((x=pop())!='(') printf("%c",x); } else { while (priorityof(stack[top]) >= priorityof(*e)) { printf("%c", pop()); push(*e); } } e++; } while(top!=-1) { printf("%c",pop()); } }
This is my attempt to write a program that can convert any expression in infix to postfix format. I assign -1 to the top to indicate the stack is empty. When push, the top is incremented, when pop, the top is decremented. However, when I type in a+b, the output only gives me ab without the + operator, while when I type (a+b), it says segmentation fault. I reckon there's something wrong with my stack, but couldn't figure out what went wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define SIZE 30
typedef struct Stack
{
int top;
int capacity;
char* storage;
} stack;
int isEmpty(stack* a);
char topelement(stack* a);
char pop(stack* a);
void push(stack* a,char b);
bool isOperand(char a);
int Precedence(char a);
stack* NewStack(char* a);
void InfixPostfix(char* a);
int main(void)
{
char expression[SIZE];
printf("Please enter an expression:");
scanf("%s",expression);
InfixPostfix(expression);
printf("\n");
}
int isEmpty(stack* a)
{
if(a->top==-1)
{
return 1;
}
else
return 0;
}
char topelement(stack* a)
{
return a->storage[a->top];
}
char pop(stack* a)
{
if(isEmpty(a)==1)
{
printf("Stack is Empty\n");
return '$';
}
else
return a->storage[a->top];
--(a->top);
}
void push(stack* a,char b)
{
++(a->top);
a->storage[a->top]=b;
}
bool isOperand(char a)
{
if ( (a >= 'a' && a<= 'z') ||(a>='A' && a<='Z'))
{
return 1;
}
else
return 0;
}
int Precedence(char a)
{
if(a=='+' || a=='-')
{
return 1;
}
if(a=='*' || a=='/')
{
return 2;
}
if(a=='^')
{
return 3;
}
else
return -1;
}
stack* NewStack(char* a)
{
stack* b= malloc(sizeof(stack));
if(b!=NULL)
{
b->top=-1;
b->storage=malloc((strlen(a))*sizeof(char));
return b;
}
else
return NULL;
}
void InfixPostfix(char* a)
{
int i; int j=-1;
stack* b=NewStack(a);
if(b!=NULL)
{
for(i=0; i<strlen(a) ;i++)
{
if(isOperand(a[i]))
{
a[++j]=a[i];
}
if(a[i]=='(')
{
push(b, a[i]);
}
if(a[i]==')')
{
while(isEmpty(b)==0 && topelement(b)!= '(')
{
a[++j]= pop(b);
}
}
else
{
while(isEmpty(b)==0 && Precedence(a[i]) <= Precedence(topelement(b)))
{
a[++j]=pop(b);
push(b,a[i]);
}
}
}
while(isEmpty(b)==0)
{
a[++j]=pop(b);
}
a[++j]='\0';
printf("%s",a);
}
}
Besides the already suggested malloc correction, there are a few more to make.
In pop() there's
return a->storage[a->top];
--(a->top);
where the last code line isn't reached; change that to return a->storage[a->top--];
In InfixPostfix(), else are missing before if(a[i]=='(') and if(a[i]==')').
In InfixPostfix(), after the loop
while(isEmpty(b)==0 && topelement(b)!= '(')
{
a[++j]= pop(b);
}
a pop(b); is missing - the element '(' must as well be removed from the stack.
In InfixPostfix(), the push(b,a[i]); must be removed from the loop
while(isEmpty(b)==0 && Precedence(a[i]) <= Precedence(topelement(b)))
{
a[++j]=pop(b);
push(b,a[i]);
}
and placed after that loop - the operator in a[i] has to be put onto the stack only once.
I tried to solve a prime number generator problem in spoj.com. I solved it and it runs perfectly on my machine. But after submitting the solution to online, it shows run time error. Please help me!
Could anyone take a look into my code and tell where the problem is?
#include <stdio.h>
int primeFactor[350],w=0,z,i;
main()
{
int m[9],n[9],t=0,tMax,j;
scanf("%d",&tMax);
while(t<tMax)
{
scanf("%d%d",&m[t],&n[t]);
primeFactors(n[t]);
t++;
}
for(j=0;j<tMax;j++)
{
for(z=m[j];z<=n[j];z++)
{
if(z>1)
primeCalc(z);
}
printf("\n\n");
}
}
primeFactors(int a)
{
int remainder;
for(z=2;z<=sqrt(a);z++)
{
if(z==2) {primeFactor[w]=z; w++;}
else
{
for(i=2;i<z;i++)
{
if(z%i==0)
{
remainder=z%i;
break;
}
else {remainder=z%i;}
}
if(remainder!=0)
{
primeFactor[w]=z;
w++;
}
}
}
return 0;
}
primeCalc(int x)
{
int remainder;
if(x==2)
{
printf("%d\n",x);
}
else
{
for(i=0;i<w;i++)
{
if(primeFactor[i]>=x)
break;
else if(x%primeFactor[i]==0)
{
remainder=x%primeFactor[i];
break;
}
else
{remainder=x%primeFactor[i];}
}
if(remainder!=0)
printf("%d\n",x);
}
return 0;
}
Your main function may be returning some indeterminate value because it doesn't have any return statement and the judge may be treating it as Runtime Error.
You will have to return 0 from the main function.
Also, you shouldn't omit the return type of each functions.
Try this:
#include <stdio.h>
int primeFactor[350],w=0,z,i;
int main(void)
{
int m[9],n[9],t=0,tMax,j;
scanf("%d",&tMax);
while(t<tMax)
{
scanf("%d%d",&m[t],&n[t]);
primeFactors(n[t]);
t++;
}
for(j=0;j<tMax;j++)
{
for(z=m[j];z<=n[j];z++)
{
if(z>1)
primeCalc(z);
}
printf("\n\n");
}
return 0;
}
int primeFactors(int a)
{
int remainder;
for(z=2;z<=sqrt(a);z++)
{
if(z==2) {primeFactor[w]=z; w++;}
else
{
for(i=2;i<z;i++)
{
if(z%i==0)
{
remainder=z%i;
break;
}
else {remainder=z%i;}
}
if(remainder!=0)
{
primeFactor[w]=z;
w++;
}
}
}
return 0;
}
int primeCalc(int x)
{
int remainder;
if(x==2)
{
printf("%d\n",x);
}
else
{
for(i=0;i<w;i++)
{
if(primeFactor[i]>=x)
break;
else if(x%primeFactor[i]==0)
{
remainder=x%primeFactor[i];
break;
}
else
{remainder=x%primeFactor[i];}
}
if(remainder!=0)
printf("%d\n",x);
}
return 0;
}
The given matrix is the problem matrix. With cell 0,0 marked as 2 to show the entry point. We have to find the path to reach element 9. I have used recursive backtracking and marked the valid path as 2 that will lead to the element 9. Can someone help me in implementing it using DP.
include <stdio.h>
int path(int [][7],int ,int);
static int exitstack=0;
int main(void) {
int result,i,j;
int arr[7][7]={
{2,1,1,1,1,1,1},
{0,1,1,0,0,0,1},
{0,1,0,1,1,0,1},
{1,0,9,0,1,1,1}, //the problem matrix
{1,0,1,0,1,1,0},
{1,1,1,1,1,0,1},
{1,0,0,0,0,0,1}
};
path(arr,0,0);
for(i=0;i<7;++i)
{
for(j=0;j<7;++j)
printf("%d",arr[i][j]);
printf("\n");
}
return 0;
}
int path(int p[7][7],int x,int y)
{
int i;
if(p[x][y]==9)
{
exitstack=1;
return 1;
}
for(i=1;i<=4;++i)
{
if(i==1) //moving right
{
if((y+1)<7)
{
if(p[x][(y+1)]==9)
path(p,x,(y+1));
if(p[x][(y+1)]==1)
{
p[x][(y+1)]=2;
if(path(p,x,(y+1)))
return 1;
}
}
}
if(i==2) //moving up
{
if((x-1)>0)
{
if(p[(x-1)][y]==9)
path(p,x-1,y);
if(p[(x-1)][y]==1)
{
p[x-1][y]=2;
if(path(p,x-1,y))
return 1;
}
}
}
if(i==3) //moving left
{
if(y-1>0)
{
if(p[x][(y-1)]==9)
path(p,x,y-1);
if(p[x][(y-1)]==1)
{
p[x][(y-1)]=2;
if(path(p,x,y-1))
return 1;
}
}
}
if(i==4) //moving down
{
if(x+1<7)
{
if(p[x+1][y]==9)
path(p,x+1,y);
if(p[x+1][y]==1)
{
p[x+1][y]=2;
if(path(p,x+1,y))
return 1;
}
}
}
}
if(p[x][y]==9)
{
return 1;
}
else
{
if(exitstack==1)
return 1;
else
{
p[x][y]=0;
return 0;
}
}
}
If you by DP mean dynamic programming, you could try to use the D* algorithm. It's a bit too long to describe here, but you can read more about it here (http://www.cs.cmu.edu/~ggordon/likhachev-etal.anytime-dstar.pdf) or here (http://en.wikipedia.org/wiki/D*), or google it.