<!DOCTYPE html>
    <html lang="vi" xmlns="https://www.w3.org/1999/xhtml" prefix="og: https://ogp.me/ns#">
    <head>
<title>Tài liệu C++ và CodeBlocks V1</title>
<meta name="description" content="Tài liệu C++ và CodeBlocks V1 - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;365.io.vn&#x002F;savefile&#x002F;tai-lieu-hoc-lap-trinh&#x002F;tai-lieu-c-va-codeblocks-v1-14.html">
<meta name="author" content="DATAONLINE.IO.VN">
<meta name="copyright" content="DATAONLINE.IO.VN [support@dataonline.io.vn]">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Tài liệu C++ và CodeBlocks V1">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;365.io.vn&#x002F;savefile&#x002F;tai-lieu-hoc-lap-trinh&#x002F;tai-lieu-c-va-codeblocks-v1-14.html">
<meta property="og:site_name" content="DATAONLINE.IO.VN">
<meta property="og:url" content="https://365.io.vn/savefile/tai-lieu-hoc-lap-trinh/tai-lieu-c-va-codeblocks-v1-14.html">
<link rel="shortcut icon" href="https://dataonline.io.vn/favicon.ico">
<link rel="canonical" href="https://365.io.vn/savefile/tai-lieu-hoc-lap-trinh/tai-lieu-c-va-codeblocks-v1-14.html">
<link rel="alternate" href="https://dataonline.io.vn/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tong-hop/" title="Tin Tức - Tổng hợp" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/gdpt/" title="Tin Tức - GDPT" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/cac-van-ban/" title="Tin Tức - Các văn bản" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tieu-hoc/" title="Tin Tức - Tiểu học" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-3/" title="Tin Tức - Lớp 3" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-4/" title="Tin Tức - Lớp 4" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-5/" title="Tin Tức - Lớp 5" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/sgk-tieu-hoc/" title="Tin Tức - SGK Tiểu học" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-3/" title="Tin Tức - Tin học 3" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-4/" title="Tin Tức - Tin học 4" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-5/" title="Tin Tức - Tin học 5" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/thcs/" title="Tin Tức - THCS" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-6-75/" title="Tin Tức - Lớp 6" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-7-75/" title="Tin Tức - Lớp 7" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-8-75/" title="Tin Tức - Lớp 8" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-9-75/" title="Tin Tức - Lớp 9" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/sgk-thcs/" title="Tin Tức - SGK THCS" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-6/" title="Tin Tức - Tin học 6" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-7/" title="Tin Tức - Tin học 7" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-8/" title="Tin Tức - Tin học 8" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-9/" title="Tin Tức - Tin học 9" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/thpt/" title="Tin Tức - THPT" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-10/" title="Tin Tức - Lớp 10" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-11/" title="Tin Tức - Lớp 11" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-12/" title="Tin Tức - Lớp 12" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/sgk-thpt/" title="Tin Tức - SGK THPT" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-10/" title="Tin Tức - Tin học 10" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-11/" title="Tin Tức - Tin học 11" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-12/" title="Tin Tức - Tin học 12" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/cntt/" title="Tin Tức - CNTT" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/phan-mem-quan-li-va-dieu-hanh/" title="Tin Tức - Phần mềm quản lí và điều hành" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/phan-mem-ung-dung/" title="Tin Tức - Phần mềm ứng dụng" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/ung-dung-ai/" title="Tin Tức - Ứng dụng AI" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/mang-may-tinh/" title="Tin Tức - Mạng máy tính" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lap-trinh/" title="Tin Tức - Lập trình" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/cau-truc-thi-hsg-va-ts-vao-10/" title="Tin Tức - Cấu trúc thi HSG và TS vào 10" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tai-lieu-hoc-lap-trinh/" title="Tin Tức - Tài liệu học lập trình" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/phan-mem-lap-trinh/" title="Tin Tức - Phần mềm lập trình" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lap-trinh-truc-quan/" title="Tin Tức - Lập trình trực quan" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lap-trinh-huong-thu-tuc-pop/" title="Tin Tức - Lập trình hướng thủ tục &#40;POP&#41;" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/de-hsg-tin-hoc-thcs/" title="Tin Tức - Đề HSG Tin học THCS" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/de-tin-hoc-ts-vao-10-thpt-chuyen/" title="Tin Tức - Đề Tin học TS vào 10 THPT chuyên" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/de-tin-hoc-hsg-thpt/" title="Tin Tức - Đề Tin học HSG THPT" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/cham-thi/" title="Tin Tức - Chấm thi" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/themis/" title="Tin Tức - Themis" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/vnoi/" title="Tin Tức - VNOI" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/danh-sach-hoc-sinh/" title="Tin Tức - Danh sách học sinh" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lap-trinh-huong-doi-tuong-oop/" title="Tin Tức - Lập trình hướng đối tượng &#40;OOP&#41;" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/web-nguon-mo/" title="Tin Tức - Web nguồn mở" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/he-dieu-hanh/" title="Tin Tức - Hệ điều hành" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/microsoft-windows/" title="Tin Tức - Microsoft Windows" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/ho-linux/" title="Tin Tức - Họ Linux" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/may-chu/" title="Tin Tức - Máy chủ" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/co-so-du-lieu/" title="Tin Tức - Cơ sở dữ liệu" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/web-server/" title="Tin Tức - Web Server" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/hosting-domain-vps/" title="Tin Tức - Hosting-domain-VPS" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/ban-tin/" title="Tin Tức - Bản tin" type="application/rss+xml">
<link rel="preload" as="style" href="https://dataonline.io.vn/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://dataonline.io.vn/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://dataonline.io.vn/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://dataonline.io.vn/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://dataonline.io.vn/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://dataonline.io.vn/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://dataonline.io.vn/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://dataonline.io.vn/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://dataonline.io.vn/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://dataonline.io.vn/themes/default/css/style.css">
<link rel="stylesheet" href="https://dataonline.io.vn/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://dataonline.io.vn/themes/default/css/news.css">
<link rel="stylesheet" href="https://dataonline.io.vn/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">DATAONLINE.IO.VN</h2>
		<p class="pull-right"><a title="DATAONLINE.IO.VN" href="https://dataonline.io.vn/">https://dataonline.io.vn</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Tài liệu C++ và CodeBlocks V1</h1>
		<ul class="list-inline">
			<li>Thứ hai - 21/03/2022 20:23</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
			<p>Tài liệu C++ và CodeBlocks gồm tài liệu C++ căn bản, hướng dẫn cài và sử dụng CodeBlocks để thực hành C++. Dành cho học sinh THCS thay thế ngôn ngữ lập trình Pascal.&nbsp;Tài liệu luôn được cập nhật, điều chỉnh và bổ sung phù hợp với những thay đổi của môi trường lập trình C++ CodeBlocks.<br />Là tài liệu cơ bản dành cho học sinh THCS</p>
		</div>
				<div class="imghome">
			<img alt="Tài liệu C++ và CodeBlocks V1" src="https://dataonline.io.vn/uploads/news/codeblock2.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p><strong>Mục lục</strong><br /><a href="https://dataonline.io.vn/#hdcaicode"><strong>I. Hướng dẫn sử cài và dụng CodeBlocks để thực hành c++</strong></a><br /><strong>II. Tài liệu C ++</strong></p><h2><a href="https://dataonline.io.vn/#chude1">1. Chủ đề 1. Làm quen với C++.</a><br /><a href="https://dataonline.io.vn/#chude2">2. Chủ đề 2. Lệnh điều kiện, lệnh lặp.</a><br /><a href="https://dataonline.io.vn/#chude3">3. Chủ đề 3. Mảng một chiều (số nguyên, số thực).</a><br /><a href="https://dataonline.io.vn/#chude4">4. Chủ đề 4. Xâu kí tự.</a><br /><a href="https://dataonline.io.vn/#chude5">5. Chủ đề 5. Đọc tệp, ghi tệp.</a><br /><a href="https://dataonline.io.vn/#chude6">6.&nbsp;Chủ đề 6. Mảng 2 chiều (Ma trận)</a><br /><a href="https://dataonline.io.vn/#chude7">7. Chủ đề 7. Luyện tập-Giải đề chuyên.</a></h2><hr /><p><strong>I. Hướng dẫn sử cài và dụng&nbsp;CodeBlocks để thực hành c++</strong><br /><strong>1. Tải và cài&nbsp;CodeBlock.</strong><br /><a href="http://www.codeblocks.org/downloads/26">http://www.codeblocks.org/downloads/26</a><br />- Đối với Windowsn 64bít:&nbsp;Chọn tệp có tên:&nbsp;<a href="https://www.mediafire.com/file/s3rki58fuxthl9p/codeblocks-20.03mingw-setup.zip/file"><strong>codeblocks-20.03mingw-setup.exe</strong></a><br />- Đối với&nbsp;Windowsn 32bít:&nbsp;Chọn tệp có tên: <a href="https://www.mediafire.com/file/5ifovyswrdgx6zl/codeblocks-20.03-32bit-mingw-32bit-setup.zip/file"><strong>codeblocks-20.03mingw-32bit-setup.exe</strong></a><br /><strong>+ Sửa lỗi khi cài bản codeblocks-20.03mingw-32bit-setup.exe báo&nbsp;&quot;Không tìm thấy trình biên dịch GNU GCC&quot;</strong> <a href="https://365.info.vn/tai-lieu-hoc-c-va-python/huong-dan-fix-loi-khi-cai-ban-codeblocks-20-03-32bit-mingw-32bit-15.html">Click vào đây để xem hướng dẫn fix lỗi.</a><br />+ Sửa lỗi cài trên win 7 không update mới báo thiều &quot;api-ms-win-crt-runtime-l1-1-0.dll &quot;. Tải VC++ cài&nbsp; bổ sung tại link bên dưới đây:<br /><a href="http://www.mediafire.com/file/p1edody8jyqcksl/vc_redist.x64.zip/file"><strong>Bản CV++ cho win 64bit&nbsp;(13,69MB)</strong></a><br /><a href="http://www.mediafire.com/file/vymetxh31sq0q5o/vc_redist.x86.zip/file"><strong>Bản VC++ cho win 32 bit&nbsp;(12,92MB)</strong></a><br />- Đặt phím tắt để tự định dạng code.<br />B1. Như hình sau<br /><img src="https://i.imgur.com/9sT0zzD.png" alt="" /><br />B2.&nbsp;Như hình sau<br /><img src="https://i.imgur.com/oM9ORFU.png" alt="" /><br /><strong>2. Tạo project mới chạy thử chương trình đầu tiên.</strong><br /><img src="https://i.imgur.com/AYceq8q.png" alt="" /><br /><img src="https://i.imgur.com/9nNnb9W.png" alt="" /><br /><img src="https://i.imgur.com/4Tzz3RH.png" alt="" /><br /><img src="https://i.imgur.com/H843XuN.png" alt="" /><br /><img src="https://i.imgur.com/ged7Cyz.png" alt="" /><br /><img src="https://i.imgur.com/gHFN1PN.png" alt="" /><br /><br />- Chuột phải vào tên tệp main.cpp chọn Rename file rồi đổi tên thành Cau1.cpp.<br /><img src="https://i.imgur.com/NEdSTwC.png" alt="" /><br /><img src="https://i.imgur.com/jyyJTv7.png" alt="" /><br /><br />Nhấn F9 để dịch và chạy.<br />Nhấn phím Enter để trở lại soạn thảo.<br /><strong>3. Khắc phục lối nếu tại mục 2 chạy thất bại.</strong><br />Sửa lỗi codeblock chỉ được 1, 2 lần trên win7 và 8<br />B1: Nhấn tổ hợp phím Windows+R<br />B2: gõ: services.msc<br />B3: Tìm mục “Application Experiences”<br />B4: Chọn “Automatic (Delayed Start)”<br />B5: Apply và Ok sau đó khởi động lại máy tính<br />Hoặc vào Start/Control Panel/System anh Security/Administrative Tools rồi nháy đúp chuột vào&nbsp;Services rồi thực hiện như bước 3 và 4, 5 ở trên.<br /><strong>II. Tài liệu C ++</strong><br /><a href="https://dataonline.io.vn/#mlchude1"><strong>1. Chủ đề 1. Làm quen với C++.</strong></a><br /><i><strong>Mục tiêu:</strong></i><br />- Các phép toán với dữ liệu kiểu số cơ bản (Toán tử số học): Cộng, trừ, nhân, chia, chia lấy phần dư, chia lấy phần nguyên.<br />- Xuất ra kết quả gồm mặc định và có định dạng (số nguyên, số thực).<br />- Hằng, biến và các lệnh nhập/xuất từ bàn phím/màn hình.<br /><i><strong>Kiến thức:</strong></i></p><figure class="table"><table><tbody><tr><td><strong>TT</strong></td><td><strong>Phép toán</strong></td><td><strong>Kí hiệ</strong></td><td><strong>Ví dụ</strong></td><td><strong>Kết quả</strong></td></tr><tr><td>1</td><td>Cộng</td><td>+</td><td>9+4</td><td>13</td></tr><tr><td>2</td><td>Trừ</td><td>-</td><td>9-4</td><td>5</td></tr><tr><td>3</td><td>Nhân</td><td>*</td><td>9*4</td><td>36</td></tr><tr><td colspan="1" rowspan="3">4</td><td colspan="1" rowspan="3">Chia</td><td colspan="1" rowspan="3">/</td><td>9/4</td><td>2</td></tr><tr><td>9.0/4</td><td>2.25</td></tr><tr><td>9/4.0</td><td>2.25</td></tr><tr><td>5</td><td>Chia lấy phần dư</td><td>%</td><td>9%4</td><td>1</td></tr><tr><td>6</td><td>Tăng 1 đơn vị</td><td>++</td><td>a++</td><td>Giống a=a+</td></tr><tr><td>7</td><td>Giảm 1 đpn vị</td><td>--</td><td>a--</td><td>Giống a=a-</td></tr></tbody></table><figcaption><strong>Các phép toán trong C++ (Toán tử số học)</strong></figcaption></figure><p><strong>Xuất ra kết quả gồm mặc định và có định dạng (số nguyên, số thực).</strong><br /><strong>- Xuất mặc định: cout&lt;&lt;Dữ liệu xuất;</strong><br /><strong>cout&lt;&lt;Dữ liệu xuất&lt;&lt;endl;</strong><br /><strong>Ví dụ 1:</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    cout &lt;&lt; &quot;9+4 = &quot;&lt;&lt;9+4 &lt;&lt; endl;    cout &lt;&lt; &quot;9-4 = &quot;&lt;&lt;9-4 &lt;&lt; endl;    cout &lt;&lt; &quot;9*4 = &quot;&lt;&lt;9*4 &lt;&lt; endl;    cout &lt;&lt; &quot;9/4 = &quot;&lt;&lt;9/4 &lt;&lt; endl; //Chi lay phan nguyen do ca hai so deu nguyen    cout &lt;&lt; &quot;9/4 = &quot;&lt;&lt;9.0/4 &lt;&lt; endl; //Lay ca phan thap phan neu mot trong hai so la so thap phan    cout &lt;&lt; &quot;9%4 = &quot;&lt;&lt;9%4 &lt;&lt; endl;    return 0;}</code></pre><p><strong>kết quả:</strong><br /><strong><img src="https://i.imgur.com/q4AdIEd.jpg" alt="" /></strong><br /><strong>Xuất định dạng mọi kiểu dữ liệu: cout&lt;&lt;setw(n)&lt;&lt;căn lề &lt;&lt;dữ liệu xuất;</strong><br />Ghi chú:<br /><code>n&gt;=0 là đô rộng danh cho dữ liệu xuất gí trị nguyên.</code><br /><code>căn lề: Trái hoặc phải có giá trị</code><strong>left&nbsp;</strong>hoặc&nbsp;<strong>right</strong><br />Số thực có đinh dạng và số chữ số thập phân<br /><strong>cout &lt;&lt;setprecision(n)&lt;&lt; fixed&lt;&lt;dữ liệu số thực;</strong><br />n&gt;=0: là số chữ số phần thập phân được xuất ra.<br />fixed: là in số thập phân làm tròn đến n.</p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;iomanip&gt;using namespace std;int main(){    cout &lt;&lt;setw(10)&lt;&lt;left&lt;&lt; &quot;9+4 = &quot;&lt;&lt;9+4 &lt;&lt; endl;    cout &lt;&lt;setw(10)&lt;&lt;right&lt;&lt; &quot;9-4 = &quot;&lt;&lt;9-4 &lt;&lt; endl;    cout &lt;&lt;setw(10)&lt;&lt;left&lt;&lt; &quot;9*4 = &quot;&lt;&lt;9*4 &lt;&lt; endl;    cout &lt;&lt;setw(10)&lt;&lt;right&lt;&lt; &quot;9/4 = &quot;&lt;&lt;9/4 &lt;&lt; endl; //Chi lay phan nguyen do ca hai so deu nguyen    cout &lt;&lt;setw(10)&lt;&lt;left&lt;&lt; &quot;9/4 = &quot;&lt;&lt;9.0/4 &lt;&lt; endl; //Lay ca phan thap phan neu mot trong hai so la so thap phan    cout &lt;&lt;setw(10)&lt;&lt;left&lt;&lt;setprecision(5)&lt;&lt; fixed&lt;&lt; &quot;9/4 = &quot;&lt;&lt;9.0/4 &lt;&lt; endl;    cout &lt;&lt;setprecision(1)&lt;&lt; fixed&lt;&lt; &quot;9/4 = &quot;&lt;&lt;9.0/4 &lt;&lt; endl;    cout &lt;&lt;setw(10)&lt;&lt;right&lt;&lt; &quot;9%4 = &quot;&lt;&lt;9%4 &lt;&lt; endl;    return 0;}</code></pre><p>Kết quả:<br /><img src="https://i.imgur.com/x1XMhQF.jpg" alt="" /><br />Giải thích:&nbsp;<strong>#include &lt;iostream&gt;</strong>&nbsp;để dùng&nbsp;hàm xuất nhập <strong>cout </strong>và <strong>cin</strong><br /><strong>using namespace std;</strong>&nbsp;Nếu không khai báo thì phải viết các lệnh như:&nbsp;<strong>std::cout &lt;&lt; &quot;9+4 = &quot;&lt;&lt;9+4 &lt;&lt; std::endl;</strong><br /><strong>#include &lt;iomanip&gt;&nbsp;setw(n), ::setfill(ch), left, setprecision(n)</strong><br /><strong>Các kiểu dữ liệu cơ bản của C++</strong><br /><strong>(Dánh cho học sinh THCS lớp 8 thay thế ngôn ngữ lập trình Pascal từ sô 1 đến 7)</strong></p><figure class="table"><table><tbody><tr><td><strong>TT</strong></td><td><strong>Kiểu dữ liệu</strong></td><td><strong>Mô tả</strong></td><td><strong>Ghi chú</strong></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>1</td><td><strong>int</strong></td><td>&nbsp;</td><td><strong>Số nguyên</strong></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>2</td><td><strong>float</strong></td><td>&nbsp;</td><td><strong>Số thực</strong></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>3</td><td><strong>char</strong></td><td>&nbsp;</td><td><strong>Kí tự</strong></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>4</td><td><strong>string</strong></td><td>&nbsp;</td><td><strong>Xâu kí tự</strong></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>5</td><td><strong>bool</strong></td><td>&nbsp;</td><td><strong>Logic</strong></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>6</td><td><strong>long</strong></td><td>&nbsp;</td><td><strong>Số nguyên</strong></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>7</td><td><strong>short</strong></td><td>&nbsp;</td><td><strong>Số nguyên</strong></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>8</td><td>&nbsp;</td><td><strong>double</strong></td><td>&nbsp;</td><td>&nbsp;</td><td><strong>Số thực</strong></td></tr><tr><td>9</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></tbody></table></figure><p><br /><strong>Phép gán, hằng, biến và các lệnh nhập/xuất giá trị cho biến từ bàn phím/màn hình.</strong><br />- Phép gán là dấu bằng =<br />Ví dụ: a = 90;<br />-&nbsp;Hằng (constant) là đại lượng có giá trị không thay đổi trong suốt quá trình thực hiện chương trình.<br />- Biến (variable) là đại lượng có giá trị có thể thay đổi trong khi thực hiện chương trình.<br />- Từ khóa khai báo hằng:&nbsp;const<br /><strong>- Cú pháp khai báo hằng:</strong><br />const kiểu dữ liệu tên_hằng = giá trị;<br />Ví dụ:&nbsp;const double PI = 3.14159;<br />const int diem_thuong = 5;<br />hoặc<br />kiểu dữ liệu const tên_hằng = giá trị;<br />Ví dụ:&nbsp;double const&nbsp; PI = 3.14159;<br />int const diem_thuong = 5;<br /><strong>- Cú pháp khai báo biến:</strong><br />kiểu dữ liệu tên_biến = giá trị;<br />kiểu dữ liệu tên_biến;<br />hoặc<br />kiểu dữ liệu danh_sách_tên_biến = giá trị;<br />kiểu dữ liệu danh_sách_tên_biến;<br />- Lưu ý: Danh sách tên biến cách nhau bởi dấu phẩy (,); giá trị có thể gán giá trị ban đầu hoặc không gán.<br />- Ví dụ:<br />int a = 0, b = 20; // Khai báo 2 biến kiểu số nguyên<br />float = 20.8;<br />char c = &#039;A&#039;;<br />string s = &quot;Thong bao&quot;;<br />hoặc&nbsp;<br />int a, b; // Khai báo 2 biến kiểu số nguyên<br />float c;<br />char c;<br />string s;<br /><strong>Ví dụ 2:</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;iomanip&gt;using namespace std;const float pi = 3.14;int main(){    float ban_kinh = 0, dien_tich,chu_vi=0;;    string tb_dien_tich = &quot;Dien tich hinh tron la: &quot;;    string tb_chu_vi = &quot;Chu vi hinh tron la: &quot;;    cout &lt;&lt; &quot;Nhap ban kinh = &quot;;    cin&gt;&gt;ban_kinh;    dien_tich = pi*ban_kinh*ban_kinh;    chu_vi = 2*pi*ban_kinh;    cout &lt;&lt; tb_dien_tich&lt;&lt;dien_tich&lt;&lt;endl;    cout &lt;&lt; tb_chu_vi&lt;&lt;chu_vi&lt;&lt;endl;    cout &lt;&lt;setw(30)&lt;&lt;right&lt;&lt;setprecision(5)&lt;&lt;fixed&lt;&lt; tb_dien_tich&lt;&lt;dien_tich&lt;&lt;endl;    cout &lt;&lt;setprecision(5)&lt;&lt;fixed&lt;&lt; tb_chu_vi&lt;&lt;chu_vi&lt;&lt;endl;    return 0;}</code></pre><p>-Kết quả:<br /><img src="https://i.imgur.com/wvhTS7q.png" alt="" /><br /><a href="https://dataonline.io.vn/#mlchude2"><strong>2. Chủ đề 2. Lệnh điều kiện, lệnh lặp.</strong></a><br /><i><strong>Mục tiêu:</strong></i><br />- Các phép so sánh (toán tử quan hệ), phép logic (toán tử logic).<br />- Câu lệnh điều kiện, lặp.<br /><i><strong>Kiến thức:</strong></i></p><figure class="table"><table><tbody><tr><td><strong>Phép so sánh</strong></td><td><strong>Kí hiệu</strong><br /><strong>(toán tử so sánh)</strong></td><td><strong>Ví dụ&nbsp;</strong></td><td><strong>Kết qủa</strong></td></tr><tr><td>Bằng</td><td>==</td><td>a==b</td><td>false</td></tr><tr><td>Khác</td><td>!=</td><td>a!=</td><td>true</td></tr><tr><td>Lớn hơn</td><td>&gt;</td><td>a&gt;b</td><td>false</td></tr><tr><td>Nhỏ hơn</td><td>&lt;</td><td>a&lt;b</td><td>true</td></tr><tr><td>Lớn hơn hoặc bằng</td><td>&gt;=</td><td>a&gt;=</td><td>false</td></tr><tr><td>Nhỏ&nbsp;hơn hoặc bằng</td><td>&lt;=</td><td>a&lt;=b</td><td>true</td></tr></tbody></table><figcaption><strong>Các phép so sánh (toán tử quan hệ)</strong><br /><strong>Ví dụ: int a = 10, int b= 20;</strong></figcaption></figure><figure class="table"><table><tbody><tr><td><strong>Phép Logic</strong></td><td><strong>Kí kiệu</strong><br /><strong>(</strong>toán tử logic)</td><td><strong>Ví dụ</strong></td><td><strong>Kết quả</strong></td></tr><tr><td>AND (Và)</td><td>&amp;&amp;</td><td>A&amp;&amp;B</td><td>false</td></tr><tr><td>OR (Hoặc)</td><td>||</td><td>A||B</td><td>true</td></tr><tr><td colspan="1" rowspan="2">NOT (Phủ định)</td><td colspan="1" rowspan="2">!</td><td>!A</td><td>false</td></tr><tr><td>!B</td><td>true</td></tr></tbody></table><figcaption>Phép logic (toán tử logic)<br />Ví dụ: A = true; B = false</figcaption></figure><p>Ví dụ dùng phép logic &amp;&amp;, || !</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0;    int b=0;    cout&lt;&lt;&quot;Nhap a = &quot;;    cin&gt;&gt;a;    cout&lt;&lt;&quot;Nhap b = &quot;;    cin&gt;&gt;b;    if((a&gt;0)&amp;&amp;(b&gt;0))    {        cout&lt;&lt;&quot;a va b deu lon hon 0&quot;&lt;&lt;endl;    }    if((a&gt;0)||(b&gt;0))    {        cout&lt;&lt;&quot;a hoac b lon hon 0&quot;&lt;&lt;endl;    }    if(!(a&gt;0))    {        cout&lt;&lt;&quot;Xuat ra man hinh khi a nho hon 0&quot;&lt;&lt;endl;    }    return 0;}</code></pre><p>Câu lệnh điều kiện if<br />Dạng thiếu:<br />if (điều kiện)<br />{<br />&nbsp; &nbsp; nội dung khi đk đúng;<br />}<br /><strong>Ví dụ:</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0, b=0, S=0;    cout &lt;&lt; &quot;a = &quot;;    cin&gt;&gt;a;    cout &lt;&lt; &quot;b = &quot;;    cin&gt;&gt;b;    if(a&gt;b)    {        cout&lt;&lt;&quot;So a lon hon so b&quot;;    }    return 0;}</code></pre><p>Dạng đủ:<br />if (điều kiện)<br />{<br />&nbsp; &nbsp; nội dung khi đk đúng;<br />}<br />else<br />{<br />&nbsp; &nbsp;nội dung khi đk sai;<br />}<br /><strong>Ví dụ:</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0, b=0, S=0;    cout &lt;&lt; &quot;a = &quot;;    cin&gt;&gt;a;    cout &lt;&lt; &quot;b = &quot;;    cin&gt;&gt;b;    if(a&gt;b)    {        cout&lt;&lt;&quot;So a lon hon so b&quot;;    }    else    {        cout&lt;&lt;&quot;So a nho hon so b&quot;;    }    return 0;}</code></pre><p><strong>Câu lệnh điều kiện switch ... case</strong><br />switch (biến hoặc biểu thức kiểm tra)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; case giá trị 1:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Lệnh thứ 1;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; &nbsp; &nbsp; case giá trị 2:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Lệnh thứ 2;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; &nbsp; &nbsp;case giá trị 3:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Lệnh thứ 3;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case giá trị thứ n:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Lệnh thứ n;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br />&nbsp; &nbsp; &nbsp; &nbsp; default:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Lệnh thực hiện khi&nbsp; các lựa chọn trên đều sai; //phần default là không bắt buộc<br />&nbsp; &nbsp; }<br /><strong>Ví dụ:</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    char dau;    int a=0, b=0, S=0;    cout &lt;&lt; &quot;a = &quot;;    cin&gt;&gt;a;    cout &lt;&lt; &quot;b = &quot;;    cin&gt;&gt;b;    cout&lt;&lt;&quot;Nhap dau phep toan la: &quot;;    cin&gt;&gt;dau;    switch (dau)    {    case &#039;+&#039;:        S= a + b;        break;    case &#039;-&#039;:        S=  a - b;        break;    case &#039;*&#039;:        S=  a * b;        break;    case &#039;/&#039;:        if (b == 0)        {            cout &lt;&lt; &quot;Phep chia cho 0!&quot;;        }        if (b != 0)        {            S=  a / b;        }    default:        cout &lt;&lt; &quot;Phep tinh khong hop le!&quot;;    }    cout&lt;&lt;&quot;Ket qua phep tinh la: &quot;&lt;&lt;S;    return 0;}</code></pre><p><strong>Câu lệnh điều kiện thay thế cặp if ..else</strong><br />c = (a&gt;b? giá trị khi đúng : giá trị khi sai);<br /><strong>Ví dụ:</strong><br />Giải thích:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0, b=0, S=0;    cout &lt;&lt; &quot;a = &quot;;    cin&gt;&gt;a;    cout &lt;&lt; &quot;b = &quot;;    cin&gt;&gt;b;    S=(a&gt;b?a-b:a+b);    cout&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p>thay thê cặp lệnh if ...else sau:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0, b=0, S=0;    cout &lt;&lt; &quot;a = &quot;;    cin&gt;&gt;a;    cout &lt;&lt; &quot;b = &quot;;    cin&gt;&gt;b;    if(a&gt;b)    {        S=a-b;    }    else    {        S=a+b;    }    cout&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p><strong>Câu lệnh lặp.</strong><br /><strong>Lệnh lặp for</strong><br />for (bien=giá trị; dieu_kien; tang_giam)&nbsp;<br />&nbsp;{&nbsp;<br />&nbsp; &nbsp;cac_lenh;&nbsp;<br />&nbsp;}<br />Ví dụ xuất ra màn hình các số từ 1 đến 15</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0;    for(a = 1; a &lt;= 15; a++)    {        cout &lt;&lt; &quot;Gia tri cua a la: &quot; &lt;&lt; a &lt;&lt; endl;    }    return 0;}</code></pre><p><strong>Câu lệnh lặp while</strong><br />while(dieu_kien)&nbsp;<br />&nbsp;{&nbsp;<br />&nbsp; &nbsp;cac_lenh;&nbsp;<br />&nbsp;}<br />Ví dụ xuất ra màn hình các số từ 1 đến 15</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0;    while(a &lt; 15)    {        a++;        cout &lt;&lt; &quot;Gia tri cua a la: &quot; &lt;&lt; a &lt;&lt; endl;    }    return 0;}</code></pre><p><strong>Câu lệnh lặp do while</strong><br />- Câu lệnh này được thực hiện ít nhất 01 lần<br />do<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; cac_lenh;<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; while(dieu_kien);<br />Ví dụ xuất ra màn hình các số từ 1 đến 15</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0;    do    {        a++;        cout &lt;&lt; &quot;Gia tri cua a la: &quot; &lt;&lt; a &lt;&lt; endl;    }    while(a&lt;15);}</code></pre><p><br /><a href="https://dataonline.io.vn/#mlchude3"><strong>3.&nbsp;Chủ đề 3. Mảng một chiều (số nguyên, số thực).</strong></a><br />Mảng là:<br />Một kiểu dữ liệu có cấu trúc do người lập trình định nghĩa.<br />Biểu diễn một dãy các biến có cùng kiểu. Ví dụ: dãy các số nguyên, dãy các ký tự…<br />Kích thước được xác định ngay khi khai báo và không bao giờ thay đổi (mảng tĩnh).<br /><i><strong>Mục tiêu:</strong></i><br />- Cách khai báo và sử dụng mảng, chèn thêm, xóa phần tử ở vị trí xác định, tìm kiếm trong mảng ...<br /><i><strong>Kiến thức:</strong></i><br />- Cú pháp khai báo mảng:&nbsp;kiểu dữ liệu tên biến mảng&#91;số phần tử&#93;;<br />Một mảng liên tục có chỉ số từ 0 đến &lt;tổng số phần tử&gt; - 1.<br />Bộ nhớ sử dụng = &lt;tổng số phần tử&gt; * sizeof(&lt;kiểu cơ sở&gt;).<br /><i><strong>Ví dụ khai báo mảng.</strong></i><br />Cách 1: Khởi tạo giá trị cho mọi phần tử của mảng:</p><pre><code class="language-cpp">int array&#91;4&#93; = { 5, 8, 2, 7 };</code></pre><p>Cách 2: Khởi tạo giá trị cho một số phần tử đầu mảng:</p><pre><code class="language-cpp">int array&#91;4&#93; = { 5, 8 };</code></pre><p>Cách 3: Khởi tạo giá trị 0 cho mọi phần tử của mảng</p><pre><code class="language-cpp">int array&#91;4&#93; = { };</code></pre><p>Cách 4: Tự động xác định số lượng phần tử</p><pre><code class="language-cpp">int array&#91;&#93; = { 5, 8, 2, 7 };</code></pre><p><strong>Cách 5: Sử dụng khởi tạo đồng nhất trong C++11 (thực hành cách này)</strong></p><pre><code class="language-cpp">int array1&#91;4&#93; { 5, 8, 2, 7 }; // 5 8 2 7int array2&#91;4&#93; { 5, 8 }; // 5 8 0 0int array3&#91;4&#93; { }; // 0 0 0 0int array4&#91;&#93; { 5, 8, 2, 7 }; // 5 8 2 7</code></pre><p><strong>Nhập dữ liệu cho mảng 1 chiều</strong><br />tên biến mảng&#91;chỉ số&#93; = giá trị;<br /><strong>Xuất các phần tử mảng 1 chiều</strong><br />tên biến mảng&#91;chỉ số&#93;;<br /><strong>Ví dụ về khai báo, nhập, xuất giá trị cho phần tử có trong mảng.</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;const int sopt=200;int main(){    int a&#91;sopt&#93;;    int n=0, i=0;    n = 5;    //Nhap gia tri cho cac phan tu cuar mang    cout&lt;&lt;&quot;a&#91;0&#93; = &quot;;    cin&gt;&gt;a&#91;0&#93;;    cout&lt;&lt;&quot;a&#91;1&#93; = &quot;;    cin&gt;&gt;a&#91;1&#93;;    cout&lt;&lt;&quot;a&#91;2&#93; = &quot;;    cin&gt;&gt;a&#91;2&#93;;    cout&lt;&lt;&quot;a&#91;3&#93; = &quot;;    cin&gt;&gt;a&#91;3&#93;;    cout&lt;&lt;&quot;a&#91;4&#93; = &quot;;    cin&gt;&gt;a&#91;4&#93;;    /*Xuat gia tri cac phan tu cua mang ra man hinh */    cout&lt;&lt;a&#91;0&#93;&lt;&lt;endl;    cout&lt;&lt;a&#91;3&#93;&lt;&lt;endl;    return 0;}</code></pre><p><strong>Ví dụ về khai báo, nhập, xuất giá trị các phần tử có trong mảng dùng vòng lặp.</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;const int sopt=200;int main(){    int a&#91;sopt&#93;;    int n=0, i=0;    /*Nhap so phan tu thuc co cua mang*/    cout&lt;&lt;&quot;Nhap so phan tu cua mang, n = &quot;;    cin&gt;&gt;n;    //Nhap gia tri cho cac phan tu cuar mang    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }    /*Xuat gia tri cac phan tu cua mang ra man hinh */    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;endl;    }    return 0;}</code></pre><p><strong>Ví dụ về sử dụng mảng (nhập, xuất, xoa, thêm phần tử).</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;const int soptmax=50;void nhap(int a&#91;&#93;, int n){    int i;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void xuat(int a&#91;&#93;, int n){    int i;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;&lt;&lt;a&#91;i&#93;&lt;&lt;endl;    }}bool xoa(int a&#91;&#93;,int &amp;n, int vitri){    bool daxoa=false;    if((vitri&lt;0)||(vitri&gt;n-1))    {        cout&lt;&lt;&quot;Vi tri &quot;&lt;&lt;vitri&lt;&lt;&quot; khong ton tai&quot;&lt;&lt;endl;        daxoa=false;    }    else    {        int i;        for(i=vitri; i&lt;n; i++)        {            a&#91;i&#93;=a&#91;i+1&#93;;        }        n--;        daxoa=true;    }    return daxoa;}bool them(int a&#91;&#93;,int &amp;n, int vitri, int giatri){    bool dathem=false;    if(n&gt;=soptmax)    {        cout&lt;&lt;&quot;Mang da day, khong them duoc nua &quot;&lt;&lt;endl;        dathem=false;    }    else    {        if((vitri&lt;0)||(vitri&gt;n))        {            cout&lt;&lt;&quot;Vi tri muon them phai nhap tu 0 den &quot;&lt;&lt;n&lt;&lt;endl;            dathem=false;        }        else        {            int i;            for(i=n; i&gt;vitri; i--)            {                a&#91;i&#93;=a&#91;i-1&#93;;            }            a&#91;vitri&#93;=giatri;            n++;            dathem=true;        }    }    return dathem;}//Xoa cac phan tu trung lap dung whilevoid Xoatrung(int a&#91;&#93;,int &amp;N){    int i=0,j=0,k=0;    for (i=0; i&lt;N-1; i++)    {        j=i+1;        while (j&lt;N)        {            if (a&#91;i&#93;==a&#91;j&#93;)            {                //xoamotphantu(j, a,N);                for (k=j; k&lt;N-1; k++)                {                    a&#91;k&#93;=a&#91;k+1&#93;;                }                N=N-1;            }            else            {                j=j+1;            }        }    }}//Xoa cac phan tu trung lap dung forvoid RemoveDuplicates(int arr&#91;&#93;, int n){    for(int i=0; i&lt;n; i++)    {        for(int j=i+1; j&lt;n;)        {            if(a&#91;i&#93;==a&#91;j&#93;)            {                for(int k=j; k&lt;n; k++)                {                    arr&#91;k&#93;=arr&#91;k+1&#93;;                }                n--;            }            else            {                j++;            }        }    }    for(int i=0; i&lt;n; i++)    {        cout&lt;&lt;arr&#91;i&#93;&lt;&lt;&quot; &quot;;    }}int main(){    int a&#91;soptmax&#93;;    int i,n;    cout &lt;&lt; &quot;Nhap so phan tu thuc co cua mang a la n = &quot;;    cin&gt;&gt;n;    nhap(a,n);    xuat(a,n);    int vitri;    cout&lt;&lt;&quot;Nhap vi tri can xoa vitri = &quot;;    cin&gt;&gt;vitri;    if(xoa(a, n, vitri)==true)    {        xuat(a,n);    }    cout&lt;&lt;&quot;Nhap vi tri can them vitri = &quot;;    cin&gt;&gt;vitri;    int giatri;    cout&lt;&lt;&quot;Nhap gia tri can them giatri = &quot;;    cin&gt;&gt;giatri;    if(them(a,n, vitri,  giatri)==true)    {        xuat(a,n);    }    return 0;}</code></pre><h2><strong>Sắp xếp mảng</strong></h2><h3>*&nbsp;Dùng hàm có sẵn <strong>sort</strong></h3><pre><code class="language-cpp">#include &lt;iostream&gt;#include&lt;algorithm&gt;#include&lt;functional&gt; // std::greaterusing namespace std;long a&#91;1000&#93;;long n;int main(){//Tự code nhập, xuất để xem kq sắp xếp    sort(a, a+n); // sắp xếp mảng a tăng dần từ phần tử 0 đến phần tử n-1    sort(a, a+n, greater&lt;int&gt;()); // sắp xếp mảng a giảm dần từ p.tử 0 đến p.tử n-1    sort(a+1, a+1+n); // sắp xếp mảng a tăng dần từ phần tử 1 đến phần tử n   }</code></pre><p>* Tự code sắp xếp</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;void nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93;= &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void xuat(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;&quot; &quot;;    }}void sapxeptangdan(int a&#91;&#93;, int n){    int i = 0,j = 0, tam = 0;    for(i=0; i&lt;n; i++)    {        for(j=0; j&lt;n; j++)        {            if(a&#91;i&#93;&lt;a&#91;j&#93;)            {                tam=a&#91;i&#93;;                a&#91;i&#93;=a&#91;j&#93;;                a&#91;j&#93;=tam;            }        }    }}void sapxepgiamdan(int a&#91;&#93;, int n){    int i = 0,j = 0, tam = 0;    for(i=0; i&lt;n; i++)    {        for(j=0; j&lt;n; j++)        {            if(a&#91;i&#93;&gt;a&#91;j&#93;)            {                tam=a&#91;i&#93;;                a&#91;i&#93;=a&#91;j&#93;;                a&#91;j&#93;=tam;            }        }    }}int main(){    //Sap xep tang dan    int a&#91;100&#93;, n = 0;    cout&lt;&lt;&quot;N =&quot;;    cin&gt;&gt;n;    nhap(a,n);    sapxeptangdan(a,n);    xuat(a,n);    cout&lt;&lt;endl;    sapxepgiamdan(a,n);    xuat(a,n);    return 0;}</code></pre><p><br /><a href="https://dataonline.io.vn/#mlchude4"><strong>4.&nbsp;Chủ đề 4. Xâu kí tự.</strong></a><br /><i><strong>Mục tiêu:</strong></i><br />- Cách khai báo và sử dụng string.<br /><i><strong>Kiến thức:</strong></i><br />- Khai báo xâu string:<br />string tên_xâu;<br />string tên_xâu = &quot;&quot;;<br />string tên_xâu = &quot;xau ki tu&quot;;<br />- Nhập xâu từ bàn phím: dùng hàm&nbsp;<strong>getline(</strong>) như sau</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S, S1;    cout&lt;&lt;&quot;Nhap xau S = &quot;;    getline(cin,S); //Mac dinh ket thuc dong bang ki tu  duoc tao ra khi nhan phim Enter    cout&lt;&lt;&quot;Xau S vua nhap la: &quot;&lt;&lt;S&lt;&lt;endl;    cout&lt;&lt;&quot;Nhap xau S1 = &quot;;    /*Tuy nhien co the dung doi so thu 3 lam ki tu ket thuc nhap du lieu*/    getline(cin,S1,&#039;.&#039;);    cout&lt;&lt;&quot;Xau S1 vua nhap la: &quot;&lt;&lt;S1&lt;&lt;endl;    return 0;}</code></pre><p>- Độ dài xâu dùng hàm (phương thức):&nbsp;<strong>length()</strong>&nbsp;hoặc&nbsp;<strong>size()</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S = &quot;Xin    chao   tat ca    cac ban   &quot;;    int do_dai_xau = S.length();    cout&lt;&lt;&quot;Do dai xau S = &quot;&lt;&lt;do_dai_xau&lt;&lt;endl;    int do_dai_xau2 = S.size();    cout&lt;&lt;&quot;Do dai xau S = &quot;&lt;&lt;do_dai_xau2&lt;&lt;endl;    return 0;}</code></pre><p>- Xâu rỗng có độ dài bằng 0. Để kiểm tra xâu rông dùng phương thức&nbsp;<strong>empty()</strong>&nbsp;trả về giá trị <strong>true </strong>khi string rỗng, ngược lại, trả về giá trị <strong>false</strong>.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;&quot;;    cout&lt;&lt;&quot;Xau S co rong hay khong?: &quot;&lt;&lt;S.empty()&lt;&lt;endl;    return 0;}</code></pre><p><strong>Chuỗi con (Substring)</strong><br />+ xaucon = xaugoc.substr(Vị trí bắt đầu, Độ dài xâu con);<br />Vị trí bắt đầu &gt;= 0<br />Độ dài xâu con &lt;= Độ dại xâu gốc -1</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string  xaugoc = &quot;abcdef&quot;;    string xaucon;    xaucon=xaugoc.substr(0, 1); //a    cout &lt;&lt; xaucon &lt;&lt; endl;    xaucon=xaugoc.substr(2, 5); //cdef    cout &lt;&lt; xaucon &lt;&lt; endl;    xaucon=xaugoc.substr(0, 3); //abc    cout &lt;&lt; xaucon &lt;&lt; endl;    return 0;}</code></pre><p>Xóa sạch xâu - làm rỗng xâu:&nbsp;<strong>clear()</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Quyn&quot;;    cout&lt;&lt;&quot;Xau S co rong hay khong?: &quot;&lt;&lt;S.empty()&lt;&lt;endl;    S.clear();    cout&lt;&lt;&quot;Xau S co rong hay khong?: &quot;&lt;&lt;S.empty()&lt;&lt;endl;    return 0;}</code></pre><p>Truy cập phần tử trong string S: S&#91;chỉ số&#93; hoặc S.at(chỉ số); 0&lt;= chỉ số &lt;S.length()</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Vang Van Quyn&quot;;    int len = S.length();    int i=0;    for(i=0; i&lt;len; i++)    {        cout&lt;&lt;&quot;Ky tu: &quot;&lt;&lt;S&#91;i&#93;&lt;&lt;endl;    }    for(i=0; i&lt;len; i++)    {        cout&lt;&lt;&quot;Ky tu: &quot;&lt;&lt;S.at(i)&lt;&lt;endl;    }    return 0;}</code></pre><p>- Xóa kí tự cuối cùng nếu xâu không rỗng:&nbsp;<strong>pop_back()</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Vang Van Quyn&quot;;    int len = S.length();    cout&lt;&lt;&quot;Do dai S &quot;&lt;&lt;len&lt;&lt;endl;    S.pop_back();    cout&lt;&lt;&quot;Do dai S &quot;&lt;&lt;S.length()&lt;&lt;endl;    return 0;}</code></pre><p>- Nối xâu S:&nbsp;push_back(&#039;kí tự&#039;) nối kí tự vào cuối xâu,&nbsp;append(&quot;xâu kí tự&quot;), phép +</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Quy&quot;;    S.push_back(&#039;n&#039;);    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;S&lt;&lt;endl;    S.clear();    S.append(&quot;Vang &quot;);    S.append(&quot;Van &quot;);    S.append(&quot;Quyn&quot;);    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;S&lt;&lt;endl;    S.clear();    S=S+&quot;Vang &quot;;    S=S+&quot;Van &quot;;    S=S+&quot;Quyn&quot;;    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p>-&nbsp;Chèn một xâu vào vị trí bất kì trong xâu khác: <strong>insert (vị trí chèn, xâu chèn vào); 0&lt;= vị trí chèn&lt;= độ dài xâu</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Quyn&quot;;    S.insert(1,&quot;C++&quot;);    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p>-&nbsp;Thay thế một phần của xâu:<strong>&nbsp;replace (vị trí thay thế, số lượng kí tự thay thế, &nbsp;xâu sẽ thay vào);</strong><br /><strong>0&lt;= Vị trí thay thế &lt;= S.length()</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Hoc lap trinh Pascal&quot;;    S.replace(14, 6, &quot;C++&quot;);    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p>- Tìm vị trí xuất hiện của S1 trong S2;&nbsp;S2.find(S1); Nếu thấy thì trả về vị trí xuất hiện của S1 trong S2, ngược lại trả về -1.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Hoc lap trinh Pascal&quot;;    string S1=&quot;Pascal&quot;;    int vts1=S.find(S1);    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;vts1&lt;&lt;endl;    return 0;}</code></pre><p>- So sanh hai xâu S1 và S2:&nbsp;S1.compare(S2)<br />S1.compare(S2) == 0 &nbsp; &nbsp; &nbsp; nếu&nbsp; S1== S2<br />S1.compare(S2) &gt; 0 &nbsp; &nbsp; &nbsp; &nbsp; nếu&nbsp; S1&gt; S2<br />S1.compare(S2) &lt; 0 &nbsp; &nbsp; &nbsp; &nbsp; nếu S1&lt; S2<br /><strong>- Chuyển đổi số sang xâu và ngược lại</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int songuyen=45.786;    string ssonguyen=to_string(songuyen); //chuyen kieu int sang string    int so=stoi(ssonguyen);//chuyen kieu string sang int    long so1=stol(ssonguyen); //chuyen kieu string sang long    float so2=stof(ssonguyen); //chuyen kieu string sang float    double so3=stod(ssonguyen); //chuyen kieu string sang double    cout&lt;&lt;songuyen&lt;&lt;endl;    cout&lt;&lt;so&lt;&lt;endl;    cout&lt;&lt;so1&lt;&lt;endl;    cout&lt;&lt;so2&lt;&lt;endl;    cout&lt;&lt;so3&lt;&lt;endl;    return 0;}</code></pre><p><strong>- Chuyển đổi chữ HOA - thường và ngược lại:&nbsp;islower(), isupper(), tolower(), toupper()</strong><br />+ Chuyển chữ thường sang chữ HOA:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string str=&quot;vang vAN quyN&quot;;    int len = str.length();    int i=0, c;    for (i = 0; i &lt; len; i++)    {        c = str&#91;i&#93;;        if (islower(c))        {            //Chuyen ki tu sang chu HOA            str&#91;i&#93; = toupper(c);        }    }    cout&lt;&lt;str;    return 0;}</code></pre><p>+ Chuyển chữ HOA sang chữ thường:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string str=&quot;VAnG vAN qUyN&quot;;    int len = str.length();    int i=0, c;    for (i = 0; i &lt; len; i++)    {        c = str&#91;i&#93;;        if (isupper(c))        {            //Chuyen ki tu sang chu thuong            str&#91;i&#93; = tolower(c);        }    }    cout&lt;&lt;str;    return 0;}</code></pre><p><strong>-&nbsp;Tách xâu (sử dụng: stringstream trong thư viện: #include &lt;sstream&gt;).</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;sstream&gt;using namespace std;int main(){    string S = &quot;xin chao tat ca cac ban&quot;;    stringstream ss(S);    string token = &quot; &quot;; //Cac phan tach nhau boi dau cach hoac xuong dong.    while (ss &gt;&gt; token)   //Doc lan luot cac phan cua xau.    {        cout &lt;&lt; token &lt;&lt; endl;    }    return 0;}</code></pre><p><br /><strong>- Chuẩn hóa (xóa kí tự dấu cách thừa) string</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;sstream&gt;using namespace std;int main(){    string kq;    string S = &quot;xin     chao    tat     ca cac ban&quot;;    stringstream ss(S);    string token = &quot; &quot;; //Cac phan tach nhau boi dau cach hoac xuong dong.    while (ss &gt;&gt; token)   //Doc lan luot cac phan cua xau.    {        kq.append(token).append(&quot; &quot;);    }    kq.pop_back(); //Xoa ki tu cuoi cung cua xau. Hoac dung lenh:    //kq.replace(kq.length()-1,1,&quot;&quot;);    S=kq;    kq.clear(); //Xoa sach xau kq    cout &lt;&lt; S &lt;&lt; endl;    return 0;}</code></pre><p><br /><strong>- Chuẩn hóa (xóa kí tự dấu cách thừa) danh từ riêng.</strong></p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;sstream&gt;using namespace std;int main(){    string kq;    string S = &quot;xin     chao    tat     ca cac ban&quot;;    stringstream ss(S);    string token = &quot; &quot;; //Cac phan tach nhau boi dau cach hoac xuong dong.    char ch;    int i =0, len =0;    while (ss &gt;&gt; token)   //Doc lan luot cac phan cua xau.    {        len=tach.length();        //Doi cac chu ve chu thuong        for(i=1; i&lt;len; i++)        {            ch = tach.at(i);            tach.at(i)=tolower(ch);        }        ch = token.at(0);        token.at(0) = toupper(ch);        kq.append(token).append(&quot; &quot;);    }    kq.pop_back(); //Xoa ki tu cuoi cung cua xau. Hoac dung lenh:    //kq.replace(kq.length()-1,1,&quot;&quot;);    S=kq;    kq.clear(); //Xoa sach xau kq    cout &lt;&lt; S &lt;&lt; endl;    return 0;}</code></pre><p><br />- Hàm&nbsp;isdigit&nbsp;kiểm tra xem một kí tự có phải là chữ số hay không?</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S = &quot;abh45gghu6f8j7k9lpn&quot;;    int i =0, len=S.length();    for(i=0; i&lt;len; i++)    {        cout&lt;&lt;isdigit(S.at(i));    }    return 0;}</code></pre><p>Kết quả:&nbsp;0001100001010101000<br /><br /><a href="https://dataonline.io.vn/#mlchude5"><strong>5.&nbsp;Chủ đề 5. Đọc tệp, ghi tệp.</strong></a><br /><i><strong>Mục tiêu:</strong></i><br />- Đọc và ghi tệp.<br /><i><strong>Kiến thức:</strong></i></p><figure class="table"><table><tr><th>Kiểu dữ liệu</th><th>Miêu tả</th></tr><tbody><tr><td><strong>ofstream</strong></td><td>Kiểu dữ liệu này biểu diễn Output File Stream và được sử dụng để tạo các file và để ghi thông tin tới các file đó</td></tr><tr><td><strong>ifstream</strong></td><td>Kiểu dữ liệu này biểu diễn Input File Stream và được sử dụng để đọc thông tin từ các file</td></tr><tr><td>fstream</td><td>Kiểu dữ liệu này nói chung biểu diễn File Stream, và có các khả năng của cả ofstream và ifstream, nghĩa là nó có thể tạo file, ghi thông tin tới file và đọc thông tin từ file</td></tr></tbody></table></figure><figure class="table"><table><tr><th>&nbsp;&nbsp;Chế độ</th><th>Miêu tả</th></tr><tbody><tr><td><strong>ios::app</strong>&nbsp;&nbsp;</td><td><strong>Chế độ Append. Tất cả output tới file đó được phụ thêm vào cuối file đó</strong></td></tr><tr><td>ios::ate</td><td>Mở một file cho output và di chuyển điều khiển read/write tới cuối của file</td></tr><tr><td>ios::in</td><td>Mở một file để đọc</td></tr><tr><td><strong>ios::out</strong></td><td><strong>Mở một file để ghi (tạo file mới nếu chưa tồn tại)</strong></td></tr><tr><td>ios::trunc</td><td>Nếu file này đã tồn tại, nội dung của nó sẽ được cắt (truncate) trước khi mở file</td></tr></tbody></table></figure><p><br /><i><strong>Bước 1: Mở tập tin lên theo chức năng mong muốn (ghi hay đọc)</strong></i><br /><i><strong>Bước 2: Thực hiện các lệnh ghi hoặc đọc</strong></i><br /><i><strong>Bước 3: Đóng tập tin</strong></i><br />- Sử dụng:&nbsp;#include &lt;<strong>fstream</strong>&gt;<br /><strong>1. Cách đọc tệp C++</strong><br />B1. Mở tệp đọc<br />ifstream inFile(“Data.txt”,ios::in); &nbsp;// Mở tập tin có tên Data.txt, tập tin này nằm trong cùng thư mục tập tin code .cpp<br />//hoặc<br />ifstream inFile(“D:Data.txt”,ios::in);//Mở tập tin có tên Data.txt trong ổ đĩa D<br />hoặc<br />ifstream &nbsp;inFile;<br />inFile.open(“Data.txt”,ios::in);<br />B2.&nbsp;Việc đọc nội dung tập tin được chia làm 2 cách, bạn có thể đọc từng ký tự, từng byte trong tập tin cho đến hết tập tin. Hoặc bạn có thể đọc từng hàng của tập tin cho đến hết tập tin.<br /><strong>+ Sử dụng phương thức &gt;&gt;.</strong><br />Việc đọc nội dung tập tin được chia làm 2 cách, bạn có thể đọc từng ký tự, từng byte trong tập tin cho đến hết tập tin. Hoặc bạn có thể đọc từng hàng của tập tin cho đến hết tập tin.<br />int songuyen;<br />inFile&gt;&gt;songuyen;<br /><strong>+ Sử dụng phương thức get(char KT)</strong><br />ifstream inFile(“Data.txt”,ios::in);<br />char kt;<br />inFile.get(kt);// Đọc ký tự từ tập tin và lưu vào biến kt<br />cout&lt;&lt;kt;// In dữ liệu đọc được lên màn hình<br /><strong>+ Sử dụng phương thức getline(char CH&#91;&#93;, int N)</strong><br />ifstream inFile(“Data.txt”,ios::in);<br />char CHUOI&#91;100&#93;;<br />inFile.getline(CHUOI,100);//Đọc dữ liệu từ tập tin theo dòng vào chuỗi với độ dài chuỗi là 100 ký tự<br />cout&lt;&lt;CHUOI&lt;&lt;endl; //In dữ liệu đọc được lên màn hình<br />B3: Đóng tập tin<br />inFile.close();<br /><strong>2.&nbsp;Cách ghi tập tin trong C++</strong><br />B1. Mở tệp<br />ofstream outFile(“Data.txt”, ios::out);//Mở tập tin để ghi nội dung vào<br />B2. Ghi nội dung<br />+ Sử dụng phương thức operator &lt;&lt;<br />ofstream outFile(“Data.txt”,ios::out);<br />char KT&#91;&#93;=“Trung tam tin hoc Tay Viet”;<br />outFile &lt;&lt; KT;//Ghi toàn bộ nội dung trong chuỗi KT vào tập tin<br />+ Sử dụng phương thức put(char KT)<br />ofstream outFile(“Data.txt”,ios::out);<br />char kt;<br />get(kt); //Nhập ký tự cần ghi vào tập tin<br />outFile.put(kt);<br />B3: Đóng tập tin<br />outFile.close();<br />Hàm eof()<br />&nbsp; &nbsp; &nbsp; Cấu trúc hàm: &nbsp;bool eof() const;<br />&nbsp; &nbsp; &nbsp; Hàm eof() là hàm kiểm tra đã hết tập tin chưa. Nếu đã hết tập tin thì hàm trả về true ( tương đương với giá trị khác 0), ngược lại nếu chưa hết tập tin thì kết quả sẽ trả về false ( tương đương với giá trị là 0).<br />Hàm fail()<br />&nbsp; &nbsp; &nbsp; Cấu trúc hàm: &nbsp;bool fail() const;<br />&nbsp; &nbsp; &nbsp; Hàm fail() là hàm phát hiện có xảy ra lỗi không trầm trọng trong quá trình đọc tập tin. Nếu phát hiện ra lỗi trong quá trình đọc tập tin thì hàm trả về true ( tương đương với giá trị khác 0), ngược lại nếu không thì kết quả sẽ trả về false ( tương đương với giá trị là 0). Ví dụ trong chương trình, chúng ta đọc tập tin sau khi đã đóng thì sẽ xuất hiện lỗi và hàm này sẽ cho giá trị true<br />Hàm bad()<br />&nbsp; &nbsp; &nbsp; Cấu trúc hàm: &nbsp;bool bad() &nbsp;const;<br />&nbsp; &nbsp; &nbsp; Hàm bad() là hàm phát hiện có xảy ra lỗi trầm trọng trong quá trình ghi tập tin. Nếu phát hiện ra lỗi trong quá trình ghi tập tin thì hàm trả về true ( tương đương với giá trị khác 0), ngược lại nếu không thì kết quả sẽ trả về false ( tương đương với giá trị là 0). Ví dụ trong chương trình, chúng ta ghi nội dung vào tập tin đã đóng thì sẽ xuất hiện lỗi và hàm này sẽ cho giá trị true.<br />Hàm good()<br />&nbsp; &nbsp; &nbsp; &nbsp;Cấu trúc hàm: &nbsp;bool good() const;<br />&nbsp; &nbsp; &nbsp; Hàm good() là hàm kiểm tra hoàn thiện việc ghi và đọc tập tin. Hàm trả về true ( tương đương với giá trị khác 0) khi tất cả các hàm eof(), fail() và bad() đều trả về giá trị 0, ngược lại chỉ cần 1 trong các hàm eof(), fail() và bad() trả về 1 thì kết quả của hàm good sẽ trả về false ( tương đương với giá trị là 0).<br />Con trỏ trong tập tin<br />&nbsp; &nbsp; &nbsp; &nbsp;Trong quá trình đọc và ghi File trong lập trình C++ thì chúng ta cần phải biết con trỏ tập tin hiện đang ở vị trí nào, điều này sẽ giúp ta rất nhiều trong việc tương tác với tập tin. Chẳng hạn bạn muốn thêm, thay đổi hoặc xóa một nội dung nào đó thì chúng ta cần phải biết chính xác vị trí cần thao tác để tránh việc làm sai dữ liệu.<br />a. Hàm trong class ifstream<br />&nbsp; &nbsp; &nbsp; &nbsp;a.1 Hàm tellg()<br />&nbsp; &nbsp; &nbsp; Hàm này được sử dụng để tìm vị trí hiện tại của con trỏ tập tin. Vị trí hiện tại của con trỏ trong tập tin chính là tổng số byte tính từ đầu tập tin đến vị trị của con trỏ.<br />&nbsp; &nbsp; &nbsp; &nbsp;a.2 Hàm seekg()<br />&nbsp; &nbsp; &nbsp; &nbsp;Hàm seekg() là hàm được sử dụng để dịch chuyên con trỏ đến vị trí nào đó. Việc di chuyển con trỏ trong tập tin cần phải có vị trí làm mốc, vị trí này có thể là đầu tập tin, cuối tập tin hoặc là vị trí hiện tại của con trỏ, điều này phụ thuộc vào tham số trong hàm. Hàm seekg có 2 cú pháp như sau:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Cú pháp 1: &nbsp;seekg(&lt;số byte cần dịch chuyển&gt;);<br />&nbsp; &nbsp; &nbsp; &nbsp;Với cú pháp 1 này thực hiện chức năng dịch chuyển con trỏ sang phải với số byte đúng bằng giá trị truyền vào. Mốc dịch chuyển là vị trí đầu của tập tin.<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Cú pháp 2: seekg(&lt;số byte cần dịch chuyển&gt;,&lt;mốc dịch chuyển&gt;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Mốc dịch chuyển là 1 trong các tham số sau:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ ios::beg : Mốc là vị trí đầu tập tin<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ ios::cur : Mốc là vị trí hiện tại của con trỏ<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ ios::end : Mốc là vị trí cuối của tập tin<br />&nbsp; &nbsp; &nbsp; &nbsp; b. Hàm trong class ofstream<br />&nbsp; &nbsp; &nbsp; &nbsp; b.1 Hàm tellp()<br />&nbsp; &nbsp; &nbsp; Tương tự như Hàm này được sử dụng để tìm vị trí hiện tại của con trỏ tập tin. Vị trí hiện tại của con trỏ trong tập tin chính là tổng số byte tính từ đầu tập tin đến vị trị của con trỏ.<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b.2 Hàm seekp()<br />&nbsp; &nbsp; &nbsp; &nbsp;Cũng giống như hàm seekg(), hàm seekp() cũng được sử dụng để dịch chuyên con trỏ đến vị trí nào đó trong tập tin. Và trong trường hợp này cũng sử dụng 2 cú pháp như trên<br />&nbsp; &nbsp; &nbsp; &nbsp; Cú pháp 1: &nbsp;seekp(&lt;số byte cần dịch chuyển&gt;);<br />&nbsp; &nbsp; &nbsp; &nbsp;Với cú pháp 1 này thực hiện chức năng dịch chuyển con trỏ sang phải với số byte đúng bằng giá trị truyền vào. Mốc dịch chuyển là vị trí đầu của tập tin.<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Cú pháp 2: seekp(&lt;số byte cần dịch chuyển&gt;,&lt;mốc dịch chuyển&gt;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Mốc dịch chuyển là 1 trong các tham số sau:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ ios::beg : Mốc là vị trí đầu tập tin<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ ios::cur : Mốc là vị trí hiện tại của con trỏ<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ ios::end : Mốc là vị trí cuối của tập tin<br />Ví dụ hoàn chỉnh:<br />#include&lt;iostream&gt;<br />#include&lt;fstream&gt;<br />#include&lt;cstring&gt;<br />#include&lt;stdio.h&gt;<br />#include&lt;conio.h&gt;<br />using namespace std;<br />int main()<br />{<br />//Chương trình ghi vào tập tin<br />ofstream outFile(“Data.txt”,ios::out);<br />if(!outFile)<br />cout&lt;&lt;“Khong mo duoc tap tin ”;<br />else<br />{<br />char CH&#91;100&#93;;<br />int tuoi;<br />cout&lt;&lt;“Nhap vao ten cua ban: “;<br />gets(CH);<br />for(int i=0;i&lt;strlen(CH);i++)//Cách ghi từng ký tự hoặc từng byte dữ liệu vào tập tin<br />outFile.put(CH&#91;i&#93;);<br />outFile&lt;&lt;“ ”;//Ghi ký tự xuống dòng để phân biệt hàng họ tên và tuổi trong tập tin<br />cout&lt;&lt;“Nhap vao tuoi ban: “;<br />cin&gt;&gt;tuoi;<br />outFile&lt;&lt;tuoi;//Cách ghi dữ liệu bất kỳ bằng toán tử &lt;&lt; vào tập tin<br />cout&lt;&lt;“Da ghi tap tin thanh cong ”;<br />outFile.close();<br />}<br />//Chương trình đọc tập tin<br />ifstream inFile(“Data.txt”,ios::in);<br />if(!inFile)<br />cout&lt;&lt;“Khong mo duoc tap tin”&lt;&lt;endl;<br />else<br />{<br />cout&lt;&lt;“ Noi dung doc duoc la:”;<br />char Ten&#91;100&#93;, age;<br />inFile.getline(Ten,100); //Đọc chuỗi họ tên<br />inFile.get(age); //Đọc tuổi<br />cout&lt;&lt;“ Ten cua ban: “&lt;&lt;Ten&lt;&lt;endl; //Xuất dữ liệu lên màn hình<br />cout&lt;&lt;“Tuoi ban: “&lt;&lt;age;<br />inFile.close();<br />}<br />getch();<br />}<br /><strong>Câu 1. Cho số nguyên N trong tệp Cau1.INP. Xuất ra màn hình kết quả kiểm tra xem N là số chẵn hay số ra màn hình</strong></p><figure class="table"><table><tbody><tr><td>INPUT</td><td>OUTPUT</td></tr><tr><td>6</td><td>6 la so chan</td></tr><tr><td>7</td><td>7 la so le</td></tr></tbody></table></figure><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;fstream&gt;using namespace std;int main(){    ifstream  fin;    fin.open(&quot;BAI1.INP&quot;,ios::in);    int N;    if(fin.fail())    {        cout&lt;&lt;&quot;Loi mo tep&quot;;        fin.close();    }    else    {        fin&gt;&gt;N;        fin.close();    }    if(N%2==0)    {        cout&lt;&lt;N&lt;&lt;&quot; la so chan&quot;;    }    else    {        cout&lt;&lt;N&lt;&lt;&quot; la so le&quot;;    }    return 0;}</code></pre><p><strong>Câu 2. Cho hai số nguyên a và b trong tệp Cau2.INP, mỗi số một dòng. Xuất ra màn hình kết quả tích a và b ra màn hình</strong></p><figure class="table"><table><tbody><tr><td>INPUT</td><td>OUTPUT</td></tr><tr><td>6<br />9</td><td>54</td></tr></tbody></table></figure><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;fstream&gt;using namespace std;int main(){    int a,b;    ifstream docvao;    docvao.open(&quot;Cau2.INP&quot;,ios::in);    if(docvao.fail())    {        cout&lt;&lt;&quot; loi tep &quot;;    }    else    {        docvao&gt;&gt;a;        docvao&gt;&gt;b;    }    int tich= a*b;    cout&lt;&lt;tich;    return 0;}</code></pre><p><strong>Câu 3. Cho hai số nguyên a và b trong tệp Cau3.INP, mỗi số một dòng. Ghi kết quả tính tổng và tích a và b ra tệp Cau3.OUT, mỗi kết quả ghi một dòng</strong>.</p><figure class="table"><table><tbody><tr><td><strong>Cau3.INP</strong></td><td><strong>Cau3.OUT</strong></td></tr><tr><td>6<br />9</td><td>15<br />54</td></tr></tbody></table></figure><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;fstream&gt;using namespace std;int main(){    ifstream fin;    ofstream fout;    int a, b, Tong = 0, Tich = 0;    fin.open(&quot;Cau3.INP&quot;,ios::in);    fout.open(&quot;Cau3.OUT&quot;,ios::out); //Tu dong tao file moi neu chua co    if(fin.fail())    {        fout&lt;&lt;&quot;Khong mo duoc tep&quot;;    }    else    {        fin&gt;&gt;a;        fin&gt;&gt;b;        Tong=a+b;        Tich=a*b;    }    fin.close();    fout&lt;&lt;Tong&lt;&lt;endl;    fout&lt;&lt;Tich;    fout.close();    return 0;}</code></pre><p><strong>Câu 4. Đọc dãy số từ tệp vào mảng.</strong></p><p><strong>Câu 4.</strong> Cho tệp Cau4.INP dòng một chứa ba số nguyên a, b, n cách nhau ít nhất một dấu cách; dòng hai chứa một dãy số gồm n phần tử (n số) cách nhau ít nhất một dấu cách. Tính tích a và b; Tính tổng giá trị các phần tử của dãy số đã cho.</p><figure class="table"><table><tbody><tr><td><p style="text-align:center;"><strong>Dữ liệu vào (Cau4.INP)</strong></p></td><td><p style="text-align:center;"><strong>Dữ liệu ra (Cau4.OUT)</strong></p></td><td><p style="text-align:center;"><strong>Ghi chú</strong></p></td></tr><tr><td><p>6 9 5</p><p>5 6 8 9 7</p></td><td><p>54</p><p>35</p></td><td><p style="text-align:center;">&nbsp;</p></td></tr><tr><td><p>8 7 10</p><p>5 6 8 9 7 2 6 7 9 10</p></td><td><p>56</p><p>69</p></td><td><p style="text-align:center;">&nbsp;</p></td></tr></tbody></table></figure><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;fstream&gt;using namespace std;int main(){    int a,b, n;    int i=0;    int day&#91;100&#93;;    int tong=0;    ifstream fin;    ofstream fout;    fin.open(&quot;Cau2.INP&quot;,ios::in);    fout.open(&quot;Cau2.OUT&quot;,ios::out);    if(fin.fail())    {        fout&lt;&lt;&quot;Loi doc tep&quot;;    }    else    {        fin&gt;&gt;a;        fin&gt;&gt;b;        fin&gt;&gt;n;        for(i=0; i&lt;n; i++)        {            fin&gt;&gt;day&#91;i&#93;;        }    }    int tich= a*b;    fout&lt;&lt;tich&lt;&lt;endl;    for(i=0; i&lt;n; i++)    {        tong=tong+day&#91;i&#93;;    }    fout&lt;&lt;tong;    fin.close();    fout.close();    return 0;}</code></pre><p><br /><a href="https://dataonline.io.vn/#mlchude6"><strong>6.&nbsp;Chủ đề 6. Mảng 2 chiều (Ma trận)</strong></a><br /><br /><br /><a href="https://dataonline.io.vn/#mlchude7"><strong>7.&nbsp;Chủ đề 7. Luyện tập-Giải đề chuyên.</strong></a><br />- Làm quen với đề các năm<br />&nbsp;</p>
		</div>
				<div id="author">
						<p>
				<strong>Tác giả:</strong>
				<a href="https://dataonline.io.vn/author/Vang-Van-Quyn/">Vàng Văn Quyn</a>
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://365.io.vn/savefile/tai-lieu-hoc-lap-trinh/tai-lieu-c-va-codeblocks-v1-14.html" title="Tài liệu C++ và CodeBlocks V1">https://365.io.vn/savefile/tai-lieu-hoc-lap-trinh/tai-lieu-c-va-codeblocks-v1-14.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; DATAONLINE.IO.VN
		</div>
		<div id="contact">
			<a href="mailto:support@dataonline.io.vn">support@dataonline.io.vn</a>
		</div>
	</div>
