You are on page 1of 39

Advance C Lab

Term Work -1
Implement a Shopping Cart System for An E-Store In C To Demonstrate The Various
Features Of User Defined Functions With The Following Functionalities.
1)Add Item
2)Delete Item
3)Display Item
4)Billing Information

#include <stdio.h>

#include <stdlib.h>

void main()

int scdetails[3][4]={

{0,0,0,0},

{0,1,10,2000},

{0,2,5,1000}

};

int origstock_count[3]={0,10,5};

int total=0,choice,pid,pqty;

for(;;)

printf("\nEnter\n 1.Add\n 2.Delete\n 3.Display\n 4.Billing\n 5.Exit\n\n");

printf("Enter Your Choice ");

scanf("%d",&choice);

switch(choice)

case 1:printf("Enter Product ID And Quantity ");

scanf("%d%d",&pid,&pqty);

total=addItem(scdetails,pid,pqty,total);

break;
Advance C Lab
case 2:printf("Enter Product ID And Quantity ");

scanf("%d%d",&pid,&pqty);

total=deleteItem(scdetails,pid,pqty,total);

break;

case 3:displayItem(scdetails,origstock_count);

break;

case 4:billing(scdetails,origstock_count);

break;

case 5:exit(1);

int addItem(int scdetails[3][4],int pid,int pqty,int total)

total=total+pqty*scdetails[pid][3];

scdetails[pid][2]-=pqty;

return total;

int deleteItem(int scdetails[3][4],int pid,int pqty,int total)

total=total-pqty*scdetails[pid][3];

scdetails[pid][2]+=pqty;

return total;

void billing(int scdetails[3][4],int origstock_count[3])

int i=1,item_cost;

do

item_cost=(origstock_count[i]-scdetails[i][2])*scdetails[i][3];
Advance C Lab
if(item_cost>0)

printf("%d\t%d\t%d\t",scdetails[i][1],(origstock_count[i]-scdetails[i][2]),item_cost);

i++;

}while(i<=2);

void displayItem(int scdetails[3][4],int origstock_count[3])

int i=1,item_cost;

do

item_cost=(origstock_count[i]-scdetails[1][2])*scdetails[1][3];

if(item_cost>0)

printf("%d\t%d",scdetails[i][1],(origstock_count[i]-scdetails[1][2]));

i++;

}while(i<2);

}
Advance C Lab

Output

Enter

1.Add

2.Delete

3.Display

4.Billing

5.Exit

Enter Your Choice 1

Enter Product ID And Quantity 1 2

Enter

1.Add

2.Delete

3.Display

4.Billing

5.Exit

Enter Your Choice 1

Enter Product ID And Quantity 2 3

Enter

1.Add

2.Delete

3.Display

4.Billing

5.Exit

Enter Your Choice 4

1 2 4000 2 3 3000

Enter

1.Add
Advance C Lab
2.Delete

3.Display

4.Billing

5.Exit

Enter Your Choice


Advance C Lab

Term Work 2
Implement A Simple Calculator Application In C. Includes The Modules For Following
1)Read The Two Numbers And The Operations (+,-*/)
2)Modules for Addition, Subtraction, Multiplication And Division
3)Display The Results
Use Pointers To Function For Add, Subtract, Multiply and Divide Operations.

#include <stdio.h>

void Add(int a,int b)

printf("Sum Is:");

printf("%d",a+b);

void sub(int a,int b)

printf("Diff is:");

printf("%d",a-b);

void Multiply(int a,int b)

printf("Product Is:");

printf("%d",a*b);

void Divide(int a,int b)

printf("Result is:");

printf("%d",a/b);

void main()

{
Advance C Lab
void(*calc[])(int,int)={Add,sub,Multiply,Divide};

int choice ,a,b;

printf("Enter\n 1.Add\n 2.Subtract\n 3.Multiply\n 4.Divide\n........Any Other Number To Quit\n");

scanf("%d",&choice);

printf("Enter A and B:\n");

scanf("%d%d",&a,&b);

while(choice<5&&choice>0)

(*calc[choice-1])(a,b);

printf("\n Enter Choice :");

scanf("%d",&choice);

if(choice<5&&choice>0)

printf("Enter\n 1.Add\n 2.Subtract\n 3.Multiply\n 4.Divide\n......Any Other Number To Quit\n");

scanf("%d%d",&a,&b);

else

break;

}
Advance C Lab
Enter

1.Add

2.Subtract

3.Multiply

4.Divide

........Any Other Number To Quit

