Professional Documents
Culture Documents
Computer Programming
Array: แถวลาดับ
การประกาศและการกาหนดค่าของ Array
การรับค่า Array
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
Algorithm ที่ควรรู้จัก
- Linear Search
- Bubble Sort
- Binary Search
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
แบบฝึกหัด
1.จงหาผลรันของข้อต่อไปนี้
1.1
#include <iostream>
using namespace std;
int main(){
int a[4][4] = {{5, 1, 8, 6}, {2, 3, 6, 3}, {4, 6, 4, 7},
{8, 1, 3, 3}};
int row[4] = {0};
int column[4] = {0};
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
row[i] += a[i][j];
column[j] += a[i][j];
}
}
for(int i=0; i<4; i++){
cout << row[i] << endl;
}
for(int i=0; i<4; i++){
cout << column[i] << " ";
}
return 0;
}
2. จงเขียนโปรแกรมเพื่อให้ได้ผลรันที่ถูกต้อง
หาค่าเบี่ยงเบนมาตรฐานจากสูตร
ตัวอย่างผลรัน
มาถึงครึ่งทางแล้ว สู้ๆนะจ๊ะ
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
STRING
สตริง (String) ในภาษา C ก็คือ อาร์เรย์ของตัวอักษร ที่มีข้อมูลชนิดตัวอักษรเรียงกันไปแต่จะต้องมีจุดสิ้นสุด
ด้วย โดยจะใช้ตัวอักษรวางหรือ Null Character เป็นจุดสิ้นสุดของสตริง
“Hello” H e l l o \0
“A String”
#include <string>
using namespace std; // Or using std::string;
Input/Output String
cin >> stringObject; //it skips over whitespace
String Functions
s.length()
s.append( string, s)
s.find( string)
แบบฝึกหัด
1. กาหนดให้
string s = "Computer Programming";
จงเขียนคาสั่งในการจัดการ string s เพื่อแสดงผลให้ได้ข้อความดังต่อไปนี้ [1/2007]
1.1 แสดงข้อความ "Program" (1 คะแนน)
__________________________________________________
__________________________________________________
2. จงหาผลการทางานของส่วนของโปรแกรม ต่อไปนี้
string d = "15/09/2555";
string d1 = d.substr(6,4) + "-" + d.substr(3,2) + "-" + d.substr(0,2);
cout << d1;
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
string star(5,'*');
cout << star;
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "computer programming";
cout << "answer 1: ";
cout << s1.substr(10,2) << s1.substr(4,2) << s1.substr(17,3) << endl;
string s2 = s1.replace(s1.find("programm"),8,"engineer");
cout << "answer 2: " << s2 << endl;
string s3 = s2.erase(s1.find("engineer"),11) + "sciences";
cout << "answer 3: " << s3 << endl;
if (s1 < s3) cout << "answer 4: " << s1 << endl;
else cout << "answer 4: " << s3 << endl;
return 0;
}
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
#include <iostream>
using namespace std;
int main(){
string s1="Overdue";
string s2="Overtime";
string s3="Overdose";
string all = s3 + " of " + s1 + " on " + s2;
cout << "all = " << all << endl;
cout << "Found " << s1 << " in [all] at " << all.find(s1) << endl;
if(s1 < s2)
cout << s1 << " come before " << s2 << endl;
else if(s1 > s2)
cout << s1 << " come after " << s2 << endl;
else
cout << s1 << " and " << s2 << " are equal" << endl;
cout << "s3.substr(5,2) = " << s3.substr(5,2) << endl;
cout << "\ts3 = " << s3 << endl;
s3.replace(5,2,"u");
cout << "After s3.replace(5,2,\"u\")" << endl;
cout << "\ts3 = " << s3 << endl;
return 0;
}
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
File
ภาษา C++ ได้เตรียมคลาสในการทางานร่วมกับไฟล์ทจี ะนามาใช้เป็นอินพุต และเอาต์พุตไว้คล้ายกับ
โปรแกรมคอมพิวเตอร์ส่วนใหญ่แล้วคือคลาส fstream สาหรับจัดการไฟล์ทเี่ ป็นอินพุตและเอาต์พุต จึงต้องมีการ
#include<fstream> หากต้องการเขียนไฟล์จะใช้อ็อบเจ็กต์จากคลาส ofstream กรณีต้องการอ่านข้อมูลจาก
ไฟล์จะใช้อ็อบเจ็กต์จากคลาส ifstream
1. การบันทึกสตริงเขาไฟล
ofstream writeText(“Data.txt”)
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
2. การอ่านสตริงจากไฟล
ifstream readText(“Data2.TXT”);
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
ตัวอย่างผลรัน เมื่อป้อนตัวเลข 5 2 4 6 8 1 0
ที่หน้าจอคอมพิวเตอร์
Please enter integers (0 to quit): 5 2 4 6 8 1 0
10. จงเติมคาในช่องว่างให้ถูกต้อง
#include <iostream>
#include __________
using namespace std;
int main() {
________ infile("input.txt");
ofstream ________("output.txt");
string word;
char c;
while ( _________ >> word) {
if (word[0] >= 'a' && word[0] <= 'z')
word[0] += 'A' - 'a';
outfile << word;
infile.___________(c);
______________.put(c);
}
infile.close();
outfile.close();
return 0;
}
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
* * * * *
* *
* *
* *
* * * * *
Input an integer: 8
ข้อมูลที่โปรแกรมบันทึกในไฟล์ square.txt
********
* *
* *
* *
* *
* *
* *
********
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
12. จงหาผลรันของโปรแกรมต่อไปนี้
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream infile("input.txt");
ofstream outfile1("output1.txt");
ofstream outfile2("output2.txt");
ofstream outfile3("output3.txt");
string checkword;
string word1;
string word2;
char c;
while (infile >> word1){
checkword = word1.substr(2,3);
outfile1 << checkword<<endl;
outfile2 << word1.replace(1,2,checkword) << endl;
}
outfile2.close();
ifstream infile2("output2.txt");
while (infile2 >> word2){
outfile3 << word2.length() << endl;
}
infile.close();
infile2.close();
outfile1.close();
outfile3.close();
return 0;
}
ข้อมูลในไฟล์ input.txt
Faculty of Engineering
Khon Kaen University
Thailand
13. จงหาผลรันของโปรแกรมต่อไปนี้
13.1
#include <iostream>
using namespace std;
int main() {
string s1 = "Computer";
string s2 = "Engineer";
if (s1 < s2)
cout << s1 << endl;
else
cout << s2 << endl;
cout << s1.find("put") + s2.length() << endl;
cout << s1.substr(5,2) + s2.substr(1,1) << endl;
cout << s1.replace(4,4,s2.erase(3,5)) << endl;
return 0;
}
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
13.2
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream infile("score.txt");
ofstream outfile("result.txt");
string name;
int score, nPass = 0, nFail = 0;
while (infile >> name) {
outfile << name << '\t';
infile >> score;
if (score >= 60) {
outfile << "Pass\n";
nPass++;
} else {
outfile << "Fail\n";
nFail++;
}
}
cout << "Passes = " << nPass << endl;
cout << "Fails = " << nFail << endl;
return 0;
}
ฮั่นแน่ ~~ มาได้ขนาดนี้เชียวหรอม
อย่าลืมส่งใบคอมเม้นด้วยนะ!!!
© ชุมนุมวิชาการ สโมสรนักศึกษาคณะวิศวกรรมศาสตร์ (Engineering Students Union Academic Club)
แสดงความคิดเห็น
โครงการเตรียมความพร้อมก่อนสอบ ปีการศึกษา 2562
นายปวเรศ นิยมเหลา (P’PondHub) ภาควิชาวิศวกรรมคอมพิวเตอร์