Sie sind auf Seite 1von 39

ICP Assignment

ASSIGNMENT
TECHNOLOGY PARK MALAYSIA
CT018-3-1 ITCP
INTRODUCTION TO C PROGRAMMING
UC1F1305IT
Name

Lim Keng Liang

TP. No

TP028092

LECTURER

HAND OUT DATE :

2 November 2013

HAND IN DATE

ICP Assignment

Ms. Supriya Singh

24 February 2014

Page 1

ICP Assignment
Contents
CT018-3-1 ITCP..................................................................................................... 1
INTRODUCTION TO C PROGRAMMING..................................................................1
Introduction................................................................................................................ 4
Assumptions............................................................................................................... 4
Program Design (Pseudocode And Flowchart)............................................................5
i.

Void main()........................................................................................................ 5

ii.

Void dataentry()................................................................................................ 7

iii. Void newloan().................................................................................................. 8


iv. Void view()........................................................................................................ 9
v.

Void del()......................................................................................................... 10

vi.

Void update()............................................................................................... 11

vii.

Exit............................................................................................................... 11

C Programming Concepts......................................................................................... 12
Sample Output ..................................................................................................... 14
Flowcharts................................................................................................................ 19
Void Main()............................................................................................................ 19
Void dataentry().................................................................................................... 21
Void newloan()....................................................................................................... 22
Void view()............................................................................................................. 23
Void del()............................................................................................................... 24
Void update()......................................................................................................... 25
Source Code............................................................................................................. 26
Conclusion................................................................................................................ 30
References................................................................................................................ 31
Hanly, J. R., Koffman, E. B. and Friedman, F. L...............................................................31

ICP Assignment

Page 2

ICP Assignment
Introduction
This system was designed for the administrative staffs at a local book lending shop,
which had difficulties with their current system. It was difficult to manage and add new
bookings, especially when the staff wants to track a specific book of choice. The organization
wants to improve its services by implementing an automatic book loan service, which is easy and
convenient for staff to access and use. It is able to improve business operations as an overall
result and perhaps gain more customers for the book lending shop, given that the program is
functional well.

Assumptions
Based on the program built, it is safe to assume that the user has to key in the correct data
input, as there is no data validation of any type built into the program. The second assumption is
that users will have to key in a unique ID as there is also no data validation to whether the ID
was registered or not. This is because each data input has to be unique for the delete/edit/view
function to work; otherwise the data will be overlapped. Next, we can also assume that the books
in the library are inserted into the database by the staff is correct as there is no method to
add/delete books using this program currently. Finally, it is also safe to say that this system is not
perfect and has many errors that may occur.

ICP Assignment

Page 3

ICP Assignment

Program Design (Pseudocode And Flowchart)


i.

Void main()

Begin
Declare newloan, view, del, update, exit
Print Welcome to Our Automated Library Loan System
Print Main Menu
Print 1. New Loan
Print 2. View Existing Loan
Print 3. Delete Existing Loan
Print 4. Update Existing Loan
Print 5. Exit Program
REPEAT
Accept ch
Read ch
IF(ch=1)
call newloan()
ELSE(ch=2)
call view()
ELSE(ch=3)
call del()
ICP Assignment

Page 4

ICP Assignment
ELSE(ch=4)
call update()
ELSE(ch=5)
call exit()
ELSE
Print Invalid Choice!
END_IF
END_IF
END_IF
END_IF
END_IF
UNTIL (ch not=5)
END

ICP Assignment

Page 5

ICP Assignment

ii.

Void dataentry()

Begin
Print Insert Book Title
Accept Book Title
Print Insert Book ID
Accept Book ID
Print Insert Name
Accept Name
Print Insert ID Number
Accept ID Number
Print Insert Date of Loan
Accept Date of Loan
END

ICP Assignment

Page 6

ICP Assignment

iii.

Void newloan()

Begin
Current node=start node
IF(start node=NULL)
Start node = current node = memory allocation
dataentry
Current node as next node = NULL
Print Record Successfully Added!
END_IF
DOWHILE (current node as next)
Current node = current node as next node
Current node as next node = memory allocation
Current node = current node as next node
dataentry
Current node as next node = NULL
Print Error in adding Record!
ENDDO
END

ICP Assignment

Page 7

ICP Assignment

iv.

Void view()

BEGIN
IF(start node = NULL)
print No Records Added Yet!
current node = start node
END_IF
DOWHILE (current node)
print Book Title: current node as title
print Book ID: current node as code
print Name current node as name
print ID Number current node as ID
print Date current node as date
current node = current node as next node
ENDDO
END

ICP Assignment

Page 8

ICP Assignment

v.

Void del()

