Anda di halaman 1dari 17

Name: Padma.H.

Channal IVsem Adiv USN no:2BV10cs056 1> Write a simple C++ program to illustrate the ATM transactions that accepts user id handling all exceptional cases.The format of user id should be <USN>ATM. ex: 2bv09cs10ATM. The program should not accept the format other than above. a) Withdrawl b) Deposit c) Balance Enquiry. Soln: #include<iostream> #include<conio.h> #include<ctype.h> #include<string.h> using namespace std; void getUserId(char UID[]) { char a[]="1001100111000"; int i=0; char ch; cout<<"<usn>ATM"<<endl; while(i!=13) { ch=getche(); if(a[i]=='1' && isdigit(ch)) UID[i++]=ch;

else if(a[i]=='0' && isalpha(ch)) UID[i++]=ch; else { cout<<endl<<"Invalid character. Please enter it again"<<endl; for(int j=0;j<i;j++) cout<<UID[j]; } } UID[i]='\0'; }

class Account { private: char Name[20],UID[14]; float balance; public: void createAccount() { cout<<"Enter your name:"; cin>>Name; cout<<"Enter your user id"<<endl; getUserId(UID); cout<<endl<<"Enter your Opening balance"<<endl; cin>>balance; } void withdraw() { float amt; cout<<endl<<"Enter amount to withdraw"<<endl; cin>>amt;

if(balance-amt<500) cout<<"Sorry! Your account needs minimum balance of 500"<<endl<<"Transaction incomplete"<<endl; else balance-=amt; } void deposit() { float amt; cout<<endl<<"Enter amount to Deposit"<<endl; cin>>amt; balance+=amt; } void BalEnquiry() { cout<<"User id"<<UID<<endl; cout<<"Name:"<<Name<<endl; cout<<"Balance:"<<balance<<endl; } int searchAccount(char id[]) { if(strcmp(UID,id)==0) return 1; else return 0; } };

main() { char uid[14]; Account a[20]; int ch,f=0,count=-1,i; do { cout<<"1->Create Account 2->Withdraw 3->Deposit 4->Balance Enquiry 5>Exit"<<endl; cin>>ch; switch(ch) { case 1: a[++count].createAccount(); break; case 2: if(count>=0) { cout<<"Enter your account user id"<<endl; getUserId(uid); for(i=0;i<=count;i++) { f=a[i].searchAccount(uid); if(f) break; } if(f==0) cout<<"No account present for the entered user id"<<endl; else { a[i].withdraw(); cout<<"Transaction completed"<<endl<<endl; } }

else cout<<endl<<endl<<"No accounts present to perform the transaction"<<endl; break; case 3: if(count>=0) { cout<<"Enter your account user id"<<endl; getUserId(uid); for(i=0;i<=count;i++) { f=a[i].searchAccount(uid); if(f) break; } if(f==0) cout<<endl<<"No account present for the entered user id"<<endl; else { a[i].deposit(); cout<<"Transaction completed"<<endl<<endl; } } else cout<<endl<<"No accounts present to perform the transaction"<<endl; break; case 4: if(count>=0) { cout<<"Enter your account user id"<<endl; getUserId(uid); for(i=0;i<=count;i++)

{ f=a[i].searchAccount(uid); if(f) break; } if(f==0) cout<<endl<<"No account present for the entered user id"<<endl; else a[i].BalEnquiry(); } else cout<<"No accounts present to perform this operation"<<endl; break; case 5:break; } }while(ch!=5); }

2> Write a C++ program to find area of a) Circle b) Triangle c) Rectangle (Using function overloading) Soln: #include<iostream> #include<math.h> using namespace std; float Area(int r)

{ return(3.142*r*r); } float Area(int a,int b,int c) { float s=(a+b+c)/2.0; float area=sqrt(s*(s-a)*(s-b)*(s-c)); return area; } float Area(int l,int b) { return(l*b); }

