Toplam hesaplama
- C++ kursu
- Başlangıç
- Ders 50/116
- 6 dk
Subtotal ve vergi dahil toplamı hesaplamayı öğrenirsin.
Toplam hesaplama
Sepetteki ürünlerin ara toplamını (subtotal) hesaplamak için getSubtotal fonksiyonunu yazacağız. Her ürünün katkısı p.price * p.quantity'dir; tüm ürünleri gezip toplayarak ara toplamı buluruz.
Bu, klasik bir birikim örneğidir: double sum = 0; ile başla, range-based for ile her ürünü gez, sum += p.price * p.quantity ile topla. Son olarak return sum; ile sonucu döndür. Bu yöntem ileride stok, ortalama gibi birçok hesapta işine yarayacak.
Vergi hesaplama
Subtotal, vergi ve ek ücretler hariç ara toplamdır. Vergi dahil toplamı bulmak için subtotal * (1 + taxRate) kullanılır; örneğin %18 vergi için 1.18 çarpanı alınır.
Bu projede getSubtotal yalnızca ara toplamı döndürür; vergili toplamı main içinde hesaplayabilirsin. İki kavramı ayırmak, vergi oranı değiştiğinde sadece çarpanı güncellemeyi kolaylaştırır. Ülkeden ülkeye değişen vergi oranları için bu ayrım önemlidir.
Uygulama getSubtotal fonksiyonunu yaz
Cart yapısına bir getSubtotal fonksiyonu ekle. İmzası double getSubtotal() olmalı. Gövdede double sum = 0; ile başla, for (const auto& p : items) ile her ürünü gez, sum += p.price * p.quantity ile topla ve return sum; ile döndür. main iki ürün ekleyip getSubtotal'ı yazdırıyor. Çıktı 80 olmalı.
Başlangıç kodu
#include <iostream>
#include <string>
#include <vector>
struct Product {
std::string name;
double price;
int quantity;
};
struct Cart {
std::vector<Product> items;
void addItem(Product p) {
items.push_back(p);
}
};
int main() {
Cart cart;
Product p1;
p1.name = "Defter";
p1.price = 25.0;
p1.quantity = 2;
cart.addItem(p1);
Product p2;
p2.name = "Kalem";
p2.price = 10.0;
p2.quantity = 3;
cart.addItem(p2);
std::cout << cart.getSubtotal() << std::endl;
return 0;
}Örnek çözüm
#include <iostream>
#include <string>
#include <vector>
struct Product {
std::string name;
double price;
int quantity;
};
struct Cart {
std::vector<Product> items;
void addItem(Product p) {
items.push_back(p);
}
double getSubtotal() {
double sum = 0;
for (const auto& p : items) {
sum += p.price * p.quantity;
}
return sum;
}
};
int main() {
Cart cart;
Product p1;
p1.name = "Defter";
p1.price = 25.0;
p1.quantity = 2;
cart.addItem(p1);
Product p2;
p2.name = "Kalem";
p2.price = 10.0;
p2.quantity = 3;
cart.addItem(p2);
std::cout << cart.getSubtotal() << std::endl;
return 0;
}Beklenen çıktı
80
Soru ve cevaplar
Dersin sorularını cevaplarıyla birlikte inceleyebilirsin. Kendini denemek istersen dersi interaktif olarak çöz.
1 Kavramı anlamına eşle
| Kavram | Anlamı |
|---|---|
Subtotal |
Vergi hariç toplam |
Tax |
Vergi oranı |
Total |
Vergi dahil toplam |
Quantity |
Ürün adedi |
2 getSubtotal gövdesini yaz
double sum = 0;
for (const auto& p : items) {
____;
}
return sum;
double sum = 0;
for (const auto& p : items) {
sum += p.price * p.quantity;
}
return sum;
Açıklama: Her ürünün katkısı price * quantity'dir.
3 Subtotal ne demektir?
// sepette- Ürün adedi
- Vergi dahil toplam
- Vergi hariç ara toplam Doğru cevap
- İndirim tutarı
Açıklama: Subtotal vergi ve ekler hariç ara toplamdır.