Jump to content
fiiq26

Budak Baru Belajar

Recommended Posts

bagaimana hendak mengira gaji bulan untuk 5 orang pekerja dimana sabtu dan ahad adalah hari cuti.
gunakan fungsi untuk mengira gaji tersebut huh.gif

Share this post


Link to post
Share on other sites
input bilangan hari dalam bulan tu
input jumlah bilangan hari untuk sabtu dan ahad dalam bulan tu
input gaji

CODE
jumlahgaji = (bulan - sabtuahad) * gaji * 5;


ni cara paling sengal la, sebab semua user yang input
kalau nak buat auto calculation kat bil bulan ngan sabtu ahad tu, kena buat research k.

Share this post


Link to post
Share on other sites
Pe ko nak pening2 ngat...ko wt cmnie ja...maaf lau da slah...nak cepat,busy owh.....huhuhu....selamat berjaya ubahsuai....aku wt utk ko je taw...hak2


Spoiler :
#include<stdio.h>
int main(void)
{
char pekerja;
int no,hari;
double gaji,jum;

printf("Nama pekerja:");
scanf("%s",&pekerja);

printf("No pekerja anda:");
scanf("%d",&no);

printf("Bilangan berapa hari bekerja dalam sebulan:");
scanf("%d",&hari);

printf("Berapakah gaji anda dalam sehari:RM");
scanf("%f",&gaji);

jum=gaji*hari;

printf("\nNama anda ialah:%c",pekerja);
printf("\nNo pekerja anda:%d",no);
printf("\nJumlah gaji anda dalam sebulan ialah:RM%.2f,jum);

return(0);
}

Share this post


Link to post
Share on other sites
QUOTE(fiiq90 @ Dec 19 2008, 01:27 AM) <{POST_SNAPBACK}>
bagaimana hendak mengira gaji bulan untuk 5 orang pekerja dimana sabtu dan ahad adalah hari cuti.
gunakan fungsi untuk mengira gaji tersebut huh.gif


1 hb x start pada hari yang sama tiap2 tahun. Camner nak tentukan bilangan hari sabtu dan ahad dalam bulan tersebut......???

mungkin,

CODE
int bilsabtuahad = (bilharidlmbulan % 7) * 2;


formula ni btul hanya kalau 1 hb tu hari ahad.

Tapi takkanlah soalan ni se'complicated' ni. Soalan penuh dia camner? Edited by Bakri_

Share this post


Link to post
Share on other sites
OH!~
tapi, kalau buat untuk satu bulan sahaja?
contoh untuk bulan Januari?
bagaimana nak buat pertanyaan gaji berulang lima kali?
kusut2! wacko.gif

Share this post


Link to post
Share on other sites
QUOTE(fiiq90 @ Dec 21 2008, 02:10 AM) <{POST_SNAPBACK}>
OH!~
tapi, kalau buat untuk satu bulan sahaja?
contoh untuk bulan Januari?
bagaimana nak buat pertanyaan gaji berulang lima kali?
kusut2! wacko.gif


for (int i = 0; i < 5; i++) {
...code...
}

Share this post


Link to post
Share on other sites
QUOTE (Bakri_ @ Dec 20 2008, 09:09 AM) <{POST_SNAPBACK}>
1 hb x start pada hari yang sama tiap2 tahun. Camner nak tentukan bilangan hari sabtu dan ahad dalam bulan tersebut......???
kita bole guna <ctime> library (<time.h> kalu guna C) utk deal ngan tarikh dan masa...
bole guna struct *tm dan ari ahad ialah tm_wday = 0 dan sabtu ialah tm_wday=6...bole jugak guna time_t saja tp kena wat sendiri la 1 array yg menentukan hari(mcm lookup table la lebih krg)...
jadi bole tolak ari ahad dan sabtu dalam bulan tertentu...

ni cth yg aku guna struct tm...
CODE
#include <iostream>
#include <ctime>

using namespace std;

int calculateSalary(int mth, int yr){
    time_t currTime;
    char temp [40];
    struct tm *userDate;
    int workingDay = 0;

    time (&currTime);
    userDate = localtime (&currTime); //initialize dulu tm kita ngan tarikh skang utk digunakan nanti
    userDate->tm_mday=1;
    userDate->tm_mon = mth-1;
    userDate->tm_year = yr-1900; //tarikh start dr taun 1900, so tolak offset tu

    strftime (temp,40,"%B, %Y is: RM",userDate); //%B = utk papar nama bulan penuh, %Y utk tahun penuh...format2 dia bole cr kat internet
    cout<<"\nSalary for the month of "<<temp;

    for(mktime(userDate); userDate->tm_mon != mth; mktime(userDate)){    //nak guna do...while pon bole tp make sure mktime tu
                                                                           //dipanggil kat luar loop utk start initalize tm_wday tu...
        workingDay += (userDate->tm_wday && userDate->tm_wday<6)? 1:0; //kalu bukan ari ahad or ari sabtu, increment jumlah working day...else, xyah increment...
                                                                       //sorry... aku guna ternary operator sbb malas nak taip....haha
        ++userDate->tm_mday;
    }

    return workingDay;
}

int checkInput (){   //sbg error handle tahap cikai...hahaha
    int userInput=0;
    if(cin>>userInput){
        return userInput;
    }else{
        cout<<"\nInvalid input\n";
        return 0;
    }
}

int main (){
    cout<<"Enter month(in integer): ";
    int month = checkInput();
    cout<<"Enter year(in integer): ";
    int year = checkInput();
    cout<<"Enter salary/day: RM";
    int salary = checkInput();

    if(month && month<13 && year){
        cout<<salary*calculateSalary(month, year)<<"\n";
    }

    return 0;
}
ada byk lagi kelemahan kat function tu tp sbg exercise utk ko, ko try kembangkan n baiki lagi function tu...huhu...
hint: strftime tu bole digunakan spenuhnya utk menggantikan cara kita tolak ari ahad n sabtu...
cara panggil function n return value workingDay tu bole dibaiki lagi...
error handler, etc...

//kalu ko bagi c0ding penuh yg ko dah buat lagi cun...bole kitorang tlg betulkan mana yg patut...lagi byk ko bljr dgn cara tu aku rasa...

Share this post


Link to post
Share on other sites
QUOTE(betik @ Dec 30 2008, 11:08 AM) <{POST_SNAPBACK}>
kita bole guna <ctime> library (<time.h> kalu guna C) utk deal ngan tarikh dan masa...
bole guna struct *tm dan ari ahad ialah tm_wday = 0 dan sabtu ialah tm_wday=6...bole jugak guna time_t saja tp kena wat sendiri la 1 array yg menentukan hari(mcm lookup table la lebih krg)...
jadi bole tolak ari ahad dan sabtu dalam bulan tertentu...

ni cth yg aku guna struct tm...
CODE
#include <iostream>
#include <ctime>

using namespace std;

int calculateSalary(int mth, int yr){
    time_t currTime;
    char temp [40];
    struct tm *userDate;
    int workingDay = 0;

    time (&currTime);
    userDate = localtime (&currTime); //initialize dulu tm kita ngan tarikh skang utk digunakan nanti
    userDate->tm_mday=1;
    userDate->tm_mon = mth-1;
    userDate->tm_year = yr-1900; //tarikh start dr taun 1900, so tolak offset tu

    strftime (temp,40,"%B, %Y is: RM",userDate); //%B = utk papar nama bulan penuh, %Y utk tahun penuh...format2 dia bole cr kat internet
    cout<<"\nSalary for the month of "<<temp;

    for(mktime(userDate); userDate->tm_mon != mth; mktime(userDate)){    //nak guna do...while pon bole tp make sure mktime tu
                                                                           //dipanggil kat luar loop utk start initalize tm_wday tu...
        workingDay += (userDate->tm_wday && userDate->tm_wday<6)? 1:0; //kalu bukan ari ahad or ari sabtu, increment jumlah working day...else, xyah increment...
                                                                       //sorry... aku guna ternary operator sbb malas nak taip....haha
        ++userDate->tm_mday;
    }

    return workingDay;
}

int checkInput (){   //sbg error handle tahap cikai...hahaha
    int userInput=0;
    if(cin>>userInput){
        return userInput;
    }else{
        cout<<"\nInvalid input\n";
        return 0;
    }
}

int main (){
    cout<<"Enter month(in integer): ";
    int month = checkInput();
    cout<<"Enter year(in integer): ";
    int year = checkInput();
    cout<<"Enter salary/day: RM";
    int salary = checkInput();

    if(month && month<13 && year){
        cout<<salary*calculateSalary(month, year)<<"\n";
    }

    return 0;
}
ada byk lagi kelemahan kat function tu tp sbg exercise utk ko, ko try kembangkan n baiki lagi function tu...huhu...
hint: strftime tu bole digunakan spenuhnya utk menggantikan cara kita tolak ari ahad n sabtu...
cara panggil function n return value workingDay tu bole dibaiki lagi...
error handler, etc...

//kalu ko bagi c0ding penuh yg ko dah buat lagi cun...bole kitorang tlg betulkan mana yg patut...lagi byk ko bljr dgn cara tu aku rasa...


bagus2. Tapi rasanya TS baru belajar C++, mungkin struct pun belum belajar.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...