mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
commit
c27bf5d624
62
convert.c
Normal file
62
convert.c
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
//convert infix to postfix
|
||||||
|
#include <stdio.h>
|
||||||
|
char stack[20]; //declaring an empty stack
|
||||||
|
int top = -1; //declaring top of stack
|
||||||
|
|
||||||
|
void push(char x) //push function
|
||||||
|
{
|
||||||
|
stack[++top] = x; //each time it is pushed top is increamented
|
||||||
|
}
|
||||||
|
|
||||||
|
char pop() //pop function
|
||||||
|
{
|
||||||
|
if(top == -1)
|
||||||
|
return -1;
|
||||||
|
else
|
||||||
|
return stack[top--]; //each time it is popped top is decreamented
|
||||||
|
}
|
||||||
|
|
||||||
|
int priority(char x) // to check the priority of characters
|
||||||
|
{
|
||||||
|
if(x == '(')
|
||||||
|
return 0;
|
||||||
|
if(x == '+' || x == '-')
|
||||||
|
return 1;
|
||||||
|
if(x == '*' || x == '/')
|
||||||
|
return 2;
|
||||||
|
if(x=='^')
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char exp[20];
|
||||||
|
char *e, x;
|
||||||
|
printf("Enter the expression :: ");
|
||||||
|
scanf("%s",exp);
|
||||||
|
e = exp;
|
||||||
|
while(*e != '\0')
|
||||||
|
{
|
||||||
|
if(isalnum(*e))
|
||||||
|
printf("%c",*e);
|
||||||
|
else if(*e == '(') //push it directly into the stack
|
||||||
|
push(*e);
|
||||||
|
else if(*e == ')') //pop until we gwt the left parenthesis
|
||||||
|
{
|
||||||
|
while((x = pop()) != '(')
|
||||||
|
printf("%c", x);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while(priority(stack[top]) >= priority(*e)) //pop until priority of top of stack is greater than equal to priority of expressions character
|
||||||
|
printf("%c",pop());
|
||||||
|
push(*e); //otherwise push
|
||||||
|
}
|
||||||
|
e++;
|
||||||
|
}
|
||||||
|
// keep popping until top is not -1
|
||||||
|
while(top != -1)
|
||||||
|
{
|
||||||
|
printf("%c",pop());
|
||||||
|
}
|
||||||
|
}
|
31
disarium.c
Normal file
31
disarium.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// a disarium number is a number such that sum of the digits of the number raised to their respective position equals the number
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int n,i=0,j,sum=0,l,k;
|
||||||
|
scanf("%d",&n);
|
||||||
|
k=n; //number stored in temporary variable
|
||||||
|
j=k;
|
||||||
|
while(n>0)
|
||||||
|
{
|
||||||
|
d=n%10;
|
||||||
|
n=n/10;
|
||||||
|
i++; //to get the number of digits which will be the position for last digit
|
||||||
|
}
|
||||||
|
while(k>0)
|
||||||
|
{
|
||||||
|
l=k%10; //to get the digit
|
||||||
|
sum=sum+pow(l,i);
|
||||||
|
k=k/10;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
if(j==sum)
|
||||||
|
{
|
||||||
|
printf("It is an disarium number");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
printf("It is not disarium number");
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user