Wednesday, April 1, 2015

C program in stack data structure for infix and postfix expression

#define max 50
#include<stdio.h>
#include<conio.h>
#include<process.h>
void push(char ch);
char pop();
char stack[max];
int top=-1;
int prec(char chr);
void main()
{
 char a[60],b[60],ch;
 int i,j,p,r,x,y,re;
 clrscr();
 printf("\n Enter Infix Expression:-\n");
 scanf("%s",&a);
 i=0;
 j=0;
 for(i=0;a[i]!='\0';i++)
 {
  switch(a[i])
  {
   case '^':
   case '$':
   case '*':
   case '/':
   case '+':
   case '-': p=prec(a[i]);
    while(top!=-1&&p<=prec(stack[top]))
    {
      b[j]=pop();
      j++;
    }
    push(a[i]);  //push operator into stack
    break;
   case '(': push(a[i]);
    break;

   case ')':
    do
    {
     ch=pop();
     if(ch!='(')
     {
      b[j]=ch;
      j++;
     }
    }while(ch!='(');
     break;
   default : b[j]=a[i];  //output exp
    j++;
  }
 }
 while(top!=-1)
 {
  b[j]=pop();  //last pop the all operators.
  j++;
 }
 b[j]='\0';
 printf("\n Postfix Expression Is:-\n %s",b);
 printf("\n Evaluation of the Postfix expression:-\n");
 i=0;
 while(b[i]!='\0')
 {
  if(b[i]>='0'&&b[i]<='9')
  {
   push(b[i]-48); //convert char into nuumber
  }
  else
  {
   y=pop(); //pop the two poerands and prefrom operation
   x=pop();
   switch(b[i])
   {
      case '+' : r=x+y;
break;
      case '-' : r=x-y;
break;
      case '*' : r=x*y;
break;
      case '/' : r=x/y;
break;
      case '$' :
      case '^' : r=pow(x,y);
break;
   }
   push(r); //push the result into stack
  }
  i++;
 }
 re=pop(); // at the last pop the result and display.
 printf("\n %d",re);
 getch();
}

int prec(char chr)
{
 switch(chr)   // check the priority of the operators.
 {
  case '$' :
  case '^' : return(5);
  case '*' :
  case '/' : return(4);
  case '+' :
  case '-' : return(3);
  case '(' : return(2);
 }
 return(0);
}
void push(char x)
{
  top++;
  stack[top]=x;
}
char pop()
{
 char y;
 y=stack[top];
 top--;
 return(y);
}



RDBMS example for one to many relationship

Q.).
         Customer (cno,cname,city)
         Account (ano,acc_type,balance)

          Relationships between  customer and account  is one-to-many.
 
         Constraints :-primary key,
                                 Balance should be>100

SQL> create table customer2
  2  ( cno number(5)primary key,
  3    cname varchar2(30),
  4    city varchar2(40)
  5  );
Table created.

SQL> insert into customer2
  2  values('&cno','&cname','&city');
Enter value for cno: 101
Enter value for cname: mahendra
Enter value for city: pali
old   2: values('&cno','&cname','&city')
new   2: values('101','mahendra','pali')
1 row created.
SQL> select * from customer2;

  CNO   CNAME   CITY
--------   -----------  ------------------
  101 mahendra       pali
  102 babulal          pune
 103 amit              mumbai

 CNO   CNAME   CITY
--------   -----------  ------------------
 104        raju                 pune
105          akash          Ajmer

SQL> create table account
  2  ( ano number(5)primary key,
  3    acc_type varchar2(20),
  4    balance number(5) check(balance>100),
  5    cno number(5)references customer2(cno)
  6  );
Table created.

SQL> insert into account
2 values('&ano','&acc_type','&balance','&cno');
SQL> select * from account;

       ANO ACC_TYPE                          BALANCE        CNO
---------- ------------------------------ ---------- ----------
      1201 saving                              13000        101
      1202 current                             20000        102
      1203 saving                              14000        103
      1204 saving                              50000        104
      1205 current                             14000        105
      1206 saving                              25000        104
      1207 current                             19000        105