Begin
Declare name
Print Select Record to be Deleted:
accept name
current node = start node
DOWHILE (current node)
IF(STRINGCOMPARE (start node as name, name)=0)
Start node = start node as next node
free (current node)
print Record deleted!
DOWHILE (current node as next node)
IF(STRINGCOMPARE (current node as next node as name, name)=0
Current node as next node = current node as next node
Print Delete Successful!
Current node as next node as next node
Print Sorry, Record not Found! Try Again!!
END

ICP Assignment

Page 9

ICP Assignment

Void update()

vi.

Begin
Declare name
Current node = start node
Print Enter name to be updated:
Accept name
DOWHILE (current node)
IF(STRINGCOMPARE (current node as name, name)= 0)
Dataentry
Print Loan Successfully Updated!
Current node = current node as next node
Print Invalid Record!
END

vii.

Exit

Begin
Exit
END

ICP Assignment

Page 10

ICP Assignment

C Programming Concepts
There are multiple types of C concepts implemented in this program. The basic C concepts such
as displaying and reading of text, declaring variables, assignment of values are used throughout
this program. Below are the listed examples of the basic C concepts being implemented in the
program.

Figure 1.1 Basic C Concept

ICP Assignment

Page 11

ICP Assignment
As shown, users are able to read the text displayed on the screen, declare new variables using the
void main() and also the comment line which describes which part of the code is the Main Menu.

Besides the basic C concept, intermediate C concepts were also used to build the program. The
pictures below shows the intermediate C concepts used in the program.

Figure 1.2 Intermediate and Advanced C Concept


In this diagram, the use of the if and while control structure and also the struct declaration was
used. The if and while statement is combined together with the struct declaration to generate a
complete piece of code, where in this case it prompts the user to enter a value into a new loan,
and also an error message if the user enters an invalid input.

ICP Assignment

Page 12

ICP Assignment

Sample Output

Figure 2.1 Main Menu of the Program


When the program is launched, users will be taken to a simple cmd.exe interface
which shows the available functions of the program, namely the New Loan, View
Existing Loan, Delete Existing Loan, Update Existing Loan and Exit Program. Users
will have to input the numbers 1-5 to execute the programs functions, otherwise an
error message will pop out, telling the user that he/she has entered an invalid input
and must try again.

ICP Assignment

Page 13

ICP Assignment

Figure 2.1 Error Message when an invalid input is entered

If the user wishes to key in a new loan, he/she may press 1, and the interface then
changes and prompts the user to input the following details in order.

Figure 2.2 New Loan Interface


After successfully entering all required information, there will be a message showing
that the record was successfully added. A user can then check the existing loan by
returning back to the main menu and entering the number 2, which is to view an
existing loan.

ICP Assignment

Page 14

ICP Assignment

Figure 2.3 Existing Loan with Dummy Input

If the user wishes to delete an existing loan, they may press 3, and the interface will
change again and prompt the user to select the record to be deleted (in this case
the name of the user has to be entered, otherwise there would be a error response
from the program).

Figure 2.4 Error when Searching for Existing Record

ICP Assignment

Page 15

ICP Assignment

Figure 2.5 Message when a Record is successfully Deleted

If the user wishes to edit an existing record, he/she can choose option 4, where the interface
changes again, prompting the users name as recorded onto the database. If the name on the
database matches the input, the delete will be successful. Otherwise, there will be an error

ICP Assignment

Page 16

ICP Assignment
message.

Figure 2.6 Deleting a Record

Figure 2.7 Error when deleting a Record

ICP Assignment

Page 17

ICP Assignment
Finally, the user can also press option 5 if he/she wishes to exit the program.

Figure 2.8 Exiting the Program

ICP Assignment

Page 18

ICP Assignment

ICP Assignment

Page 19

ICP Assignment
Flowcharts
Void Main()

ICP Assignment

Page 20

ICP Assignment

ICP Assignment

Page 21

ICP Assignment

ICP Assignment

Page 22

ICP Assignment
Void dataentry()

ICP Assignment

Page 23

ICP Assignment

Void newloan()

ICP Assignment

Page 24

ICP Assignment

ICP Assignment

Page 25

ICP Assignment
Void view()

ICP Assignment

Page 26

ICP Assignment

ICP Assignment

Page 27

ICP Assignment

ICP Assignment

Page 28

ICP Assignment
Void del()

ICP Assignment

Page 29

ICP Assignment

ICP Assignment

Page 30

ICP Assignment

ICP Assignment

Page 31

ICP Assignment
Void update()

ICP Assignment

Page 32

ICP Assignment

Source Code
#include
#include
#include
#include

<stdio.h> // standard input output


<conio.h> // for running getch() function
<stdlib.h> // for functions like string handling and mathematical computations
<string.h> // for editing string command line

void dataentry();
struct menu
{
char title[20];
char name[20];
char ID[10] ;
char code[10];
char date[15];
struct menu *next;
}*start,*curr;
void main()
{
void newloan(),view(),del(),update(),exit();
int ch;
start=curr=NULL;
do
{

// Main Menu
system("cls");
printf("\n\n\n\t\t ************************************************");
printf("\n\n\t\t Welcome to our Automated Library Loan System!! ");
printf("\n\n\t\t ************************************************");
printf("\n\n\t\t **
Main Menu
**");
printf("\n\n\t\t **
1. New Loan
**");
printf("\n\n\t\t **
2. View Existing Loan
**");
printf("\n\n\t\t **
3. Delete Existing Loan
**");
printf("\n\n\t\t **
4. Update Exisiting Loan
**");
printf("\n\n\t\t **
5. Exit Program
**");
printf("\n\n\n\t\t ************************************************");
printf("\n\n\n\n\t\t Your Choice:");
scanf("%d",&ch);fflush(stdin);
switch(ch)
{
case 1:
newloan();
break;
case 2:
view();
break;
case 3:
del();
break;
case 4:
update();
break;
case 5:

ICP Assignment

Page 33

ICP Assignment
exit();
break;
default:
printf("\n\n\t Invalid choice! Please try again!");
}
getch();
}while(ch!=6);

}
void dataentry() // data entry list for Menu
{
printf("\n\t Insert Book Title:");
gets(curr->title);fflush(stdin);
printf("\n\t Insert Book ID:");
gets(curr->code);fflush(stdin);
printf("\n\t Insert Name:");
gets(curr->name);fflush(stdin);
printf("\n\t Insert ID Number:");
gets(curr->ID);fflush(stdin);
printf("\n\t Insert Date Of Loan:");
gets(curr->date);fflush(stdin);
}
void newloan() // Option 1, To enter a new loan
{
curr=start;
if(start==NULL)
{
//Empty list
start=curr=(struct menu *)malloc(sizeof(struct menu));
system("cls");
dataentry();
curr->next=NULL;
printf("\n\t Record Successfully Added!");
return;
}
//Reaching end of list
while(curr->next)
curr=curr->next;
curr->next=(struct menu *)malloc(sizeof(struct menu));
curr=curr->next;
system("cls");
dataentry();
curr->next=NULL;
printf("\n\t Error in adding Record!");
}
void view() // Option 2, View Existing Loan
{
system("cls");
if(start==NULL)
printf("\n\t No Records Added Yet!");
curr=start;
while(curr)
{
printf("\n\n\t Book Title: %-20s",curr->title);
printf("\n\n\t Book ID: %-10s",curr->code);
printf("\n\n\t Name: %-20s",curr->name);
printf("\n\n\t ID Number: %-15s",curr->ID);
printf("\n\n\t Date: %-20s",curr->date);

ICP Assignment

Page 34

ICP Assignment
printf("\n\n\t ======================================");
curr=curr->next;
}
getch();
}
void del() // Option 3, Delete Existing Loan
{
char name[20]; //Declaring a character (name)
system("cls");
printf("\n\t Select Record To Be Deleted:");
gets(name);
curr=start;
while(curr)
{
if(strcmp(start->name,name)==0)
{
start=start->next;
free(curr);
printf("\n\t Record deleted!");
return;
}
while(curr->next)
{
if(strcmp(curr->next->name,name)==0)
{
curr->next=curr->next->next;
printf("\n\n\t Delete successful!");
return;
}
curr=curr->next;
}
break;
}
printf("\n\n\t Sorry, Record not Found! Try Again!!");
}
void update() // Option 4 : Update Existing Loan
{
char name[20];
curr=start;
system("cls");
printf("\n\t Enter name to be updated:");
gets(name);
while(curr)
{
if(strcmp(curr->name,name)==0)
{
dataentry();
printf("\n\n\t Loan Successfully Updated!");
return;
}
curr=curr->next;
}
printf("\n\n\t Invalid Record!");

ICP Assignment

Page 35

ICP Assignment
}
void exit()
{
exit(0);
return;
}

ICP Assignment

Page 36

ICP Assignment

Conclusion
In conclusion, there were many faults in this program and it can be further improved to
better suit the working environment and necessities of the book loaning store. There are much
better ways to code this program, as the functions implemented in it are basic and not advanced.
I believe that given time, this program can have many useful functions implemented that can
help boost clientele and work efficiency of the book loaning store.

ICP Assignment

Page 37

ICP Assignment

References
Hanly, J. R., Koffman, E. B. and Friedman, F. L.
Problem solving and program design in C
In-text: (Hanly and Koffman et al., 1993)
Bibliography: Hanly, J. R., Koffman, E. B. and Friedman, F. L. 1993. Problem solving and
program design in C. Reading, Mass.: Addison-Wesley Pub. Co.

Cprogramming.com. 2013. C programming.com - Learn C and C++ Programming Cprogramming.com. [online] Available at: http://www.cprogramming.com/ [Accessed: 23 Feb
2014].

ICP Assignment

Page 38

Das könnte Ihnen auch gefallen