You are on page 1of 15

# Ho ̣ và tên : Phí Hoàng Đức

Lớp : ĐT03-K60

MSSV : 20151060

## Bài tâ ̣p thư ̣c hành

MODUL 1

Program 1.1

#include <iostream>
using namespace std;
int main()
{
char a;
cout<<"nhap mot ki tu\t";
cin >> a;
cout<<"Ki tu la "<< a <<"\nMa ASSI la "<< dec << (int)a<<"\n";
cout<< hex << "He co so 16 la 0x" << (int)a << "\n";
cout<< oct <<"He co so 8 la 0"<<(int)a << "\n";
system("pause");
}

Program 1.2

#include <iostream>
using namespace std;
int main()
{
double f,C,pi;
pi=3.14159;
cout<<"Nhap f=";
cin>> f;
cout<<"\n";
cout<<"Nhap C=";
cin>>C;
cout<<"\n";
cout<<"Xc="<<1/(2*pi*f*C);

1
system("pause");
}

Program 1.3

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
double R1,R2,R3;
char cach_mac[20];
cout<<"R1=";
cin>>R1;
cout<<"R2=";
cin>>R2;
cout<<"R3=";
cin>>R3;
cin.ignore();
cout<<"Cach mac 3 dien tro la: ";
cin.get(cach_mac, 20);
if (strcmp(cach_mac, "noi tiep") == 0)
cout<<"Rt= "<<R1+R2+R3;
else
cout<<"Rt= "<<(1/(1/R1+1/R2+1/R3));
system("pause");
}

2
MODUL 2

Program 2.1

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
double R[20],Rt,Rn;
int i,n;
char cach_mac[20];
Rt=0;
Rn=0;
cout<<"so dien tro trong mach la\t";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"R["<<i<<"]=";
cin>>R[i];
Rt=Rt+R[i];
Rn=Rn+1/R[i];
}
cin.ignore();//bo qua dau trang khong can thiet khi nhap vào
cout<<"Cach mac"<<i-1<<" dien tro la: ";
cin.get(cach_mac, 20);
if (strcmp(cach_mac, "noi tiep") == 0)
cout<<"Rt= "<<Rt;
else cout<<"Rt= "<<1/Rn;
system("pause");
}

3
Program 2.2
#include<iostream>
#include<string.h>
using namespace std;

int main()
{
char qua[200];
int i,l,k,t=1;
k=0;
cout<<"Ban dang nghi gi\n";
cin.get(qua, 200);
l=strlen(qua);

if(qua[0]==32)
{
cout<<"Dau cach dau cau\n";
t=0;
}

if(qua[l-1]==32)
{
cout<<"Dau cach cuoi cau\n";
t=0;
}

for(i=0;i<l;i++)
{
if(qua[i]==32&& qua[i+1]==32)
{
cout<<"hai dau cach lien nhau\n";
t=0;
break;
}
}

if (t==1)
{
for (i = 0; i<l; i++)
if (qua[i]==32)
k++;
cout << "so tu la: " << k+1 << endl;
}
system("pause");
}

4
Program 2.3

#include<iostream>
using namespace std;
int main()
{
double a[10][10],h[100],k[100];
int i,j,n,m,S;
cout<<"nhap m=";
cin>> m;
cout<<"nhap n=";
cin>>n;
if(n>10||n<0||m>10||m<0)
cout<<"khong dung quy cach\n";
else
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"Nhap a["<<i+1<<"]["<<j+1<<"]=";
cin>> a[i][j];
}
}
for(i=0;i<m;i++)
{
h[i]=0;
for(j=0;j<n;j++)
{
h[i]=h[i]+a[i][j];
}
cout<<"tong hang "<<i+1<<" la "<<h[i]<<"\n";
}
for(j=0;j<n;j++)
{

5
k[j]=0;
for(i=0;i<m;i++)
{
k[j]=k[j]+a[i][j];
}
cout<<"tong cot "<<j+1<<" la "<<k[j]<<"\n";
}
S=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
S=S+a[i][j];
}
}
cout<<"Tong ma tran la "<<S;
}
system("pause");
}

6
Bài 1

