Thursday, 3 September 2015

calculator by using switch statement

#include "stdafx.h"
#include<iostream>
using namespace std;
void main()
{
float n1,n2,s;
char ch;
cout<<"ENter the values"<<endl;
cin>>n1;
cout<<"ENter the operator"<<endl;
cin>>ch;
cout<<"ENter the 2nd values"<<endl;
cin>>n2;
switch(ch)
{
case '+':

       s=n1+n2;
       break;
       case '-':

       s=n1-n2;
       break;
       case '*':

    s=n1*n2;
       break;
       case '/':

       s=n1/n2;
       break;

default:
       cout<<"invalid character "<<endl;
}
       if (ch=='+'||ch=='-'||ch=='*'||ch=='/')
              cout<<n1<<ch<<n2<<"="<<s<<endl;
system("pause");
}

Output screen should have Options o Login o Register  If a user goes for “Login” ask him user name & Password. • Password should be in digits (10000-65000) • On successful login user will be given two options o Read Your Profile o Overwrite your profile.  If user asks for “Register” ask user • User name • Password • On successful registration ask user o Create your profile.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#define MAX 100
using namespace std;
class prof
{
public:
string user;
int pword;
string name;
string fname;
string cname;
string dob;
string uname;
int reg;
int pass;
int age;
void login();
void showProf();
void overWrite();
};
void prof::login()
{
system("cls");
string duser;
int dpass, ch1;
ifstream dfile("default.txt");
dfile >> duser >> dpass;
do
{
cout << "\nEnter User Name: ";
cin >> uname;
if (uname.compare(duser) == 0)
{
cout << "Success";
break;
}
else
cout << "\nWrong User Name";
} while (1);
do
{
cout << "\nEnter Password: ";
cin >> pass;
if (pass == dpass)
{
cout << "Success!!";
break;
}
else
cout << "\nInvalid Password!!";
} while (1);
system("cls");
do{
cout << "\n 1. Veiw Your Profile"
<<  "\n 2. Overwrite/Update Your Profile"
<< "\n 3. Back"<<endl;
cin  >> ch1;
switch (ch1)
{
case 1:
showProf();
break;
case 2:
overWrite();
break;
case 3:
break;
default:
cout << "Invalid Choice";
break;
}break;
} while (1);

}
void prof::showProf()
{
system("cls");
system("color f1");
ifstream dprof("dprof.txt");
dprof >> name >> fname >> cname >> age >> dob >> reg;
cout << "\nName: " << name;
cout << "\nFather Name: " << fname;
cout << "\nClass: " << cname;
cout << "\nAge: " << age <<" Years";
cout << "\nDate of Birth: " << dob;
cout << "\nRegistration Number: " << reg;

}
void prof::overWrite()
{
system("cls");
cout << "***Overwrite/Update Menu***"<<endl;
cout << "Enter Name: ";
cin >> name;
cout << "Enter Father Name: ";
cin >> fname;
cout << "Enter Class: ";
cin >> cname;
cout << "Enter Age: ";
cin >> age;
cout << "Enter D.O.B: ";
cin >> dob;
cout << "Enter Reg # : ";
cin >> reg;
ofstream dprof("dprof.txt");
dprof << name << " " << fname << " " << cname << " " << age << " " << dob << " " << reg;
dprof.close();
}
void regist(prof ddata[])
{
int flag = -1, ch;
flag++;
cout << "Enter Username: ";
cin >> ddata[flag].user;
cout << "Enter Password: ";
cin >> ddata[flag].pword;
string temp = ddata[flag].user
do
{
system("cls");
cout << "Successfully Saved\n";
cout << "1. Create Profile\n"
<< "2. Go Back" << endl;
cin >> ch;
switch (ch)
{
case 1:
system("cls");
cout << "**Profile Menu**\n";
cout << "Enter Name: ";
cin >> ddata[flag].name;
cout << "Enter Father Name: ";
cin >> ddata[flag].fname;
cout << "Enter Class: ";
cin >> ddata[flag].cname;
cout << "Enter D.O.B: ";
cin >> ddata[flag].dob;
cout << "Enter Age: ";
cin >> ddata[flag].age;
cout << "Enter Reg # : ";
cin >> ddata[flag].reg;
break;
case 2:
break;
default:
cout << "Invalid Choice";
break;
}
if (ddata[flag].user == ddata[flag].name)
{
ofstream samename("samenames.txt");
samename << ddata[flag].user << " "
<< ddata[flag].pword << " "
<< ddata[flag].name << " "
<< ddata[flag].fname << " "
<< ddata[flag].cname << " "
<< ddata[flag].dob << " "
<< ddata[flag].age << " "
<< ddata[flag].reg;
samename.close();
}
else
{
ofstream others("others.txt");
others << ddata[flag].user << " "
<< ddata[flag].pword << " "
<< ddata[flag].name << " "
<< ddata[flag].fname << " "
<< ddata[flag].cname << " "
<< ddata[flag].dob << " "
<< ddata[flag].age << " "
<< ddata[flag].reg;
others.close();
}
break;
} while (1);

}
int main()
{
prof data[MAX];
prof func;
int ch;
cout << "\t\t\t***Wel Come***\n"
<< "Note: (Default User is iiui(case sensitive) pass: 10000)\n";
do
{
cout<< "\n\n1. Login\n"
<< "2. Register\n"
<< "3. Exit\n"
<< endl;
cin >> ch;


switch (ch)
{
case 1:
func.login();
break;
case 2:
regist(data);
break;
case 3:
exit(1);
default:
cout << "Invalid Choice";
break;
}
} while (1);

cout << endl;
system("pause");
return 0;
}

