Vous êtes sur la page 1sur 6

Software/Hardware design - ECE 2551 Prepared by: Date Performed:

Title: Lab #7 Lab Instructor:

Objectives:
This set of experiments will: 1. Allow the student to demonstrate the C programming skills learned during the first part of ECE2551.

Problem Statement:
In this lab, you are tasked to create a program that either: 1. Expands on any of the labs covered so far. 2. Demonstrates the C programming skills covered during class in your own small project.

Program Requirement:
We will implement a program to covered all topic in class: 1. Selection statements. 2. Loops and Logic. 3. Pointers and Arrays. 4. String. 5. Dynamic Memory Allocation and Pointer Indexing.

Project Specification:
I think about doing a mini project for booking the tickets at the airport. The scope of project is: Start the program, you can see all the cities you can fly to. The menu is available for you to choose the option: 1. See the details of the flights ( see the details of ticket to the specific city about where we go, the number of tickets remain, the departure hour, the arrive hour and the price to fly). 2. Book tickets ( you can book the ticket if the ticket in pocket isnt empty and the price of all tickets you book is lower than your account balance) When booking successfully, you balance and the number of tickets in pocket will be decreased. Note: You can book tickets once you have changed your account number for at least one time. 3. You can change your account number, when change with any number you want, I assume I will provide you 500$ for this account(not in the real life). 4. You can see information about your bank account (account number and balance). Note: You can see info once you have changed your account number for at least one time. 5. Exit the program.

Selection Statement:
Slection statement is very popular in the programming language:

Some of them: an if statement with no else is a one-way selector an if-else statement is a two-way selector a switch or case statement is an n-way selector With the menu, I use an n-way selector:
switch (opt) { case 1: //see the details of the flight break; case 2: //Book tickets break; case 3: // change bank account number for booking break; case 4: // show information of your bank account break; case 5: //exit the program break; default: //print the error when don't find the suitable option printf("Error: Unknown option!"); break; }

When determine the booking is successful or not, I use two-way selector:


// require number of tickets greater than in the pocket if (nTickets > tickets[num]->quantity) { printf("Sorry, we have only %d tickets! Please try with other number!\n", tickets[num]->quantity); } // book successfull else { // sum of money you have to pay int money = nTickets * tickets[num]->price; // your budget can afford it if (money <= balance) { // decrease the quantity of tickets in pocket tickets[num]->quantity -= nTickets; printf("You have booked %d tickets successfully!\n",nTickets); // decrease the balance of this account balance -= money; printf("You have to pay %d$, the rest in your account(number: %d) is %d$!\n",money,accNum,balance); printf("Please remember we will depart at %d:00h!\n", tickets[num]->hourDeparture); break; } // you don't have enough money else { printf("You don't have enough money!\n"); }

Loops and Logics:


With loop we can have: for, while, do while. With loop we can make the source code run repeatly as we want. I use for when initiate for all element of tickets (an array of Ticket *).
for (i = 0; i < nCities; i++) { tickets[i] = malloc(sizeof(Ticket)); tickets[i]->quantity = gen_ran(5); tickets[i]->hourDeparture = gen_ran(23); int temp; while (TRUE) { temp = gen_ran(23) + 1; if (temp > tickets[i]->hourDeparture) { tickets[i]->hourArrive = temp; break; } } tickets[i]->price = gen_ran(100) + 100; }

(1)

I use loop for an endless loop:


//the endless loop until read option 4 while (1) { }

With the logics, you can see I always use it in my program, logic is about determining one logic expresion if its true or false to excute some business code.

Pointers and Arrays:


An array is a collection of same type of elements which are sheltered under a common name. I use array city_name that is an array of string (each string is an array of character with length max is 20). You have to choice to access to one city name: - city_name[i] -> the string represent name of the i-th city. - *(city_name + i) -> the string represent name of the i-th city.
char city_name[13][20]={"Los Angeles", "New York", "Miami", "Chicago", "Atlanta", "Las Vegas", "Orlando", "Washington", "Houston", "New Orleans", "San Francisco", "San Diego", "X"}; (2)

String:
From source (2) above, I have used string to represent the name of each city. The string here is an array of character. Use strcmp(string compare) to compare two string and decide if its equal or not. Learnt in this course many other function supported in string.h library: strcpy, strlen
while (strcmp(*(city_name+i),"X") != 0) {

if (isPrint) printf("%s\n",city_name[i]); i++; }

Dynamic Memory Allocation and Pointer Indexing:


As you can see, the source code (1) described above is about pointer and dynamic memory allocation. I have used dynamic memory allocation:
tickets[i] = malloc(sizeof(Ticket));

I use -> for point to the attribute of the record:


tickets[i]->quantity = gen_ran(5);

Before exit, we have to free the allocation we have done before:


void freeTickets(Ticket* tickets[]) { int i = 0; for (i = 0; i < nCities; i++) { free(tickets[i]); } }

Source Code:
Please reading file Lab7.c attached. Copying source code to word may destroy some viewing structures.

Conclusion:
After learning this course, I can understand all the materials with some topics: 1. 2. 3. 4. 5. Selection statements. Loops and Logic. Pointers and Arrays. String. Dynamic Memory Allocation and Pointer Indexing.

After that, I can use all knowledge I have studied from this to write my own project: Ticket booking at the airport which can resolve some simple problems.

Some Screenshots:

Vous aimerez peut-être aussi