B). wirte a cursor to add interest of 3% to the balance of all account whose balance is greater than 10000.

  1  declare
  2     cursor c1 is select ano from account
  3     where balance>10000;
  4  begin
  5     for x in c1 loop
  6       update account
  7       set balance=balance+balance*3/100
  8       where ano=x.ano;
  9       commit;
 10     end loop;
 11* end;
SQL> /
PL/SQL procedure successfully completed.

SQL> select * from account;

       ANO ACC_TYPE                          BALANCE        CNO
---------- ------------------------------ ---------- ----------
      1201 saving                              13390        101
      1202 current                             20600        102
      1203 saving                              14420        103
      1204 saving                              51500        104
      1205 current                             14420        105
      1206 saving                              25750        104
      1207 current                             19570        105

7 rows selected.


a). create  or replace a PL/ SQL procedure to find total balance of all customers of pune city.
  1  create or replace procedure dis(x in varchar)
  2  is
  3      b number(20);
  4  begin
  5      select sum(a.balance) into b from customer2 c,
  6      account a where c.cno=a.cno and
  7      c.city=x;
  8        dbms_output.put_line('total balance of all customers of pune city:-'||b);
  9* end;
 10  /

Procedure created.

SQL> declare
  2
  3  begin
  4    dis('pune');
  5  end;
  6  /
total balance of all customers of pune city:-97850

PL/SQL procedure successfully completed.




RDBMS example of many to many relationship

Q.).    book ( bno, bname, pubname, price)
 
         Author ( ano, aname)
 
         Relationships between book and author is many to many.

         Constraints:-primary key,
                                Aname and pubname should NOT NULL.


SQL> select * from book_1;

       BNO BNAME                PUBNAME                             PRICE
---------- -------------------- ------------------------------ ----------
       101 C++                           nirali                                150
       102 M A/C                      vision                                250
       103 RDBMS                    BPB                                   175
       104 data structre              BPB                                   165
       105 software engineering   nirali                                135

SQL> select * from author;

       ANO ANAME
---------- --------------------
      1201  mr.dewasi
      1202  mr.shiravi b
      1203  mr.pankaj
      1204  kanetkar
      1205  babulal

SQL> select * from bookauth;

    BNO        ANO
---------- ----------
       101       1201
       102       1202
       103       1203
       104       1204
       102       1204
       105       1205
       101       1203

7 rows selected.


************************************************************************
a). create or replace a PL/SQL procedure to display details of all books written by ‘kanetkar’.

SQL>
  1  create or replace procedure disa(t in varchar2)
  2  is cursor ca is select b.bno,b.bname,b.pubname,b.price from book_1 b,
  3     author a, bookauth ba where b.bno=ba.bno and a.ano=ba.ano and
  4     a.aname=t;
  5  begin
  6     for x in ca loop
  7        dbms_output.put_line(x.bno||' '||x.bname||' '||x.pubname||' '||x.price);
  8     end loop;
  9* end;
SQL> /

Procedure created.

SQL>
  1  declare
  2  begin
  3     disa('kanetkar');
  4* end;
  5  /
104 data structre  BPB 165
102 M A/C vision 250

PL/SQL procedure successfully completed.


************************************************************************

b). create or replace a trigger that restricts insertion or updation of books having price less than 0.

SQL>
  1  create or replace trigger ta1
  2  before insert or update
  3  on book_1
  4  for each row
  5  declare
  6    p book_1.price%type;
  7  begin
  8    p:=:new.price;
  9    if(p<0) then
 10      raise_application_error(-20089,'book price must be>0');
 11    end if;
 12* end;
SQL> /

Trigger created.

SQL> insert into book_1
  2  values(106,'SE','nirali',-45);
insert into book_1
            *
ERROR at line 1:
ORA-20089: book price must be>0
ORA-06512: at "SCOTT.TA1", line 6
ORA-04088: error during execution of trigger 'SCOTT.TA1'


SQL> update book_1
  2  set price=-45
  3  where bno=101;
update book_1
       *
ERROR at line 1:
ORA-20089: book price must be>0
ORA-06512: at "SCOTT.TA1", line 6
ORA-04088: error during execution of trigger 'SCOTT.TA1'

************************************************************************

RDBMS example of book and dept many to one relationship

Q.) Book( bno, bname, pudname, price)

        Department( dno, dname)

        Relationships between book and department is many to one.
   
        Constraints:-primary key,  Price should bo>0.

