Bài đăng

Hiển thị các bài đăng có nhãn Lập trình C

danh sách liên kết đơn***

#include<iostream> #include <string> #include <iomanip> using namespace std; struct sinhvien{ string ten; int tuoi; string diachi; }; struct Node{ sinhvien * data; Node * next ; }; struct Danhsach{ Node * dau; Node * cuoi; }; Danhsach * ds; void khoitao(){ ds = new Danhsach(); ds -> dau = ds -> cuoi = NULL; } void ktrarong(){ if (ds -> dau = ds -> cuoi = NULL) cout << "danh sach nay rong " ; else cout << "k rong " ; } void themcuoi(Node * node){ if (ds -> dau == NULL){ ds -> dau = ds -> cuoi = node; } else { ds -> cuoi -> next = node; ds -> cuoi = node; } } Node * getNode(string ten, int tuoi, string diachi){ Node * p = new Node(); p -> data = new sinhvien(); p -> data -> ten = ten; p -> data -> tuoi = tuoi; p -> data -> diachi = diachi; p -> next = NULL; return p; } void input (){ int n; co...

sắp xếp nổi bọt

Đề bài: Đoc ghi trên file Sắp xếp theo thuật toán nổi bọt #include<iostream> #include<fstream> using namespace std; bool doctep( int a[ 100 ], int & n) { ifstream Doc( "output.txt" ); if (Doc . fail()) { return false; } Doc >> n; for ( int i = 0 ; i < n; i ++ ) { Doc >> a[i]; } return true; } int sapxeptang( int a[ 100 ], int n) { for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( a[i] > a[j]) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } } return 0 ; } int ghi( int a[ 100 ], int n) { ofstream ghi( "sapxeptang.txt" ); ghi << "sap xep tang dan la: " ; for ( int i = 0 ; i < n; i ++ ) { ghi << a[i] << " " ; } return 0 ; } int main() { int a[ 100 ]; int n; if (doctep(a,n)) { sapxeptang(a,n); ghi(a,n); } }

cấu trúc struct

Đề bài: Đọc và ghi ra file Danh sách sinh viên đăng kí học cho từng sinh viên và tính điểm trung bình cho từng sinh viên. // khai bao sinh vien // khai bao mon hoc // khai bao sinh vien hoc mon hoc gi #include<iostream> #include<cstring> #include<string> #include<fstream> #include<iomanip> using namespace std; typedef struct sinhvien{ string ten; string namsinh; }; typedef struct monhoc{ string ten; }; typedef struct dangkyhoc{ sinhvien * sv; monhoc * ds[ 100 ]; int somonhoc; }; typedef struct diem{ int sodiem; monhoc * mh; }; typedef struct bangDiem{ sinhvien * sv; diem * dsDiem[ 100 ]; int doDaiBang; }; bangDiem * dsBangDiem[ 100 ]; int soSv; sinhvien * dssv[ 100 ]; int soMonhoc; monhoc * dsMonhoc[ 100 ]; dangkyhoc * dsDangky[ 100 ]; int vitri; void input (){ fstream f( "dssv.cpp" ); f >> soSv; f . ignore(); for ( int i = 0 ; i < soSv; i ++ ){ dssv[i] = new sinhvien(); // khoi tao doi tu...

chèn/xóa vào đầu hoặc cuối / bất kỳ trong danh sách

// A -> B -> C -> D // [ 0 ] -> [ 1 ] -> [ 2 ] -> [ 3 ] #include <iostream> using namespace std; // 2 1 struct Node{ int data; Node * next ; }; Node * dau; Node * cuoi; // chen vao cuoi danh sach lien ket void insertCuoi(Node * node){ cuoi -> next = node; cuoi = node; } void inputChenVaoCuoi(){ // 1 - 10 dau = new Node(); dau -> data = 1 ; dau -> next = NULL; cuoi = dau; for ( int i = 2 ; i <= 10 ; i ++ ){ Node * b = new Node(); b -> data = i; b -> next = NULL; insertCuoi(b); } } void insertDau(Node * node){ // 2 -> 1 // a = b; node -> next = dau; dau = node; } void inputChenVaoDau(){ // 1 - 10 dau = new Node(); dau -> data = 1 ; dau -> next = NULL; cuoi = dau; for ( int i = 2 ; i <= 10 ; i ++ ){ Node * b = new Node(); b -> data = i; b -> next = NULL; insertDau(b); } } // 1 -> 2 -> 4 -> 5 // 3 // ...

sắp xếp danh sách liên kết đơn

Bài làm: #include<iostream> using namespace std; struct Node{ int data; Node * next ; }; Node * dau; Node * cuoi; void khoitao(){ dau = cuoi = NULL; } void themcuoi(Node * node){ if (dau == NULL){ dau = cuoi = node; } else { cuoi -> next = node; cuoi = node; } } Node * getNode( int x){ Node * p = new Node(); p -> data = x; p -> next = NULL; return p; } void input (){ int n; cout << "nhap so node " ; cin >> n; khoitao(); for ( int i = 1 ; i <= n; i ++ ){ int x; cout << "nhap vao data: " ; cin >> x; Node * p = getNode(x); themcuoi(p); } } void in (){ Node * p = dau; cout << endl; while ( p != NULL){ cout << p -> data << " " ; p = p -> next ; } } void hoanvi( int & a, int & b){ int tmp = a; a = b; b = tmp; } void sapxep(){ for ( Node * p = dau ; p != cuoi; p = p -> next ){ for...