Enter A and B:

10 20

Sum Is:30

Enter Choice :2

Enter

1.Add

2.Subtract

3.Multiply

4.Divide

......Any Other Number To Quit

20 10

Diff is:10

Enter Choice :3

Enter

1.Add

2.Subtract

3.Multiply

4.Divide

......Any Other Number To Quit

55

Product Is:25

Enter Choice :4

Enter

1.Add

2.Subtract

3.Multiply
Advance C Lab
4.Divide

......Any Other Number To Quit

50 2

Result is:25

Enter Choice :
Advance C Lab

Term Work 3
Implement a Simple Banking Application in C by Making Use Of Array Of Structures With
Following Functionalities.
1)Create A New Account
2)Deposit Amount
3)Withdraw Amount
4)Balance Enquiry

#include <stdio.h>

struct account

char name[20];

int accid;

int balance;

};

int create(struct account arracc[20],char name[10],int indexacc)

strcpy(arracc[indexacc].name,name);

arracc[indexacc].accid=10000+indexacc;

arracc[indexacc].balance=0;

indexacc++;

return indexacc;

void deposit(struct account arracc[20],int accno,int amtdep)

int custaccindex=accno-10000;

arracc[custaccindex].balance+=amtdep;

void withdraw(struct account arracc[20],int accno,int amtwithdraw)

int custaccindex=accno-10000;

arracc[custaccindex].balance-=amtwithdraw;
Advance C Lab
}

void balenq(struct account arracc[20],int accno)

int custaccindex=accno-10000;

printf("Balance Is %d\n",arracc[custaccindex].balance);

void main()

struct account arracc[20];

char name[10];

int choice,custaccno,amtdep,amtwithdraw,countacc=1;

for(;;)

printf("\n Enter 1.Create 2.Deposit 3.Withdraw 4.Balance Enquiry 5.Exit\n");

printf("Enter Choice");

scanf("%d",&choice);

switch(choice)

case 1:printf("Enter Name");

scanf("%s",name);

countacc=create(arracc,name,&countacc);

break;

case 2:printf("Enter Account Number and Deposit amount");

scanf("%d%d",&custaccno,&amtdep);

deposit(arracc,custaccno,amtdep);

break;

case 3:printf("Enter Account Number and withdrawal amount");

scanf("%d%d",&custaccno,&amtwithdraw);

withdraw(arracc,custaccno,amtwithdraw);

break;

case 4:printf("Enter Account Number to Check");

scanf("%d",&custaccno);

balenq(arracc,custaccno);
Advance C Lab
break;

case 5:exit(0);

}
Advance C Lab

Term Work 4
Implement a Simple Inventory Using Data Files In C, Includes the Modules
1)Add/Delete a New Post
2)Manufacture/Sale Transaction
3)Display A Particular Part
4)Display a Stock Of All Parts
Use Separate files for each of the above operations and execute them with make utility.

Main Function
#include <stdio.h>

#include <stdlib.h>

#include "inv.h"

int main(int argc, char *argv[])

char con;

int opt=0;

do

printf(" Menu\n");

printf("1...Add an item\n");

printf("2...Display list of all the parts\n");

printf("3...Query a particular part\n");

printf("4...Make Transaction\n");

printf("5...Delete an item\n");

printf("6...Exit\n");

printf("Enter your option:");

scanf("%d",&opt);

switch (opt)

case 1:
Advance C Lab
read_part();

break;

case 2:

print_part();

break;

case 3:

query_part();

break;

case 4:

transaction();

break;

case 5:

delete_part();

break;

case 6:

exit(0);

default:

printf("Wrong option! Try again!!\n");

}while(opt != 6);

return 0;

}
Advance C Lab

Cr Function
#include "inv.h"

void read_part() {

FILE *outfile;

inventory input;

char c='y';

outfile = fopen ("Part.dat","w");

if (outfile == NULL)

fprintf(stderr, "\nError opening Part.dat\n");

exit (1);

while (c=='y' || c=='Y')

printf("\nPart Number: ");

scanf ("%d", &input.part_no);

printf("Part Name : ");

scanf ("%s", input.name);

printf("Stock : ");

scanf ("%d", &input.stock);

printf("Price : ");

scanf ("%f", &input.price);

// write entire structure to Accounts file

fwrite (&input, sizeof(input), 1, outfile);

printf("Press y or Y to continue:");

fflush(stdin);

scanf("%c",&c);

fclose(outfile);

}
Advance C Lab