</div>
        <div class="cookie-notice"><div><button onclick="cookie_notice_hide();">&times;</button>Chúng tôi đang sử dụng cookie để cung cấp cho bạn những trải nghiệm tốt nhất trên trang web này. Bằng cách tiếp tục truy cập, bạn đồng ý với <a href="https://dataonline.io.vn/siteterms/privacy.html">Chính sách thu thập và sử dụng cookie</a> của chúng tôi.</div></div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://dataonline.io.vn/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
<div id="run_cronjobs" style="visibility:hidden;display:none;"><img alt="cron" src="/index.php?second=cronjobs&amp;p=z53w3M9V" width="1" height="1" /></div>
<script src="https://dataonline.io.vn/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://dataonline.io.vn/assets/js/language/vi.js"></script>
<script src="https://dataonline.io.vn/assets/js/DOMPurify/purify3.js"></script>
<script src="https://dataonline.io.vn/assets/js/global.js"></script>
<script src="https://dataonline.io.vn/assets/js/site.js"></script>
<script src="https://dataonline.io.vn/themes/default/js/news.js"></script>
<script src="https://dataonline.io.vn/themes/default/js/main.js"></script>
<script src="https://dataonline.io.vn/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://dataonline.io.vn",
            "logo": "https://dataonline.io.vn/assets/images/logo.svg"
        }
        </script>
<script src="https://dataonline.io.vn/themes/default/js/bootstrap.min.js">
        </script>
<script>
    var LIMIT = 600000; // 10 phút

    var startTime = sessionStorage.getItem("redirect_timer");
    if (!startTime) {
        startTime = Date.now();
        sessionStorage.setItem("redirect_timer", startTime);
    } else {
        startTime = parseInt(startTime, 10);
    }

    var elapsed = Date.now() - startTime;
    var remain = LIMIT - elapsed;

    if (remain > 0) {
        setTimeout(function () {
            alert("Hệ thống sẽ chuyển sang trang chấm bài.");
            window.location.replace("https://vnoj.io.vn");
        }, remain);
    } else {
        window.location.replace("https://vnoj.io.vn");
    }
</script>
</body>
</html>