Nutcha0751 / filemix

0 stars 0 forks source link

Practical Exam 2 [2/66] #13

Open Nutcha0751 opened 6 months ago

Nutcha0751 commented 6 months ago

ข้อ 1

แพมทำ แต่ไม่ถูกทุกกรณี

include

include

using namespace std;

int ids[] = {99,12,48,105,10,15,25,46,499,128,87,52,221,300,714,188,189,910,412,555}; char genders[] = {'m','f','m','m','f','f','f','m','m','f','m','f','f','m','f','f','f','m','m','f'}; string faculty[] = {"eng","med","econ","med","eng","law","law","dent","med","camt","med","dent","econ","eng","med","eng","eng","econ","econ","agri"}; int N = sizeof(ids)/sizeof(ids[0]);

int main(){ cout << "Input gender and faculty to search:\n"; char gen; string fac; int count = 0; cin >> gen >> fac;

for(int i = 0; i < N; i++){
    if(gen == genders[i] && fac == faculty[i]){
        cout << "Student " << count+1 << ": " << ids[i] << endl;
        count++;
    }
    else if(count < 1){
        cout << "not found" << endl;
        break;
    }
}

return 0;

}

ข้อ 2

จงเขียน Function ชื่อ sort3ints ให้สอดคล้องกับตัวอย่างการใช้งานด้านล่างนี้ ซึ่งจากตัวอย่างการใช้งานจะเห็นได้ว่า sort3ints() จะมีการรับ Input 3 ตัว และไม่มีการ Return ค่าใด ๆ โดยเมื่อเรียกใช้งาน sort3ints() กับตัวแปร a, b, c ตามในตัวอย่าง จะเห็นได้ว่า ค่าที่ถูกเก็บไว้ในตัวแปรทั้ง 3 จะถูกสลับใหม่ โดยที่ค่าที่มากที่สุดจะมาอยู่ที่ตัวแปร a ค่าตรงกลางจะมาอยู่ที่ตัวแปร b และค่าที่น้อยที่สุดจะมาอยู่ที่ตัวแปร c

Nutcha0751 commented 6 months ago

By Pai

ข้อ 1

include

include

using namespace std;

int ids[] = {99, 12, 48, 105, 10, 15, 25, 46, 499, 128, 87, 52, 221, 300, 714, 188, 189, 910, 412, 555}; char genders[] = {'m', 'f', 'm', 'm', 'f', 'f', 'f', 'm', 'm', 'f', 'm', 'f', 'f', 'm', 'f', 'f', 'f', 'm', 'm', 'f'}; string faculty[] = {"eng", "med", "econ", "med", "eng", "law", "law", "dent", "med", "camt", "med", "dent", "econ", "eng", "med", "eng", "eng", "econ", "econ", "agri"}; int N = sizeof(ids) / sizeof(ids[0]);

int main() { cout << "Input gender and faculty to search:\n"; char gen; string fac; int count = 0; cin >> gen >> fac;

for (int i = 0; i < N; i++)
{
    if (gen == genders[i] && fac == faculty[i])
    {
        cout << "Student " << count + 1 << ": " << ids[i] << endl;
        count++;
    }       
}

if(count == 0) cout << "not found" << endl; 

return 0;

}

Nutcha0751 commented 6 months ago

ข้อ 2 แบบที่ 1

include

using namespace std;

void sort3ints(int a, int b, int c) { int high = max(max(a, b), c); int low = min(min(a, b), c); int med = min(min(max(a, b), max(b, c)), max(a, *c));

*a = high;
*b = med;
*c = low;

}

int main() { int x = 8, y = 92, z = 33; sort3ints(&x, &y, &z); cout << x << endl; cout << y << endl; cout << z << endl; }

ข้อ 2 แบบที่ 2

include

include

include

using namespace std;

void sort3ints(int a, int b, int c) { vector num = {a, b, c}; sort(num.begin(), num.end()); a = num[2]; b = num[1]; *c = num[0]; }

int main() { int x = 8, y = 92, z = 33; sort3ints(&x, &y, &z); cout << x << endl; cout << y << endl; cout << z << endl; }

ข้อ 2 แบบที่ 3

include

include

using namespace std;

void sort3ints(int a, int b, int c) { priority_queue num; num.push(a); num.push(b); num.push(c);

*a = num.top();
num.pop();
*b = num.top();
num.pop();
*c = num.top();
num.pop();

}

int main() { int x = 8, y = 92, z = 33; sort3ints(&x, &y, &z); cout << x << endl; cout << y << endl; cout << z << endl; }

Nutcha0751 commented 6 months ago

ข้อ 3

ไผ่จำได้ลางๆ

include

include

include

void String2Upper(std::string & str){ for(unsigned i = 0; i < str.size(); i++){ str[i] = toupper(str[i]); } }

std::string faceValue[] = {"SPADE", "HEART", "DIAMOND", "CLUB"}; std::string suitValue[] ={"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};

int GetFaceValue(std::string face){ for(unsigned i = 0; i < 4; i++){ if(face == faceValue[i]) return i; } return -1; }

int GetSuitValue(std::string suit){ for(unsigned i = 0; i < 13; i++){ if(suit == suitValue[i]) return i; } return -1; }

class Card{ public: std::string face; std::string suit; Card(std::string code){ int splitter = code.find(" "); suit = code.substr(0, splitter); face = code.substr(splitter + 1); String2Upper(suit); String2Upper(face); }

    bool operator>(const Card& x){
        if(suit == x.suit){
            return GetFaceValue(face) < GetFaceValue(x.face);
        }else return GetSuitValue(suit) < GetSuitValue(x.suit);
    }

};

Card strongestCard(std::vector hand){ Card strongest = hand[0]; for(auto card: hand){ if(card > strongest) strongest = card; } return strongest; }

int main(){ std::vector hand;

Card a = Card("7 Diamond");
Card b = Card("A Spade");
Card z = Card("2 CLUB");
Card w = Card("2 SpaDe");
hand.push_back(a);
hand.push_back(b);
hand.push_back(z);
hand.push_back(w);
std::cout << strongestCard(hand).suit << " " << strongestCard(hand).face;

}