theme | class | size | paginate | marp | ||
---|---|---|---|---|---|---|
default |
|
14580 |
true |
true |
Lec 6
- Write a program for cashier of a store.
- The program should keep the data regarding each customer and reciepts.
- It should be able find total revenue for any time frame.
- It should be able to list out chronologically sorted list of reciepts for a particular customer.
-
Customer has a
- Phone No.
- Name
-
Reciept has a
- date time
- value
- customer
typedef struct Customer {
char name[100];
int phone_no;
} Customer;
Use time_t from time.h https://en.cppreference.com/w/c/chrono/time_t
typedef struct Reciept {
time_t time;
float value;
Customer *customer;
} Reciept;
typedef struct Database {
Customer customers[100];
Reciept reciepts[1000];
} Database;
- Assuming max customers < 100 and reciepts < 1000.
- Suppose we need to add a new customer, where should it go?
- How many customers are there currently?
typedef struct Database {
Customer customers[100];
Reciept reciepts[1000];
int customer_count;
int reciept_count;
} Database;
customer_count
stores the current number of customers. New customers are added to thecustomers[customer_count]
andcustomer_count
is incremented.
Customer* add_customer(char *name,
int phone_no,
Database *db) {
Customer c = db->customers[db->customer_count];
c.phone_no = phone_no;
c.name = name;
db->customer_count++;
return c;
}
Customer* add_customer(char *name,
int phone_no,
Database *db) {
Customer *c =
&(db->customers[db->customer_count]);
c->phone_no = phone_no;
strcpy(c->name, name);
db->customer_count++;
return c;
}