8.30.2012

[OpenSource C++::Updated] Homework #1 Class QL HS + Book

Tài Liệu "Kiến Trúc Máy Tính"
Nhấp vào đây để xem. Powered by GDocument

Lưu ý:
   1. Nếu các bạn đọc không hiểu thì có thể đánh dấu trao đổi với mình tại lớp hoặc qua Y!
   2. Không sao chép nguyên gốc code này (thay đổi ít nhất là các câu thông báo ra màn hình và các nhập xuất cơ bản, các tên biến,...), hiểu trước viết lại sau. 
   3. Hy vọng các bạn đăng ký nhận bài qua mail để và nếu được thì làm thành viên của blog mình, rất hoan nghênh vì sau này có thể một số bài mình sẽ chỉ private cho thành viên mà thôi không thể để leak  một cách vô nghĩa được. Mong các bạn thông cảm ! 

[Click vào dấu ngắt nhảy bên dưới để xem code (nếu có) ]


//By Viet Trung. (c) All rights Reserved
#include<iostream.h>
#include<conio.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char rob[50],robot[100],robot2[50];
char gtrim(char a[],int f)
{
 int n=strlen(a);
 while(a[f]==' ')
 {
 for(int p=f ;p<n;p++)
 a[p]=a[p+1];
 a[n--]=NULL;
 }
}
char ctrim(char a[])  
{
 int n=strlen(a);
 for(int i=1;i<n;i++)
 if(a[i]==' '&&a[i+1]==' ')
 gtrim(a,i+1);
}

char fix(char ten[],int n)//instead of "strlwr+strupr"
{
 for(int y=0;y<n;y++)
     if(ten[y]!=' '&&'A'<=ten[y]&&ten[y]<='Z') ten[y]=ten[y]+' ';//lower case all characters
    for(int y=0;y<n;y++)
    {
     if(ten[y]==' '&&ten[y+1]!=' '||y==0)
     {
      if(y==0)
      ten[y]=toupper(ten[y]);//upper case first letter
    else
        ten[y+1]=toupper(ten[y+1]);    
     }
    }
}
class hocsinh
{
 private:
  float toan, ly, hoa; 
  char name[50];
 public: 
 float avg;
 void in();
 void out();
};
static hocsinh *p;
int a=-1;
void hocsinh::in()
{
    int n=0;
 cout<<"Ho ten:";
    fflush(stdin);
    gets(rob);
    gtrim(rob,0);
    ctrim(rob);
    fix(rob,strlen(rob));
    strcpy(name,rob);
    cout<<"Diem toan:";
    cin>>toan;
    cout<<"Diem ly:";
    cin>>ly;
    cout<<"Diem hoa:";
    cin>>hoa;
    avg=(toan+ly+hoa)/3;
}
void hocsinh::out()
{
    cout<<endl<<"Ho ten: ";
    puts(name);
    cout<<"Diem toan: "<<toan;
    cout<<"\nDdiem ly: "<<ly;
    cout<<"\nDiem hoa: "<<hoa;
    cout<<"\nDiem trung binh: "<<avg;
}