#include<iostream>
using namespace std;
int Factorial(int n)
{
if(n<2)
return 1;
else
{
return n*Factorial(n-1);
}
}
int main()
{
int n;
cout<<"Nhap n= ";
cin>> n;
cout<<n<<"!="<<Factorial(n)<<"\n";
system("pause");
}

Bài 2
#include<iostream>
using namespace std;

## double Pow(double x, int n)

{
if (n==0)
{
return 1;
}
else if (n==1) {
return x;
} else return x*Pow(x, n-1);
}

int main()
{
int n;
double x;
cout<<"Nhap x=";
cin>> x;
cout<<"Nhap n=";
cin>>n;
cout <<"x^n="<<Pow(x, n)<<"\n";
system("pause");}

7
Bài 3
#include<iostream>
using namespace std;
int USCLN (int a,int b)
{
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
}
int main()
{
int a,b;
cout<<"Nhap a=";
cin>>a;
cout<<"Nhap b=";
cin>>b;
if(a==0 || b==0)
cout<<"Khong co uoc\n";
else
cout<<"USCLN="<<USCLN(a,b)<<"\n";
system("pause");
}

Bài 4
#include<iostream>
#include<math.h>
using namespace std;
double Pow(double x, int n)
{
if (n==0)
{
return 1;
}
else if (n==1) {
return x;
} else return x*Pow(x, n-1);
}

## double Value(double a[],int n,double x)

{
int i;

8
double f=0;
for(i=0;i<=n;i++)
{
f=a[i]*Pow(x,i)+f;
}
return f;
}
int main()
{
int i,n,x;
double a[10];
cout<<"Nhap n=";
cin>> n;
cout<<"Nhap x=";
cin>> x;
for(i=0;i<=n;i++)
{
cout<<"Nhap a["<<i<<"]=";
cin>> a[i];
}
cout<<"f(x)="<<Value(a,n,x);
system("pause");
}

1.1

## Program 1.1: Số phức

Code:

#include <iostream>

class Complex{

private:

double re,im;

public:

## Complex(double r = 0, double i = 0): re(r), im(i) {}

9
// Ham tao copy tu 1 so phuc

public:

public:

## friend ostream& operator<<(ostream& out,const Complex& C)

return out << '(' << C.re << ", " << C.im << "i) ";

};

## Complex Complex::operator / (Complex C){

double m=C.re*C.re+C.im*C.im;

return Complex((this->re*C.re+this->im*C.im)/m,(-this->re*C.im+this->im*C.re)/m);

10
}

int main(){

Complex y(6.0,1.6);

Complex z(1.2,-6.0);

double a=2.0;

system("PAUSE");

## Program 1.2: Xâu kí tự

Code:

#include<iostream>

#include<cstring>

class String{

private:

int length;

char *data;

11
private:

public:

## String(const char* s){

length=strlen(s);

strcpy(data,s);

// ham huy

public:

public:

## String& operator = (const String &s);

public:

12
// toan tu cong thuc hien them ki tu c vao cuoi String

public:

};

## String& String:: operator = (const String &s){

delete[] this->data;

length = s.length;

## this->data = new char[length+1];

strcpy(this->data,s.data);

return (*this);

## String String:: operator + (char c){

13
int length=this->length + 1;

## char *data = new char[length+1];

strcpy(data, this->data);

data[this->length] = c;

data[length]='\0';

return String(length,data);

## String String:: operator + (String s){

int length=this->length+s.length;

## char *data = new char[length+1];

strcpy(data,this->data);

for(int i=0;i<s.length;i++){

data[this->length+i]=s[i];

data[length]='\0';

return String(length,data);

## String::String(const String& s){

this->length=s.length;

## this->data= new char[this->length+1];

strcpy(this->data,s.data);

## // ham so sanh 2 String

14
int String::Compare(String s){

return strcmp(this->data,s.data);

## bool String::operator == (String s){

return !strcmp(this->data,s.data);

## bool String::operator != (String s){

return strcmp(this->data,s.data);

int main(){

## String s="PhiHoangDuc ";

cout<<s+'S'<<endl;

cout<<s+"PhiHoangDuc "<<endl;

system("PAUSE");

15