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.
semicon_lab.jpg

Tổng hợp bài tập và bài giải C/C++ (P2) - tong hop bai tap va bai giai

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

 

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