main() { int ch; float area; cout<<"1->Circle 2->Triangle 3->Rectangle"<<endl; cout<<"Your choice:"; cin>>ch; switch(ch) { case 1: int r; cout<<"Enter the radius"<<endl; cin>>r; area=Area(r); cout<<"Area="<<area<<endl; break;

case 2: int a,b,c; cout<<"Enter the 3 sides of the triangle"<<endl; cin>>a>>b>>c; area=Area(a,b,c); cout<<"Area="<<area<<endl; break; case 3: int len,bth; cout<<"Enter length and breadth"<<endl; cin>>len>>bth; area=Area(len,bth); cout<<"Area="<<area<<endl; break; default:cout<<"Invalid choice"<<endl; } //system("pause"); } 3> Write a C++ program to implement queue.(Linear FIFO)
Soln:

#include <iostream> using namespace std; class Queue { private: int *a; int front,rear,size; public:

Queue() { size=5; a=new int[size]; front=0; rear=-1; } Queue(int sz) { size=sz; a=new int[size]; front=0; rear=-1;
}

void insert(int ); int Delete (); int isFull(); int isEmpty(); void display(); ~Queue() { delete[] a; } };

void Queue:: insert (int d) { a[++rear]=d;

int Queue:: Delete () { return(a[front++]); }

int Queue:: isFull () { if(rear==size-1) return(1); else return(0); }

int Queue:: isEmpty () { if(rear==-1) return(1); else return(0); }

void Queue:: display ()

{ cout<<"The contents of queue are\n"; for(int i=front;i<=rear;i++) cout<<a[i]<<"\n"; }

main () { Queue x(8); int ch,d; do { cout<<"1->insert\n2->delete\n3->display\n4->exit\nyour choice:"; cin>>ch; switch(ch) { case 1:if(x.isFull()) { cout<<"Queue is full\n"; } else { cout<<"Enter data"; cin>>d;

x.insert(d); } break; case 2:if(x.isEmpty()) { cout<<"Queue is empty\n"; } else { d=x.Delete(); cout<<"Deleted data:"<<d<<"\n"; } break; case 3:if(x.isEmpty()) { cout<<"Queue is Empty\n"; } else x.display(); break; case 4:break; } }while(ch!=4); }

4> Writa a C++ program to store information of cricketers( Name,place,ranking,total runs),(minimum 5 cricketers) using dynamic memory allocation. Soln: 2)#include <iostream> using namespace std; class Cricketer { private: char name[20], place[15]; int rank,runs; public: void Read() { cout<<"Enter player's name:"; cin>>name; cout<<"Place:"; cin>>place; cout<<"Ranking:"; cin>>rank; cout<<"Runs:"; cin>>runs; } void Display () { cout<<"\n"<<"Name:"<<name<<"\n"<<"Place:"<<place<<"\n"<<"Ranking:"<<rank<<"\n"< <"Runs:"<<runs<<"\n"; }

}; main () { Cricketer *c; int n,i; input: cout<<"Enter the number of players"<<endl; cin>>n; if(n<5) { cout<<"Minimum players should be 5\n"; goto input; } c=new Cricketer[n]; for(i=0;i<n;i++) { cout<<"Enter info of player "<<i+1<<endl; c[i].Read(); } cout<<"The information of players is\n"; for(i=0;i<n;i++) c[i].Display(); delete[] c; }

5> Write a C++ program to implement library management system (book id,book name,author). Display

particular book information taking book id as input and display number of books in library using constructors. Soln: #include<iostream> using namespace std; class Book { private: int id; char Name[20],Author[20]; public: void Read() { cout<<"Enter Book id,Name and Author"<<endl; cin>>id>>Name>>Author; } void Display() { cout<<"Book ID:"<<id<<" Name:"<<Name<<" Author:"<<Author<<endl;; } Book() { id=0; for(int i=0;i<20;i++) Name[i]=Author[i]='\0'; } friend int Search(Book [],int,int); };

int Search (Book B[],int BookId,int count) { int occ=0; for(int i=0;i<=count;i++) if(B[i].id==BookId) occ++; return occ; } main() { Book B[20]; int ch,count=-1,Bookid,occ; do { cout<<"1->Create a new book record\n2->Search for a book\n3->Display all the books\n4->Exit"<<endl; cin>>ch; switch(ch) { case 1:B[++count].Read(); break; case 2: cout<<"Enter the book id to be searched"<<endl; cin>>Bookid; occ=Search(B,Bookid,count); if(occ>0) cout<<"There are "<<occ<<" book(s) available in the library"<<endl; else cout<<"No books for the eneterd Book id"<<endl; break; case 3: if(count<0) cout<<"No books available in the library"<<endl;

else for(int i=0;i<=count;i++) B[i].Display(); break; case 4:break; } }while(ch!=4); }

Anda mungkin juga menyukai