Menu
Coddy logo textTech

סיכום – הבנת מצביעים

חלק מהיחידה יסודות במסלול ה-GO של Coddy. שיעור 66 מתוך 109.

challenge icon

אתגר

בינוני

באתגר הזה תבנו מערכת פשוטה לניהול מלאי עבור חנות קטנה. תשתמשו במצביעים כדי לעדכן כמויות במלאי, לחשב ערכים ולעקוב אחר שינויים.

השלימו את שלוש הפונקציות הבאות:

  1. updateQuantity(quantity *int, change int) (int, bool)
    שנו את הערך שאליו מצביע quantity על ידי הוספת change (ערך שלילי עבור מכירות, ערך חיובי עבור חידוש מלאי).
    אם התוצאה תהיה קטנה מ־0, הציבו במקום זאת 0 ב־*quantity.
    החזירו את הכמות החדשה ואת ערך בוליאני שיהיה true אם הפריט אזל מהמלאי (*quantity == 0).
  2. calculateValue(itemName string, quantity int, price float64, mostValuableItem *string, highestValue *float64) float64
    חשבו את totalValue בתור float64(quantity) * price.
    אם totalValue גדול מ־*highestValue, עדכנו את *highestValue והציבו את itemName ב־*mostValuableItem באמצעות המצביעים.
    החזירו את totalValue.
  3. displayInventory(apples, oranges, bananas *int, applePrice, orangePrice, bananaPrice float64)
    הדפיסו את שם כל פריט, את הכמות שלו (לאחר ביצוע הפניה לערך דרך המצביע) ואת הערך המחושב באמצעות fmt.Printf.
    עיצוב: Apples: 10 (Value: $5.00): השתמשו ב־%.2f עבור המחירים.

הפלט הצפוי עבור המלאי ההתחלתי הוא:

Initial Inventory:
Apples: 10 (Value: $5.00)
Oranges: 15 (Value: $10.50)
Bananas: 8 (Value: $2.40)
Total inventory value: $17.90
Most valuable item: Oranges

טיפ: השתמשו ב־*quantity כדי לקרוא את הערך שאליו מצביע מצביע או לכתוב אליו. לדוגמה, *quantity += change משנה ישירות את המשתנה המקורי.

נסו בעצמכם

package main

import "fmt"

// updateQuantity מעדכנת את הכמות של פריט ומחזירה את הכמות החדשה
// ואת המידע האם הפריט אזל מהמלאי (quantity = 0)
func updateQuantity(quantity *int, change int) (int, bool) {
    // TODO: עדכנו את המצביע לכמות והחזירו את הערך החדש ואת מצב המלאי
    return 0, false
}

// calculateValue מחשבת את הערך הכולל של פריט על סמך הכמות והמחיר
// היא גם מעדכנת את mostValuableItem אם הפריט הזה בעל ערך גבוה יותר
func calculateValue(itemName string, quantity int, price float64, mostValuableItem *string, highestValue *float64) float64 {
    // TODO: חשבו את הערך הכולל, עדכנו את mostValuableItem אם צריך, והחזירו את הערך הכולל
    return 0.0
}

// displayInventory מדפיסה את פרטי המלאי
// היא מקבלת מצביעים כדי שתוכל להציג נתונים בזמן אמת
func displayInventory(apples, oranges, bananas *int, applePrice, orangePrice, bananaPrice float64) {
    fmt.Printf("Apples: %d (Value: $%.2f)\n", *apples, float64(*apples) * applePrice)
    fmt.Printf("Oranges: %d (Value: $%.2f)\n", *oranges, float64(*oranges) * orangePrice)
    fmt.Printf("Bananas: %d (Value: $%.2f)\n", *bananas, float64(*bananas) * bananaPrice)
}

