ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ဒီ project မှာ variable၊ array၊ loop၊ function၊ struct နဲ့ if...else တွေကို program တစ်ခုတည်းထဲမှာ ပေါင်းသုံးပါမယ်။ "Aye 75" လို line တွေကို ဖတ်ပြီး မှန်တဲ့ line တွေကိုပဲ သိမ်းထားပါမယ်။ ပြီးရင် ကျောင်းသားတစ်ယောက်ချင်းရဲ့ result၊ အောင်တဲ့သူ ဘယ်နှယောက်ရှိလဲ၊ class average ဘယ်လောက်လဲဆိုတာကို report အနေနဲ့ ထုတ်ပေးပါမယ်။
စာမေးပွဲ result တွေကို ကိုင်တွယ်တဲ့ ရုံးခန်းလေးတစ်ခုကို စဉ်းစားကြည့်ပါ။ စာရေးတစ်ယောက်က form တစ်ရွက်ချင်းကို စစ်တယ်၊ နောက်တစ်ယောက်က တွက်ချက်တယ်၊ တတိယတစ်ယောက်က နောက်ဆုံး report ကို ရိုက်တယ်။ ဘယ်သူမှ အလုပ်အားလုံးကို တစ်ယောက်တည်း မလုပ်တဲ့အတွက် ဂဏန်းတစ်ခု မှားထွက်လာရင် ဘယ်စားပွဲကို သွားမေးရမလဲ ချက်ချင်းသိပါတယ်။ ကျွန်တော်တို့ program လည်း ဒီအတိုင်းပါပဲ။ parse_line() က စစ်တယ်၊ average() နဲ့ count_passed() က တွက်တယ်၊ print_report() က print ထုတ်တယ်၊ main() ကတော့ သူတို့ကြားမှာ အလုပ်လွှဲပေးရုံပဲ လုပ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: အားလုံးကို main ထဲမှာ ပုံထားတာ
ပထမဆုံး ရေးကြည့်တဲ့ version က အများအားဖြင့် အားလုံးကို main ထဲမှာ ထည့်ထားတတ်ပါတယ်။ scanf နဲ့ score တောင်းမယ်၊ total ထဲ ပေါင်းမယ်၊ စားမယ်၊ print ထုတ်မယ်။ ဂဏန်းသန့်သန့် သုံးလုံး ရိုက်ထည့်တဲ့ ပထမအကြိမ်မှာတော့ အလုပ်လုပ်ပါတယ်။ ပြီးတော့ 75 ကို ရိုက်ပြီး နောက်တစ်ခါ 8two လို့ မတော်တဆ ရိုက်မိတယ်ဆိုပါစို့။ scanf က 8 ကို ဖတ်ပြီး "two" ကို input ထဲမှာ ချန်ထားခဲ့ပါတယ်။ "two" က number မဟုတ်လို့ တတိယအကြိမ် ခေါ်တာက ချက်ချင်း fail ဖြစ်ပါတယ်။ scores[2] ကို ဘယ်တော့မှ assign မလုပ်ခဲ့လို့ အဲဒီ memory ထဲမှာ ရှိနေတဲ့ ဘာ value မဆို ဖြစ်နေနိုင်ပြီး total က အမှိုက် value ဖြစ်သွားပါတယ်။
စားတဲ့နေရာမှာလည်း ဒုတိယ bug တစ်ခု ပုန်းနေပါတယ်။ 75၊ 82၊ 69 ဆိုရင် တကယ့် average က 75.33 ပါ။ ဒါပေမဲ့ total / 3 က int နှစ်ခုကို စားတာမို့ ဒသမပိုင်းကို ဖြတ်ပစ်ပြီး 75 လို့ပဲ ထုတ်ပါတယ်။ တွက်ချက်တဲ့ code က input loop ထဲမှာ ရောနေလို့ average မှန်မမှန် စမ်းချင်တိုင်း keyboard ကနေ ပြန်ရိုက်နေရပါတယ်။
#include <stdio.h>
int main(void) {
int scores[3];
int total = 0;
for (int i = 0; i < 3; i++) {
printf("Score %d: ", i + 1);
scanf("%d", &scores[i]); /* return value ignored */
total += scores[i];
}
printf("Average: %d\n", total / 3); /* int / int drops the fraction */
if (total / 3 >= 60) {
printf("Passed\n");
}
/* grades, best student, saving to a file... all of it piles up here */
return 0;
}ဘာကြောင့် ဒီလောက် ရှုပ်ထွေးတဲ့ပုံစံနဲ့ ပျက်တာလဲ
scanf က value ဘယ်နှခု convert လုပ်နိုင်ခဲ့လဲဆိုတာကို return ပြန်ပေးပါတယ်။ Input မှန်မမှန် သိနိုင်တဲ့ တစ်ခုတည်းသော လက္ခဏာက အဲဒီ number ပါပဲ။ အဲဒါကို လျစ်လျူရှုရင် read fail ဖြစ်တဲ့အခါ variable က initialize မလုပ်ရသေးဘဲ ကျန်ခဲ့ပြီး မှားနေတဲ့ character တွေလည်း input ထဲမှာ ပိတ်မိနေပါတယ်။ ဖတ်တာ၊ တွက်တာ၊ print ထုတ်တာ အားလုံးကို block တစ်ခုထဲ ရောထားရင် bug တိုင်းက input bug လိုပဲ မြင်ရပါတယ်။ သီးသန့် run ပြီး စစ်လို့ရတဲ့ အပိုင်းသေးသေးလေး တစ်ခုမှ မရှိလို့ပါ။
Core concept: Program ရဲ့ အပိုင်းတစ်ခုစီကို အလုပ်တစ်ခုပဲ ပေးပါ
C program ဘယ်လောက်ပဲ ကြီးကြီး၊ data သွားတဲ့လမ်းကြောင်းအတိုင်း အပိုင်းခွဲထားရင် မှန်အောင်ရေးရတာ ပိုလွယ်ပါတယ်။ ဒီ report မှာ data က အဆင့်လေးဆင့်ကို ဖြတ်သွားပြီး အဆင့်တစ်ခုစီက ရှင်းလင်းတဲ့ contract ပါတဲ့ function တစ်ခု ဒါမှမဟုတ် နှစ်ခု ဖြစ်လာပါတယ်။ Contract ကို signature ထဲမှာ ရေးထားပါတယ်။ Function က ဘာကို လက်ခံမလဲ၊ ဘာကို return ပြန်မယ်လို့ ကတိပေးလဲ၊ ဘာကို မထိရဘူးလဲ ဆိုတာပါ။
၁။ Input: text ကို စစ်ပြီးသား struct အဖြစ် ပြောင်းပါ
parse_line() က line တစ်ကြောင်းနဲ့ ဖြည့်ရမယ့် Student ရဲ့ pointer ကို လက်ခံပါတယ်။ Line ထဲမှာ နာမည်တစ်ခုနဲ့ 0 ကနေ 100 ကြား number တစ်ခုပဲ အတိအကျ ပါမှသာ 1 ကို return ပြန်ပါတယ်။ "Daw abc"၊ "Ei 105"၊ "Aye 75 extra" အပါအဝင် ကျန်တာအားလုံးအတွက် 0 ပြန်ပါတယ်။ ဒါကြောင့် program ရဲ့ ကျန်တဲ့အပိုင်းတွေက မစစ်ရသေးတဲ့ data ကို ဘယ်တော့မှ မမြင်ရပါဘူး။
၂။ Storage: capacity စစ်ထားတဲ့ fixed array
မှန်တဲ့ ကျောင်းသားတွေကို struct Student list[MAX_STUDENTS] ထဲ ထည့်ပါတယ်။ တစ်ခါရေးတိုင်း main() က count ကို MAX_STUDENTS နဲ့ အရင် နှိုင်းယှဉ်ပါတယ်။ Element 8 ခုပဲရှိတဲ့ array မှာ list[8] ကို ရေးရင် တခြား memory ကို တိတ်တဆိတ် overwrite လုပ်သွားမှာမို့ပါ။ ဒီနေရာမှာ fixed array နဲ့ လုံလောက်ပါတယ်။ Challenge မှာတော့ လိုသလို ကြီးလာတဲ့ heap array နဲ့ အစားထိုးပါမယ်။
၃။ Logic: array ပေါ်မှာ အလုပ်လုပ်တဲ့ pure function တွေ
average() နဲ့ count_passed() က array ကို const အဖြစ် လက်ခံပြီး count ကို သီးသန့် parameter အနေနဲ့ ယူပါတယ်။ Array parameter က pointer အဖြစ် ရောက်လာလို့ သူ့ length ကို သူ မသိပါဘူး။ ဒီ function တွေက ဘယ်တော့မှ print မထုတ်၊ input မဖတ်ပါဘူး။ ဒါကြောင့် ဘယ် test data နဲ့မဆို ခေါ်ပြီး ရလဒ်ကို နှိုင်းယှဉ်စစ်လို့ ရပါတယ်။
၄။ Output: format ကို function တစ်ခုတည်းက ပိုင်ပါတယ်
Column width နဲ့ စကားလုံးတွေကို ဆုံးဖြတ်တဲ့ နေရာက print_report() တစ်ခုတည်းပါ။ Report ပုံစံကို ပြောင်းတဲ့အခါ တွက်ချက်တဲ့ code ကို ထိခိုက်သွားမှာ မစိုးရိမ်ရတော့ပါဘူး။
ဘာကြောင့် total / (double)count လဲ
Operand နှစ်ခုလုံး int ဖြစ်ရင် C က integer division လုပ်ပြီး ဒသမပိုင်းကို ဖြတ်ပစ်ပါတယ်။ 270 / 4 က 67.5 မဟုတ်ဘဲ 67 ဖြစ်သွားပါတယ်။ Operand တစ်ခုကို double အရင်ပြောင်းလိုက်ရင် division တစ်ခုလုံး floating point နဲ့ လုပ်ပါတယ်။ Cast ကို ရလဒ်ကို အုပ်ပြီး မထားဘဲ operand ပေါ်မှာ ထားရပါမယ်။ (double)(total / count) ဆိုရင် 67.0 ပဲ ရပါတယ်။
- Text line တစ်ကြောင်း ဝင်လာတယ်
- parse_line() က လက်ခံ/ငြင်းတယ်
- နေရာရှိသေးရင် struct ကို သိမ်းတယ်
- Logic function တွေက array ကနေ တွက်တယ်
- print_report() က ရလဒ်ကို format လုပ်တယ်
Report ထဲက number တစ်ခု မှားနေရင် အဲဒါကို ထုတ်ပေးတဲ့ အဆင့်တစ်ခုတည်းကိုပဲ ရှာကြည့်ဖို့ လိုပါတယ်။
အခြေခံ ပုံစံ
#define CAPACITY 8
struct Record { char name[16]; int value; };
int parse_record(const char *line, struct Record *out); /* 1 = valid */
double summarise(const struct Record list[], int count); /* no I/O */
void print_records(const struct Record list[], int count); /* only I/O */
/* main: for each line, if count < CAPACITY and parse_record(...)
succeeds, count++; then summarise and print */Data shape ကို အရင် ကြေညာပြီး အဆင့်တစ်ခုကို function တစ်ခုစီ ပေးပါ။ Input function တွေက pointer ကနေ struct ကို ဖြည့်ပြီး အောင်မြင်မှုကို return ပြန်ပါတယ်။ Logic function တွေက const array နဲ့ count ကို ယူပါတယ်။ Print ထုတ်တာကတော့ output function ကပဲ လုပ်ပါတယ်။
Score report ကို layer တစ်ခုချင်း
ဒီ program က user ရိုက်ထည့်မယ့် text အမျိုးအစားအတိုင်း script လုပ်ထားတဲ့ line ခြောက်ကြောင်းကို ကိုယ့်ဘာသာ ထည့်ပေးပါတယ်။ ဒါကြောင့် run တိုင်း output အတူတူ ထွက်ပါတယ်။ Line နှစ်ကြောင်းကို တမင် မှားထားပါတယ်။ "Daw abc" မှာ number မပါဘဲ "Ei 105" က range ပြင်ပ ဖြစ်နေပါတယ်။ အဲဒီ line တွေကို တံခါးဝမှာတင် ဘယ် line လဲဆိုတဲ့ message နဲ့ ငြင်းလိုက်ပြီး မှန်တဲ့ ကျောင်းသား လေးယောက်ကတော့ report ဆီ ဆက်စီးဆင်းသွားတာကို ကြည့်ပါ။
Task ထဲမှာ ကျောင်းသားတိုင်းကို တန်းစီပြီး result ပြဖို့၊ အောင်တဲ့သူ ရေတွက်ဖို့နဲ့ average ထုတ်ဖို့ လိုပါတယ်။ အပိုင်းတစ်ခုစီက ဘယ် function မှာ ရှိလဲဆိုတာ မှတ်ထားပါ။ Challenge မှာ ဒီ layer တွေကို တခြား program တစ်ခုမှာ ပြန်သုံးရမှာပါ။
#include <stdio.h>
#define MAX_STUDENTS 8
#define PASS_MARK 60
struct Student {
char name[16];
int score;
};
/* Input layer: one "name score" line -> Student. Returns 1 if valid. */
int parse_line(const char *line, struct Student *out) {
char extra;
if (sscanf(line, "%15s %d %c", out->name, &out->score, &extra) != 2) {
return 0;
}
return out->score >= 0 && out->score <= 100;
}
/* Logic layer: numbers in, numbers out. No printf, no input. */
double average(const struct Student list[], int count) {
if (count == 0) {
return 0.0;
}
int total = 0;
for (int i = 0; i < count; i++) {
total += list[i].score;
}
return total / (double)count;
}
int count_passed(const struct Student list[], int count) {
int passed = 0;
for (int i = 0; i < count; i++) {
if (list[i].score >= PASS_MARK) {
passed++;
}
}
return passed;
}
/* Output layer: the only place that formats the report. */
void print_report(const struct Student list[], int count) {
printf("%-6s %5s %s\n", "Name", "Score", "Result");
for (int i = 0; i < count; i++) {
const char *result = list[i].score >= PASS_MARK ? "Passed" : "Try again";
printf("%-6s %5d %s\n", list[i].name, list[i].score, result);
}
printf("Passed: %d of %d\n", count_passed(list, count), count);
printf("Average score: %.2f\n", average(list, count));
}
int main(void) {
/* Scripted input: the lines a user would type, so the run is repeatable. */
const char *input[] = {"Aye 75", "Bo 82", "Chit 68", "Daw abc", "Ei 105", "Hla 45"};
int lines = (int)(sizeof input / sizeof input[0]);
struct Student list[MAX_STUDENTS];
int count = 0;
for (int i = 0; i < lines; i++) {
if (count == MAX_STUDENTS) {
printf("List is full, ignoring the rest\n");
break;
}
if (parse_line(input[i], &list[count])) {
count++;
} else {
printf("Skipped line %d: \"%s\"\n", i + 1, input[i]);
}
}
print_report(list, count);
return 0;
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Reject trailing junk
if (sscanf(line, "%15s %d %c", out->name, &out->score, &extra) != 2) {sscanf က item ဘယ်နှခု ဖြည့်နိုင်ခဲ့လဲ return ပြန်ပါတယ်။ မှန်တဲ့ line က နာမည်နဲ့ score ကို ဖြည့်ပြီး %c အတွက် ဘာမှ မတွေ့တော့လို့ ရလဒ်က 2 အတိအကျ ဖြစ်ပါတယ်။ "Aye 7x" မှာ x က extra ထဲ ရောက်သွားလို့ 3 ရပြီး "Daw abc" က 1 ပဲ ဖြည့်နိုင်ပါတယ်။ %15s ထဲက 15 က နာမည်ရှည်တွေ 16-byte array ကို ကျော်မထွက်အောင် တားပေးပါတယ်။
- Range check as the return value
return out->score >= 0 && out->score <= 100;C မှာ comparison တစ်ခုရဲ့ ရလဒ်က 1 ဒါမှမဟုတ် 0 ပါ။ ဒါကြောင့် range check ကို တိုက်ရိုက် return ပြန်လို့ရပါတယ်။ "Ei 105" က text အနေနဲ့ parse ဖြစ်ပေမယ့် ငြင်းခံရတာ ဒါကြောင့်ပါ။
- Capacity before writing
if (count == MAX_STUDENTS) {ဒီ check က parse_line က list[count] ထဲ မရေးခင်မှာ ဖြစ်ပါတယ်။ နေရာ 8 ခုရှိရင် list[8] က array အဆုံးကို ကျော်နေပြီး C က တားမှာ မဟုတ်ပါဘူး။
- Floating-point division
return total / (double)count;Operand တစ်ခုကို cast လုပ်လိုက်လို့ division ကို double နဲ့ လုပ်ပါတယ်။ 270 / 4.0 က 67.5 ရပါတယ်။ အပေါ်က count == 0 guard က list အလွတ်အတွက် သုညနဲ့ စားတာကို ကာကွယ်ပေးပါတယ်။
- Counting the array length
int lines = (int)(sizeof input / sizeof input[0]);Array တစ်ခုလုံးရဲ့ sizeof ကို element တစ်ခုရဲ့ sizeof နဲ့ စားရင် 6 ရပါတယ်။ input က array အစစ်ဖြစ်တဲ့ နေရာမှာပဲ ဒါက အလုပ်လုပ်ပါတယ်။ Pointer အဖြစ် လက်ခံရတဲ့ function ထဲမှာဆိုရင် sizeof က pointer ရဲ့ size ကိုပဲ ပေးပါလိမ့်မယ်။
Skipped line 4: "Daw abc"
Skipped line 5: "Ei 105"
Name Score Result
Aye 75 Passed
Bo 82 Passed
Chit 68 Passed
Hla 45 Try again
Passed: 3 of 4
Average score: 67.50Script line တွေကို input အစစ်နဲ့ လဲမယ်
Script လုပ်ထားတဲ့ array က ဒီ lesson ကို ထပ်ခါထပ်ခါ run လို့ရအောင် ထိန်းထားပေးပါတယ်။ Program အစစ်မှာတော့ line တွေက keyboard ဒါမှမဟုတ် file ကနေ လာပါတယ်။ သတင်းကောင်းက parse_line() ကို လုံးဝ ပြင်စရာ မလိုတာပါ။ main() ရဲ့ loop ပဲ ပြောင်းပြီး line တစ်ကြောင်းချင်းကို fgets နဲ့ buffer ထဲ ဖတ်ပါမယ်: char line[64]; while (count < MAX_STUDENTS && fgets(line, sizeof line, stdin) != NULL) { ... }
fgets က newline ရောက်ရင် ဒါမှမဟုတ် buffer ပြည့်ရင် ရပ်ပြီး input ကုန်သွားတဲ့အခါ NULL ပြန်ပါတယ်။ ဒါကြောင့် user က Ctrl+D (Windows မှာ Ctrl+Z ပြီး Enter) နှိပ်တဲ့အခါ loop က သေသေသပ်သပ် ပြီးသွားပါတယ်။ fgets က သိမ်းထားတဲ့ newline ကို %c ရှေ့က space က ကျော်ပေးလို့ parse_line က အဲဒီ line ကို ဒီအတိုင်း လက်ခံပါတယ်။ Line တစ်ကြောင်းလုံးကို အရင်ဖတ်ပြီးမှ parse လုပ်တာမို့ scanf မှာလို line မှားတစ်ကြောင်းက input ထဲမှာ ပိတ်မိမနေတော့ပါဘူး။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
ဒီလို ခွဲခြားပုံကို ဒီ program ထက် အများကြီးကြီးတဲ့ C tool အစစ်တွေမှာလည်း တွေ့ရပါတယ်။ Log analyser လို command-line utility တစ်ခုမှာ line တစ်ကြောင်းချင်းကို struct အဖြစ် ပြောင်းပေးတဲ့ parser၊ ဘယ်တော့မှ print မထုတ်တဲ့ analysis function တွေနဲ့ plain text ဒါမှမဟုတ် CSV ထုတ်ပေးနိုင်တဲ့ formatter ရှိပါတယ်။ Embedded firmware မှာလည်း sensor reading တွေကို ဒီအတိုင်းပဲ ကိုင်တွယ်ပါတယ်။ Hardware ကလာတဲ့ raw value ကို function တစ်ခုက validate လုပ်တယ်၊ နောက်တစ်ခုက average တွက်တယ်၊ တတိယတစ်ခုက display ဆီ ပို့ပေးတယ်။
အကျိုးအမြတ်ကတော့ testing ပါ။ average() နဲ့ parse_line() က ရိုးရိုး argument တွေကို ယူပြီး ရိုးရိုး ရလဒ်ကို ပြန်ပေးလို့ test file တစ်ခုက list အလွတ်၊ 60 အတိအကျ score၊ ရှည်လွန်းတဲ့ နာမည် စတဲ့ input ဆိုးဆိုး ဒါဇင်ချီနဲ့ ခေါ်စစ်နိုင်ပါတယ်။ ဘယ်သူမှ keyboard မှာ ရိုက်ပေးစရာ မလိုပါဘူး။ Team တစ်ခုက feature အသစ် ထည့်တဲ့အခါ အများအားဖြင့် layer တစ်ခုထဲမှာပဲ ရောက်ပါတယ်။ Column အသစ်ဆိုရင် printer ကိုပဲ ထိပြီး rule အသစ်ဆိုရင် logic ကိုပဲ ထိပါတယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
scanf/sscanf ရဲ့ return value ကို လျစ်လျူရှုတာ
ပေးလိုက်တဲ့ variable တွေက return value က ပြောတဲ့အတိုင်း ဖြည့်ခံရမှသာ တန်ဖိုးရှိပါတယ်။ Check ကို ကျော်လိုက်ရင် read fail ဖြစ်တဲ့အခါ initialize မလုပ်ရသေးတဲ့ variable ကို ပေါင်းတာ၊ နှိုင်းယှဉ်တာ၊ print ထုတ်တာ ဖြစ်သွားပါတယ်။ ရလဒ်ကို တောင်းခဲ့တဲ့ item အရေအတွက်နဲ့ အမြဲ နှိုင်းယှဉ်ပါ။
Capacity မစစ်ခင် ရေးလိုက်တာ
count က MAX_STUDENTS နဲ့ ညီနေပြီးမှ parse_line(input[i], &list[count]) ကို ခေါ်ရင် array အဆုံးကို element တစ်ခု ကျော်ပြီး ရေးသွားပါတယ်။ Program က ဆက် run နေပြီး report ကလည်း ပြီးပြည့်စုံနေသလို ပြနေနိုင်ပေမယ့် တခြား variable တစ်ခုကို တိတ်တဆိတ် ဖျက်ဆီးနေပါတယ်။ Count ကို အရင်စစ်ပြီးမှ ရေးပါ။
စားပြီးမှ cast လုပ်တာ
(double)(total / count) က မှန်သလို ထင်ရပေမယ့် ဖြတ်ပြီးသား int ကို ပြောင်းတာပါ။ 270 / 4 က 67 ဖြစ်ပြီးမှ 67.0 ဖြစ်သွားပါတယ်။ total / (double)count လို cast ကို operand တစ်ခုပေါ်မှာ ထားပါ။
Logic function ထဲမှာ print ထုတ်တာ
average() က ကိုယ့်ရလဒ်ကို ကိုယ်တိုင် print ထုတ်နေရင် CSV export အတွက်ဖြစ်ဖြစ်၊ test အတွက်ဖြစ်ဖြစ် အဲဒီ အပို line မပါဘဲ ပြန်သုံးလို့ မရတော့ပါဘူး။ Value ကို return ပြန်ပြီး ဘာပြမလဲဆိုတာကို output layer ကို ဆုံးဖြတ်ခိုင်းပါ။
Mini challenge
ကြီးလာတဲ့ array နဲ့ contact book တစ်ခု ဆောက်ပါ
တူညီတဲ့ layer တွေကို တခြား program တစ်ခုမှာ သုံးပါ။ "name,phone" line တွေကို ဖတ်တဲ့ contact book တစ်ခု ရေးပါ။ Contact တွေကို ပြည့်တဲ့အခါ ကြီးလာတဲ့ heap array ထဲ သိမ်းပြီး နာမည်ထပ်နေတာကို ငြင်းပါ။ နာမည်နဲ့ contact ကို ရှာနိုင်ရမယ်၊ အားလုံးကို cleanup function တစ်ခုတည်းကနေ free လုပ်ရပါမယ်။
- parse_contact() က phone ဟာ digit 3 လုံးကနေ 15 လုံးအထိ ဖြစ်မှသာ "name,phone" ကို လက်ခံပါ
- book_add() က ထည့်ပြီးရင် 1၊ နာမည်ထပ်ရင် 0၊ realloc fail ဖြစ်ရင် -1 ပြန်ပါ
- Array က အလွတ်ကနေ စပြီး ပြည့်တိုင်း capacity ကို နှစ်ဆတိုး (2, 4, 8...) ပါ
- book_find() က contact ရဲ့ pointer ကို ပြန်ပေးပြီး နာမည်မရှိရင် NULL ပြန်ပါ
- book_free() က array ကို တစ်ကြိမ်တည်း free လုပ်ပြီး main() က ထွက်ခါနီးမှာ ခေါ်ပါ
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Contact {
char name[24];
char phone[16];
};
struct ContactBook {
struct Contact *items; /* heap array, NULL while the book is empty */
size_t count;
size_t capacity;
};
int parse_contact(const char *line, struct Contact *out) {
// TODO: split "name,phone" (hint: %23[^,]), reject anything extra,
// then make sure the phone is 3-15 digits. Return 1 if valid.
}
const struct Contact *book_find(const struct ContactBook *book, const char *name) {
// TODO: return a pointer to the matching contact, or NULL
}
int book_add(struct ContactBook *book, const struct Contact *c) {
// TODO: return 0 for a duplicate name
// TODO: when count == capacity, grow with realloc into a temporary pointer
// TODO: copy *c into the next slot and return 1
}
void book_free(struct ContactBook *book) {
// TODO: free the array and reset all three fields
}
int main(void) {
const char *input[] = {"Aye,0912345678", "Bo,09777", "Aye,0999999999",
"Chit 0911", "Daw,09-123", "Ei,0944444444"};
// TODO: parse and add each line, reporting bad lines and duplicates
// TODO: print count and capacity, look up "Bo" and "Zaw", free the book
}ရလဒ်: Aye၊ Bo၊ Ei ကို ထည့်ပြီး ဒုတိယ Aye ကို duplicate လို့ ပြပါမယ်။ "Chit 0911" နဲ့ "Daw,09-123" က bad line တွေပါ။ Book ထဲမှာ contact 3 ခု၊ capacity 4 ဖြစ်ပြီး Bo ရဲ့ number ကို ရှာတွေ့ပြီး Zaw ကိုတော့ ရှာမတွေ့ပါဘူး။
အရိပ်အမြွက် ကြည့်ရန်
ခွဲထုတ်ဖို့ "%23[^,],%15s %c" က comma မဟုတ်တဲ့ character 23 လုံးအထိ၊ comma တစ်ခု၊ ပြီးရင် phone ကို ဖတ်ပါတယ်။ ရလဒ် 2 ဆိုရင် နောက်က ဘာမှ အပို မပါဘူးလို့ အဓိပ္ပာယ်ရပါတယ်။ Array ကို ကြီးအောင်လုပ်တဲ့အခါ realloc ရဲ့ ရလဒ်ကို pointer အသစ်ထဲ သိမ်းပြီး NULL မဟုတ်မှန်း စစ်ပြီးမှ book->items ကို assign လုပ်ပါ။ ဒါမှ fail ဖြစ်ရင်တောင် array အဟောင်း မပျောက်ပါဘူး။
အဖြေ ကြည့်ရန်
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Contact {
char name[24];
char phone[16];
};
struct ContactBook {
struct Contact *items;
size_t count;
size_t capacity;
};
/* "name,phone" -> Contact. Phone must be 3-15 digits. Returns 1 if valid. */
int parse_contact(const char *line, struct Contact *out) {
char extra;
if (sscanf(line, "%23[^,],%15s %c", out->name, out->phone, &extra) != 2) {
return 0;
}
size_t len = strlen(out->phone);
if (len < 3) {
return 0;
}
for (size_t i = 0; i < len; i++) {
if (!isdigit((unsigned char)out->phone[i])) {
return 0;
}
}
return 1;
}
const struct Contact *book_find(const struct ContactBook *book, const char *name) {
for (size_t i = 0; i < book->count; i++) {
if (strcmp(book->items[i].name, name) == 0) {
return &book->items[i];
}
}
return NULL;
}
/* 1 = added, 0 = duplicate name, -1 = out of memory (book unchanged). */
int book_add(struct ContactBook *book, const struct Contact *c) {
if (book_find(book, c->name) != NULL) {
return 0;
}
if (book->count == book->capacity) {
size_t new_cap = book->capacity == 0 ? 2 : book->capacity * 2;
struct Contact *bigger = realloc(book->items, new_cap * sizeof *bigger);
if (bigger == NULL) {
return -1;
}
book->items = bigger;
book->capacity = new_cap;
}
book->items[book->count] = *c;
book->count++;
return 1;
}
void book_free(struct ContactBook *book) {
free(book->items);
book->items = NULL;
book->count = 0;
book->capacity = 0;
}
int main(void) {
const char *input[] = {"Aye,0912345678", "Bo,09777", "Aye,0999999999",
"Chit 0911", "Daw,09-123", "Ei,0944444444"};
size_t lines = sizeof input / sizeof input[0];
struct ContactBook book = {NULL, 0, 0};
for (size_t i = 0; i < lines; i++) {
struct Contact c;
if (!parse_contact(input[i], &c)) {
printf("Bad line: \"%s\"\n", input[i]);
continue;
}
int result = book_add(&book, &c);
if (result == 1) {
printf("Added %s\n", c.name);
} else if (result == 0) {
printf("Duplicate: %s\n", c.name);
} else {
printf("Out of memory\n");
break;
}
}
printf("%zu contacts, capacity %zu\n", book.count, book.capacity);
const char *wanted[] = {"Bo", "Zaw"};
for (size_t i = 0; i < 2; i++) {
const struct Contact *found = book_find(&book, wanted[i]);
printf("%s -> %s\n", wanted[i], found ? found->phone : "not found");
}
book_free(&book);
return 0;
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Program ကို data လမ်းကြောင်းအတိုင်း input၊ storage၊ logic၊ output ဆိုပြီး ခွဲပါ
- Variable တွေကို မယုံဘဲ sscanf ရဲ့ return value ကို ယုံပါ၊ trailing junk ကို %c နဲ့ ဖမ်းပါ
- Element အသစ် မရေးခင် array ရဲ့ capacity ကို စစ်ပါ
- ဒသမ မပျောက်အောင် operand တစ်ခုကို cast လုပ်ပါ (total / (double)count)
- Print မထုတ်တဲ့ logic function တွေကို test လုပ်ရ၊ ပြန်သုံးရ လွယ်ပါတယ်
AddressSanitizer — LLVM/Clang