Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. /* PROGRAM: Modify contents of phonebook
  2. AUTHOR: Tinashe Chapfunga and Brandon Le
  3. DATE: November 10, 2018
  4. PURPOSE: Modify contents of phonebook
  5. LEVEL OF DIFFICULTY: 10
  6. CHALLENGES: Nodes and array calling
  7. HOURS SPENT: 6
  8. */
  9.  
  10. #include <stdio.h>
  11. #include "header.h"
  12.  
  13. int ExistingAreaCodeNums[5] = {613, 416, 647, 519, 905};
  14. char *ExistingAreaCodeDesc[5] = {"Ottawa", "Toronto", "Toronto", "Windsor", "Niagra"};
  15.  
  16. /* return number of nodes in the list. */
  17. int Length (struct node* head) {
  18. int count = 0;
  19. struct node* current = head;
  20.  
  21. while (current != NULL) {
  22. count++;
  23. current = current->next;
  24. }
  25. return count;
  26. }
  27.  
  28. /* Prints all the nodes on a list */
  29. void PrintList(struct node* head) {
  30. struct node* current = head;
  31.  
  32. if (current == NULL) {
  33. printf("Linked list is empty!\n");
  34. return;
  35. }
  36.  
  37. printf("The list is: ");
  38. while (current != NULL) {
  39. printf("%d -> ", current->data);
  40. current = current->next;
  41. }
  42.  
  43. printf("\n");
  44. return;
  45.  
  46. }
  47.  
  48. /*Given an int and a reference to the head pointer, add a new node at the head of the list. */
  49. void Add(struct node** headRef, int new) {
  50. struct node* head;
  51.  
  52. head = (struct node*) malloc(sizeof(struct node));
  53. head->data = new;
  54. head->next = *headRef;
  55. *headRef = head;
  56.  
  57. printf("New node added to the list.\n");
  58. return;
  59. }
  60.  
  61. /*Takes a non-empty list, deletes the head node, and returns the head node's data.*/
  62. int Delete(struct node** headRef) {
  63. struct node current;
  64. int val = 0;
  65.  
  66. if (*headRef == NULL) {
  67. printf("List is empty. Nothing to delete.\n");
  68. return val;
  69. }
  70.  
  71. current = *headRef;
  72. val = current->data;
  73. headRef = current->next;
  74. free(current);
  75.  
  76. printf("The head is deleted.\n");
  77. return val;
  78. }
  79.  
  80. /* Deallocates all of its memory and sets its head pointer to NULL*/
  81. void ZeroList(struct node** headRef) {
  82. struct node p;
  83.  
  84. if (headRef == NULL) {
  85. printf("List is empty, nothing to free\n");
  86. return;
  87. }
  88.  
  89. while (headRef != NULL) {
  90. p = *headRef;
  91. *headRef = p->next;
  92. free(p);
  93. }
  94.  
  95. printf("All nodes have been deleted.\n");
  96. return;
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103. /* PROGRAM: Modify contents of phonebook
  104. AUTHOR: Tinashe Chapfunga and Brandon Le
  105. DATE: November 10, 2018
  106. PURPOSE: Modify contents of phonebook
  107. LEVEL OF DIFFICULTY: 10
  108. CHALLENGES: Nodes and array calling
  109. HOURS SPENT: 6
  110. */
  111.  
  112. #include <stdlib.h>
  113.  
  114. typedef struct Area{
  115. int areaCode;
  116. char areaName[20];
  117. struct Area *nextArea;
  118. } Area;
  119.  
  120. typedef struct PhEntry{
  121. int areaCode;
  122. int phoneNumber;
  123. char firstName[20];
  124. char lastName[20];
  125. struct PhEntry *nextPhEntry;
  126. } PhEntry;
  127.  
  128. int ExistingAreaCodeNums[5];
  129. char *ExistingAreaCodeDesc[5];
  130.  
  131. int Length(struct node* head);
  132. void PrintList(struct node* head);
  133. void Add(struct node** headRef, int new);
  134. int Delete(struct node** headRef);
  135. void ZeroList(struct node** headRef);
  136.  
  137.  
  138.  
  139.  
  140. /* PROGRAM: Modify contents of phonebook
  141. AUTHOR: Tinashe Chapfunga and Brandon Le
  142. DATE: November 10, 2018
  143. PURPOSE: Modify contents of phonebook
  144. LEVEL OF DIFFICULTY: 10
  145. CHALLENGES: Nodes and array calling
  146. HOURS SPENT: 6
  147. */
  148.  
  149. #include<stdio.h>
  150. #include<stdlib.h>
  151. #include "header.h"
  152.  
  153. int main(){
  154. char userInput = '0';
  155.  
  156. while(userInput != 'q'){
  157. printf("Choose one of the following options:");
  158. printf("Press [1] to Enter Area information:");
  159. printf("Press [2] to Enter PhoneBook Entry:");
  160. printf("Press [3] to Modify an existing PhoneBook Entry:");
  161. printf("Press [4] to Delete an existing PhoneBook Entry:");
  162. printf("Press [5] to Delete an unused Area using doubly link list:");
  163. printf("Press [q] to quit:");
  164. scanf("%s", &userInput);
  165.  
  166. switch(userInput){
  167. case 1 :
  168. int areaCode;
  169. int areaCodeCheck;
  170. char *areaDesc;
  171.  
  172. printf("You pressed: 1");
  173. printf("Enter Area Code: ");
  174. scanf("%d", &areaCode);
  175.  
  176. printf("\n");
  177.  
  178. areaCodeCheck = areaCode/100;
  179.  
  180. if((areaCodeCheck != 0)&&(areaCodeCheck != 1)){
  181. printf("Enter Area Description: ");
  182. scanf("%s", &areaDesc);
  183. }
  184. else{
  185. printf("Invalid Area Code.");
  186. }
  187. break;
  188.  
  189. case 2 :
  190. int areaCode;
  191. int areaCodeCheck;
  192. int phoneNum;
  193.  
  194. printf("You pressed: 2");
  195. printf("Enter Area Code: ");
  196. scanf("%d", &areaCode);
  197.  
  198. printf("\n");
  199.  
  200. areaCodeCheck = areaCode/100;
  201.  
  202. if((areaCodeCheck != 0)&&(areaCodeCheck != 1)){
  203. printf("Enter 7-digit phone number: ");
  204. scanf("%d", &phoneNum);
  205. }
  206. else{
  207. printf("Invalid Area Code.");
  208. }
  209. break;
  210.  
  211. case 3 :
  212. int areaCode;
  213. int areaCodeCheck;
  214. int phoneNum;
  215. char *lastName;
  216.  
  217. printf("You pressed: 3");
  218. printf("Enter Area Code: ");
  219. scanf("%d", &areaCode);
  220.  
  221. printf("\n");
  222.  
  223. areaCodeCheck = areaCode/100;
  224.  
  225. if((areaCodeCheck != 0)&&(areaCodeCheck != 1)){
  226. printf("Enter 7-digit phone number: ");
  227. scanf("%d", &phoneNum);
  228.  
  229. printf("Enter last name: ");
  230. scanf("%s", &lastName);
  231. }
  232. else{
  233. printf("Invalid Area Code.");
  234. }
  235. break;
  236.  
  237. case 4 :
  238. int areaCode;
  239. int areaCodeCheck;
  240. int phoneNum;
  241. char *lastName;
  242.  
  243. printf("You pressed: 4");
  244. printf("Enter Area Code: ");
  245. scanf("%d", &areaCode);
  246.  
  247. printf("\n");
  248.  
  249. areaCodeCheck = areaCode/100;
  250.  
  251. if((areaCodeCheck != 0)&&(areaCodeCheck != 1)){
  252. printf("Enter 7-digit phone number: ");
  253. scanf("%d", &phoneNum);
  254.  
  255. printf("Enter last name: ");
  256. scanf("%s", &lastName);
  257. }
  258. else{
  259. printf("Invalid Area Code.");
  260. }
  261. break;
  262.  
  263. case 5 :
  264. int areaCode;
  265. int areaCodeCheck;
  266. char *areaDesc;
  267. int phoneNum;
  268. char *lastName;
  269.  
  270. printf("You pressed: 3");
  271. printf("Enter Area Code: ");
  272. scanf("%d", &areaCode);
  273.  
  274. printf("\n");
  275.  
  276. areaCodeCheck = areaCode/100;
  277.  
  278. if((areaCodeCheck != 0)&&(areaCodeCheck != 1)){
  279. printf("Enter Area Description: ");
  280. scanf("%s", &areaDesc);
  281. }
  282. else{
  283. printf("Invalid Area Code.");
  284. }
  285. break;
  286.  
  287. default:
  288. printf("Invalid Input");
  289. break;
  290. }
  291. }
  292. }
  293.  
  294.  
  295.  
  296.  
  297. CC = gcc
  298. CC_FLAGS = -g -ansi -pedantic -Wall
  299. FILES = main.c2, header.c, header.h
  300. OUT_EXE = Assignment 2
  301.  
  302. build: $(FILES)
  303. $(CC) $(CC_FLAGS) -o $(OUT_EXE) $(FILES)
  304.  
  305. clean:
  306. rm -f *.o core *.exe *~
  307.  
  308. rebuild: clean build
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement