Sie sind auf Seite 1von 21

TERMWORK 1

#include <stdio.h>
#include <stdlib.h>
#define max_cart 10

void disp_stock(void);
void disp_menu(void);
void Add(void);
void Delete(void);
void disp_cart(void);
void bill(void);

int stock[][3] = {{0, 10, 400},{1, 20, 600}, {2, 30, 250}, {3, 50, 200}, {4, 20, 750}, {5, 25, 100}};
int cart[max_cart][3]={{0, 0, 0}, {1, 0, 0},{2, 0, 0}, {3, 0, 0}, {4, 0, 0}, {5, 0, 0}};
char name[][20] = {{"Shirt"}, {"Trouser"}, {"belt"}, {"Shoe"}, {"goggles"}, {"wallet"}};
int i, j, code;

int main()
{
for(;;)
{
printf("\n\n");
disp_stock();
disp_menu();

int choice;
printf("\nEnter your choice:");
scanf("%d", &choice);

switch(choice)
{
case 1: Add();
system("pause");
break;
case 2: Delete();
system("pause");
break;
case 3: disp_cart();
system("pause");
break;
case 4: bill();
system("pause");
break;
case 5: printf("Thank you for shopping!\n");
exit(0);

default: printf("Invalid Choice!\n");


}
}

return 0;
}

void disp_stock(void)
{
printf("-------------------STOCK--------------------\n");
printf("NAME\tCODE\tQUANTITY\tRATE\n");
for(i=0;i<6; i++)
{
printf("%s\t %d\t %d\t %d\n", name[i], stock[i][0], stock[i][1], stock[i][2]);

}
}

void disp_menu(void)
{
printf("\n\tSHOPPING CART MENU\n");
printf("1. Add an item\n2. Delete an item\n3. Display Cart\n4. Cart Bill\n5. Exit\n");
printf("--------------------------------------------\n");
}

void Add(void)
{
int qty;
printf("Enter the code: ");
scanf("%d", &code);
if(code<0 || code> 5)
{
printf("INVALID CODE!\n");
Add();
}
printf("Enter the quantity:");
scanf("%d", &qty);
cart[code][1] += qty;
cart[code][2] = cart[code][1]*stock[code][2];
stock[code][1] -= qty;
}

void Delete(void)
{
int qty;
printf("Enter the code of the item to be deleted: ");
scanf("%d", &code);
if(code<0 || code> 5)
{
printf("INVALID CODE!\n");
Delete();
}
if(cart[code][1]==0)
printf("This item is not available in the cart!\n");
else
{
printf("Enter the quantity to be reduced:");
scanf("%d", &qty);
if(cart[code][1]<qty)
printf("Warning: excess quantity specified!");
else
{
cart[code][1] -= qty;
cart[code][2] = cart[code][1]*stock[code][2];
stock[code][1] += qty;
}
}
}

void disp_cart(void)
{
int flag=0;
for (i=0;i<6; i++)
{
if(cart[i][1]!=0)
flag++;
}
if(flag==0)
printf("Shopping Cart is empty!\n");
else
{
printf("\n----------------------------------------\n");
printf("ITEM CODE\tQUANTITY\tRATE\n");
for (i=0;i<6; i++)
{
if(cart[i][1]!=0)
printf(" %d\t %d\t %d\n", cart[i][0], cart[i][1], cart[i][2]);
}
printf("----------------------------------------\n");
}
}

void bill(void)
{
int amt=0;
for(i=0;i<6;i++)
amt += cart[i][2];
printf("Your Bill is Rs.%d.00/-\n", amt);
}

Term Work 2

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
float add (int, int);
float subtract (int, int);
float multiply (int, int);
float divide (int, int);
void display (float);
int main()
{
int a, b;
unsigned char c;
float sum;
float (*p)(int, int);
void (*q)(float);
printf("enter the two no.: ");
scanf("%d %d",&a, &b);
getchar();
printf("enter the operator(+,-,*,/) : ");
scanf(" %c",&c);

switch(c)
{
case '+':p=add;
break;
case '-':p=subtract;
break;
case '*':p=multiply;
break;
case '/':p=divide;
break;
}
sum = (*p)(a,b);
q=display;
(*q)(sum);

}
float add(int x, int y)
{
return(x+y);
}
float subtract(int x, int y)
{
return(x-y);
}
float multiply(int x, int y)
{
return(x*y);
}
float divide(int x, int y)
{
if(y==0)
printf("error\n");
else
return(x/y);
}
void display(float d)
{
printf("Answer=%f\n",d);
}
Term Work 3

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct
{
int acc_no;
char name[20];
float balance;
} ACCOUNT;
ACCOUNT acc[100];
int curacc=0;

void newaccount();
void deposit();
void withdraw();
void balance_enq();

int main()
{
int opt=0;
do
{
printf(" Menu\n");
printf(" 1.New account\n 2.Deposit\n 3.Withdraw\n 4.Balance\n 5.Exit\n");
printf("Enter your option:");
scanf("%d",&opt);

switch(opt)
{
case 1: newaccount();
break;
case 2: deposit();
break;
case 3: withdraw();
break;
case 4: balance_enq();
break;
case 5: exit(0);

default:printf("Invalid option");
}
}while(opt!=5);

return 0;
}

void newaccount()
{
int i,n;
bool found=false; /*found is the name of the bool*/
float amt=0;
printf("Enter Account Number:");
scanf("%d",&n);

for(i=0; i < curacc;i++) {


if(n == acc[i].acc_no)
{
found=true;
break;
}
}
if( !found || !curacc)
{
printf("Enter Account Name:");
scanf("%s",acc[curacc].name);
while (amt < 500)
{
printf("Enter the amount:");
scanf("%f",&amt);
if(amt < 500)
printf("Amount Insufficient\n");
}

acc[curacc].acc_no=n;
acc[curacc].balance=amt;
curacc++;

}
else
printf("Account already exists\n");

void deposit()
{
int i,n;
bool found=false;
float amt;
printf("Enter your account no:");
scanf("%d",&n);
for(i=0;i<curacc;i++) {
if(n == acc[i].acc_no)
{
found=true;
break;
}
}
if(found)
{
printf("Enter the amount");
scanf("%f",&amt);
acc[i].balance+=amt;
}

else
printf("Account do not exists");
}

void withdraw()
{

int i,n;
bool found=false;
float amt;
printf("Enter your account no:");
scanf("%d",&n);
for(i=0;i<curacc;i++) {
if(n == acc[i].acc_no)
{
found=true;
break;
}
}
if(found)
{
do
{
printf("Enter the amount");
scanf("%f",&amt);
if((acc[i].balance - amt) < 500)
printf("Amount indufficient");
}while((acc[i].balance - amt) < 500);
acc[i].balance-=amt;
}
else
printf("Account do not exists\n");
}

void balance_enq()
{
int i,n;
bool found=false;

printf("Enter your account no:");


scanf("%d",&n);
for(i=0;i<curacc;i++)
{
if(n==acc[i].acc_no)
{
found=true;
break;
}
}
if(found)
{
printf("Account Name:%s\n",acc[i].name);
printf("Balance amount=%.2f",acc[i].balance);

}
else
printf("Account does not exists\n");
}
Termwork 4

//header inv.h

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct
{
char name[30];
int part_no;
int stock;
float price;
}inventory;

typedef struct
{
int part_no;
char type;
int quantity;
}transact;
extern void read_part();
extern void delete_part();
extern void print_part();
extern void query_part();
extern void transaction();

//main.c

#include "inv.h"

int main(int agrc,char *agrv[])


{
int opt=0;
do
{
printf(" Menu\n");
printf("1...Add an item\n");
printf("2...Delete an item\n");
printf("3...Display list of all the parts\n");
printf("4...Query a particular part\n");
printf("5...Make transactions\n");
printf("6...Exit\n");
printf("please Enter your option :");
scanf("%d",&opt);
switch(opt)
{
case 1 :read_part();break;
case 2 :delete_part();break;
case 3 :print_part();break;
case 4 :query_part();break;
case 5 :transaction();break;
case 6 :exit(0);
default :
{
printf("invalid input\n");
printf("press any key to continue\n");
fflush(stdin);
getc(stdin);
}
}
}while(opt!=6);
return 0;
}

// cr.c

#include"inv.h"
void read_part()
{
FILE *outfile;
inventory input;
char c='y';
//open accounts file for writing
outfile=fopen("part.dat","a+");
if(outfile==NULL){
fprintf(stderr,"\nError opening accounts.dat\n");
exit(1);
}

while(c=='y' || c=='Y')
{
printf("\nPart Number : ");
scanf("%d",&input.part_no);
printf("\nPart Name : ");
scanf("%s",input.name);
printf("\nStock : ");
scanf("%d",&input.stock);
printf("\nPrice : ");
scanf("%f",&input.price);
fwrite(&input,sizeof(input),1,outfile);
printf("Do you want to continue(y/n) : ");
getchar();
scanf("%c",&c);

}
fclose(outfile);

//dr.c

#include"inv.h"

void delete_part()
{
FILE *infile,*tmpfile;
int n,found=0;
inventory npart;
infile=fopen("part.dat","r");
tmpfile=fopen("parttemp.dat","w");
printf("Enter the part no to delete : ");
scanf("%d",&n);
while(fread(&npart,sizeof(npart),1,infile))
{
if(n!=npart.part_no)
fwrite(&npart,sizeof(npart),1,tmpfile);
else
found=1;
}
fclose(infile);
fclose(tmpfile);
if(found=1)
{
remove("part.dat");
rename("parttemp.dat","part.dat");
printf("element successfully deleted\n");
}
else
{
printf("element not found\n");
remove("parttemp.dat");
}

//pr.c

#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=
%.2f\n",npart.part_no,npart.name,npart.stock,npart.price);
}
fclose(infile);
}

//qr.c

#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(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=
%.2f\n",npart.part_no,npart.name,npart.stock,npart.price);
fclose(infile);
}

//tra.c

#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;
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);
}

TERMWORK 5

#include <iostream>
#include <string>

using namespace std;

class student
{ char name[30],usn[10],grade;
int m1,m2,m3;
float prec;
public :
void input()
{
cout<<"please enter the student details "<<endl;
cout<<"Name : ";
cin>>name;
cout<<"USN : ";
cin>>usn;
cout<<"Marks in 3 subjects : ";
cin>>m1;cin>>m2;cin>>m3;
}
void compute()
{
int tot=m1+m2+m3;
prec=(float)tot/300.0*100;
if (prec >= 80.0) grade='A';
if(prec >= 70.0 && prec < 80.0) grade='B';
if(prec >= 60.0 && prec < 70.0) grade='C';
if(prec >= 50.0 && prec < 60.0) grade='D';
if(prec < 50.0) grade='F';
}
void disp()
{
cout<<"USN : "<<usn<<endl;
cout<<"NAME : "<<name<<endl;
cout<<"PERCENTAGE : "<<prec<<endl;
cout<<"GRADE : "<<grade<<endl<<endl;
}
};
int main()
{

int n;
cout<<"Enter the number of students : ";
cin>>n;
student st[n];
for(int i=0;i<n;i++){
st[i].input();
st[i].compute();
}
for(int i=0;i<n;i++){
st[i].disp();
}
return 0;
}

TERMWORK 6

#include <iostream>

using namespace std;


class shape
{
int radius ,width, height;
public:
double area(int r)
{
return 3.143*r*r;
}
double area(int w, int h) //function overloading
{
return w*h;
}
double area(int w, double h)
{
return w*h*0.5;
}
};

int main()
{
shape c;
cout<<"Area of circle : "<<c.area(5)<<endl;
cout<<"Area of rectangle : "<<c.area(5,10)<<endl;
cout<<"Area of triangle : "<<c.area(5,1.0)<<endl;
return 0;
}

TERMWORK 7

#include <iostream>

using namespace std;

class Complex
{
double real,imag;
public:
Complex()
{
real=0; imag=0;
}
Complex(double r, double i)
{
real=r;imag=i;
}
void read()
{
cout<<"Enter the real part : ";
cin>>real;
cout<<"Enter the imaginary part : ";
cin>>imag;
}
void display()
{
cout<<real<<"+i"<<imag<<endl;
}
double getreal(){ return real;}
double getimag(){ return imag;}
};
Complex Add(Complex ob1,Complex ob2)
{
Complex r(ob1.getreal()+ob2.getreal(),ob1.getimag()+ob2.getimag());
return r;
}

int main()
{ Complex a,b,c;
cout<<"input the first complex number : "<<endl;
a.read();
cout<<endl;
cout<<"input the second complex number : "<<endl;
b.read();
cout<<endl;
c=Add(a,b);
cout<<"the addition is : ";
c.display();
return 0;
}

TERMWORK 8

#include <iostream>

using namespace std;

class manager
{
static int ob;
char name[20];
float basic, hra, da;
public :
void read()
{
cout<<"Enter the name : ";
cin>>name;
cout<<"Enter the basic salary : ";
cin>>basic;
cout<<"Enter the Hra : ";
cin>>hra;
cout<<"Enter the Da : ";
cin>>da;
ob++;
}
float getbasic()
{
return basic;
}
float gethra()
{
return hra;
}
float getda()
{
return da;
}
int getob()
{
return ob;
}
void computegross()
{
float gross;
gross=basic+hra*basic/100+da*basic/100;
cout<<"Gross Salary = "<<gross<<endl;
}
};
int manager::ob;
class prodmanager: public manager
{
float allow;
public:
void read()
{
manager::read();
cout<<"Enter the Allowance : ";
cin>>allow;
}
float getallow()
{
return allow;
}
void computegross()
{
float gross;
gross=getbasic()+gethra()*getbasic()/100+getda()*getbasic()/100+getbasic()*allow/100;
cout<<"Gross salary of production manager = "<<gross<<endl;

};
class salesmanager : public prodmanager
{
float ta;
public:
void read()
{
prodmanager::read();
cout<<"Enter traveling Allowance : ";
cin>>ta;
}
float getta()
{
return ta;
}
void computegross()
{
float gross;

gross=getbasic()+gethra()*getbasic()/100+getda()*getbasic()/100+getbasic()*getallow()/100+get
basic()*ta/100;
cout<<"Gross salary of Sales manager = "<<gross<<endl;
}
};

int main()
{
prodmanager obj1;
salesmanager obj2;
cout<<"Enter the details of Production manager : "<<endl;
obj1.read();
obj1.computegross();
cout<<"Enter the details of sales manager : "<<endl;
obj2.read();
obj2.computegross();
cout<<"Number of objects : "<<obj2.getob()<<endl;
return 0;
}

Das könnte Ihnen auch gefallen