Trung tâm đào tạo thiết kế vi mạch Semicon


  • ĐĂNG KÝ TÀI KHOẢN ĐỂ TRUY CẬP NHIỀU TÀI LIỆU HƠN!
  • Create an account
    *
    *
    *
    *
    *
    Fields marked with an asterisk (*) are required.
wafer.jpg

Tổng hợp bài tập và bài giải C/C++ (P2)

E-mail Print PDF
Article Index
Tổng hợp bài tập và bài giải C/C++ (P2)
2
All Pages

1:SẮP XẾP MẢNG

 

#include<alloc.h>

#include<stdio.h>

#include<conio.h>

//=======================================

void taolap(int *A,int n)

  {

    int i;

    printf("\n Tao lap day so:\n");

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

      {

        printf("\n A[%d]=",i);

        scanf("%d",&A[i]);

      }

  }

void dayso(int *A,int n)

  {

    int i;

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

      printf("%5d",A[i]);

  }

 

void select(int *A,int n)

  {

    int i,j,temp;

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

      {

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

          {

            if(A[i]>A[j])

              {

               temp=A[i];

               A[i]=A[j];

               A[j]=temp;

              }

          }

       }

    printf("\n Ket qua thu duoc la:");

    dayso(A,n);

  }

void luachon()

  {

    clrscr();

    int *A,n;

    printf("\n \t SAP XEP KIEU LUA CHON\n");

    printf("\n Nhap so phan tu n=");

    scanf("%d",&n);

    A=(int*)malloc(n*sizeof(int));

    taolap(A,n);

    select(A,n);

    free(A);

    getch();

  }

 

//=======================================

void in2(int *A,int n)

  {

    int i;

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

    printf("%5d",A[i]);

  }

void tructiep()

  {

    clrscr();

    int *A,i,j,n,temp;

    printf("\n SAP XEP KIEU TRUC TIEP\n");

    printf("\n\t SAP XEP KIEU TRUC TIEP\n");

    printf("\n Nhap so phan tu n=");

    scanf("%d",&n);

    A=(int*)malloc(n*sizeof(int));

    printf("\n Tao lap day so:\n");

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

      {

        printf("\n A[%d]=",i);

        scanf("%d",&A[i]);

      }

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

      {

        temp=A[i];

        for(j=i-1;j>=0&&temp<A[j];j--)

          A[j+1]=A[j];

        A[j+1]=temp;

        printf("\n\nKet qua lan thu %d:",i);

        in2(A,i+1);

      }

    free(A);

    getch();

  }

//=======================================

void tlap(int *A,int n)

  {

 

    int i;

    printf("\n");

    printf("\n Tao lap day so:\n");

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

      {

        printf("\n A[%d]=",i);

        scanf("%d",&A[i]);

      }

  }

void in1(int *A,int n)

  {

    int i;

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

    printf("%5d",A[i]);

  }

 

void bubble(int *A,int n)

  {

    int i,j,temp;

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

      {

        for(j=n-1;j>=i;j--)

          {

            if(A[j-1]>A[j])

              {

               temp=A[j-1];

               A[j-1]=A[j];

               A[j]=temp;

              }

          }

        printf("\n\n Ket qua lan %d:",i);

        in1(A,n);

      }

  }

void suibot()

  {

    clrscr();

    int *A,n;

    printf("\n SAP XEP KIEU SUI BOT\n");

    printf("\n Nhap so phan tu n=");

    scanf("%d",&n);

    A=(int*)malloc(n*sizeof(int));

    tlap(A,n);

    bubble(A,n) ;

    free(A);

    getch();

  }

//=======================================

void qs(int *A,int left,int right)

  {

    int i,j,x,y;

    i=left;

    j=right;

    x=A[(left+right)/2];

    do

      {

        while(A[i]<x&&i<right)i++;

        while(A[j]>x&&j>left)j--;

        if(i<=j)

          {

            y=A[i];

            A[i]=A[j];

            A[j]=y;

            i++;

            j--;

          }

      }while(i<=j);

    if(left<j)qs(A,left,j);

    if(i<right)qs(A,i,right);

  }