Saturday, 29 August 2015

insert|display|sort|delete|linklist

// own project.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class student
{
int regno;
float marks;
string grade;
student *next;
student *head,*last;
public:
student()
{
head=last=NULL;
}
void add();
void display();
void sort();
void Delete(int);
};
void student::add()
{
system("CLS");
cout<<endl;
student *s=new student();
cout<<"enter Reg No "<<endl;
cin>>s->regno;
cout<<"enter marks "<<endl;
cin>>s->marks;
s->next=NULL;
if (head== NULL)  //Case 1
{
head = s;
last = s;
}
else               //Case 2
{
last->next = s;
last = s;
}
}


void student::display()
{
system("cls");
cout << "\n\n\n";
student * head = this->head;
student * temp;

temp = head; //For assigning marks

while (temp != NULL)
{
if (temp->marks >= 80)
temp->grade = 'A';
else if (temp->marks >= 75)
temp->grade = 'B';
else if (temp->marks >= 70)
temp->grade = 'B';
else if (temp->marks >= 65)
temp->grade = 'C';
else if (temp->marks >= 60)
temp->grade = 'C';
else
temp->grade = 'F';
temp = temp->next;
}

temp = head; //For printing marks

cout << "Roll" << setw(20) << "Marks" << setw(20) << "Grade" << endl;
cout << "__________________________________________________" << endl;

while (temp != NULL)
{
cout << temp->regno << setw(20)
<< temp->marks << setw(20)
<< temp->grade << endl;
temp = temp->next;
}

cout << endl << endl << endl;
system("pause");
}
void student::sort()
{
system("CLS");
cout<<"\n\n\nOrder of sorting\n \n1. Ascending order \n2.Descending Order\n";
int order;
cin>>order;
if (order==1)
{
for (student *new1=head; new1!=NULL ; new1=new1->next)
{
for (student *pass=new1->next; pass!=NULL; pass=pass->next)
{
if(new1->regno >pass->regno)
{
int temp=new1->regno;
new1->regno=pass->regno;
pass->regno=temp;
}

if(new1->marks >pass->marks)
{
float temp=new1->marks;
new1->marks=pass->marks;
pass->marks=temp;
}
if(new1->grade >pass->grade)
{
string temp=new1->grade;
new1->grade=pass->grade;
pass->grade=temp;
}
}
}
}

if (order==2)
{
for (student *new1=head; new1!=NULL ; new1=new1->next)
{
for (student *pass=new1->next; pass!=NULL; pass=pass->next)
{
if(new1->regno < pass->regno)
{
int temp=new1->regno;
new1->regno=pass->regno;
pass->regno=temp;
}

if(new1->marks < pass->marks)
{
float temp=new1->marks;
new1->marks=pass->marks;
pass->marks=temp;
}
if(new1->grade < pass->grade)
{
string temp=new1->grade;
new1->grade=pass->grade;
pass->grade=temp;
}
}
}
}
}
void student::Delete(int deleteitem)
{
student *current;
student *trailcurrent=NULL;
bool found;
if (head==NULL)
cout<<"Cannot delete item from empty item"<<endl;
else
{
current=head;
found =false;
}
while(current!=NULL && !found)
{
if(current->regno>=deleteitem)
found =true;
else
{
trailcurrent=current;
current=current->next;
}
}
if (current==NULL)
cout<<"Cannot delete file"<<endl;
else if(current->regno==deleteitem)
{
if(head==current)
{
head=head->next;
if (head==NULL)
last=NULL;
delete current;
}

else
{
trailcurrent->next=current->next;
if(current==last)

last=trailcurrent;
delete current;
}
}
else
cout<<"Item cannot delete "<<endl;

}
void Mainmenu();
void main()
{
student F;
for(;;)
{
Mainmenu();
int ch;
cin>>ch;
switch(ch)
{
case 1:
F.add();
break;
case 2:
F.display();
break;
case 3:
F.sort();
break;
case 4:
system("CLS");
cout<<"Enter regno you want to delete "<<endl;
int n;
cin>>n;
F.Delete(n);
break;
case 5:
exit(0);

break;
default:
cout<<"Wrong selection"<<endl;
}
}
}
void Mainmenu()
{
system("cls");
cout << "\n\n\nStudent Records v0.1\n" << endl;
cout << "1. Insert Record \t" << endl
<< "2. Display Record \t(All Records) " << endl
<< "3. Sort \t\t (Asc/Desc)"<< endl
<< "4. Delete" << endl
<< "5. Exit" << endl;
}