Pr Function
#include "inv.h"

void print_part()

FILE *infile;

inventory npart;

infile=fopen("Part.dat","r");

while(fread(&npart,sizeof(npart),1,infile))

printf("Part No=%d, Name=%s, Stock=%d, Price=%5.2f\n",

npart.part_no,npart.name,npart.stock,npart.price);

fclose(infile);

}
Advance C Lab

T function

#include "inv.h"

int main(int argc, char *argv[])

char con;

int opt=0;

while ((opt < 1) || (opt > 5)) {

printf(" Menu\n");

printf("1...Add an item\n");

printf("2...Display list of all the parts\n");

printf("3...Query a particular part\n");

printf("4...Make Transaction\n");

printf("5...Exit\n");

printf("Enter your option:");

scanf("%d",&opt);

switch (opt) {

case 1:

read_part();

opt=0;

break;

case 2:

print_part();

opt=0;

break;

case 3:

query_part();

opt=0;

break;

case 4:

transaction();

opt=0;
Advance C Lab
break;

case 5:

exit(0);

default:

printf("Wrong option\n");

printf("Press any to key to continue:");

scanf(" %c",&con);

return 0;

}
Advance C Lab

Delete Function
#include "inv.h"

void delete_part()

FILE *infile;

FILE *tmpFile;

int n, found=0;

inventory npart;

infile=fopen("Part.dat","r");

tmpFile=fopen("tmp.dat","w");

printf("Enter the part number to delete:");

scanf("%d",&n);

while(fread(&npart,sizeof(npart),1,infile)!=NULL)

if(npart.part_no == n)

printf("The part you entered exists.");

found = 1;

else

fwrite(&npart, sizeof(npart), 1, tmpFile);

if(!found)

printf("Part Not found\n");

else

printf("Part No %d deleted\n",n);

fclose(infile);

fclose(tmpFile);

remove("Part.dat");
Advance C Lab
rename("tmp.dat", "Part.dat");

}
Advance C Lab

Tr Function
#include "inv.h"

void transaction()

FILE *infile1;

FILE *infile2;

long inv_size,trans_size;

char c;

inventory npart;

transact t;

inv_size=sizeof(npart);

trans_size=sizeof(t);

infile1=fopen("Part.dat","r+");

infile2=fopen("Trans.dat","a");

printf("Enter the part number of transaction:");

scanf("%d",&t.part_no);

printf("Enter s for sale or m for manufacture:");

fflush(stdin);

scanf("%c",&t.type);

c=t.type;

printf("Enter the quantity of transaction:");

scanf("%d",&t.quantity);

while(fread(&npart,inv_size,1,infile1) && npart.part_no !=t.part_no);

if (feof(infile1))

printf("Part Not found");

else

if (c=='m' || c=='M')

npart.stock = npart.stock + t.quantity;


Advance C Lab
else if (c=='s' || c=='S')

npart.stock = npart.stock - t.quantity;

fseek(infile1, -inv_size, SEEK_CUR);

fwrite (&npart, sizeof(npart), 1, infile1);

fwrite (&t, trans_size, 1, infile2);

fclose(infile1);

fclose(infile2);

}
Advance C Lab

Query Function
#include "inv.h"

void query_part()

FILE *infile;

int n;

inventory npart;

infile=fopen("Part.dat","r");

printf("Enter the part number to display:");

scanf("%d",&n);

while(!feof(infile))

fread(&npart,sizeof(npart),1,infile);

if(npart.part_no == n)

break;

//while (fread(&npart,sizeof(npart),1,infile) && npart.part_no !=n);

if (feof(infile))

printf("Part Not found\n");

else

printf("Part No=%d, Name=%s, Stock=%d, Price=%5.2f\n",

npart.part_no,npart.name,npart.stock,npart.price);

fclose(infile);

}
Advance C Lab

Inv Function
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct {

int part_no;

char name[30];

int stock;

float price;

} inventory;

typedef struct {

int part_no;

char date[10];

char type;

int quantity;

} transact;

extern void read_part();

extern void print_part();

extern void query_part();

extern void transaction();

extern void delete_part();


Advance C Lab

Term Work 5
Write and execute a C++ program to read n students details-Name, USN, And Marks in 3
Subjects. Calculate and display the total, percentage and grade obtained for each student.
Refer The Following Table For Grading
>=80 Grade is A
>=70 and <80 Grade B
>=60 and <70 Grade C
#include <iostream>

#include<cstring>

using namespace std;

class Student

public:char name[30];

char usn[10];

int marks[3];

Student(char name[30],char usn[10],int marks[3])

int i;

strcpy(this->name,name);

strcpy(this->usn,usn);

for(i=0;i<=2;i++)

this->marks[i]=marks[i];

int computeTotal()

int i,total=0;

for(i=0;i<=2;i++)

total+=this->marks[i];

}
Advance C Lab
return total;

};

int main()

char name[20],usn[10];

int i,j,n,total=0,marks[3];

double avg=0.0;

cout<<"Enter No Of Students :";

cin>>n;

for(i=1;i<=n;i++)

total=0;

cout<<"Enter Name :";

cin>>name;

cout<<"Enter USN :";

cin>>usn;

cout<<"Enter Marks :";

for(j=0;j<=2;j++)

cin>>marks[j];

Student st=Student(name,usn,marks);

total+=st.computeTotal();

avg=total/3;

cout<<"\n Avg Is :"<<avg;

cout<<"\n";

if(avg>=80)

cout<<"\n Grade A\n";

else

if(avg>=70&&avg<80)

cout<<"\n Grade B\n";

else
Advance C Lab
cout<<"Grade C\n";

}
Advance C Lab

Output
Enter No Of Students :3

Enter Name :Asrar

Enter USN :406

Enter Marks :87 87 89

Avg Is :87

Grade A

Enter Name :Smith

Enter USN :588

Enter Marks :67 55 45

Avg Is :55

Grade C

Enter Name :Akhil

Enter USN :578

Enter Marks :43 45 41

Avg Is :43

Grade C

Process returned 0 (0x0) execution time : 46.963 s

Press any key to continue.


Advance C Lab

Term Work 6
Write and Execute C++ Program with Function Overloading To Calculate The Area Of Circle,
Rectangle and a Triangle.
#include <iostream>

using namespace std;

int area(int);

int area(int,int);

float area(float,float);

main()

int r,l,b;

float bs,ht;

cout<<"Enter The Sides Of The Circle :";

cin>>r;

cout<<"Enter The Sides Of Rectangle :";

cin>>l>>b;

cout<<"Enter The Bs and Height Of a Triangle :";

cin>>bs>>ht;

cout<<"Area Of Circle :"<<area(r);

cout<<"\n";

cout<<"Area Of Rectangle :"<<area(l,b);

cout<<"\n";

cout<<"Area Of Triangle :"<<area(bs,ht);

cout<<"\n";

int area(int r)

return (3.14*r*r);

int area(int l,int b)

return(l*b);
Advance C Lab
}

float area(float bs,float ht)

return(0.5*bs*ht);

class area

int r,l,b;

float bs,ht;

public:

void read();

void display();

};
Advance C Lab

Output
Enter The Sides Of The Circle :3

Enter The Sides Of Rectangle :3 4

Enter The Bs and Height Of a Triangle :2.1 3.4

Area Of Circle :28

Area Of Rectangle :12

Area Of Triangle :3.57

Process returned 0 (0x0) execution time : 18.720 s

Press any key to continue.


Advance C Lab

Term Work 7
Write And Execute a C++ Program To Implement the COMPLEX number class and perform the
following operations:
1.Read a COMPLEX number
2.Display a COMPLEX number
3.Add 2 COMPELX numbers(use objects as function arguments)
4.Add an Integer number to one of the COMPLEX number
#include <iostream>

using namespace std;

class COMPLEX

private:int realp;

int imgp;

public:void readValues(void)

cout<<"Enter The Real Part & Imaginary Part"<<endl;

cin>>realp>>imgp;

friend COMPLEX ADD(COMPLEX,COMPLEX);

friend COMPLEX ADD(int,COMPLEX);

friend ostream& operator<<(ostream&, COMPLEX&);

};

COMPLEX ADD(COMPLEX S1,COMPLEX S2)

COMPLEX C;

C.realp=S1.realp+S2.realp;

C.imgp=S1.imgp+S2.imgp;

return C;

}
Advance C Lab
COMPLEX ADD(int ival,COMPLEX S2)

COMPLEX C;

C.realp=ival+S2.realp;

C.imgp=S2.imgp;

return C;

ostream& operator<<(ostream& out,COMPLEX& comp)

cout<<comp.realp<<"+i"<<comp.imgp

<<endl;

return out;

int main()

COMPLEX C1,C2,C3,C4;

int choice,ival;

cout<<"1.To Add Two Complex Objects"<<endl;

cout<<"2.To Add Real Part To A Complex Object"<<endl;