void quick(int *A,int n)

  {

    qs(A,0,n-1);

  }

 

void in3(int *A,int n)

  {

    int i;

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

      printf("%5d ",A[i]);

  }

void nhanh()

  {

    clrscr();

    int *A,n;

    printf("\n SAP XAP NHANH\n");

    printf("\n So phan tu n=");

    scanf("%d",&n);

    A=(int*)malloc(n*sizeof(int));

    printf("\n\n Tao lap day so:\n");

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

      {

        printf("\n A[%d]=",i);

        scanf("%d",&A[i]);

      }

    quick(A,n);

    printf("\n\n");

    printf("Ket qua thu duoc la:\n\n");

    in3(A,n);

    getch();

    free(A);

  }

//=======================================

void in4(int *A,int n)

  {

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

      printf("%5d",A[i]);

  }

void merge(int *A,int n)

  {

    int i,k,j,low1,up1,low2,up2,size;

    int *ds;

    size=1;

    ds=(int*)malloc(n*sizeof(int));

    while(size<n)

      {

        low1=0;

        k=0 ;

        while(low1+size<n)

          {

            low2=low1+size;

            up1=low2-1;

            if(low2+size-1<n)

              up2=low2+size-1;

            else

              up2=n-1;

            for(i=low1,j=low2;i<=up1 && j<=up2;k++)

              {

               if(A[i]<=A[j])

                 ds[k]=A[i++];

               else

                 ds[k]=A[j++];

              }

            for(;i<up1;k++)

              ds[k]=A[i++];

            for(;j<up2;k++)

              ds[k]=A[j++];

            low1=up2+1;

          }

        for(i=low1;k<n;i++)

          ds[k++]=A[i];

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

          A[i]=ds[i];

        size*=2;

      }

    printf("\n \n Ket qua thu duoc la:\n\n");

    in4(A,n);

    free(ds);

  }

void hoanhap()

  {

    clrscr();

    int *A,n,i;

    printf("\n \t SAP XEP KIEU HOA NHAP\n");

    printf("\n So phan tu n=");

    scanf("%d",&n);

    A=(int*)malloc(n*sizeof(int));

    printf("\n Tao lap day so:\n");

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

      {

        printf("\nA[%2d]=",i);

        scanf("%d",&A[i]);

      }

    merge(A,n);

    printf("\n");

    getch();

    free(A);

  }

 

//=======================================

void in5(int *A,int n)

  {

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

      printf("%5d",A[i]);

  }

void shaker(int *A,int n)

  {

    int i,j,temp,tdoi;

    do

      {

        tdoi=0;

        for(i=n-1;i>0;i--)

          {

            if(A[i-1]>A[i])

              {

               temp=A[i-1];

               A[i-1]=A[i];

               A[i]=temp;

               tdoi=1;

              }

          }

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

          {

            if(A[j-1]>A[j])

              {

               temp=A[j-1];

               A[j-1]=A[j];

               A[j]=temp;

               tdoi=1;

              }

          }

 

      }while(tdoi);

    printf("\n\n Ket qua la :",tdoi);

    in5(A,n);

  }

void shaker()

  {

    clrscr();

    int *A,n,i;

    printf("\n \tSHAKER_SORT\n");

    printf("\n So phan tu n=");

    scanf("%d",&n);

    A=(int*)malloc(n*sizeof(int));

    printf("\n \n Tao lap day so:\n");

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

      {

        printf("\n A[%2d]=",i);

        scanf("%d",&A[i]);

      }

    shaker(A,n);

    getch();

    free(A);

  }

 

 

//=======================================