calculate grade by using pointer in classes


#include "stdafx.h"
#include<iostream>
#include<string>
#include <iomanip>
using namespace std;
class page
{
public:
int pageno;
string pagetitle;
string pagecontant;
page *pre;
};
void main()
{
page *firstpage=NULL;
int tp;
cout<<"Enter the number of pages "<<endl;
cin>>tp;
for (int i=1;i<=tp;i++)
{
page *newpage=new page();
cout<<"Enter Page number "<<endl;
cin>>newpage->pageno;
cout<<"Enter Page Title "<<endl;
cin>>newpage->pagetitle;
cout<<"Enter Page contant "<<endl;
cin>>newpage->pagecontant;
newpage->pre=firstpage;
firstpage=newpage;
}


page *temp=firstpage;

while (temp!=NULL)
{

cout<<"The page no is "<<temp->pageno<<endl;
cout<<"The page title "<<temp->pagetitle<<endl;
cout<<"The page contants is "<<temp->pagecontant<<endl;
        temp=temp->pre;
}
temp=firstpage;



while (temp!=NULL)
{
cout<<"The page no is "<<temp->pageno<<endl;
cout<<"The page title "<<temp->pagetitle<<endl;
cout<<"The page contants is "<<temp->pagecontant<<endl;
        temp=temp->pre;
}
while (temp->pre!=NULL)
{
temp=temp->pre;
}

delete temp->pre;


// p.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;
class student
{
public:
int regno;
int marks;
char grade;
    student *st;
};
void main()
{
student *hand=NULL;
int y;
cout<<"Enetr the student "<<endl;
cin>>y;
for (int i=1;i<=y;i++)
{
student *s1=new student();
cout<<"regno"<<endl;
cin>>s1->regno;
cout<<"Enter marks"<<endl;
cin>>s1->marks;
s1->st=hand;
if (s1->marks >=60)
s1->grade='A';
else
s1->grade= 'F';
hand=s1;
}
student *temp=hand;
while (temp!=NULL)
{
cout<<"Reg no is = "<<temp->regno<<endl;
cout<<"marks is = "<<temp->marks<<endl;
cout<<"The grade is "<<temp->grade<<endl;
temp=temp->st;
}

system("pause");
}


Hashcode example

#include "stdafx.h"
#include <iostream>
using namespace std;
int hashcode(int v)
{
  int l=0,s=0;
  while (v>0)
  {
   s+=v%10;
   v/=10;
   l++;

  }
return s;
}
int main()
{
cout<<hashcode(12345)<<endl;
system("pause");
return 0;
}

Roman numbers

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

string dec_to_numeral
(int x)
{
int dec[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string num[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
string numeral;

for(int i = 0; i < 13; i++)
{
while (x >= dec[i])
{
x -= dec[i];
numeral.append(num[i]);
}
}

return numeral;
}

int main()
{
int n;
cout<<"Enter a decimal number: ";
cin>>n;
cout <<"Roman = "<< dec_to_numeral(n)<<endl;
system("pause");
return 0;
}

Wednesday, 26 August 2015

This Program Will take Input from Files. Now When a user enter a Name Then This program will Search that name in the files Stored in the Computers Hard Drive. If Found Then It will show the message That It's a Boy name Else Vice Versa.


void main()
{
char choice='Y';
do
{
int i,k;
string std,test;
cout<<"Please The First Letter Should Be  A CAPITAL CASE.\n\n";
cout<<"Now Enter Your Name : ";
cin>>test;
if(test[0]=='A')
{
ifstream input("F:\\database\\A.txt");
while(!input.eof())
{
getline(input,std);
i = std.find(test);
if(i==0) 
{
cout<<"\n\nYes! It is A Boy Name.\n\n";
k=0;
}
}
if(k!=0)
{
cout<<"Sorry!\n\n";
cout<<"Eighter It Is A Girl Name.\n\nOr\n";
cout<<"\nMay be You Entered The Wrong Spelling.\n";
cout<<"\nPlease Try Again.\n\n";
}
}

/*/////////////////////// FOR B,D,F,G,H,I \\\\\\\\\\\\\\\\\\\\\\\*/


if(test[0]=='B' || test[0]=='D' || test[0]=='F' || test[0]=='G' || test[0]=='H' || test[0]=='I')
{
ifstream input("F:\\database\\BDFGHI.txt");
while(!input.eof())
{
getline(input,std);
i = std.find(test);
if(i==0) 
{
cout<<"\n\nYes! It is A Boy Name.\n\n";
k=0;
}
}
if(k!=0)
{
cout<<"Sorry!\n\n";
cout<<"Eighter It Is A Girl Name.\n\nOr\n";
cout<<"\nMay be You Entered The Wrong Spelling.\n";
cout<<"\nPlease Try Again.\n\n";
}
}


/*/////////////////////////// FOR J,K,L,M \\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/


if(test[0]=='J' || test[0]=='K' || test[0]=='L' || test[0]=='M')
{
ifstream input("F:\\database\\JKLM.txt");
while(!input.eof())
{
getline(input,std);
i = std.find(test);
if(i==0) 
{
cout<<"\n\nYes! It is A Boy Name.\n\n";
k=0;
}
}
if(k!=0)
{
cout<<"Sorry!\n\n";
cout<<"Eighter It Is A Girl Name.\n\nOr\n";
cout<<"\nMay be You Entered The Wrong Spelling.\n";
cout<<"\nPlease Try Again.\n\n";
}
}


/*/////////////////////// FOR N,O,P,Q,R \\\\\\\\\\\\\\\\\\\\\\\*/


if(test[0]=='N' || test[0]=='O' || test[0]=='P' || test[0]=='Q' || test[0]=='R')
{
ifstream input("F:\\database\\NOPQR.txt");
while(!input.eof())
{
getline(input,std);
i = std.find(test);
if(i==0) 
{
cout<<"\n\nYes! It is A Boy Name.\n\n";
k=0;
}
}
if(k!=0)
{
cout<<"Sorry!\n\n";
cout<<"Eighter It Is A Girl Name.\n\nOr\n";
cout<<"\nMay be You Entered The Wrong Spelling.\n";
cout<<"\nPlease Try Again.\n\n";
}
}


/*/////////////////////////// FOR S,T \\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/


if(test[0]=='S' || test[0]=='T')
{
ifstream input("F:\\database\\ST.txt");
while(!input.eof())
{
getline(input,std);
i = std.find(test);
if(i==0) 
{
cout<<"\n\nYes! It is A Boy Name.\n\n";
k=0;
}
}
if(k!=0)
{
cout<<"Sorry!\n\n";
cout<<"Eighter It Is A Girl Name.\n\nOr\n";
cout<<"\nMay be You Entered The Wrong Spelling.\n";
cout<<"\nPlease Try Again.\n\n";
}
}


/*/////////////////////// FOR U,V,W,Y,Z \\\\\\\\\\\\\\\\\\\\\\\*/


if(test[0]=='U' || test[0]=='V' || test[0]=='W' || test[0]=='Y' || test[0]=='Z')
{
ifstream input("F:\\database\\UVWYZ.txt");
while(!input.eof())
{
getline(input,std);
i = std.find(test);
if(i==0) 
{
cout<<"\n\nYes! It is A Boy Name.\n\n";
k=0;
}
}
if(k!=0)
{
cout<<"Sorry!\n\n";
cout<<"Eighter It Is A Girl Name.\n\nOr\n";
cout<<"\nMay be You Entered The Wrong Spelling.\n";
cout<<"\nPlease Try Again.\n\n";
}
}
cout<<"Enter Your Choice:\n\n";
cout<<"If You Want To Carry On Then Enter  Y\n\n";
cout<<"Else If You Want To Brake Then Press  N\n\n";
cin>>choice;
}
while(choice!='N');
}