cout<<"Please Enter Your Choice"<<endl;

cin>>choice;

switch(choice)

case 1:C1.readValues();

C2.readValues();

cout<<"\nC1="<<C1;

cout<<"C2="<<C2;

cout<<"------------"<<endl;

C3=ADD(C1,C2);

cout<<"C3="<<C3;

break;

case 2:cout<<"Enter An Integer:"<<endl;

cin>>ival;
Advance C Lab
COMPLEX C4;

C1.readValues();

cout<<"\n ival="<<ival<<endl;

cout<<"C1="<<C1;

cout<<"-------------------"<<endl;

C4=ADD(ival,C1);

cout<<"C4="<<C4;

break;

default: cout<<"Error In Input"<<endl;

break;

}
Advance C Lab

Output
1.To Add Two Complex Objects

2.To Add Real Part To A Complex Object

Please Enter Your Choice

Enter The Real Part & Imaginary Part

23

Enter The Real Part & Imaginary Part

34

C1=2+i3

C2=3+i4

------------

C3=5+i7

Process returned 0 (0x0) execution time : 18.695 s

Press any key to continue.

2nd Option

1.To Add Two Complex Objects

2.To Add Real Part To A Complex Object

Please Enter Your Choice

Enter An Integer:

Enter The Real Part & Imaginary Part

13

ival=3

C1=1+i3

-------------------

C4=4+i3

Process returned 0 (0x0) execution time : 7.287 s

Press any key to continue.


Advance C Lab

Term Work 8
1) Write and execute a C++ program to implement the following class hierarchy
MANAGER

PRODUCTION SALES
MANAGER MANAGER

Perform the following operations with the help of runtime polymorphism:


1. Read the basic salary and calculate the net salary for both PRODUCTION MANAGER
and SALES MANAGER using the following details:
PRODUCTION MANAGER – HRA =10%, DA=75%, Allowance=60%
SALES MANAGER – HRA =10%, DA=75%, Allowance=20%, Travelling Allowance=80%
2. Display the gross salary of both managers.
Display the number of objects created for each class in the hierarchy using static data
member
#include <iostream>

#include <cstring>

using namespace std;

class Manager

public:

char name[10];

int age,basicSal,hra,da,all,tAll;

static int objectCount;

Manager(){}

Manager(char name[10],int age)

{
Advance C Lab
strcpy(this->name,name);

this->age = age;

objectCount++;

virtual void computeSal(int basicSal)

cout<<"\nCompute Sal in Manager";

cout<<"Gross is:"<<this->basicSal*(this->hra+this->da+this->all)/100;

static int getCount() {

return objectCount;

};

class ProdManager : public Manager

public:

static const int hra=10,da=75,all=60,tax=20;

ProdManager(char name[10],int age): Manager(name,age)

objectCount++;

void computeSal(int basicSal)


Advance C Lab
{

float grossSal;

cout<<"\nCompute Sal in ProdManager";

grossSal=(float)( basicSal+ basicSal*(this->hra+this->da+this->all)/100);

cout<<"\nGross is:"<<grossSal<<"\n";

cout<<"Net is:"<<grossSal -((grossSal*this->tax)/100) <<"\n";

};

class SalesManager : public Manager

public:

static const int hra=10,da=75,all=60,tAll = 80,tax=20;

SalesManager(char name[10],int age):Manager(name,age)

objectCount++;

void computeSal(int basicSal)

float grossSal;

cout<<"\nCompute Sal in SalesManager";

grossSal=(float)( basicSal+ basicSal*(this->hra+this->da+this->all+this->tAll)/100);

cout<<"\nGross is:"<<grossSal<<"\n";

cout<<"Net is:"<<grossSal - (grossSal*(this->tax)/100)<<"\n";

};

int Manager::objectCount = 0;

int main()

int basicSal;

Manager *mgr;
Advance C Lab

cout<<"Enter basic salary:";

cin>>basicSal;

mgr = new ProdManager("PVT",25);

mgr->computeSal(basicSal);

mgr = new SalesManager("RPT",30);

mgr->computeSal(basicSal);

cout << "\nTotal objects: " << Manager::getCount() << endl;

cout<<"\n";

Output
Enter basic salary:10000

Compute Sal in ProdManager

Gross is:24500

Net is:19600

Compute Sal in SalesManager

Gross is:32500

Net is:26000

Total objects: 4

Process returned 0 (0x0) execution time : 24.526 s

Press any key to continue.

You might also like