func main() {
    // אתחול המלאי
    apples := 10
    oranges := 15
    bananas := 8
    
    // מחירים
    applePrice := 0.5  // $0.50 ליחידה
    orangePrice := 0.7 // $0.70 ליחידה
    bananaPrice := 0.3 // $0.30 ליחידה
    
    // עקוב אחר הפריט בעל הערך הגבוה ביותר
    var mostValuableItem string
    var highestValue float64
    
    // הצג את המלאי ההתחלתי
    fmt.Println("Initial Inventory:")
    displayInventory(&apples, &oranges, &bananas, applePrice, orangePrice, bananaPrice)
    
    // חשב את הערכים ההתחלתיים ומצא את הפריט בעל הערך הגבוה ביותר
    appleValue := calculateValue("Apples", apples, applePrice, &mostValuableItem, &highestValue)
    orangeValue := calculateValue("Oranges", oranges, orangePrice, &mostValuableItem, &highestValue)
    bananaValue := calculateValue("Bananas", bananas, bananaPrice, &mostValuableItem, &highestValue)
    
    fmt.Printf("Total inventory value: $%.2f\n", appleValue+orangeValue+bananaValue)
    fmt.Printf("Most valuable item: %s\n\n", mostValuableItem)
    
    // הדמה כמה מכירות
    fmt.Println("Processing sales...")
    _, applesOutOfStock := updateQuantity(&apples, -4) // מכור 4 תפוחים
    _, orangesOutOfStock := updateQuantity(&oranges, -8) // מכור 8 תפוזים
    _, bananasOutOfStock := updateQuantity(&bananas, -10) // נסה למכור 10 בננות (יותר ממה שיש לנו)
    
    // בדוק אם פריטים כלשהם אזלו מהמלאי
    if applesOutOfStock {
        fmt.Println("Apples are out of stock!")
    }
    if orangesOutOfStock {
        fmt.Println("Oranges are out of stock!")
    }
    if bananasOutOfStock {
        fmt.Println("Bananas are out of stock!")
    }
    
    // הצג את המלאי המעודכן
    fmt.Println("\nUpdated Inventory:")
    displayInventory(&apples, &oranges, &bananas, applePrice, orangePrice, bananaPrice)
    
    // אפס את המעקב אחר הפריט בעל הערך הגבוה ביותר לצורך חישוב מחדש
    mostValuableItem = ""
    highestValue = 0
    
    // חשב מחדש את הערכים
    appleValue = calculateValue("Apples", apples, applePrice, &mostValuableItem, &highestValue)
    orangeValue = calculateValue("Oranges", oranges, orangePrice, &mostValuableItem, &highestValue)
    bananaValue = calculateValue("Bananas", bananas, bananaPrice, &mostValuableItem, &highestValue)
    
    fmt.Printf("Total inventory value: $%.2f\n", appleValue+orangeValue+bananaValue)
    fmt.Printf("Most valuable item: %s\n\n", mostValuableItem)
    
    // חדש את מלאי הפריטים
    fmt.Println("Restocking...")
    updateQuantity(&apples, 5)  // הוסף 5 תפוחים
    updateQuantity(&oranges, 10) // הוסף 10 תפוזים
    updateQuantity(&bananas, 12) // הוסף 12 בננות
    
    // הצג את המלאי הסופי
    fmt.Println("\nFinal Inventory:")
    displayInventory(&apples, &oranges, &bananas, applePrice, orangePrice, bananaPrice)
    
    // אפס את המעקב אחר הפריט בעל הערך הגבוה ביותר לקראת החישוב הסופי
    mostValuableItem = ""
    highestValue = 0
    
    // חישוב הערך הסופי
    appleValue = calculateValue("Apples", apples, applePrice, &mostValuableItem, &highestValue)
    orangeValue = calculateValue("Oranges", oranges, orangePrice, &mostValuableItem, &highestValue)
    bananaValue = calculateValue("Bananas", bananas, bananaPrice, &mostValuableItem, &highestValue)
    
    fmt.Printf("Total inventory value: $%.2f\n", appleValue+orangeValue+bananaValue)
    fmt.Printf("Most valuable item: %s\n", mostValuableItem)
}

כל השיעורים ביחידה יסודות

תרגלו בעצמכם: קומפיילר Go אונליין