Programming

Just another WordPress.com weblog

ประวัติเอกสารจากหมวดหมู่ ‘CT212’

ข้อสอบ CT212 final เรื่องไฟล์

แสดงความเห็นโดย จั่น บน ธันวาคม 6, 2008

โจทย์ ให้เขียน program หยิบข้อมูล Input จากแฟ้มข้อมูล

(StudentScore.txt) ซึ่งประกอบไปด้วยข้อมูล

รหัสนักศึกษา, ชื่อนักศึกษา, คะแนนเก็บ, คะแนน midterm ,คะแนน final

ดังนี้

5102026     Sakon        15    40    40
5102027     Damrong      10    25    26
5102028     Lanna        5     10    18
5102029     Kwee         10    10    15
2102030     Udon         5     30    29
2102031     Tee          20    35    40

ให้แสดงผล output ออกทางหน้าจอ ตามนี้

นักศึกษาที่ได้เกรด G ได้แก่
5102026 Sakon 95
2102031 Tee 95

นักศึกษาที่ได้เกรด P ได้แก่
5102027 Damrong 61
2102030 Udon 64

นักศึกษาที่ได้เกรด F ได้แก่
5102028 Lanna 33
5102029 Kwee 35

code ด้านล่างจะรันได้ก็ต่อเมื่อมี file StudentScore.txt อยู่แล้ว โดยสร้างตามโจทย์

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

#define sourceFile “StudentScore.txt”

using namespace std;

int ProcessScore(int);
void PrintOutput(const string, const string, const int);

void main(){

cout << “Student Grade G ” << endl;
ProcessScore(1);

cout << endl;

cout << “Student Grade P ” << endl;
ProcessScore(2);

cout << endl;

cout << “Student Grade F ” << endl;
ProcessScore(3);

}

int ProcessScore(int type){

ifstream ifs;
ifs.open(sourceFile);

if(ifs.fail()){

cerr << “Error Cannot Open ” << sourceFile << endl;
return EXIT_FAILURE;

}

string studentcode, studentname;
int homework, midterm, finale;
int totalscore;

ifs >> studentcode >> studentname >> homework >> midterm >> finale;

while(!ifs.eof()){

totalscore = homework + midterm + finale;

if(type == 1){

if(totalscore >= 80){

PrintOutput(studentcode, studentname, totalscore);

}

}else if(type == 2){

if(totalscore >= 60 && totalscore <= 79 ){

PrintOutput(studentcode, studentname, totalscore);

}

}else if(type == 3){

if(totalscore < 60){

PrintOutput(studentcode, studentname, totalscore);

}

}

ifs >> studentcode >> studentname >> homework >> midterm >> finale;

}

ifs.close();
return 0;

}

void PrintOutput(const string studentcode, const string studentname, const int totalscore){

cout << studentcode << ” ” << studentname << ” ” << totalscore << endl;

}

เขียนแล้วใน CT212, Programming | 1 ความคิดเห็น »

ข้อสอบ CT212 final เรื่อง Recursive ปี 2551

แสดงความเห็นโดย จั่น บน ธันวาคม 5, 2008

พิจารณา Arkaman ’s function นี้

A(m,n) ={ n+1 เมื่ือ m=0;

A(m-1,1) เมื่อ n =0;

A(m-1, A(m, n-1)) เมื่อกรณีอื่นๆ }

จงเขียนเป็น Recursive function และให้หาด้วยว่า A(3, 1) มีค่าเท่าไหร่

int A(int m, int n){

if(m == 0){

return n+1;

}else if( n == 0){

return A(m-1,1);

}else{

return A(m-1, A(m, n-1));

}

}

สำหรับคำตอบของ A(3,1) จะเท่ากับ 13 (ไล่หาค่าใช้เวลานานมาก แนะนำให้เอาเวลาที่เหลือมาไล่หาค่าจะดีกว่า)

เขียนแล้วใน CT212, Programming | Leave a Comment »

ข้อสอบ CT212 finale ข้อย่อย Recursive ปี 2551

แสดงความเห็นโดย จั่น บน ธันวาคม 5, 2008

จงแปลง function นี้ให้เป็น Recursive

