bai tap

Màu nền
Font chữ
Font size
Chiều cao dòng

Lớp Time

#include<iostream.h>

 #include<conio.h>

 #include<math.h>

class time

 {  int gio,phut,giay;

  public:

  time(int g=0, int p=0,int s=0)

  { gio=g; phut=p; giay=s;}

  void set(int g,int p,int s);

  void xuat();

  };

void time::set(int g,int p,int s)

{p=p+s/60;

g=(g+p/60);

gio=g%24;

phut=p%60;

giay=s%60;

 }

 void time::xuat()

{

cout<<"

Gio he thong la: "<<gio<<":"<<phut<<":"<<giay;

}

void main()

{

clrscr();

time tg(12,12,12);

int g,p,s;

tg.xuat();

 getch();

clrscr();

cout<<"\Thay doi thoi gian";

cout<<"

Gio: ";cin>>g;

cout<<"

Phut: ";cin>>p;

cout<<"

Giay: ";cin>>s;

tg.set(g,p,s);

tg.xuat();

getch();

 }

Xây dựng lớp Queue

#include<iostream.h>

#include<string.h>

#include<conio.h>

#include<stdlib.h>

#include<ctype.h>

const Max = 100;

class queue

{

      private:

 int front,rear; //dau va cuoi queue

int nodes[Max];

      public:

 queue()   // cau tu khoi dong queue

 { front = 0;

  rear = -1;

}

void xuat();

int empty();  // kiem tra queue bi rong khong

int full();   // kiem tra queue co bi day chua

int insert(); // them mot nut vao cuoi queue

int remove(); // xoa nut o dau queue

 int queuesize(); // xac dinh so nut co trong queue

};

int queue::queuesize()

{

return(rear - front + 1);

}

int queue::empty()

{       int kq;

if (rear<front) kq=1;

else kq=0;

return (kq);

}

int queue::full()

{

int  kq;

 if (rear == (Max-1)) kq=1;

else kq=0 ;

return(kq);

}

int queue::insert()

{

 int x;

cout<<"

Nhap phan tu can them vao queue ";

cin>>x;

if(full()) cout<<endl<<"queue bi day ";

else

return (nodes[++rear]=x);

}

int queue::remove()

{

 if(empty())

   cout<<endl<<"

queue bi rong khong the lay ra them phan tu nao";

       else

return nodes[front++];

}

void queue::xuat()

{

for(int i=rear;i>=front;i--)

    cout<<"   "<<nodes[i];

}

void menu()

{

cout<<"

An (t/T) them phan tu vao queue ";

cout<<"

An (l/L) lay phan tu ra khoi queue ";

  cout<<"

An (x/X) xuat cac phan tu con lai cua queue ";

  cout<<"

An (q/Q) de thoat ra ";

}

int main()

