You are on page 1of 15

Ho ̣ và tên : Phí Hoàng Đức

Lớp : ĐT03-K60

MSSV : 20151060

Bài tâ ̣p thư ̣c hà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");
}

Bài tập nâng cao

1.1

Program 1.1: Số phức


Code:

#include <iostream>

using namespace std;

class Complex{

private:

double re,im;

public:

// Ham tao thiet lap tu 2 tham so

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

9
// Ham tao copy tu 1 so phuc

Complex(const Complex& C): re(C.re), im(C.im) {}

public:

Complex operator + (Complex C); // phep cong

Complex operator - (Complex C); // phep tru

Complex operator * (Complex C); // phep nhan

Complex operator / (Complex C); // phep chia

public:

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

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

};

Complex Complex::operator + (Complex C){

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

Complex Complex::operator - (Complex C){

return Complex(this->re - C.re, this->im - C.im);

Complex Complex::operator * (Complex C){

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

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;

cout<<y<<" + "<<z<<" + "<<a<<" = "<<y+z+a<<endl;

cout<<y<<" - "<<z<<" = "<<y-z<<endl;

cout<<y<<" * "<<z<<" = "<<y*z<<endl;

cout<<y<<" / "<<z<<" = "<<y/z<<endl;

system("PAUSE");

Program 1.2: Xâu kí tự


Code:

#include<iostream>

#include<cstring>

using namespace std;

class String{

private:

int length;

char *data;

11
private:

// Ham tao String tu mang ki tu voi do dai cho truoc

String(int length,char *data):length(length), data(data) {}

public:

// ham tao mac dinh xau rong

String(): length(0), data(new char[1]) {data[0]='\0';}

// ham tao String tu 1 hang xau ki tu

String(const char* s){

length=strlen(s);

data = new char[length+1];

strcpy(data,s);

// ham tao String copy tu 1 String da co

String (const String& s);

// ham huy

~String() {delete[] data;}

public:

// ham so sanh 2 String

int Compare(String s);

public:

// toan tu truy nhap ki tu tai vi tri index

char & operator[] (int index) {return data[index];}

//toan tu gan s vao String

String& operator = (const String &s);

public:

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

String operator + (char c);

// toan tu cong noi s vao cuoi String

String operator + (String s);

public:

// toan tu so sanh bang

bool operator == (String s);

// toan tu so sanh khac

bool operator != (String s);

// toan tu in String ra man hinh

friend ostream& operator <<(ostream& out,const String &s)

return out << s.data;

};

// toan tu gan 1 hang xau vao String s

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);

// toan tu cong them 1 hang ki tu vao String s

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);

// toan tu cong them 1 hang xau vao String s

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);

// ham tao String copy tu 1 String da co

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);

// toan tu so sanh bang

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

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

// toan tu so sanh khac

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

You might also like