<!DOCTYPE html>
	<html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
	<head>
<title>Tài liệu C++ và CodeBlocks</title>
<meta name="description" content="Tài liệu C++ và CodeBlocks - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;tinhocdct.xyz&#x002F;savefile&#x002F;hoat-dong-chuyen-mon&#x002F;tai-lieu-c-va-codeblocks-106.html">
<meta name="author" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG">
<meta name="copyright" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG [webmaster@tinhocdct.xyz]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Tài liệu C++ và CodeBlocks">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;tinhocdct.xyz&#x002F;savefile&#x002F;hoat-dong-chuyen-mon&#x002F;tai-lieu-c-va-codeblocks-106.html">
<meta property="og:site_name" content="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG">
<meta property="og:url" content="https://tinhocdct.xyz/hoat-dong-chuyen-mon/tai-lieu-c-va-codeblocks-106.html">
<link rel="shortcut icon" href="https://tinhocdct.xyz/favicon.ico">
<link rel="canonical" href="https://tinhocdct.xyz/hoat-dong-chuyen-mon/tai-lieu-c-va-codeblocks-106.html">
<link rel="alternate" href="https://tinhocdct.xyz/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tin-tuc/" title="Tin Tức - TIN TỨC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/thong-cao-bao-chi/" title="Tin Tức - THÔNG BÁO" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/ban-tin-noi-bo/" title="Tin Tức - BẢN TIN NỘI BỘ" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tin-cong-nghe/" title="Tin Tức - CÔNG NGHỆ - KHOA HỌC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/thu-thuat/" title="Tin Tức - THỦ THUẬT" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tien-ich/" title="Tin Tức - TIỆN ÍCH" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/ai/" title="Tin Tức - AI" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/khoa-hoc/" title="Tin Tức - KHOA HỌC" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-chuyen-mon/" title="Tin Tức - HOẠT ĐỘNG CHUYÊN MÔN" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-cong-doan/" title="Tin Tức - CÔNG ĐOÀN" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/hoat-dong-doan-the/" title="Tin Tức - ĐOÀN THỂ" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tuyen-sinh-huong-nghiep/" title="Tin Tức - TUYỂN SINH - HƯỚNG NGHIỆP" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/tuyen-sinh-2025/" title="Tin Tức - TUYỂN SINH" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/huong-nghiep/" title="Tin Tức - HƯỚNG NGHIỆP" type="application/rss+xml">
<link rel="alternate" href="https://tinhocdct.xyz/rss/lich-cong-tac-tuan/" title="Tin Tức - Lịch công tác tuần" type="application/rss+xml">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/jquery/jquery.min.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/language/vi.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/assets/js/global.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/news.js">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/main.js">
<link rel="preload" as="script" href="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1816161148536198">
<link rel="preload" as="script" href="https://tinhocdct.xyz/themes/default/js/bootstrap.min.js">
<link rel="StyleSheet" href="https://tinhocdct.xyz/assets/css/font-awesome.min.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/bootstrap.non-responsive.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/style.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://tinhocdct.xyz/themes/default/css/news.css">
<style>
	body{background: #fff;}
</style>
	</head>
	<body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG</h2>
		<p class="pull-right"><a title="TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG" href="https://tinhocdct.xyz/">https://tinhocdct.xyz</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Tài liệu C++ và CodeBlocks</h1>
		<ul class="list-inline">
			<li>Chủ nhật - 11/04/2021 23:55</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">
			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 THPT thay thế ngôn ngữ lập trình Pascal.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.
		</div>
				<div class="imghome">
			<img alt="Tài liệu C++ và CodeBlocks" src="https://tinhocdct.xyz/uploads/news/2021_04/codeblock2.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<strong>Mục lục</strong><br  />
<strong><a href="http://tinhocdct.xyz/hoat-dong-chuyen-mon/tai-lieu-c-va-codeblocks-106.html#hdcaicode" id="mlhdcaicode">I. Hướng dẫn sử cài và dụng CodeBlocks để thực hành c++</a><br  />
II. Tài liệu C ++</strong>
<h2><a href="http://tinhocdct.xyz/hoat-dong-chuyen-mon/tai-lieu-c-va-codeblocks-106.html#chude1" id="mlchude1" name="mlchude1">1. Chủ đề 1. Làm quen với C++.</a><br  />
<a href="http://tinhocdct.xyz/hoat-dong-chuyen-mon/tai-lieu-c-va-codeblocks-106.html#chude2" id="mlchude2" name="mlchude2">2. Chủ đề 2. Lệnh điều kiện, lệnh lặp.</a><br  />
<a href="http://tinhocdct.xyz/hoat-dong-chuyen-mon/tai-lieu-c-va-codeblocks-106.html#chude3" id="mlchude3" name="mlchude3">3. Chủ đề 3. Mảng một chiều (số nguyên, số thực).</a><br  />
<a href="http://tinhocdct.xyz/hoat-dong-chuyen-mon/tai-lieu-c-va-codeblocks-106.html#chude4" id="mlchude4" name="mlchude4">4. Chủ đề 4. Xâu kí tự.</a><br  />
<a href="http://tinhocdct.xyz/hoat-dong-chuyen-mon/tai-lieu-c-va-codeblocks-106.html#chude5" id="mlchude5" name="mlchude5">5. Chủ đề 5. Đọc tệp, ghi tệp.</a><br  />
&nbsp;</h2>

<hr  /><a id="hdcaicode" name="hdcaicode"><strong>I. Hướng dẫn sử cài và dụng&nbsp;CodeBlocks để thực hành c++</strong></a><br  />
<strong>1. Tải và cài&nbsp;CodeBlock.</strong><br  />
<a href="http://www.codeblocks.org/downloads/26" target="_blank">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" target="_blank"><strong>codeblocks-20.03mingw-setup.exe</strong></a><br  />
- Đối với&nbsp;Windowsn 32bít:&nbsp;Chọn tệp có tên:&nbsp;<a href="https://www.mediafire.com/file/5ifovyswrdgx6zl/codeblocks-20.03-32bit-mingw-32bit-setup.zip/file" target="_blank"><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>&nbsp;<a href="https://xn--nhitrc-tya.vn/tai-lieu-boi-duong-hoc-sinh-gioi-va-thi-vao-10/huong-dan-fix-loi-khi-cai-ban-codeblocks-20-03-32bit-mingw-32bit-96.html" target="_blank">Click vào đây để xem hướng dẫn fix lỗi.</a><br  />
- Đặt phím tắt để tự định dạng code.<br  />
B1. Như hình sau<br  />
<img alt="" src="https://i.imgur.com/9sT0zzD.png" /><br  />
B2.&nbsp;Như hình sau<br  />
<img alt="" height="452" src="https://i.imgur.com/oM9ORFU.png" width="541" /><br  />
<strong>2. Tạo&nbsp;project mới chạy thử chương trình đầu tiên.</strong><br  />
<img alt="" height="401" src="https://i.imgur.com/AYceq8q.png" width="541" /><br  />
<img alt="" height="422" src="https://i.imgur.com/9nNnb9W.png" width="541" /><br  />
<img alt="" src="https://i.imgur.com/4Tzz3RH.png" /><br  />
<img alt="" src="https://i.imgur.com/H843XuN.png" /><br  />
<img alt="" src="https://i.imgur.com/ged7Cyz.png" /><br  />
<img alt="" height="173" src="https://i.imgur.com/gHFN1PN.png" width="541" /><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  />
<strong><a id="chude1" name="chude1"></a><a href="https://xn--nhitrc-tya.vn/tai-lieu-boi-duong-hoc-sinh-gioi-va-thi-vao-10/tai-lieu-c-va-codeblocks-22.html#mlchude1">1. Chủ đề 1. Làm quen với C++.</a></strong><br  />
<em><strong>Mục tiêu:</strong></em><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  />
<em><strong>Kiến thức:</strong></em>

<table border="1" cellpadding="1" cellspacing="1">
	<caption><strong>Các phép toán trong C++ (Toán tử số học)</strong></caption>
	<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>
<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;<br  />
cout&lt;&lt;Dữ liệu xuất&lt;&lt;endl;</strong><br  />
<strong>Ví dụ 1:</strong>

<pre>
<code>#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>
<strong>kết quả:</strong><br  />
<strong><img alt="" src="https://i.imgur.com/q4AdIEd.jpg" /><br  />
Xuất định dạng mọi kiểu dữ liệu</strong><b><strong>:</strong>&nbsp;cout&lt;&lt;setw(n)&lt;&lt;căn lề &lt;&lt;dữ liệu xuất;</b><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><b>left&nbsp;</b>hoặc&nbsp;<b>right</b><br  />
Số thực có đinh dạng và số chữ số thập phân<br  />
<b>cout &lt;&lt;setprecision(n)&lt;&lt; fixed&lt;&lt;dữ liệu số thực;</b><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.
<pre>
<code>#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>
Kết quả:<br  />
<img alt="" src="https://i.imgur.com/x1XMhQF.jpg" /><br  />
Giải thích:&nbsp;<strong>#include &lt;iostream&gt;</strong>&nbsp;để dùng&nbsp;hàm xuất nhập&nbsp;<strong>cout&nbsp;</strong>và&nbsp;<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;<br  />
#include &lt;iomanip&gt;&nbsp;</strong><b>setw(n), ::setfill(ch), left, setprecision(n)<br  />
Các kiểu dữ liệu cơ bản của C++</b><br  />
&nbsp;
<table border="1" cellpadding="1" cellspacing="1">
	<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>
		</tr>
		<tr>
			<td>1</td>
			<td><strong>int</strong></td>
			<td>&nbsp;</td>
			<td><strong>Số nguyên</strong></td>
		</tr>
		<tr>
			<td>2</td>
			<td><strong>float</strong></td>
			<td>&nbsp;</td>
			<td><strong>Số thực</strong></td>
		</tr>
		<tr>
			<td>3</td>
			<td><strong>char</strong></td>
			<td>&nbsp;</td>
			<td><strong>Kí tự</strong></td>
		</tr>
		<tr>
			<td>4</td>
			<td><strong>string</strong></td>
			<td>&nbsp;</td>
			<td><strong>Xâu kí tự</strong></td>
		</tr>
		<tr>
			<td>5</td>
			<td><strong>bool</strong></td>
			<td>&nbsp;</td>
			<td><strong>Logic</strong></td>
		</tr>
		<tr>
			<td>6</td>
			<td><strong>long</strong></td>
			<td>&nbsp;</td>
			<td><strong>Số nguyên</strong></td>
		</tr>
		<tr>
			<td>7</td>
			<td><strong>short</strong></td>
			<td>&nbsp;</td>
			<td><strong>Số nguyên</strong></td>
		</tr>
		<tr>
			<td>8</td>
			<td>double</td>
			<td>&nbsp;</td>
			<td>Số thực</td>
		</tr>
		<tr>
			<td>9</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
	</tbody>
</table>
<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>

<pre>
<code>#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>
-Kết quả:<br  />
<img alt="" height="274" src="https://i.imgur.com/wvhTS7q.png" width="541" /><br  />
<strong><a id="chude2" name="chude2"></a><a href="https://xn--nhitrc-tya.vn/tai-lieu-boi-duong-hoc-sinh-gioi-va-thi-vao-10/tai-lieu-c-va-codeblocks-22.html#mlchude2">2. Chủ đề 2. Lệnh điều kiện, lệnh lặp.</a></strong><br  />
<em><strong>Mục tiêu:</strong></em><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  />
<em><strong>Kiến thức:</strong></em>

<table border="1" cellpadding="1" cellspacing="1">
	<caption><strong>Các phép so sánh (toán tử quan hệ)<br  />
	Ví dụ: int a = 10, int b= 20;</strong></caption>
	<tbody>
		<tr>
			<td><strong>Phép so sánh</strong></td>
			<td><strong>Kí hiệu<br  />
			(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>

<table border="1" cellpadding="1" cellspacing="1">
	<caption>Phép logic (toán tử logic)<br  />
	Ví dụ: A = true; B = false</caption>
	<tbody>
		<tr>
			<td><strong>Phép Logic</strong></td>
			<td><strong>Kí kiệu<br  />
			(</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>
Ví dụ dùng phép logic &amp;&amp;, || !

<pre>
<code>#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>
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>

<pre>
<code>#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>
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>

<pre>
<code>#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>
<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>

<pre>
<code>#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>
<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:
<pre>
<code>#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>
thay thê cặp lệnh if ...else sau:

<pre>
<code>#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>
<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
<pre>
<code>#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>
<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
<pre>
<code>#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>
<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
<pre>
<code>#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>
<br  />
<strong><a id="chude3" name="chude3"></a><a href="https://xn--nhitrc-tya.vn/tai-lieu-boi-duong-hoc-sinh-gioi-va-thi-vao-10/tai-lieu-c-va-codeblocks-22.html#mlchude3">3.&nbsp;Chủ đề 3. Mảng một chiều (số nguyên, số thực).</a></strong><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  />
<em><strong>Mục tiêu:</strong></em><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  />
<em><strong>Kiến thức:</strong></em><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  />
<em><strong>Ví dụ khai báo mảng.</strong></em><br  />
Cách 1: Khởi tạo giá trị cho mọi phần tử của mảng:
<pre>
<code>int array&#91;4&#93; = { 5, 8, 2, 7 };</code></pre>
Cách 2: Khởi tạo giá trị cho một số phần tử đầu mảng:

<pre>
<code>int array&#91;4&#93; = { 5, 8 };</code></pre>
Cách 3: Khởi tạo giá trị 0 cho mọi phần tử của mảng

<pre>
<code>int array&#91;4&#93; = { };</code></pre>
Cách 4: Tự động xác định số lượng phần tử

<pre>
<code>int array&#91;&#93; = { 5, 8, 2, 7 };</code></pre>
<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>

<pre>
<code>int array1&#91;4&#93; { 5, 8, 2, 7 }; // 5 8 2 7
int array2&#91;4&#93; { 5, 8 }; // 5 8 0 0
int array3&#91;4&#93; { }; // 0 0 0 0
int array4&#91;&#93; { 5, 8, 2, 7 }; // 5 8 2 7
</code></pre>
<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>

<pre>
<code>#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>
<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>

<pre>
<code>#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>
<strong>Ví dụ về sử dụng mảng (nhập, xuất, xoa, thêm phần tử).</strong>

<pre>
<code>#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 while
void 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 for
void RemoveDuplicates(int arr&#91;&#93;, int n)
{
    for(int i=0; i&lt;n; i++)
    {
        for(int j=i+1; j&lt;n;)
        {
            if(arr&#91;i&#93;==arr&#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&nbsp;<strong>sort</strong></h3>

<pre>
<code>#include &lt;iostream&gt;
#include&lt;algorithm&gt;
#include&lt;functional&gt; // std::greater
using 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>
* Tự code sắp xếp

<pre>
<code>#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>
<br  />
<strong><a id="chude4" name="chude4"></a><a href="https://xn--nhitrc-tya.vn/tai-lieu-boi-duong-hoc-sinh-gioi-va-thi-vao-10/tai-lieu-c-va-codeblocks-22.html#mlchude4">4.&nbsp;Chủ đề 4. Xâu kí tự.</a></strong><br  />
<em><strong>Mục tiêu:</strong></em><br  />
- Cách khai báo và sử dụng string.<br  />
<em><strong>Kiến thức:</strong></em><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

<pre>
<code>#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 \n 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>
- Độ dài xâu dùng hàm (phương thức):&nbsp;<strong>length()</strong>&nbsp;hoặc&nbsp;<strong>size()</strong>

<pre>
<code>#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>
- 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ị&nbsp;<strong>true&nbsp;</strong>khi string rỗng, ngược lại, trả về giá trị&nbsp;<strong>false</strong>.

<pre>
<code>#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>
Xóa sạch xâu - làm rỗng xâu:&nbsp;<strong>clear()</strong>

<pre>
<code>#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>
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()

<pre>
<code>#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>
- Xóa kí tự cuối cùng nếu xâu không rỗng:&nbsp;<strong>pop_back()</strong>

<pre>
<code>#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>
- 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 +

<pre>
<code>#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>
-&nbsp;Chèn một xâu vào vị trí bất kì trong xâu khác:&nbsp;<strong>insert (vị trí chèn, xâu chèn vào);&nbsp;0&lt;= vị trí chèn&lt;= độ dài xâu</strong>

<pre>
<code>#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>
-&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);<br  />
0&lt;= Vị trí thay thế &lt;= S.length()</strong>

<pre>
<code>#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>
- 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.

<pre>
<code>#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>
- 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>

<pre>
<code>#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>
<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:
<pre>
<code>#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>
+ Chuyển chữ HOA sang chữ thường:

<pre>
<code>#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>
<strong>-&nbsp;Tách xâu (sử dụng: stringstream trong thư viện: #include &lt;sstream&gt;).</strong>

<pre>
<code>#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>
<br  />
<strong>- Chuẩn hóa (xóa kí tự dấu cách thừa) string</strong>

<pre>
<code>#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>
<br  />
<strong>- Chuẩn hóa (xóa kí tự dấu cách thừa) danh từ riêng.</strong>

<pre>
<code>#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>
<br  />
- Hàm&nbsp;isdigit&nbsp;kiểm tra xem một kí tự có phải là chữ số hay không?
<pre>
<code>#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>
Kết quả:&nbsp;0001100001010101000<br  />
<br  />
<strong><a id="chude5" name="chude5"></a><a href="https://xn--nhitrc-tya.vn/tai-lieu-boi-duong-hoc-sinh-gioi-va-thi-vao-10/tai-lieu-c-va-codeblocks-22.html#mlchude5">5.&nbsp;Chủ đề 5. Đọc tệp, ghi tệp.</a></strong><br  />
<em><strong>Mục tiêu:</strong></em><br  />
- Đọc và ghi tệp.<br  />
<em><strong>Kiến thức:</strong></em>

<table border="1">
	<tbody>
		<tr>
			<th width="30%">Kiểu dữ liệu</th>
			<th>Miêu tả</th>
		</tr>
		<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>

<table border="1">
	<tbody>
		<tr>
			<th width="30%">Chế độ</th>
			<th>Miêu tả</th>
		</tr>
		<tr>
			<td><strong>ios::app</strong></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>
<br  />
<em><strong>Bước 1: Mở tập tin lên theo chức năng mong muốn (ghi hay đọc)<br  />
Bước 2: Thực hiện các lệnh ghi hoặc đọc<br  />
Bước 3: Đóng tập tin</strong></em><br  />
- Sử dụng:&nbsp;#include &lt;<strong>fstream</strong>&gt;<br  />
<b>1. Cách đọc tệp C++</b><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\n”;<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;“\n”;//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\n”;<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;“\nNoi 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;“\nTen 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  />
<b>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</b>

<table border="1" cellpadding="1" cellspacing="1">
	<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>

<pre>
<code>#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>
<b>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</b>

<table border="1" cellpadding="1" cellspacing="1">
	<tbody>
		<tr>
			<td>INPUT</td>
			<td>OUTPUT</td>
		</tr>
		<tr>
			<td>6<br  />
			9</td>
			<td>54</td>
		</tr>
	</tbody>
</table>

<pre>
<code>#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>
<b>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</b>.

<table border="1" cellpadding="1" cellspacing="1">
	<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>

<pre>
<code>#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>
<strong>Câu 4. Đọc dãy số từ tệp vào mảng.</strong>

<p><b>Câu 4</b><b>.</b>&nbsp;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>

<table border="1" cellpadding="4" cellspacing="0" width="605">
	<tbody>
		<tr valign="top">
			<td width="193">
			<p align="center"><b>Dữ liệu vào (Cau4.INP)</b></p>
			</td>
			<td width="263">
			<p align="center"><b>Dữ liệu ra (Cau4.OUT)</b></p>
			</td>
			<td width="123">
			<p align="center"><b>Ghi chú</b></p>
			</td>
		</tr>
		<tr valign="top">
			<td width="193">
			<p align="left">6 9 5</p>

			<p align="left">5 6 8 9 7</p>
			</td>
			<td width="263">
			<p align="left">54</p>

			<p align="left">35</p>
			</td>
			<td width="123">
			<p align="center">&nbsp;</p>
			</td>
		</tr>
		<tr valign="top">
			<td width="193">
			<p align="left">8 7 10</p>

			<p align="left">5 6 8 9 7 2 6 7 9 10</p>
			</td>
			<td width="263">
			<p align="left">56</p>

			<p align="left">69</p>
			</td>
			<td width="123">
			<p align="center">&nbsp;</p>
			</td>
		</tr>
	</tbody>
</table>

<pre>
<code>#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>
<br  />
&nbsp;
		</div>
				<div id="author">
						<p>
				<strong>Tác giả bài viết:</strong>
				Thanh Sơn
			</p>
						<p>
				<strong>Nguồn tin:</strong>
				xn--nhitrc-tya.vn
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://tinhocdct.xyz/hoat-dong-chuyen-mon/tai-lieu-c-va-codeblocks-106.html" title="Tài liệu C++ và CodeBlocks">https://tinhocdct.xyz/hoat-dong-chuyen-mon/tai-lieu-c-va-codeblocks-106.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; TỔ TIN HỌC TRƯỜNG THPT ĐỖ CÔNG TƯỜNG
		</div>
		<div id="contact">
			<a href="mailto:webmaster@tinhocdct.xyz">webmaster@tinhocdct.xyz</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://tinhocdct.xyz/#">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>
<script src="https://tinhocdct.xyz/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_is_recaptcha=0;</script>
<script src="https://tinhocdct.xyz/assets/js/language/vi.js"></script>
<script src="https://tinhocdct.xyz/assets/js/global.js"></script>
<script src="https://tinhocdct.xyz/themes/default/js/news.js"></script>
<script src="https://tinhocdct.xyz/themes/default/js/main.js"></script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1816161148536198"
     crossorigin="anonymous"></script>
<script src="https://tinhocdct.xyz/themes/default/js/bootstrap.min.js"></script>
</body>
</html>