คือ ผมเขียนโค้ดภาษาซี เกี่ยวกับปฏิทิน โดยการใส่ ปี คศ และเดือน (เช่น 1996-07) โดยในโค้ดนั้นมีสูตรและฟังก์ชันมากมาย ซึ่งผมไม่ค่อยเข้าใจนัก เนื่องจากได้การช่วยจากอาจารย์ และรุ่นพี่ ซึ่งก็ต่างให้สูตร ให้โค้ดมาปราศจากคำอธิบาย ผมจึงอยากขอให้คนในพันทิปช่วย เขียนอธิบายโค้ดขอผมให้หน่อย จะสอบพรีเซ้นแล้ว รบกวนช่วยหน่อยครับ
#include<stdio.h>
#include<conio.h>
#include<windows.h> //ใส่สี
#define Setcolor(txt,back) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), back*16+txt)
#define resetcolor() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15)
#define N 300 //กำหนดค่าตัวแปรทั่วไป N=300
struct specialday //กำหนดโครงสร้างเป็นวันสำคัญ
{
int date, month;
char name[50];
}specialday[N];
void Read()
{
FILE *fp; //ตัวชี้ตำแหน่งข้อมูลในแฟ้ม มีผลทำให้ตัว file pointer ชี้ไปที่ตำแหน่งเริ่มต้นของแฟ้มข้อมูลทันที
fp = fopen("specialday","r"); //(ชื่อแฟ้มข้อมูล,โหมดอ่าน)
int i;
if( fp != 0 )
for(i=0;i<N;i++)
{
fscanf(fp, "%d/%d ", &specialday.date, &specialday.month); // เป็นฟังก์ชันที่ใช้อ่านข้อมูลจากแฟ้มข้อมูล
fgets(specialday.name, 100, fp);
}
fclose(fp); //จะให้ค่าเท่ากับ 0 เมื่อปิดไฟล์ได้สำเร็จ
}
void Head()
{
printf("\n\n\n");Setcolor(12,0);
printf(" ****** ****** ****** * ****** ** * ***** ******* ******\n");Setcolor(15,0);
printf(" * * * * * * * * * * * * * * * *\n");Setcolor(9,0);
printf(" * ****** ****** * ****** * * * * * ******* ******\n");Setcolor(15,0);
printf(" * * * * * * * * * * * * * * * *\n");Setcolor(12,0);
printf(" ****** * * * * ***** ****** * ** ***** * * * *\n");Setcolor(15,0);
}
void Showday(int date,int month ,int r)
{
if((date==1&&month==1)||(date==6&&month==4)||(date==13&&month==4)||(date==14&&month==4)
||(date==15&&month==4)||(date==1&&month==5)||(date==5&&month==5)||(date==12&&month==8)
||(date==23&&month==10)||(date==5&&month==12)||(date==10&&month==12)||(date==31&&month==12))
{
Setcolor(12,0);
}
else if((date==10&&month==1)||(date==13&&month==1)||(date==14&&month==1)||(date==16&&month==1)||(date==17&&month==1)
||(date==18&&month==1)||(date==25&&month==1)||(date==2&&month==2)||(date==3&&month==2)||(date==10&&month==2)||(date==14&&month==2)
||(date==24&&month==2)||(date==25&&month==2)||(date==26&&month==2)||(date==5&&month==3)||(date==8&&month==3)||(date==13&&month==3)
||(date==20&&month==3)||(date==21&&month==3)||(date==22&&month==3)||(date==27&&month==3)||(date==31&&month==3)||(date==1&&month==4)
||(date==2&&month==4)||(date==7&&month==4)||(date==22&&month==4)||(date==8&&month==5)||(date==12&&month==5)||(date==31&&month==5)
||(date==5&&month==6)||(date==8&&month==6)||(date==9&&month==6)||(date==24&&month==6)||(date==26&&month==6)||(date==29&&month==7)
||(date==4&&month==8)||(date==7&&month==8)||(date==16&&month==8)||(date==18&&month==8)||(date==16&&month==9)||(date==19&&month==9)
||(date==20&&month==9)||(date==24&&month==9)||(date==27&&month==9)||(date==1&&month==10)||(date==13&&month==10)||(date==14&&month==10)
||(date==16&&month==10)||(date==19&&month==10)||(date==21&&month==10)||(date==31&&month==10)||(date==14&&month==11)||(date==20&&month==11)
||(date==25&&month==11)||(date==27&&month==11)||(date==1&&month==12)||(date==3&&month==12)||(date==4&&month==12)||(date==16&&month==12)
||(date==25&&month==12)||(date==26&&month==12)||(date==28&&month==12))
{
Setcolor(2,0);
}
printf("\t %d",date);
Setcolor(15,0);
}
void Calendar(int year,int month)
{
int end, d=1;
int r, c; //roll and column
int sum, var; //x แทนวันแรกของปี
//คำสั่งกำหนดจำนววันในแต่ละเดือน
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
end=31;
}
else if(month==4||month==6||month==9||month==11)
{
end=30;
}
else
{
if((year%4)==0)
{
end=29;
if(year0==0)
{
end--;
if(year%400==0)
{
end++;
}
}
}
else
{
end=28;
}
}
sum=(((((year+(year-1)/4-(year-1)/100+(year-1)/400))%7)+7)%7); //สุ่มเลขตั้งแต่ -1
if (month==2) {sum=(sum+31)%7;}
else if(month==3) {sum=(sum+31+28)%7;}
else if(month==4) {sum=(sum+31+28+31)%7;}
else if(month==5) {sum=(sum+31+28+31+30)%7;}
else if(month==6) {sum=(sum+31+28+31+30+31)%7;}
else if(month==7) {sum=(sum+31+28+31+30+31+30)%7;}
else if(month==8) {sum=(sum+31+28+31+30+31+30+31)%7;}
else if(month==9) {sum=(sum+31+28+31+30+31+30+31+31)%7;}
else if(month==10) {sum=(sum+31+28+31+30+31+30+31+31+30)%7;}
else if(month==11) {sum=(sum+31+28+31+30+31+30+31+31+30+31)%7;}
else if(month==12) {sum=(sum+31+28+31+30+31+30+31+31+30+31+30)%7;}
if((year%4)==0&&month>2)
{
sum=(sum+1)%7;
if(year0==0)
{
sum=(sum+6)%7;
if(year%400==0)
{
sum=(sum+1)%7;
}
}
}
var=(end+sum)/7;
if((end+sum)%7>0)
{
var++;
}
var++;
printf("\t_____________________________________________________\n\n");
if (month==1){printf("\t\t\t January %d\n",year);}
else if (month==2){printf("\t\t\t February %d\n",year);}
else if (month==3){printf("\t\t\t March %d\n",year);}
else if (month==4){printf("\t\t\t April %d\n",year);}
else if (month==5){printf("\t\t\t May %d\n",year);}
else if (month==6){printf("\t\t\t June %d\n",year);}
else if (month==7){printf("\t\t\t July %d\n",year);}
else if (month==8){printf("\t\t\t August %d\n",year);}
else if (month==9){printf("\t\t\t September %d\n",year);}
else if (month==10){printf("\t\t\t October %d\n",year);}
else if (month==11){printf("\t\t\t November %d\n",year);}
else if (month==12){printf("\t\t\t December %d\n",year);}
;
printf("\t_____________________________________________________\n\n");
//printf("\t Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
Setcolor(12,0); printf("\tSun");
Setcolor(14,0); printf("\tMon");
Setcolor(13,0); printf("\tTue");
Setcolor(10,0); printf("\tWed");
Setcolor(4,0); printf("\tThu");
Setcolor(11,0); printf("\tFri");
Setcolor(5,0); printf("\tSat\n"); Setcolor(15,0);
printf("\t_____________________________________________________\n\n");
for(c=1;c<=var;c++)
{
printf("\n");
if (c>1)
{
printf("\n");
}
for(r=1;r<=7;r++)
{
if(d<=end)
{
if(sum>0)
{
printf("\t ");
sum=sum--;
}
else if(sum==0)
{
Showday (d,month,r);
//printf("\t %d",day);
d=d++;
}
}
}
}
printf("\t_____________________________________________________\n\n");
//กำหนดวันสำคัญในแต่ละเดือน
Setcolor(3,0); printf("\t\t\tShow the relevant Day\n"); Setcolor(15,0);
if(month==1)
{
printf("\n\t 1st : New Year Day\n\t 10th: Children's Day\n\t 13rd: National Aviation Day");
printf("\n\t 14th: Day of National Forest Conservation\n\t 16th: Teacher's Day\n\t 17th: King Ramkhamhaeng's Day");
printf("\n\t 18th: Army's Day\n\t");
}
if(month==2)
{
printf("\n\t 2nd : National Argicuture Day\n\t 3th : Veterans Day\n\t 10th: Volunteer Guard Day");
printf("\n\t 14th: Valentine's Day\n\t 24th: Day of National Artists\n\t 25th: Day of National Radio");
printf("\n\t 26th: National Cooperative Day");
}
if(month==3)
{
printf("\n\t 5th : Day of Journalists\n\t 8th : International Women's Day\n\t 13rd: Day of Chang Thailand");
printf("\n\t 20th: National Volunteer Day\n\t 21st: International Poetry Day\n\t 22nd: Day of World Water Conservation");
printf("\n\t 27th: Day of Commemorati Air force\n\t 31st: Day of King Nang Kloa");
}
if(month==4)
{
printf("\n\t 1st : Thailand's Government Savings Day\n\t 2nd : Heritage Conservation Day\n\t 6th : Chakri Day");
printf("\n\t 7th : World Health Days\n\t 13rd-15th: Songkran Festival\n\t 22nd: Earth Day");
}
if(month==5)
{
printf("\n\t 1st : Labour Day\n\t 5th : Coronation Day\n\t 8th : International Red Cross Day");
printf("\n\t 12nd: International Nursing Day\n\t 31st: World No Tobacco Day ");
}
if(month==6)
{
printf("\n\t 5th : World Environment Day\n\t 8th : World Ocean Day\n\t 9th : VIII's Day");
printf("\n\t 24th: Day of Rule Change\n\t 26th: Soonthornphu's day");
}
if(month==7)
{
printf("\n\t 29th: National Day in Thailand");
}
if(month==8)
{
printf("\n\t 4th : National Communication Day\n\t 7th : Day of LTD.\n\t 12th: Mother Day");
printf("\n\t 16th: Peace of Thailand Day\n\t 18th: National Science Day");
}
if(month==9)
{
printf("\n\t 16th: World Ozone Day\n\t 19th: Day of Museum Thailand\n\t 20th: National Youth Day\n\t 24th: Mahidol Day");
}
if(month==10)
{
printf("\n\t 1st : International Day for Older Persons\n\t 13rd: Police Day\n\t 14th: Democracy Day");
printf("\n\t 16th: World Food Day\n\t 19th: Technology Day in Thailand\n\t 21st: Social Worker's Day");
printf("\n\t 23th: Day of Commemoration\n\t 31st: Halloween Day");
}
if(month==11)
{
printf("\n\t 14th: Day of King's Rain\n\t 20th: Navy Day\n\t 25th: Day of Ementary\n\t 27th: National Day ");
}
if(month==12)
{
printf("\n\t
รบกวนอธิบายโค้ดภาษาซีให้หน่อยครับ
#include<stdio.h>
#include<conio.h>
#include<windows.h> //ใส่สี
#define Setcolor(txt,back) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), back*16+txt)
#define resetcolor() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15)
#define N 300 //กำหนดค่าตัวแปรทั่วไป N=300
struct specialday //กำหนดโครงสร้างเป็นวันสำคัญ
{
int date, month;
char name[50];
}specialday[N];
void Read()
{
FILE *fp; //ตัวชี้ตำแหน่งข้อมูลในแฟ้ม มีผลทำให้ตัว file pointer ชี้ไปที่ตำแหน่งเริ่มต้นของแฟ้มข้อมูลทันที
fp = fopen("specialday","r"); //(ชื่อแฟ้มข้อมูล,โหมดอ่าน)
int i;
if( fp != 0 )
for(i=0;i<N;i++)
{
fscanf(fp, "%d/%d ", &specialday.date, &specialday.month); // เป็นฟังก์ชันที่ใช้อ่านข้อมูลจากแฟ้มข้อมูล
fgets(specialday.name, 100, fp);
}
fclose(fp); //จะให้ค่าเท่ากับ 0 เมื่อปิดไฟล์ได้สำเร็จ
}
void Head()
{
printf("\n\n\n");Setcolor(12,0);
printf(" ****** ****** ****** * ****** ** * ***** ******* ******\n");Setcolor(15,0);
printf(" * * * * * * * * * * * * * * * *\n");Setcolor(9,0);
printf(" * ****** ****** * ****** * * * * * ******* ******\n");Setcolor(15,0);
printf(" * * * * * * * * * * * * * * * *\n");Setcolor(12,0);
printf(" ****** * * * * ***** ****** * ** ***** * * * *\n");Setcolor(15,0);
}
void Showday(int date,int month ,int r)
{
if((date==1&&month==1)||(date==6&&month==4)||(date==13&&month==4)||(date==14&&month==4)
||(date==15&&month==4)||(date==1&&month==5)||(date==5&&month==5)||(date==12&&month==8)
||(date==23&&month==10)||(date==5&&month==12)||(date==10&&month==12)||(date==31&&month==12))
{
Setcolor(12,0);
}
else if((date==10&&month==1)||(date==13&&month==1)||(date==14&&month==1)||(date==16&&month==1)||(date==17&&month==1)
||(date==18&&month==1)||(date==25&&month==1)||(date==2&&month==2)||(date==3&&month==2)||(date==10&&month==2)||(date==14&&month==2)
||(date==24&&month==2)||(date==25&&month==2)||(date==26&&month==2)||(date==5&&month==3)||(date==8&&month==3)||(date==13&&month==3)
||(date==20&&month==3)||(date==21&&month==3)||(date==22&&month==3)||(date==27&&month==3)||(date==31&&month==3)||(date==1&&month==4)
||(date==2&&month==4)||(date==7&&month==4)||(date==22&&month==4)||(date==8&&month==5)||(date==12&&month==5)||(date==31&&month==5)
||(date==5&&month==6)||(date==8&&month==6)||(date==9&&month==6)||(date==24&&month==6)||(date==26&&month==6)||(date==29&&month==7)
||(date==4&&month==8)||(date==7&&month==8)||(date==16&&month==8)||(date==18&&month==8)||(date==16&&month==9)||(date==19&&month==9)
||(date==20&&month==9)||(date==24&&month==9)||(date==27&&month==9)||(date==1&&month==10)||(date==13&&month==10)||(date==14&&month==10)
||(date==16&&month==10)||(date==19&&month==10)||(date==21&&month==10)||(date==31&&month==10)||(date==14&&month==11)||(date==20&&month==11)
||(date==25&&month==11)||(date==27&&month==11)||(date==1&&month==12)||(date==3&&month==12)||(date==4&&month==12)||(date==16&&month==12)
||(date==25&&month==12)||(date==26&&month==12)||(date==28&&month==12))
{
Setcolor(2,0);
}
printf("\t %d",date);
Setcolor(15,0);
}
void Calendar(int year,int month)
{
int end, d=1;
int r, c; //roll and column
int sum, var; //x แทนวันแรกของปี
//คำสั่งกำหนดจำนววันในแต่ละเดือน
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
end=31;
}
else if(month==4||month==6||month==9||month==11)
{
end=30;
}
else
{
if((year%4)==0)
{
end=29;
if(year0==0)
{
end--;
if(year%400==0)
{
end++;
}
}
}
else
{
end=28;
}
}
sum=(((((year+(year-1)/4-(year-1)/100+(year-1)/400))%7)+7)%7); //สุ่มเลขตั้งแต่ -1
if (month==2) {sum=(sum+31)%7;}
else if(month==3) {sum=(sum+31+28)%7;}
else if(month==4) {sum=(sum+31+28+31)%7;}
else if(month==5) {sum=(sum+31+28+31+30)%7;}
else if(month==6) {sum=(sum+31+28+31+30+31)%7;}
else if(month==7) {sum=(sum+31+28+31+30+31+30)%7;}
else if(month==8) {sum=(sum+31+28+31+30+31+30+31)%7;}
else if(month==9) {sum=(sum+31+28+31+30+31+30+31+31)%7;}
else if(month==10) {sum=(sum+31+28+31+30+31+30+31+31+30)%7;}
else if(month==11) {sum=(sum+31+28+31+30+31+30+31+31+30+31)%7;}
else if(month==12) {sum=(sum+31+28+31+30+31+30+31+31+30+31+30)%7;}
if((year%4)==0&&month>2)
{
sum=(sum+1)%7;
if(year0==0)
{
sum=(sum+6)%7;
if(year%400==0)
{
sum=(sum+1)%7;
}
}
}
var=(end+sum)/7;
if((end+sum)%7>0)
{
var++;
}
var++;
printf("\t_____________________________________________________\n\n");
if (month==1){printf("\t\t\t January %d\n",year);}
else if (month==2){printf("\t\t\t February %d\n",year);}
else if (month==3){printf("\t\t\t March %d\n",year);}
else if (month==4){printf("\t\t\t April %d\n",year);}
else if (month==5){printf("\t\t\t May %d\n",year);}
else if (month==6){printf("\t\t\t June %d\n",year);}
else if (month==7){printf("\t\t\t July %d\n",year);}
else if (month==8){printf("\t\t\t August %d\n",year);}
else if (month==9){printf("\t\t\t September %d\n",year);}
else if (month==10){printf("\t\t\t October %d\n",year);}
else if (month==11){printf("\t\t\t November %d\n",year);}
else if (month==12){printf("\t\t\t December %d\n",year);}
;
printf("\t_____________________________________________________\n\n");
//printf("\t Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
Setcolor(12,0); printf("\tSun");
Setcolor(14,0); printf("\tMon");
Setcolor(13,0); printf("\tTue");
Setcolor(10,0); printf("\tWed");
Setcolor(4,0); printf("\tThu");
Setcolor(11,0); printf("\tFri");
Setcolor(5,0); printf("\tSat\n"); Setcolor(15,0);
printf("\t_____________________________________________________\n\n");
for(c=1;c<=var;c++)
{
printf("\n");
if (c>1)
{
printf("\n");
}
for(r=1;r<=7;r++)
{
if(d<=end)
{
if(sum>0)
{
printf("\t ");
sum=sum--;
}
else if(sum==0)
{
Showday (d,month,r);
//printf("\t %d",day);
d=d++;
}
}
}
}
printf("\t_____________________________________________________\n\n");
//กำหนดวันสำคัญในแต่ละเดือน
Setcolor(3,0); printf("\t\t\tShow the relevant Day\n"); Setcolor(15,0);
if(month==1)
{
printf("\n\t 1st : New Year Day\n\t 10th: Children's Day\n\t 13rd: National Aviation Day");
printf("\n\t 14th: Day of National Forest Conservation\n\t 16th: Teacher's Day\n\t 17th: King Ramkhamhaeng's Day");
printf("\n\t 18th: Army's Day\n\t");
}
if(month==2)
{
printf("\n\t 2nd : National Argicuture Day\n\t 3th : Veterans Day\n\t 10th: Volunteer Guard Day");
printf("\n\t 14th: Valentine's Day\n\t 24th: Day of National Artists\n\t 25th: Day of National Radio");
printf("\n\t 26th: National Cooperative Day");
}
if(month==3)
{
printf("\n\t 5th : Day of Journalists\n\t 8th : International Women's Day\n\t 13rd: Day of Chang Thailand");
printf("\n\t 20th: National Volunteer Day\n\t 21st: International Poetry Day\n\t 22nd: Day of World Water Conservation");
printf("\n\t 27th: Day of Commemorati Air force\n\t 31st: Day of King Nang Kloa");
}
if(month==4)
{
printf("\n\t 1st : Thailand's Government Savings Day\n\t 2nd : Heritage Conservation Day\n\t 6th : Chakri Day");
printf("\n\t 7th : World Health Days\n\t 13rd-15th: Songkran Festival\n\t 22nd: Earth Day");
}
if(month==5)
{
printf("\n\t 1st : Labour Day\n\t 5th : Coronation Day\n\t 8th : International Red Cross Day");
printf("\n\t 12nd: International Nursing Day\n\t 31st: World No Tobacco Day ");
}
if(month==6)
{
printf("\n\t 5th : World Environment Day\n\t 8th : World Ocean Day\n\t 9th : VIII's Day");
printf("\n\t 24th: Day of Rule Change\n\t 26th: Soonthornphu's day");
}
if(month==7)
{
printf("\n\t 29th: National Day in Thailand");
}
if(month==8)
{
printf("\n\t 4th : National Communication Day\n\t 7th : Day of LTD.\n\t 12th: Mother Day");
printf("\n\t 16th: Peace of Thailand Day\n\t 18th: National Science Day");
}
if(month==9)
{
printf("\n\t 16th: World Ozone Day\n\t 19th: Day of Museum Thailand\n\t 20th: National Youth Day\n\t 24th: Mahidol Day");
}
if(month==10)
{
printf("\n\t 1st : International Day for Older Persons\n\t 13rd: Police Day\n\t 14th: Democracy Day");
printf("\n\t 16th: World Food Day\n\t 19th: Technology Day in Thailand\n\t 21st: Social Worker's Day");
printf("\n\t 23th: Day of Commemoration\n\t 31st: Halloween Day");
}
if(month==11)
{
printf("\n\t 14th: Day of King's Rain\n\t 20th: Navy Day\n\t 25th: Day of Ementary\n\t 27th: National Day ");
}
if(month==12)
{
printf("\n\t