void main()

  {

    while(1) {

    clrscr();

    int key;

    printf("\n\tSAP XEP VA TIM KIEM\n");

    printf("\n 1.Selection_sort\n");;

    printf("\n 2.Bubble_sort\n");

    printf("\n 3.Insertion_sort\n");

    printf("\n 4.Quick_sort\n");

    printf("\n 5.Merge_sort\n");

    printf("\n 6.Shaker_sort\n");

    printf("\n 0.Tro ve");

    printf("\nBam mot phim de chon chuc nang:");

    scanf("%d",&key);

    if(key==0) break;

  

    switch(key)

      {

        case 1:

               clrscr();

               luachon();

               printf("\n\n\tAn phim bat ky de tro lai menu chinh");

               getch();

               clrscr();

               break;

        case 2:

               clrscr();

               suibot();

               printf("\n\n\tAn phim bat ky de tro lai menu chinh");

               getch();

               clrscr();

               break;

        case 3:

               clrscr();

               tructiep();

               printf("\n");

               printf("\nAn phim bat ky de tro lai menu chinh");

               getch();

               clrscr();

               break;

        case 4:

               clrscr();

               nhanh();

               printf("\n");

               printf("\nAn phim bat ky de tro lai menu chinh");

               getch();

               clrscr();

               break;

        case 5:

               clrscr();

               hoanhap();

               printf("\n");

               printf("\nAn phim bat ky de tro lai menu chinh");

               getch();

               clrscr();

               break;

        case 6:

               clrscr();

               shaker();

               printf("\n");

               printf("\nAn phim bat ky de tro lai menu chinh");

               getch();

               clrscr();

               break;

 

      }

     }

   // getch();

  }

 

2:Một ví dụ về Đa hình

#include <iostream.h>
#include <conio.h>
#include <math.h>
class hinhve
{
   public:
       
virtual float dientich() = 0;
        
virtual char *ten() = 0;
        
virtual void in()=0;
};

class 
haichieu : public hinhve
{
    public:
       
virtual float chuvi() = 0;
      
void in()
      {
           
cout<<"ten cua hinh: "<<ten()
             <<
" ,dien tich la: "<<dientich()
             <<
" ,chu vi la: "<<chuvi()<<endl;
      }
};

class 
bachieu : public hinhve
{
  public:
      
virtual float thetich() = 0;
      
void in()
      {
           
cout<<"ten cua hinh: "<<ten()
             <<
" ,dien tich la: "<<dientich()
             <<
" ,the tich la: "<<thetich()<<endl;
      }
};


class 
hinhtron : public haichieu
{
      private:
       
float r;
   public:
       
hinhtron() { 0;}
       
hinhtron(float bk) {bk;}
      
float chuvi()
      {
            return 
2*3.14*r;
      }
      
float dientich()
      {
             return 
3.14*r*r;
      }
      
char *ten()
      {
             return 
"Hinh Tron";
      }

};


class 
hinhvuong : public haichieu
{
      private:
       
float a;
   public:
       
hinhvuong(float x)
      {
               
x;
      }
      
float chuvi()
      {
          return 
a*4;
      }
      
float dientich()
      {
              return 
a*a;
      }
      
char *ten()
      {
             return 
"Hinh Vuong";
      }
};

class 
tgdeu : public haichieu
{
     private:
       
float a;
   public:
       
tgdeu(float x) : a(x){}
      
float chuvi()
      {
              return 
3*a;
      }
      
float dientich()
      {
           return 
a*a*sqrt(3)/2;
      }
      
char *ten()
      {
              return 
"Hinh tam giac deu";
      }
};
class 
cau: public bachieu
{
  private:
      
float r;
   public:
      
cau(float bk): r(bk){}
      
float thetich() { return r*r*r*3.14;}
      
float dientich() { return 4*3.14*r*r; }
      
char *ten()
      {
           return 
"Hinh Cau";
      }
};

class 
lapphuong : public bachieu
{
      private:
       
float a;
   public:
       
lapphuong(float x) : a(x) {}
       
float thetich() { return a*a*a; }
       
float dientich() { return 6*a*a; }
       
char ten() { return "Hinh Lap Phuong"; }
};