SQL> select * from department_1;
    DNO DNAME
---------- ------------------------------
       101 computer
       102 science
       103 arts
       104 MBA
       105 MSC
SQL> select * from books_1;
 BNO     BNAME   PUBNAME    PRICE  DNO
-------   -------------  ---------------   --------- --------
201      C++              nirali               150      101
1202    numerical     BPB                180      102
             method  
1203     RDBMS     vision               140      101


  BNO     BNAME   PUBNAME    PRICE  DNO
-------   -------------  ---------------   --------- --------
1204    data structure       nirali          145       104
1205     social sic         vision            120      105
1206       POM              BPB             130      103

6 rows selected.
************************ ** ** ******************************************

a).create or replace a PL/SQL function to return total expenditure on books of a given department.

SQL>
  1  create or replace function disb(x in varchar2)return number
  2  is
  3    c number(5);
  4  begin
  5    select sum(b.price) into c from department_1 d,books_1 b
  6    where d.dno=b.dno and d.dname=x;
  7    return(c);
  8* end;
SQL> /
Function created.

SQL>
  1  declare
  2     dn department_1.dname%type;
  3     t number(10);
  4  begin
  5     dn:='&dn';
  6     t:=disb(dn);
  7     dbms_output.put_line('total expenditure on books of a'||' '||dn||' '||'is:-'||t);
  8* end;
SQL> /
Enter value for dn: computer
old   5:    dn:='&dn';
new   5:    dn:='computer';
total expenditure on books of a computer is:-290

PL/SQL procedure successfully completed.


************************************************************************
************************************************************************

b). write a cursor to display details of all books brought for a ‘computer’ department.

SQL>
  1  declare
  2    cursor c8 is select b.bno,b.bname,b.pubname,b.price from department_1 d,
  3    books_1 b where d.dno=b.dno and d.dname='computer';
  4  begin
  5    for x in c8
  6    loop
  7      dbms_output.put_line(x.bno||' '||x.bname||' '||x.pubname||' '||x.price);
  8    end loop;
  9* end;
SQL> /
1201 C++ nirali 150
1203 RDBMS vision 140

PL/SQL procedure successfully completed.


************************************************************************

RDBMS examples

Q.3). movie (mvno,mvname,releaseyear)

         Actor (actno,actname)

         Relationships between movie and actor is many to many with descriptive attribute rate of actor for movie.
   
         Constraints:- primary key.


SQL> select * from movie;

 MVNO    MVNAME         RELEASEYEAR
----------  --------------------  -----------
         1     dhoom                       2002
         2     DDLJ                        1999
         3    MBBS                        2000
         4    om kare                     2002
         5    singham                     2011

SQL> select * from actor;

 ACTNO  ACTNAME
----------   ------------------------------
       101    hrithik
       102    amitabh
       103    abhishek
       104    ajay
       105    salaman

SQL> select * from movact;

MVNO  ACTNO    RATE
---------- ---------- ----------
         1        101     500000
         2        102     800000
         3        103     900000
         4        105    1200000
         5        104    1500000
         2        104     700000
         1        101    1200000
7 rows selected.

SQL> set serveroutput on;

a).create or replace a PL/SQL procedure to display details of all movies of actor ‘amitabh’.

SQL>
  1  create or replace procedure showma(t in varchar2)
  2  is cursor c5 is select m.mvno,m.mvname,m.releaseyear
  3     from movie m,actor a,movact ma
  4     where m.mvno=ma.mvno and a.actno=ma.actno and
  5     a.actname=t;
  6  begin
  7     for x in c5 loop
  8       dbms_output.put_line(x.mvno||' '||x.mvname||' '||x.releaseyear);
  9     end loop;
 10* end;
SQL> /

Procedure created.

SQL> declare
  2
  3  begin
  4     showma('amitabh');
  5  end;
  6  /
2 DDLJ 1999

PL/SQL procedure successfully completed.


b).write a cursor to display names of all movies which are released in year 2002.
SQL>
  1  declare
  2    cursor c7 is select mvname from movie
  3    where releaseyear=2002;
  4  begin
  5    for x in c7 loop
  6      dbms_output.put_line(x.mvname);
  7    end loop;
  8* end;
SQL> /
dhoom
om kare

PL/SQL procedure successfully completed.