void hs(int &a)
{
    system("cls");
    //hocsinh *p;
    int n, i;
    float max_s=0;
    cout<<"-@root\\Quan Ly Hoc Sinh";
    cout<<"\nSo hoc sinh can nhap thong tin: ";
    cin>>n;
    p=new hocsinh[n];
    for(i=0; i<n; i++)
    {
        cout<<endl<<i+1<<endl;
        p[i].in();
        if(p[i].avg>max_s)
        {
         max_s=p[i].avg;
         a=i;
        }
    }
    system("cls");
    cout<<"@root\\Quan Ly Hoc Sinh";
    cout<<"\n------------------------------ Data Report ------------------------------------";
    if(n==0)
    cout<<"\n<Empty>";
 else
 {
    for(i=0; i<n; i++)
       {
           cout<<"\n#"<<i+1<<"";
   p[i].out();
   cout<<"\n";
       }
  
    }        
//    delete p;
}
void br()//hocsinh *p, int a)//best result
{
 system("cls");
 cout<<"-------------------------------- Filter Result ---------------------------------";
 cout<<"   Ban dang xem thong tin chi tiet cua hoc sinh co diem trung binh cao nhat ";
 cout<<"\n#"<<a+1<<endl;
 p[a].out();

}
class CARD
{
 private:
  char title[300],author[50];
 // int q_ty;//q_ty la chu viet tat Q'TY: So luong ^^
 public:
 int q_ty;//Variable else can access 
 void nhap();
 void xuat();
};
static CARD *l;//Global variable
int ind=-1;//like above
void CARD::nhap()
{
    cout<<"Tua de sach: ";
    fflush(stdin);
    gets(robot);
    gtrim(robot,0);
    ctrim(robot);
    fix(robot,strlen(robot));
    strcpy(title,robot);
    cout<<"Tac Gia: ";
    fflush(stdin);
    gets(robot2);
    gtrim(robot2,0);
    ctrim(robot2);
    fix(robot2,strlen(robot2));
    strcpy(author,robot2);
    cout<<"So luong: ";
    cin>>q_ty;
}
void CARD::xuat()
{
 cout<<"\nTua de sach: ";
    puts(title);
    cout<<"Tac Gia: ";
    puts(author);
    cout<<"So luong: "<<q_ty<<"\n";
}
void bs()//CARD *l,int ind)
{
 system("cls");
 cout<<"-------------------------------- Filter Result ---------------------------------\n";
 cout<<"      Ban dang xem thong tin chi tiet cua cuon sach co so luong nhieu nhat ";
 cout<<"\n#"<<ind+1<<endl;
 l[ind].xuat();
}
void tv(int &ind)
{
 system("cls");
    //static CARD *l;
    static int q, d,max=0;
    char z;
    cout<<"-@root\\quan li sach";
    cout<<"\nSo sach can nhap thong tin: ";
    cin>>q;
    l=new CARD[q];
    for(d=0; d<q; d++)
    {
        cout<<endl<<d+1<<endl;
        l[d].nhap();
        if(l[d].q_ty>max)
        {
         max=l[d].q_ty;
         ind=d;
        }
    }
    system("cls");
    cout<<"@root\\Quan Ly Sach";
    cout<<"\n-------------------------------- Data Report------------------------------------\n";
    if(q==0)
    cout<<"\n<Empty>";
 else
 {
  for(d=0; d<q; d++)
     {
         cout<<"\n#"<<d+1;
   l[d].xuat();
     }  
 }
 if(d!=0)
 {
  cout<<"\n->Nhan 'V' de xem thong tin ve sach co so luong nhieu nhat";
  z=getch();
  if(z=='V'||z=='v')
  {
   system("cls");
   cout<<"\a:function/filter?best_seller\n--------------------------------------------------------------------------------";
   cout<<"     Ban dang xem thong tin chi tiet cua cuon sach co so luong nhieu nhat\n ";
   cout<<"\n#"<<ind+1;
   l[ind].xuat();
  }  
 }
 //   delete l; //Release Memory
}
main()
{
 system("cls");
 char w='E',k, ten[100],Anon[]={'A','n','o','n','y','m','o','u','s','\0'};
 int y=0,n=0;
// static CARD *l;
// static int ind;
    l = new CARD[1];//block freeze system when HEAP not available
    p = new hocsinh[1];
    B:system("cls");
 cout<<"\t\t\t     Home work #1  C++  29.Aug.12\n";
 cout<<"\t\tby Viet Trung - 05CDCT1 http://izone4u.blogspot.com\n";
 cout<<"\t       IDE,CLR is powered by C-Free 5.0. All rights reserved\n";
    cout<<"\t\t\t       Welcome to Msys-Console\n";
    cout<<"--------------------------------------------------------------------------------\n";
    cout<<"\aMoi ban nhap ten cua minh: ";
fflush(stdin);
    gets(ten);
    ctrim(ten);
    gtrim(ten,0);
 n=strlen(ten); 
 fix(ten,n);
 fflush(stdin);
 if(n==0)
  strcpy(ten,Anon);
 do
 {
   C:system("cls");
   w='E';
   cout<<"\t                       CONSOLE CONTROL\n";
   cout<<"--------------------------------------------------------------------------------\n";
   printf("\t        Chao %s ! Moi ban chon cac chuc nang ben duoi\n",ten);
   cout<<"> Nhap Lieu ____________________________________________________________________\n";
   cout<<"1. Quan Ly Thong Tin Hoc Sinh";
   cout<<"\n2. Quan Ly Sach";
   if(ind!=-1)
   {
    cout<<"\t\n\n\a* New function (Da co du lieu ve sach)-----------------------------";
    cout<<"\t\n|            s: Xem ten sach co so luong nhieu nhat                |>>>>>>>>>>>>";
    cout<<" ------------------------------------------------------------------";
   }
   if(a!=-1)
   {
    cout<<"\t\n\n\a* New function (Da co du lieu ve hoc sinh) ------------------------";
    cout<<"\t\n|          r: Xem ten hoc sinh co diem trung binh cao nhat         |>>>>>>>>>>>>";
    cout<<" ------------------------------------------------------------------";
   }
   cout<<"\n\n> Thao tac tren du lieu va chuong trinh________________________________________\n";
   cout<<"\nc. Doi ten nguoi su dung";
   cout<<"\nd. Xoa tat ca du lieu da nhap";
   cout<<"\n________________________________________________________________________________";
   cout<<"\n\t\t\t\t\t\t\t\t\t(E) Exit\n";
   k=toupper(getch());
   switch(k)
   {
    case '1': hs(a);
    break;
    case '2': tv(ind);
    break;
    case 'S': bs();
    break;
    case 'R': br();
    break;
    case 'C': goto B;
    case 'E': goto A;
    break;
    case 'D': 
    {
     cout<<"\t\n\a>>>  Thong bao: Tat ca du lieu se bi xoa, ban chac chu ? (Y/N)";
     if(toupper(getch())=='N');
     {
      a=-1;ind=-1;
      goto C;
     }
    }
    break;
    default: cout<<"\n> Thong Bao: Ban cho sai roi hoac chua chon ma da nhan Enter !";
   }
   cout<<"\n------\nConsole Navigation:\n-> Nhan 'B' de quay lai trinh don chinh\n-> Nhan phim bat ky de ket thuc chuong trinh.\n";
   w=toupper(getch());   
   //if(w!='B')//Bao ve bien w khoi cach chay tuyen tinh
   // A:w='P';
 }
 while(w=='B');
A:  cout<<"\n\t\t\t\t !Caution";
 cout<<"\n\t\t\t\a Thoat chuong trinh ? (Y/N)";
 if(toupper(getch())=='N')
  goto C;
 else
  k='A';
 delete l;//Release HEAP
 delete p;
 if(k!='E')
  cout<<"\n> Thong Bao:\n\a\n\t\t\t      |Disconnected|\n";
}

0 Nhận xét:

Đăng nhận xét

Đăng ký Đăng Nhận xét [Atom]

<< Trang chủ