double test(int a, int b){

double sum = 1.0;

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

sum = sum * (1/a);

}

}

แปลงเป็น Recursive function ได้เป็น

double test(int a, int b){

if(b == 0){

return 1.0;

} else{

return (1.0/a) * test(a, b-1);

}

}

เขียนแล้วใน CT212, Programming | Leave a Comment »

ข้อสอบ CT212 midterm ปี 2551 เขียน code ยาว

แสดงความเห็นโดย จั่น บน ธันวาคม 5, 2008

ห้างสรรพสินค้าแหงหนึ่งต้องการสมนาคุณลูกค้าด้วยการเก็บยอดซื้อของลูกค้า

เพื่อมาแลกเป็นคูปองเพื่อที่จะได้รับของสมนาคุณดังนี้

ถ้าซื้อครบ 100 บาทจะได้คูปอง 1 ใบ ซึ่งคูปองนี้เมื่อสะสมครบจำนวนจะได้สินค้าดังนี้

โดยลูกค้าไม่สามารถเลือกสินค้าได้ ลูกค้่าจะได้ของตามลำดับด่านล่างนี้เท่านั้น

  • ครบ 30 ใบได้หม้อ 1 ใบ
  • ครบ 20 ใบได้ปากกา 1 ด้าม
  • ครบ 10 ใบได้แก้ว 1 ใบ

#include <iostream>

using namespace std;
void main(){

int totalamount, totalpot, totalpen, totalglass, Numcoupon;

char choice;
totalpot = 0, totalpen = 0; totalglass =0;

do{

cout << “Total Credits: “;
cin >> totalamount;

cout << “Your coupons “;
Numcoupon = totalamount / 100;
cout << Numcoupon << ” item(s)” << endl;

int pot;
int pen;
int glass;

pot = Numcoupon/30;
pen = Numcoupon%30/20;
glass = Numcoupon%30%20/10;

totalpot += pot;
totalpen += pen;
totalglass += glass;

if(pot != 0){

cout << “You will get Pot ” << pot << ” piece(s)” << endl;

}
if(pen != 0){

cout << “You will get Pen ” << pen << ” piece(s)” << endl;

}
if(glass != 0){

cout << “You will get Glass ” << glass << ” piece(s)” << endl;

}

cout << “Run again Y/N?”;
cin >> choice;

}while(choice == ‘Y’ || choice == ‘y’);

cout << “Summary” << endl;
cout << “Total Pot to Customers = ” << totalpot << endl;
cout << “Total Pen to Customers = ” << totalpen << endl;
cout << “Total Glass to Customers = ” << totalglass << endl;

}

เขียนแล้วใน CT212, Programming | Leave a Comment »

แนวข้อสอบ CT212 ข้อย่อย midterm ใช้ for

แสดงความเห็นโดย จั่น บน ธันวาคม 5, 2008

โจทย์ เขียน program ใช้ for loop หาจำนวนเงินที่ไ้ด้รับสุทธิเมื่อสิ้นสุด Y ปี โดยฝากด้วยเงินต้น A ปี และมีอัตราดอกเบี้ยร้อยละ K บาทต่อปี

เช่น FindMoney( 2000, 2, 8 ) คือฝากเงินต้น 2000 บาท เมื่อฝากเงินครบ 2 ปี ด้วยอัตราดอกเบี้ยร้อยละ 8 ต่อปี

จะได้เงินเมื่อสิ้นสุดปีที่ 1 เท่ากับ 2000 + 0.08(2000) = 2160 บาท

จะได้เงินเมื่อสิ้นสุดปีที่ 2 เท่ากับ 2160 + 0.08(2160) = 2332.80 บาท

double FindMoney(double A, int Y, int K){

double total = A;

//Sample Data of K = 5, 8

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

total = total + (K/100.0) * total;

}

return total;

}

เขียนแล้วใน CT212, Programming | Leave a Comment »

แนวข้อสอบ CT212 ข้อย่อย midterm ใช้ while

แสดงความเห็นโดย จั่น บน ธันวาคม 4, 2008

โจทย์ ให้เขียน function โดยใช้ While loop รับจำนวนรอบของการบวกสะสมของ

10+20+30+40+…..+n

เช่นเมื่อใส่ n=3

