0% found this document useful (0 votes)
71 views21 pages

NNLT Report

The document contains code for several C++ programs that demonstrate different programming concepts: 1. The first program accepts a character from the user and displays its ASCII and hexadecimal values. 2. The second program calculates the capacitive reactance of a capacitor given its frequency and capacitance. 3. The third program calculates the equivalent resistance of resistors connected in series or parallel by comparing the user's input to strings. 4. Additional programs demonstrate arrays, functions, classes, and other programming fundamentals.

Uploaded by

Hải Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views21 pages

NNLT Report

The document contains code for several C++ programs that demonstrate different programming concepts: 1. The first program accepts a character from the user and displays its ASCII and hexadecimal values. 2. The second program calculates the capacitive reactance of a capacitor given its frequency and capacitance. 3. The third program calculates the equivalent resistance of resistors connected in series or parallel by comparing the user's input to strings. 4. Additional programs demonstrate arrays, functions, classes, and other programming fundamentals.

Uploaded by

Hải Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Program 1.

1
#include<stdio.h>

int main(){

char ch; // Store input character

printf("Xin moi nhap vao ky tu: ");

scanf("%c",&ch);

printf("\nMa ASCII cua ky tu vua nhap la: %d",ch);

printf("\nMa co so 8 cua ky tu vua nhap la: %o",ch);

printf("\nMa co so 16 cua ky tu vua nhap la: %x",ch);

Program 1.2
#include<iostream>

#define PI 3.14

int main(){

double f,C;

std::cout<<"Xin moi nhap gia tri tan so: ";

std::cin>>f;
std::cout<<"\nXin moi nhap gia tri dien dung cua tu: ";

std::cin>>C;

std::cout<<"\nGia tri dien khang cua tu la: "<<1/(2*PI*f*C);

Program 1.3
#include<iostream>

int check(char ch1[], char ch2[2]){

for(int i = 0;ch1[i]; i++)

if(ch1[i] != ch2[i])

return 0;

return 1;

int main(){

double R1,R2,R3,Rtd;

char ch[10];

std::cout<<"Xin moi nhap vao gia tri dien tro R1 R2 R3:";

std::cin>>R1>>R2>>R3;
std::cout<<"\nXin moi nhap vao cach mac dien tro 'noi tiep' hoac 'song song'";

fflush(stdin);

std::cin.getline(ch,10);

if(check(ch,"noi tiep"))

std::cout<<"\n Gia tri dien tro tuong duong la: "<<R1+R2+R3;

if(check(ch,"song song"))

std::cout<<"\n Gia tri dien tro tuong duong la: "<<(R1*R2*R3)/(R1*R2 + R1*R3 +
R2*R3);

Program 2.1
#include<iostream>

double nt(double a[], int n){

double s = 0;

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

s += a[i];

return s;

}
double ss(double a[], int n){

double s = 0;

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

s += 1/a[i];

return 1/s;

int check(char ch1[], char ch2[2]){

for(int i = 0;ch1[i]; i++)

if(ch1[i] != ch2[i])

return 0;

return 1;

int main(){

int n;

double *a = new double[n];

char ch[10];

do{

std::cout<<"Nhap vao so dien tro";

std::cin>>n;

}while(n<0 || n>20);

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

std::cout<<"\nNhap vao gia tri dien tro (>0) thu "<<i+1 <<": ";

std::cin>>a[i];
if(a[i]<0){

printf("\nNhap sai moi nhap lai: ");

i--;

continue;

std::cout<<"\nNhap vao cach mac dien tro 'noi tiep' hoac 'song song': ";

fflush(stdin);

std::cin.getline(ch,10);

if(check(ch,"noi tiep"))

std::cout<<"\n Gia tri dien tro tuong duong la: "<<nt(a,n);

if(check(ch,"song song"))

std::cout<<"\n Gia tri dien tro tuong duong la: "<<ss(a,n);

Program 2.2
#include <iostream>
using namespace std;

int check(char ch){

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))

return 1;

if(ch == ' ' )

return 2;

if(ch == '.' || ch == '?' || ch == '!' || ch == ',')

return 3;

return 0;

int main(){

char s[201];

int count = 0;

int kt = 1;

cin.getline(s,200);

for(int i = 0; s[i]; i++){

if(!check(s[i])){

printf("Du lieu khong hop le\n");

kt = 0;

break;

if((i == 0 && check(s[i]) == 1) || (check(s[i]) == 1 && check(s[i-1]) == 2))

count++;
}

if(kt)

printf("\nCo %d tu",count);

Program 2.3
#include <iostream>

using namespace std;

int main(){

double A[9][9];

int M, N,i,j;

std::cout<<"Moi nhap so hang 0<M<10: ";cin>>M;

std::cout<<"Moi nhap so cot 0<N<10: ";cin>>N;

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

for (j=0;j<N;j++)

cout<<"Nhap phan tu cua hang"<< i+1<<" cot "<<j+1 << ": "; cin>>A[i][j];

}
int tonghang;

tonghang=0;

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

cout<<"Tong hang"<<i+1<<" la: ";

for(j=0;j<N;j++)

tonghang+=A[i][j];

cout<<tonghang<<endl;

tonghang=0;

for (j=0;j<M;j++)

cout<<"Tong cot"<<j+1<<" la: ";

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

tonghang+=A[i][j];

cout<<tonghang<<endl;

tonghang=0;

}
int tong=0;

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

for(j=0;j<N;j++)

tong+=A[i][j];

cout<<"Tong la: "<< tong;

Bài tập 1
#include <iostream>

using namespace std;

int Factorial(int N){

if(N == 1)

return 1;

return N*Factorial(N-1);

int main(){
int N;

cout << "Nhap vao N >= 0: ";

cin >> N;

cout <<"N! = "<<Factorial(N);

Bài tập 2
#include <iostream>

using namespace std;

double Pow(double x, int n){

if(n == 0)

return 1;

return

(n > 0) ? x*Pow(x,n-1) : 1/Pow(x,-n);

int main(){

double x;
int n;

cout << "Nhap vao gia tri x = "; cin >> x;

cout << "\nNhap vao so mu n = "; cin >> n;

cout << "\n" <<x << "^" << n << " = " << Pow(x,n);

Bài tập 3
#include <iostream>

using namespace std;

int USCLN(int a, int b){

if(a == b)

return a;

if(a < b)

return USCLN(a,b-a);

else

return USCLN(a-b,b);

int main(){
int a = 12, b = 6;

std::cout<<"USCLN cua "<< a << " va " << b << " la: " << USCLN(a,b);

Bài tập 4
#include <iostream>

#include <math.h>

using namespace std;

double Value(double a[], int n, double x0){

x0 = 1;

double s = 0;

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

x0 *= x0;

s += a[i]*x0;

return s;

}
int main(){

double a[10];

int n;

double x0;

cout << "Nhap vao bac cua da thuc: "; cin >> n;

cout << "\nNhap vao mang cac he so: ";

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

cout <<"\nNhap vao he so a" << i <<": "; cin >> a[i];

cout <<"\nNhap vao gia tri x0: "; cin >> x0;

cout <<"\nGia tri da thuc tai x0 la: " << Value(a,n,x0);

Lớp Complex
// Complex.cpp : Defines the entry point for the console application.

//

#include <iostream>
using namespace std;

class Complex

private:

double re,im;

public:

Complex(double r = 0, double i = 0)

: re(r), im(i)

Complex(Complex &C) // Copy constructor

:re(C.re), im(C.im)

Complex &operator + (const Complex c);

Complex &operator - (Complex c);

Complex &operator * (Complex c);

Complex &operator / (Complex c);

friend ostream & operator << (ostream & os, Complex &C){

return os << '(' << C.re << " + " << C.im <<"i) ";

};

Complex& Complex::operator+(const Complex c){


Complex z(this->re + c.re,this->im + c.im);

return z;

Complex& Complex::operator-(Complex c){

Complex z(this->re - c.re,this->im - c.im);

return z;

Complex& Complex::operator*(Complex c){

Complex z(this->re * c.re - this->im * c.im, this->re * c.im + this->im * c.re);

return z;

Complex& Complex::operator/(Complex c){

Complex a(c.re,-c.im);

double b;

b = c.re * c.re + c.im * c.im;

a = *this * a;

Complex z(a.re/b,a.im/b);

return z;

int main()

Complex y(1,2);
Complex z(2,3);

double a = 8;

std::cout << y << " + " << z << " = " << y + z << endl;

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

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

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

return 0;

Lớp String
#include <iostream>

#include <string.h>

using namespace std;

class String{

private:

int lenght;

char* data;
private:

String(char* data, int lenght )

: lenght(lenght), data(data)

{}

public:

String()

: lenght(0), data(new char[1])

data[0] = 0;

String(const char*s){

lenght = strlen(s);

data = new char[lenght+1];

strcpy(data,s);

String(String &s){

lenght = s.lenght;

data = new char[lenght+1];

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

data[i] = s[i];

data[lenght] = 0;

~String(){
delete []data;

/* int getLenght(){

return this->lenght;

*/

char* getData(){

return data;

public:

// So sanh 2 string

// tra ve -1 , 0 , 1 tuong ung voi String < , = , > string s

int Compare(String s){

for(int i = 0; i < this->lenght; i++)

if(data[i] != s[i])

return data[i] > s[i] ? 1:-1;

return 0;

public:

char& operator[] (int index){

return data[index];

String & operator = (String &s){ // Toan tu gan


/* lenght = s.lenght;

data = new char[lenght+1];

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

data[i] = s[i];

data[lenght] = 0;

*/

delete []data;

lenght = s.lenght;

data = new char[lenght+1];

strcpy(data, s.data);

return(*this);

public:

String operator + (char c){

/*char * data1 = new char[lenght+2];

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

data1[i] = data[i];

data1[lenght] = c;

data1[lenght+1] = 0;

String s1(data1,lenght+1);

return s1;

*/

int lenght = this->lenght+1;


char * data = new char[lenght+1];

strcpy(data,this->data);

data[this->lenght] = c;

data[lenght] = 0;

String s1(data,lenght);

return s1;

String operator + ( String s){

/*char* data1 = new char[lenght+s.lenght+1];

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

data1[i] = data[i];

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

data1[i+lenght] = s[i];

data[lenght+s.lenght] = 0;

String s1(data1,lenght+s.lenght);

return s1;

*/

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

char * data = new char [lenght+1];

strcpy(data,this->data);

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

String s1(data,lenght);

return s1;
}

public:

friend ostream& operator << (ostream& os,const String &s){

return os << s.data;

};

int main(){

char c = '1';

String s1("HaiTran");

cout <<"s1 = "<<s1;

String s2("1994");

cout <<"\ns2 = "<<s2;

cout <<"\nc = "<<c;

cout <<"\ns1 + c = "<<s1+c;

cout <<"\ns1 + s2 = "<<s1+s2;

You might also like