danh sách sinh viên (ds liên kết đơn)

Đề bài: tao menu nhap vao danh sach sinh vien. -> chen vao dau danh sach ->chen vao cuoi danh sach -> chen vao 1 vi tri bat ky -> xoa sinh vien o dau -> xoa sinh vien o cuoi -> xoa sinh vien o 1 vi tri bat ky -> sap xep sinh vien theo ten Bài giải: #include<iostream> #include <string> using namespace std; struct sinhvien{ string ten; }; struct Node{ sinhvien * data; Node * next ; }; Node * dau; Node * cuoi; void khoitao(){ dau = cuoi = NULL; } void themcuoi(Node * node){ if (dau == NULL){ dau = cuoi = node; } else { cuoi -> next = node; cuoi = node; } } Node * getNode(string x){ Node * p = new Node(); p -> data = new sinhvien(); p -> data -> ten = x; p -> next = NULL; return p; } void input (){ int n; cout << "nhap so sinhvien " ; cin >> n; cin . ignore(); khoitao(); for ( int i = 1 ; i <= n; i ++ ){ string x; cout << "nhap...

sắp xếp chèn ( Insertion sort )

Bài giải : #include<iostream> #include<fstream> using namespace std; void doctep( float a[ 100 ], int & n) { ifstream doc( "output.txt" ); doc >> n; for ( int i = 0 ; i < n ; i ++ ) doc >> a[i]; } void ghi( float a[ 100 ], int n) { ofstream ghi( "sapxepchen.txt" ); ghi << "sap xep chen la: " ; for ( int i = 0 ; i < n; i ++ ) { ghi << a[i] << " " ; } } void doicho( float & x, float & y) { int tmp = x; x = y; y = tmp; } void chen( float N[ 100 ], int & n, int x, int i){ n = n + 1 ; for ( int j = n - 1 ; j > i; j -- ){ N[j] = N[j - 1 ]; } N[i] = x; } void sapxep( float a[ 100 ], int n){ float b[ 100 ]; int m = 0 ; b[ 0 ] = a[ 0 ]; m = 1 ; for ( int i = 1 ; i < n; i ++ ){ bool lonNhat = true; for ( int j = 0 ; j < m; j ++ ){ if (b[j] > a [i]){ cout << "chen " <...

sắp xếp chọn ( selection sort)

Đề bài : sắp xếp theo thuật toán chọn Bài giải: #include<iostream> #include<fstream> using namespace std; bool doctep( int a[ 100 ], int & n){ ifstream doc( "output.txt" ); if (doc . fail()){ return false; } doc >> n; for ( int i = 0 ; i < n; i ++ ){ doc >> a[i]; } return true; } void doicho( int & x, int & y){ int tmp = x; x = y; y = tmp; } // 4 ! 5 2 8 3 // 2 5 ! 4 8 3 // 2 3 4 ! 8 5 // 2 3 4 8 ! 5 // 2 3 4 5 8 void sapxepchon( int a[ 100 ], int n){ int csMin; for ( int i = 0 ; i < n - 1 ; i ++ ){ csMin = i; for ( int j = i + 1 ; j < n; j ++ ){ if (a[j] < a[csMin]){ csMin = j; } } if (csMin != i) { doicho(a[i], a[csMin]); } } } void ghitep( int a[ 100 ], int n){ ofstream ghi( "sxepchon.txt" ); ghi << "sap xep chon la: " ; for ( int i = 0 ; i < n; i ++ ) { ghi << a[i] <<...

đếm in xâu nhị phân (dùng thuật toán sinh )

Đề bài:  Một xâu nhị phân độ dài n được gọi là thuận nghịch hay đối xứng nếu đảo ngược xâu nhị phân đó ta vẫn nhận được chính nó. Cho số tự nhiên n (n nhập từ bàn phím). Hãy viết chương trình liệt kê tất cả các xâu nhị phân thuận nghịch có độ dài n . Các xâu nhị phân tìm được ghi lại trong file ketqua.out theo khuôn dạng: ·         Dòng đầu tiên ghi lại số K là số các xâu thuận nghịch có độ dài n tìm được; ·         K dòng kế tiếp ghi lại mỗi dòng một xâu nhị phân thuận nghịch có độ dài n. Hai phần tử khác nhau của xâu thuận nghịch được ghi cách nhau một vài khoảng trống. Ví dụ với n = 4 ta tìm được 4 xâu nhị phân thuận nghịch như dưới đây. ketqua.out                          4                  ...

sử dụng quay lui in số thỏa mãn các điều kiện

Đề bài: Hãy viết chương trình liệt kê tất cả các số tự nhiên K thỏa mãn đồng thời những điều kiện dưới đây: (i)                   K là số có 5 chữ số; (ii)                 K là số nguyên tố; (iii)               Đảo ngược các chữ số trong của K cũng là một số nguyên tố; (iv)                Tổng các chữ số của K cũng là một số nguyên tố; (v)                  Mỗi chữ số trong K cũng là những số nguyên tố. bài giải: #include<iostream> #include<cmath> using namespace std; int namchuso( int a[]){ if (a[ 1 ] == 0 ) return 0 ; else return 1 ; } long giatri( int * a , int ...