ผลบวกสะสมจะต้องเท่ากับ 10+20+30

int SUM(int n){

int total = 0;

while(n > 0){

total += n * 10;

n = n – 1;

}

return total;

}

เขียนแล้วใน CT212, Programming | Leave a Comment »

ข้อสอบ CT212 final เรื่อง Class ปี 2551

แสดงความเห็นโดย จั่น บน ธันวาคม 4, 2008

พิจารณาการทำงานของ program ต่อไปนี้

//File main.cpp

#include “student.h”
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
student sak;
student sri(“49001″,42,36);
sak.setId(“51002″);
sak.setMid(30);
sak.setFinal(60);
sak.computeTotal();
sri.computeTotal();

cout << “ID ” << “Mid ” << ” Final” << ” Total” << endl;
cout << sri.getId() << ” ” << sri.getMid() << ” ” << sri.getFinal()
<< ” ” << sri.getTotal() << endl;

cout << sak.getId() << ” ” << sak.getMid() << ” ” << sak.getFinal()
<< ” ” << sak.getTotal() << endl;

}

เมื่อนำ program ไปทำงานจะได้ output ดังนี้

ID Mid Final Total

49001 42 36 78

51002 30 60 90

การทำงานของ program เป็นการรวมคะแนนให้แก่นักเรียนสองคนได้แก่ sak และ sri โดยทั้งสองเป็นสมาชิกของ class student ซึ่งคุึณสมบัติของคลาสพื้นฐานคือรหัสนักเรียน (Id), คะแนนกลางภาค (Mid), คะแนนปลายภาค (Final) นอกจากนี้อาจมีคุณสมบัติอื่นๆที่จำเป็นอีก

สำหรับ Method พื้นฐานประกอบด้วย

1.setId

2.SetMid

2.SetFinal

4.computeTotal

5.getId

6.getMid

7.getFinal

8.getTotal

จงออกแบบและสร้างคลาสเพื่อให้ทำงานได้ตามโปรแกรมที่กำหนดข้างต้น (25คะแนน)

filestudentcpp

//File student.h

#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>

using namespace std;

class student {

public:

student();
student(string, int, int);

void setId(string);
void setMid(int);
void setFinal(int);
void computeTotal();
string getId();
int getMid();
int getFinal();
int getTotal();

private:

string Id;
int Mid;
int Final;
int Total;

};
#endif

//STUDENT_H

—————
//File student.cpp

#include “student.h”
#include <iostream>

using namespace std;

student::student(){

Id = ” “;
Mid = 0;
Final = 0;
Total = 0;

}
student::student(string Idv, int Midv, int Finalv){

Id = Idv;
Mid = Midv;
Final = Finalv;

Total = 0;

}

void student::setId(string Idv){

Id = Idv;

}
void student::setMid(int Midv){

Mid = Midv;

}
void student::setFinal(int Finalv){

Final = Finalv;

}
void student::computeTotal(){

Total = Mid + Final;

}
string student::getId(){

return Id;

}
int student::getMid(){

return Mid;

}
int student::getFinal(){

return Final;

}
int student::getTotal(){

return Total;

}

เขียนแล้วใน CT212, Programming | Leave a Comment »

ข้อสอบ CT212 midterm ข้อใหญ่ข้อที่ 4 ปี2551

แสดงความเห็นโดย จั่น บน ธันวาคม 4, 2008

บริษัทผลิตเสื้อสีเหลืองผลิตสินค้าแบ่งออกเป็น 3 แบบ Type1,Type2,Type3 แต่ละแบบมีขนาดแตกต่างกัน 4 ขนาดได้แก่ S,M,L,X โดยมีการกำหนดราคาไว้ดังนี้

Type\Size                S                 M               L               X
1                        150               180            200             250
2                        180               200            250             300
3                        200               250            300             350

จงเขียน program เพื่อทำงานโดยกำหนด funtions ดังต่อไปนี้

-เขียน function Input เพื่อทำการป้อนราคาของเสื้อเหลืองที่บริษัทนี้กำหนดราคาไว้เก็บไว้ใน array 2 มิติ 3×4 ชื่อว่า cost

————->[ INPUT] ———->cost