{

  queue a;

   clrscr();

   menu();

   while(1)

    {

  switch(toupper(getch()){

case 't':

case 'T':

cout<<"

Phan tu "<<a.insert()<<" them vao thanh cong

";

menu();

break;

 case 'l':

 case 'L':if(!a.empty())

cout<<"

Phan tu "<<a.remove()<<" duoc lay ra thanh cong

";

else cout<<"

queue bi rong khong the lay ra them phan tu nao

";

menu();

break;

case 'x':

case 'X':if(!a.empty())

 cout<<"

Phan tu con lai la : ";

 else

 cout<<"

queue bi rong";

  a.xuat();

menu();

break;

case 'q':

case 'Q':cout<<"

Ban da thoat ra thanh cong ";

}

}

 tt:getch();

}

STACK

#include<iostream.h>

#include<conio.h>

#define size 50

class stack

{

 char s[size];

 public:

  stack(){

top=0;

}

  ~ stack(){

cout<<"

";

}

  void insert(char a);

  char pop();

  int isempty();

  int isfull();

};

void stack::insert(char a){

  int kt;

  kt=isfull();

  if (kt==1)

return;

  s[top]=a;

  top++;

}

char stack::pop(){

  int kt;

  kt=isempty();

  if (kt==0)

return 0;

  top--;

  return s[top];

}

int stack::isempty(){

  int kt=1;

  if (top==0)

{

cout<<"\ Stack Trong";

kt=0;

}

  return kt;

}

int stack::isfull(){

  int kt=0;

  if (top==size)

{

cout<<"

Stack tran";

kt=1;

}

  return kt;

}

void main()

{ clrscr();

 stack s1;

 int n,b;

 cout<<"

";

 cout<<"

Nhap so nguyen bat ky";

 cin>>n;

 while (n!=0)

  {

b=n%2;

s1.insert(b);

n=n/2;

  }

  while (s1.top!=0)

cout<<s1.pop();

 getch();

}

Lập chương trình nhập danh sách kết quả thi của các thí sinh gồm 3 môn toán, lý, hóa, đưa ra màn hình các thí sinh trúng tuyển vào (điểm chuẩn vào trường là 22). Trong chương trình dùng new để cấp phát bộ nhớ cho các đối tượng, delete để xóa các đối tượng trước khi kết thúc chương trình.

Bài Làm:

#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

#include<stdlib.h>

class thisinh

{

float t,l,h;

char ht[25];

public:

void nhap()

{

cout<<"

nhap ho ten sinh vien:";cin>>ht;

cout<<"

nhap diem toan:";cin>>t;

cout<<"

nhap diem ly:";cin>>l;

cout<<"

nhap diem hoa:";cin>>h;

}

float td()

{

float s;

s=(t+l+h);

return s;

}

void xuat()

{

cout<<"

ten sinh vien:"<<ht<<"

";

cout<<"diem mon toan:"<<t<<"

";

cout<<"diem mon ly:"<<l<<"

";

cout<<"diem mon hoa:"<<h<<"

";

cout<<"tong diem la:"<<td()<<"

";

}

};

void main()

{

clrscr();

 thisinh *a;

 int n,i,d;

 cout<<"

So thi sinh:";

 cin>>n;

 a=new thisinh[n+1];

 if (a==NULL)

{

 cout<<"

Loi cap phat vung nho";

 getch();

 exit(0);

}

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

{

cout<<"sinh vien thu:"<<i+1;

a[i].nhap();

}

cout<<"

danh sach sinh vien la:";

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

 a[i].xuat();

//-------------------------------------

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

if (a[i].td()>=22)

d++;

if (d==0)

cout<<"Ko co SV nao trung tuyen";

else

{

cout<<" ds sv trung tuyen";

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

if (a[i].td()>=22)

a[i].xuat();

}

delete a;

getch();

}

Lập chương trình nhập vào phân số, thực hiện các phép toán cộng, trừ, nhân, chia hai phân số. Trong chương trình sử dụng các phép chồng toán tử <<,>> để xuất và nhập phân số; +,-,*,/ để thực hiện các phép cộng, trừ, nhân, chia tương ứng.

Bài làm:

#include <iostream.h>

#include <conio.h>

#include<math.h>

int ucln(int a,int b)

{

 a=abs(a);

 b=abs(b);

 while (a!=b)

  if (a>b)

a=a-b;

  else

b=b-a;

  return a;

}

void RG(int tu,int mau)

{

 int uc;

 uc=ucln(tu,mau);

 tu=tu/uc;

 mau=mau/uc;

}

class PS

{

 int tu,mau;

 public:

  PS(int x=0,int y=1)

{tu=x;mau=y;

}

  PS operator +(PS a)

  {

PS c;

int uc,bc;

uc=ucln(mau,a.mau);

bc=(mau*a.mau)/uc;

c.tu=bc/mau*tu+bc/a.mau*a.tu;

c.mau=bc;

RG(c.tu,c.mau);

return c;

  }

  PS operator -(PS a)

  {

PS c;

int uc,bc;

uc=ucln(mau,a.mau);

bc=(mau*a.mau)/uc;

c.tu=bc/mau*tu-bc/a.mau*a.tu;

c.mau=bc;

RG(c.tu,c.mau);

return c;

  }

  PS operator *(PS a)

  {

PS c;

 c.tu=tu*a.tu;

             c.mau=mau*a.mau;

 RG(c.tu,c.mau);

return c;

  }

  PS operator /(PS a)

  {

PS c;

c.tu=tu*a.mau;

c.mau=mau*a.tu;

RG(c.tu,c.mau);

 return c;

  }

  void xuat()

  {        cout<<tu<<"/"<<mau;

  }

  };

 void main()

 {clrscr();

  PS a(3,2);

  PS b(3,5);

  PS c;

  c=a+b;

  cout<<"

Tong 2 phan so la:";

  c.xuat();

  c=a-b;

  cout<<"

Hieu 2 phan so la:";

  c.xuat();

  c=a*b;

  cout<<"

Tich 2 phan so la:";

  c.xuat();

  c=a/b;

  cout<<"

Thuong 2 phan so la:";

  c.xuat();

  getch();

 }

Hãy lập chương trình quản lý kết quả thi của một lớp không quá 100 sinh viên.

Lap chuong trinh thuc hien:

Xay dung lop co so SV chi luu ho ten va so bao danh.

Lop DT ke thua lop SV va luu ket qua cua 3 mon thi,

lop ket qua lluu tong so diem dat duoc cua cac SV*/

Bài làm:

#include <iostream.h>

#include <stdio.h>

#include <conio.h>

#include <iomanip.h>

#include <math.h>

class sv

{

char ht[30];

int sbd;

public: void nhap();

void in();

};

void sv:: nhap()

{

cout <<"

Nhap ho ten:"; gets(ht);

cout<<"

so bao danh:"; cin>>sbd;

}

void sv::in()

{

cout <<"

Ho ten:"<<ht;

cout<<"

So bao danh:"<<sbd;

}

class dt:public sv

{

public:

float toan,ly,hoa;

void nhap();

void in();

};

void dt::nhap()

{

sv::nhap();

cout<<"

Diem toan:";cin>>toan;

cout<<"

Diem ly:";cin>>ly;

cout<<"

Diem Hoa:";cin>>hoa;

}

void dt::in()

{

sv::in();

cout<<"

Diem toan la:"<<toan;

cout<<"

Diem ly la:"<<ly;

cout<<"

Diem hoa la:"<<hoa;

}

class kq:public dt

{

public:

float tong();

};

float kq::tong()

{

return(toan+ly+hoa);

}

void main()

{

clrscr();

kq a[100];

cout<<"

Nhap so luong sinh vien  ";

cin>>n        ;

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

{

cout<<"\Nhap SV thu: "<<i;

a[i].nhap();

}

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

{

a[i].in();

cout<<"

Tong diem cua SV thu "<<i<<"la: " <<a[i].tong();

}

getch();

}

Bạn đang đọc truyện trên: Truyen2U.Pro