void main()
{
   
hinhve *p;
   
= new hinhtron(3);
   
p->in();
   
delete p;
   
= new lapphuong(3);
   
-> in();
   
delete p;
   
= new cau(3);
   
-> in();
   
delete p;
   
= new tgdeu(5);
   
-> in();
   
delete p;
   
= new hinhvuong(6);
   
-> in();
   
getch();

3:Tiếp một ví dụ về Đa hình

#include <iostream.h>
#include <conio.h>
#include <math.h>

class Point
{
      private:
       
int xint y;
   public:
       
Point()
      {
            
00;
      }
       
Point(int a,int b)
      {
            
ab;
      }
      
virtual void set(int a,int b)
      {
            
ab;
      }
      
float gettung()
      {
            return 
y;
      }
      
float gethoanh()
      {
            return 
x;
      }
      
float kc(Point t)
      {
             return ((
t.x)*(t.x) + (t.y)*(t.y));
      }
      
virtual void in()
      {
            
cout<<"\n("<<x<<";"<<y<<")";
      }
};
class 
Cpoint : protected Point
{
    private:
       
int mau;
   public:
      
Cpoint() : Point()
      {
             
mau 0;
      }
       
Cpoint(int a,int b,int mau_) : Point(a,b)
      {
             
mau mau_;
      }
      
void set(int a,int b,int mau_)
      {
            
Point::set(a,b);
          
mau mau_;
      }
      
void in()
      {
             
Point::in();
               
cout<<" Co mau la "<<mau;
      }
} ;

void main()
{
    
Point *p;
   
= new Cpoint(3,5,6);
   
p->in();
   
delete p;
   
= new Point(5,2);
   
p->in();
   
getch();
}


 

4:Tổng hai ma trận

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
void congmt(float a[][10],float b[][10],float c[][10],int hang,int cot);
void nhapmt(float a[][10],int hang,int cot);
void inmt(float a[][10],int hang,int cot);
void main()
{
    
system("color 3e");
    
float a[10][10],b[10][10],c[10][10];
    
int hang1,cot1;

   
cout<<"Moi ban nhap vao ma tran a: \n";
   
cout<<"Nhap vao so hang cua ma tran a: ";
   
cin>>hang1;
   
cout<<"Nhap vao so cot cua ma tran a: ";
   
cin>>cot1;
   
nhapmt(a,hang1,cot1);
   
inmt(a,hang1,cot1);

   
int hang2,cot2;
   
cout<<"Moi ban nhap vao ma tran b: \n";
   do
   {
       
cout<<"Nhap vao so hang cua ma tran b: ";
       
cin>>hang2;
   }while(
hang2 != hang1);
   do
   {
       
cout<<"Nhap vao so cot cua ma tran b: ";
       
cin>>cot2;
   }while(
cot2 != cot1);
   
nhapmt(b,hang2,cot2);
   
inmt(b,hang2,cot2);

   
cout<<"\nVay tong cua hai ma tran a,b la: \n";
   
congmt(a,b,c,hang1,cot1);
   
inmt(c,hang1,cot1);
   
getch();
}

void congmt(float a[][10],float b[][10],float c[][10],int hang,int cot)
{
     for (
int i=0i<hangi++)
        for (
int j=0j<cotj++)
          
c[i][j] = a[i][j] + b[i][j];
}

void nhapmt(float a[][10],int hang,int cot)
{
     for(
int i 0;hang;i++)
   {
       for(
int j 0cotj++)
      {
           
cout<<"Nhap vao phan tu ["<<i<<";"<<j<<"]: ";
         
cin>>a[i][j];
      }
   }
}

void inmt(float a[][10],int hang,int cot)
{
     for(
int i 0hangi++)
   {
        for(
int j 0cotj++)
      {
           
cout<<a[i][j]<<"\t";
      }
      
cout<<endl;
   }

5:Một ví dụ về sử dụng template và quá tải toán tử Nhập xuất

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
class sv
{
    private :
       
char ten[100];
      
float Diem;
   public:
       
sv()
      {
          
Diem=0;
      }
      
sv(char a[],float D)
      {
            
strcpy(ten,a);
         
Diem=D;
      }
      
sv(sv&a)
      {
          
Diem a.Diem;
              
strcpy(ten,a.ten);
      }
      
void set_sv(char a[],float D)
      {
            
strcpy(ten,a);
         
Diem=D;
      }
      
float get_diem()const
      {
          return 
Diem;
      }
      
charget_ten()
      {
          return 
ten;
      }
      
friend ostream&operator <<(ostream&out,sv&);
      
friend istream&operator>>(istream&in,sv&);
      
operator float()
      {
          return 
float(Diem);
      }


};
ostream&operator <<(ostream&out,sv&a)
{
     
cout<<"\n\n\t\t\tTen "<<a.ten<<endl;
   
cout<<"\t\t\tDiem "<<a.Diem<<endl;
}
istream&operator>>(istream&in,sv&a)
{
    
cout<<"\t\t\tNhap ten ";
   
cin.ignore();
   
cin.getline(a.ten,50);
   
cout<<"\t\t\tNhap diem ";
   
cin>>a.Diem;

}
int ucln(int a,int b)
{
    
int r;
   while(
b)
   {
       
a%b;
      
b;
      
b=r;
   }
   return 
a;
}

class 
phanso
{
    private:
       
float tu,mau;
   public:
       
phanso(float a=1,float b=1)
      {
          if(
b)
         {
             
tu a;
            
mau b;
         }
         else
         {
             
tu =1;
            
mau=1;
         }
      }
      
void set_phanso(float a,float b)
      {
          
tu =a;
         
mau b;
      }
      
void nhap()
      {
          
cout<<"\t\t\tNhap du lieu cho phan so "<<endl;
         
cout<<"\t\t\tTu ";
         
cin>>tu;
         
cout<<"\t\t\tMau ";
         
cin>>mau;
         
toigian();
      }
      
void toigian()
      {
          
int t=ucln(tu,mau);
         
tu tu/t;
         
mau mau/t;
      }
      
operator float()
      {
          return 
float(tu/mau);
      }
        
friend ostream&operator <<(ostream&out,phanso&a);
      
friend istream&operator >>(istream&in,phanso&a);

};
ostream&operator<<(ostream&out,phanso&a)
{
    
out<<a.tu<<"/"<<a.mau<<"->";
}
istream&operator >>(istream&in,phanso&a)
{
    
cout<<"\t\tTu ";
   
cin>>a.tu;
   
cout<<"\t\tMau ";
   
cin>>a.mau;
}
template <class T,int n>
class 
set
{
    private:
       
T data[n];
      
int spt;
   public:
       
set()
      {
          
spt=0;
      }
        
set(const set&a)
      {
          for(
int i=0;i<a.spt;i++)
             
data[i]=a.data[i];
            
spt a.spt;
      }
      
void them(T&a);
      
bool search(T&a);
      
friend ostreamoperator<<(ostream&out,set<T,n>&a);
      
friend set operator +(set&a,set&b);
      
friend set operator *(set&a,set&b);
      
friend set operator -(set&a,set&b);
      
set operator =(const set&b)
      {
              for(
int i=0;i<b.spt;i++)
                   
data[i]=b.data[i];
              
spt=b.spt;
            return (*
this);
      }

};
template <class T,int n>
void set<T,n>::them(T&a)
{
    if(
spt<n)
       
data[spt++]=a;
   else
       
cout<<"\t\tMang da day rui khong them duoc nua dau "<<endl;
}
template <class T,int n>
bool set<T,n>::search(T&a)
{
    for(
int i=0;i<spt;i++)
       if(
data[i]==a)
          return 
true;
        return 
false;
}
template <class T,int n>
ostream&operator<<(ostream&out,set<T,n>&a)
{
   if(
a.spt==0)
       
out<<" rong "<<endl;
    for(
int i=0;i<a.spt;i++)
   {
       
out<<a.data[i];
          if(
i<a.spt-1)
          
cout<<"->";
   }
}
template <class T,int n>
set<T,noperator +(set<T,n>&a,set<T,n>&b)
{
    
set<T,nr(a);

     for(
int i=0;i<b.spt;i++)
          if(!
a.search(b.data[i]))
                
r.them(b.data[i]);



   return 
r;
}
template <class T,int n>
set<T,noperator -(set<T,n>&a,set<T,n>&b)
{
    
set<T,nr;
    for(
int i=0;i<a.spt;i++)
       if(!
b.search(a.data[i]))
          
r.them(a.data[i]);
    return 
r;
}
template <class T,int n>
set<T,noperator *(set<T,n>&a,set<T,n>&b)
{
    
set<T,nr;

    for(
int i=0;i<a.spt;i++)
       if(
b.search(a.data[i]))

          
r.them(a.data[i]);

         return 
r;
}
void main()
{
      
set<float,100a;
   
set<float,100c;
   
set<float,100d;
   
set<float,100e;
   
set<float,100f;
   
set<sv,100g;
   
set<phanso,100b;
   
int n,m,l;
   
float r;
   
sv A;
   
phanso s;
   
cout<<"\t\t\tNhap so luong cac so thu ";
   
cin>>n;
   for(
int i=0;i<n;i++)
   {
       
cout<<" nhap so thu "<<(i+1)<<":";
      
cin>>r;
      
a.them(r);
   }
clrscr();
   
cout<<"\t\t\tNhap so luong phan so ";
   
cin>>m;
   for(
int i=0;i<m;i++)
   {
       
cout<<"\t\t\tNhap phan so thu "<<(i+1)<<endl;
      
cin>>s;
      
b.them(s);
      
c.them(s);clrscr();
   }

   
clrscr();
   
cout<<"\t\t\tNhap so luong cac sinh vien ";
   
cin>>l;
   for(
int i=0;i<l;i++)
    {
       
cout<<"\t\t\tNhap du lieu cho sinh vien thu "<<(i+1)<<endl;
      
cin>>A;
      
g.them(A);
      
clrscr();
   }
   
clrscr();
   
textcolor(YELLOW+RED);
   
cprintf("%s","\t\t\tchuong trinh da gan cac so 1 cach tu dong ta duoc ");
   
cout<<"\n\nday so thuc vua nhap  "<<endl;
   
cout<<a;
   
cout<<"\n\nday phan so vua nhap  "<<endl;
      
cout<<b;
   
cout<<"\n\tDay sinh vien vua nhap "<<endl;
   
cout<<g;
   
getch();clrscr();
   
a+c;
   
cout<<"\n\n hop cua hai tap hop phan so va so thuc  la "<<endl;;
   
cout<<d;
   
e=a*c;
   
cout<<"\n\n giao cua hai tap so thuc va phan so  la "<<endl;
   
cout<<e;
   
cout<<"\n\nhieu cua hai tap so thuc va phan so la "<<endl;
   
f=a-c;
   
cout<<f;

    
getch();

6:Ví dụ về quá tải toán tử

#include <iostream.h>
#include <conio.h>
#include <math.h>
class PS
{
    public:
        
long tu,mau;

        
PS()
        {
            
tu=0;
            
mau=0;
        }
        ~
PS(){};
      
int uscln(long a,long b);
      
void rutgon();
      
void nhap();
      
void  xuat();
      
PS operator+(PS &a);
      
PS operator-(PS &a);
      
PS operator*(PS &a);
      
PS operator/(PS &a);

};
    
int PS::uscln(long a,long b)
    {
        if(
a!=&& b!=0)
        {
            
a=abs(a);
            
b=abs(b);
            while(
a!=b)
            {
                if(
a>b)
                    
a=a-b;
                else
                    
b=b-a;
            }
            return 
a;
        }
        else
            return 
1;
    }

   
void PS::rutgon()
    {
        
int u;
            
u=uscln(tu,mau);
            
tu=tu/u;
            
mau=mau/u;
    }

    
void PS::nhap()
    {
        
Nhap:
        
cout<<"Nhap tu so ";
        
cin>>tu;
        
cout<<"Nhap mau so ";
        
cin>>mau;
        if(
mau==0)
        {
            
cout<<"Mau phai khac 0"<<endl;
            goto 
Nhap;
        }
    }

    
void PS::xuat()
    {
            
rutgon();
        if(
mau<0)
        {
mau=-mautu=-tu;}
        if(
tu==0)
            
cout<<"0"<<endl;
        else
            if(
mau==1)
                
cout<<tu<<endl;
            else
                
cout<<tu<<"/"<<mau<<endl;
    }

    
PS PS::operator+(PS &a)
    {
        
a.tu=tu*a.mau+mau*a.tu;
        
a.mau=mau*a.mau;
        return 
a;
    }
    
PS PS::operator-(PS &a)
    {

        
a.tu=tu*a.mau-mau*a.tu;
        
a.mau=mau*a.mau;
        return 
a;
    }
    
PS PS::operator*(PS &a)
    {
        
a.tu=tu*a.tu;
        
a.mau=mau*a.mau;
        return 
a;
    }
    
PS PS::operator/(PS &a)
    {
        
a.tu=tu*a.mau;
        
a.mau=mau*a.tu;
        return 
a;
    } 

7:Đếm số lần xuất hiện của các ký tự trong chuỗi

#include <stdio.h>
#include <ctype.h>
 
void main()
{
  char chuoi[80];
  int i = 0, count = 0;
 
  printf("\nNhap vao mot chuoi bat ky : ");
  gets(chuoi);
 
  while (chuoi[i] != 0)
  {
    if (isalpha(chuoi[i++]))
      count++;
  }
 
  printf("So ky tu trong chuoi = %d", count);
  getch();
}

8:Bài toán Ancarokhi

#include <stdio.h>
 
void main()
{
  int dai, rong;
 
  printf("\nBai toan Ancarokhi : Tim dien tich hinh chu nhat co chieu dai gap hai");
  printf("\nchieu rong va dien tich = chu vi");
  for (dai = 1; dai < 100; dai ++)
    for (rong=1; rong < 100; rong++)
      if (dai == 2 * rong && (dai + rong)*2 == dai*rong)
         printf("\nDai = %d; Rong = %d", dai, rong);
  getch();
}

 

9:Chứng minh đẳng thức An Casi

#include <stdio.h>
 
unsigned long vetrai(unsigned long n)
{
  unsigned long tmp=0, i;
  for (i=1; i<=n; i++)
    tmp += i*i*i*i;
  return tmp;
}
 
unsigned long vephai(unsigned long n)
{
  unsigned long tmp;
  tmp = (long)6*n*n*n*n*n + (long)15*n*n*n*n + (long)10*n*n*n - n;
  tmp = tmp / (long)30 ;
  return tmp;
}
 
void main()
{
  unsigned long tong1, tong2, n;
 
  for (n=1; n<=50; n++)
  {
    tong1 = vetrai(n);
    tong2 = vephai(n);
    if (tong1 == tong2)
    {
      printf("\nSo %d thoa man dang thuc An Casi. ", n);
      printf("Tong1 = %ld - Tong2 = %ld", tong1, tong2);
    }
    else
    {
      printf("\nSo %d khong thoa man dang thuc An Casi. ", n);
      printf("Tong1 = %ld - Tong2 = %ld", tong1, tong2);
    }
  }
  getch();
}

 

10:Hiện bảng mã ASCII

#include <stdio.h>
#include <conio.h>
 
void main()
{
  int i, j;
 
  clrscr();
  printf("    ");
  for (j=0; j<16; j++)
    printf("%3d", j);
  for (i=2; i<16; i++)
    for (j=0; j<16; j++)
    {
       if (j == 0)
         printf("\n%2d  ", i);
       printf("  %c", i*16+j);
    }
  getch();
}

 

11:In ra năm âm lịch tương ứng với năm nhập vào.

#include <stdio.h>
#include <conio.h>
 
void main()
{
  unsigned nam;
  char can[][5] = {"Giap", "At", "Binh", "Dinh", "Mau", "Ky",
                   "Canh", "Tan", "Nham", "Quy"};
  char chi[][5] = {"Ty", "Suu", "Dan", "Meo", "Thin", "Ty",
                   "Ngo", "Mao", "Than", "Dau", "Tuat", "Hoi"};
 
  printf("\nNhap nam can biet : ");
  scanf("%d", &nam);
  printf("Nam am lich cua %d la %s %s", nam, can[(nam+6)%10], chi[(nam+8)%12]);
  getch();
}

 

12:In ra bảng cửu chương

#include <stdio.h>
#include <conio.h>
#include <string.h>
 
void main()
{
  int i, j;
  char chuoi[] = "B A N G   C U U   C H U O N G";
  char ten[10][5] = {"","","Hai", "Ba", "Bon", "Nam",
                     "Sau", "Bay", "Tam", "Chin"};
  clrscr();
  textcolor(YELLOW);
  gotoxy((80 - strlen(chuoi)) / 2, 1);
  cprintf("%s\n\n", chuoi);
 
  for (i=2; i<=9; i++)
  {
    gotoxy(10*(i-2) + (10 - strlen(ten[i]))/2, 4);
    textcolor(i);
    cprintf("%s", ten[i]);
  }
  for (j=1; j<=10; j++)
    for (i=2; i<=9; i++)
    {
      gotoxy(10*(i-2) + 1, j+4);
      textcolor(i);
      cprintf("%dx%2d = %2d", i, j, i*j);
    }
  getch();
}

 

13:Nhập chuỗi và in chuỗi

#include <stdio.h>
#include <conio.h>
 
void main()
{
   char name[80];
 
   printf("\nXin cho biet ten cua ban : ");
   gets(name);
 
   printf("Chao %s\n", name);
   getch();
}

 

14:Giải hệ phương trình bậc nhất.

#include <stdio.h>
#include <conio.h>
 
void main()
{
  int a, b, c, d, e, f, dthuc;
  float x, y;
 
  printf("\nNhap vao cac he so a,b,c,d,e,f : ");
  scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
  dthuc = b*d - e*a;
  if (dthuc != 0)
  {
    y = (float)(c*d-a*f)/dthuc;
    x = (float)(b*f-c*e)/dthuc;
    printf("Nghiem x = %f, y = %f", x, y);
  }
  else
    printf("\nHe phuong trinh vo ngiem.");
 
  getch();
}

 

15:Tính thứ của ngày

#include <stdio.h>
#include <conio.h>
 
struct date
{
   int     month;
   int    day;
   int    year;
} date_1;
 
long int funct1 (int y,int m)
{
  long int result;
     if ( m <= 2 )
       y -= 1;
     result = y;
     return (result);
}
 
long int funct2 (int m)
    {
     long int result;
     if ( m <= 2 )
       result = m + 13;
     else
       result = m + 1;
       return(result);
    }
 
long int day_count (int m, int d, int y)
    {
     long int number;
     number = 1461 *  funct1(y,m) / 4 + 153 * funct2(m) / 5 + d;
 
     return (number);
    }
 
void main ()
{
    long int number_of_days1;
    int day_of_week;
 
    printf ("Nhap vao mot ngay (dd mm yyyy), vd  12 03 1999  \n");
    scanf ("%d %d %d", &date_1.day, &date_1.month, &date_1.year);
    number_of_days1 = day_count (date_1.month, date_1.day, date_1.year);
    printf ("\nNgay la : " );
 
    day_of_week = (number_of_days1 - 621049) % 7;
    switch (day_of_week)
      {
        case 0 :
            printf ("Chu Nhat,");
            break;
        case 1 :
            printf ("Thu Hai,");
            break;
        case 2 :
            printf ("Thu Ba,");
            break;
        case 3 :
            printf ("Thu Tu,");
            break;
        case 4 :
            printf ("Thu Nam,");
            break;
        case 5 :
            printf ("Thu Sau,");
            break;
        case 6 :
            printf ("Thu Bay,");
            break;
      }
  getch();
}

Bạn có đam mê ngành thiết kế vi mạch và bạn muốn có mức lương 1000 usd cùng lúc bạn

đang muốn tìm một Trung tâm để học vậy hãy đến với ngành vi mạch tại SEMICON

  HotLine: 0972 800 931 Ms Duyên

 

 

 

 

Last Updated ( Monday, 05 October 2015 14:14 )  

Related Articles

Chat Zalo