-เขียน functionในการคิดราคาสินค้า (Sale) โดยมีการส่งผ่านราคา(cost),ชนิด(tyoe),ขนาด(size)และจำนวนที่ต้องการซื้อ(n) ไปยังฟังก์ชั่น ผลของการทำงานนั้นจะคิดราคาสินค้า(total)ตามข้อมูลที่ส่งไป โดยส่งกลับมายังจุดเรียกใช้

cost,type,size,n

————>[Sale]————–>total

-การทำงานของ function main นั้นจะมีการให้ผู้ใช้นำข้อมูลตามตารางกำหนดราคาเพื่อเก็บใน array 2 มิติชื่อ cost ต่อจากนั้น program สามารถขายสินค้าให้แก้ลูกค้าได้โดยป้อนชนิด,ขนาดและจำนวนที่ลูกค้าต้องการทางแป้นพิมพ์ โปรแกรมจะคิดราคาเสื้อ โดยพิมพ์ราคารวมออกทางจอภาพ และทำงานวนรอบเพื่อคิดราคาเสื้อของลูกค้าคนต่อๆไป จนกระทั่งหยุดการทำงาน โปรแกรมจะสรุปยอดขายทั้งหมดออกทางจอภาพ

นักศึกษาสามารถออกแบบโปรแกรมได้ตามความต้องการแต่ในโปรแกรมต้องมีฟังก์ชันตามที่กำหนด อาจมีการเพิ่มเติม function อื่นๆได้ โดยเฉพาะอย่างยิ่งการใช้ตัวแปรต้องตามที่กำหนดไว้เท่านั้น

ตัวอย่างของ Input และ Output

ลูกค้าคนที่ 1

Type=1

Size=L

N=2

จำนวนเงิน=400.00

Run again Y/N? Y

ลูกค้าคนที่ 2

Type=3

Size=S

N=1

จำนวนเงิน=200.00

Run again Y/N? N

รายรับรวม=600.00

codecpp
#include <iostream>

using namespace std;

void Input(float[3][4]);
float Sale(float[3][4], int, int, int);
char GetSizeCharFromInt(int);
float GetCost(float[3][4], int, char);

void main(){

float cost[3][4];
char choice;
int customerindex = 0;
int type, N;
char size;
float total = 0.0;
float totalall = 0.0;

cout << “Program Calculate Sale of Yellow Shirts” << endl;
//Insert XXX Baht to array cost 3×4
Input(cost);

do{

cout << “Customer ” << customerindex+1 << endl;
cout << “Type = “;
cin >> type;
cout << “Size = “;
cin >> size;
cout << “N = “;
cin >> N;

total = Sale(cost, type, size, N);
totalall += total;

cout << “Total = ” << Sale(cost, type, size, N) << endl;

customerindex++;

cout << “Run again Y/N? “;
cin >> choice;

}while(choice == ‘Y’ || choice == ‘y’);

cout << “Total All = ” << totalall;

}

void Input(float cost[3][4]){

float costval;

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

for(int j=0; j<4; j++){

cout << “Insert Shirt Cost: Type ” << i+1 << “, Size ” << GetSizeCharFromInt(j) << ” = “;
cin >> costval;

cost[i][j] = costval;

}

}

}

float Sale(float cost[3][4], int type, int size, int n){

float total;

total = GetCost(cost, type, size) * n;

return total;

}

char GetSizeCharFromInt(int size){

if(size == 0){

return ‘S’;

}else if(size == 1){

return ‘M’;

}else if(size == 2){

return ‘L’;

}else if(size == 3){

return ‘X’;

}

}
float GetCost(float cost[3][4], int type, char size){

//Type from keyboard (1,2,3)
//size from keyboard (S,M,L,X) or (s,m,l,x)

int typeindex = type -1;
int sizeindex = 0;

if(size == ‘S’ || size == ’s’){

sizeindex = 0;

}else if(size == ‘M’ || size == ‘m’){

sizeindex = 1;

}else if(size == ‘L’ || size == ‘l’){

sizeindex = 2;

}else if(size == ‘X’ || size == ‘x’){

sizeindex = 3;

}

return cost[typeindex][sizeindex];

}

เขียนแล้วใน CT212, Programming | Leave a Comment »