C program to swap the values of two variables by using call by references.

rite a ‘C’ program to swap the values of two variables by using call by references.

#include<stdio.h>
#include<conio.h>
void main()
{
char name[5][10],temp[10];
int i,j;
clrscr();
printf("\nENTER THE 5 NAMES");
for(i=0;i<5;i++)
{
scanf("%s",&name[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(&name[i],&name[j])>0)
{
strcpy(temp,&name[i]);
strcpy(&name[i],&name[j]);
strcpy(&name[j],temp);
}
}
}
printf("\n SORTED NAMES ARE \n");
for(i=0;i<5;i++)
{
printf("%s\n",name[i]);
}
getch();
}

C program to swap the values of two variables by using call by reference using pointer.

Write a ‘C’ program to swap the values of two variables by using call by reference using pointer.

#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a,b;
clrscr();
printf("\nENTER THE VALUE FOR A & B");
scanf("%d%d",&a,&b);
printf("\n\nVALUES BEFORE SWAPPING \n");
printf("%d\t%d",a,b);
swap(&a,&b);
getch();
}
void swap(int *x,int *y)
{
int t;
{
t=*x;
*x=*y;
*y=t;
}
printf("\n\nVALUES AFTER SWAPPING \n");
printf("%d\t%d",*x,*y);
}

C program to write a macro definition

Write a ‘C’ program to write a macro definition for the following.
> To test whether a character is a lower case letter or not.
> To check whether a character is alphabet or not.
> To obtain the largest of two numbers.

#include<stdio.h>
#include<conio.h>
#define LOWER(ch) islower(ch)
#define ALPHA(ch) isalpha(ch)
#define MAX(a,b) a>b?a:b
void main()
{
int n,a,b;
char c;
clrscr();
do
{
printf("\n1.LOWER");
printf("\n2.ALPHABET");
printf("\n3.MAXIMUM");
printf("\n4.EXIT");
printf("\nENTER UR CHOICE");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nENTER THE CHARACTER");
c=getche();
if(LOWER(c))
{
printf("\nCHARACTER IS IN LOWERCASE\n");
}
else
{
printf("\nCHARACTER IS IN UPPERCASE");
}
break;
case 2:
printf("\nENTER THE CHARACTER");
c=getche();
if(ALPHA(c))
{
printf("\nCHARACTER IS ALPHABET");
}
else
{
printf("CHARACTER IS NOT ALPHABET");
}
break;
case 3:
printf("\nENTER THE VALUES FOR A AND B");
scanf("%d%d",&a,&b);
  if(MAX(a,b))
{
printf("A IS MAXIMUM");
}
else
{
printf("B IS MAXIMUM");
}
break;
case 4:
exit(0);
}
}
while(n!=4);
getch();
}

C program to copy the contents of one file into another file.

Write a ‘C’ program to copy the contents of one file into another file.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp,*fq;
char ch;
clrscr();
fp=fopen("a.c","r");
fq=fopen("b.c","w");
if(fp==NULL)
{
printf("FILE DOES NOT EXIST");
exit(0);
}
if(fq==NULL)
{
printf("FILE DOES NOT EXIST");
exit(0);
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
fputc(ch,fq);
}
fclose(fp);
fclose(fq);
getch();
}

C program to accept a string from user and generate given pattern.

 Write a ‘C’ program to accept a string from user and generate following pattern.
         (e.g. input is string “abcd”)

          a
          ab
          abc
          abcd
          abc
          ab
          a        
-------------------------------------------------------------------------------------------

Solution:-

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,cnt=0;
char name[10];
clrscr();
printf("enter the string");
scanf("%s",&name);
cnt= strlen(name);
        {
  cnt++;
}
printf("\n");
for(i=0;i<=cnt-1;i++)
{
for(j=0;j<=i;j++)
{
printf("%c",name[j]);
}
printf("\n");
}
for(k=i-1;k>=0;k--)
{
for(j=0;j<k;j++)
{
printf("%c",name[j]);
}
printf("\n");
}
getch();
}

Featured posts

Happy Independence Day August 15th

 Here's a message for India's Independence Day (August 15th): "शुभ स्वतंत्रता दिवस! आजादी की 79वीं वर्षगांठ पर, आइए हम अपने देश...

Popular posts