Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_CART 100
  6.  
  7. typedef char string15[16];
  8. typedef char string20[21];
  9. typedef char string50[51];
  10. typedef char string8[9];
  11. typedef struct{
  12.  
  13. string20 first,
  14. middle,
  15. last;
  16.  
  17. }nameType;
  18. typedef struct{
  19.  
  20. nameType name;
  21. string50 address;
  22.  
  23. }userInfoType;
  24. typedef struct{
  25.  
  26. string8 code;
  27. int qty;
  28.  
  29. }prodBoughtType;
  30. typedef prodBoughtType arrBought[MAX_CART];
  31. struct userTag{
  32.  
  33. string15 username,
  34. password;
  35.  
  36. userInfoType info;
  37.  
  38. char type;
  39. float creditlimit,
  40. outstanding;
  41.  
  42. arrBought cart;
  43.  
  44. int items;
  45.  
  46. struct userTag *pNext;
  47.  
  48. };
  49.  
  50. typedef struct userTag userType;
  51.  
  52. typedef struct userTag * ptrUser;
  53.  
  54. void displayAll(ptrUser pUsers){
  55.  
  56. while (pUsers != NULL){
  57. printf("%s\n", pUsers -> username);
  58. pUsers = pUsers -> pNext;
  59. }
  60.  
  61. }
  62.  
  63. void freeAll(ptrUser * pFirst){
  64.  
  65. ptrUser pDel;
  66.  
  67. while (*pFirst != NULL){
  68. pDel = *pFirst;
  69. *pFirst = (*pFirst) -> pNext;
  70. free(pDel);
  71. }
  72. }
  73.  
  74. void getUserInfo(userInfoType *pInfo){
  75.  
  76. printf("Enter Name \n");
  77. printf("First Name: "); fgets(pInfo -> name.first, 21, stdin);
  78. printf("Middle Name: "); fgets(pInfo -> name.middle, 21, stdin);
  79. printf("Last Name: "); fgets(pInfo -> name.last, 21, stdin);
  80.  
  81. printf("%s,%s%s \n", pInfo -> name.last, pInfo -> name.first, pInfo -> name.middle);
  82.  
  83.  
  84. printf("Enter Address: "); fgets(pInfo -> address, 51, stdin);
  85.  
  86. pInfo -> address[strlen(pInfo -> address) - 1] = '\0';
  87. pInfo -> name.first[strlen(pInfo -> name.first) - 1] = '\0';
  88. pInfo -> name.middle[strlen(pInfo -> name.middle) - 1] = '\0';
  89. pInfo -> name.last[strlen(pInfo -> name.last) - 1] = '\0';
  90.  
  91. }
  92.  
  93. void signUp(userType *pUser){
  94.  
  95. string8 tempcode;
  96. char cDump;
  97. char acode[9]={"DLSU2017"};
  98.  
  99. do{
  100.  
  101. printf("Enter username: "); scanf("%s%c", (*pUser).username, &cDump);
  102.  
  103. }while (strlen((*pUser).username) < 3 || strlen((*pUser).username) > 15);
  104.  
  105. do{
  106.  
  107. printf("Enter password: "); scanf("%s%c", pUser -> password, &cDump);
  108.  
  109. }while (strlen(pUser -> password) < 6 || strlen(pUser -> password) > 15);
  110.  
  111. do{
  112.  
  113. printf("Type A for Admin and S for Shopper: "); scanf("%c%c", pUser->type, &cDump);
  114. if(pUser->type == 'A' || pUser->type == 'a')
  115. printf("Enter authorization code: "); scanf("%s%c",&tempcode,&cDump);
  116. while(strcmp(acode, tempcode)!=0)
  117. printf(" Wrong Authorization Code try again");
  118.  
  119. if(pUser->type == 'S' || pUser->type == 's')
  120. printf("Congratulations you are now registered as a shopper!");
  121.  
  122. }while (pUser->type != 'A' && pUser->type != 'a' && pUser->type != 'S' && pUser->type != 's');
  123.  
  124. getUserInfo(&pUser -> info);
  125.  
  126. }
  127.  
  128. void displayAllRecur(ptrUser pUsers){
  129.  
  130. if (pUsers != NULL){
  131. printf("%s\n", pUsers -> username);
  132. displayAllRecur(pUsers -> pNext);
  133. }
  134. }
  135.  
  136. ptrUser search(ptrUser pFirst, string15 username){
  137.  
  138. ptrUser pRun;
  139. pRun = pFirst;
  140.  
  141. while (pRun != NULL && strcmp(username, pRun -> username) != 0){
  142.  
  143. pRun = pRun -> pNext;
  144.  
  145. }
  146.  
  147. return pRun;
  148.  
  149. }
  150.  
  151. void deleteNode(ptrUser *pFirst, string15 username){
  152.  
  153. ptrUser pFind, pRun;
  154.  
  155. if (*pFirst == NULL)
  156. printf("The List is empty");
  157.  
  158. else {
  159.  
  160. pFind = search(*pFirst, username);
  161. if (pFind == NULL)
  162.  
  163. printf("%s is not in the List\n", username);
  164.  
  165. else { //found the node to be deleted
  166.  
  167. if(pFind == *pFirst){ //deleting first node
  168.  
  169. *pFirst = (*pFirst) -> pNext;
  170.  
  171. }
  172.  
  173. else{ //delete from middle or end
  174.  
  175. pRun = *pFirst;
  176. while (pRun -> pNext == pFind)
  177. pRun = pRun -> pNext;
  178.  
  179. pRun -> pNext = pFind -> pNext;
  180.  
  181. }
  182.  
  183. free(pFind);
  184. pFind = NULL;
  185.  
  186. }
  187.  
  188. }
  189.  
  190. }
  191.  
  192. void login(userType *pUsers, userInfoType *pInfo){
  193. int ctr;
  194. string15 userchk, passchk;
  195.  
  196. do{
  197. printf("Enter Username:");scanf("%s",&userchk);
  198. printf("Enter Password:");scanf("%s",&passchk);
  199. while(pUsers != NULL){
  200. if(strcmp(pUsers -> username, userchk)!=0)
  201. {
  202. pUsers = pUsers->pNext;
  203. printf("Username Invalid try again");
  204. }
  205. else if(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)!=0)
  206. {
  207. pUsers = pUsers->pNext;
  208. printf("Incorrect Password try again");
  209. ctr++;
  210. }
  211. else if(strcmp(pUsers -> username, userchk)==0 && strcmp(pUsers -> password, passchk)==0)
  212. {
  213. if(pUsers->type=='A' || pUsers->type == 'a')
  214. printf("going to admin menu");
  215. else
  216. printf("welcome to the shopper menu");
  217.  
  218. }
  219. }
  220. }while(ctr<=3);
  221. printf("counter exceeded account locked");
  222.  
  223. }
  224.  
  225.  
  226. int main(){
  227.  
  228. /*
  229. userType user;
  230. userType *pUsers = NULL,
  231. *pNew;
  232. */
  233. ptrUser pUsers = NULL,
  234. pNew, pLast;
  235.  
  236. int opt,n;
  237. char cDump;
  238. ptrUser pRun, pTrail;
  239.  
  240. printf("Welcome to the shopping kiosk\n\n");
  241. printf("type 1 if you would like to log in\n0 if you would like to sign up: ");
  242. scanf("%d",&n);
  243.  
  244. switch(n){
  245.  
  246. case 1:login(pUsers, &pUsers->info);break;
  247.  
  248. default: do{
  249. /*
  250. pNew = malloc(sizeof(userType));
  251.  
  252. signUp(pNew);
  253. pNew -> pNext = NULL;
  254.  
  255. if (pUsers == NULL)
  256. pUsers = pNew;
  257.  
  258. else
  259. pLast -> pNext = pNew;
  260.  
  261. pLast = pNew;
  262. */
  263.  
  264. pNew = malloc(sizeof(userType));
  265.  
  266. signUp(pNew);
  267. pNew -> pNext = NULL;
  268.  
  269. if (pUsers == NULL) //if list is empty
  270. pUsers = pNew;
  271.  
  272. else if (strcmp(pUsers -> username, pNew -> username) > 0){ //conect as first node
  273.  
  274. pNew -> pNext = pUsers;
  275. pUsers = pNew;
  276.  
  277. }
  278.  
  279. else { //modifying middle of list
  280.  
  281. pRun = pUsers;
  282. while (pRun != NULL && strcmp(pRun -> username, pNew -> username) < 0){
  283.  
  284. pTrail = pRun;
  285. pRun = pRun -> pNext;
  286.  
  287. }
  288.  
  289. pTrail -> pNext = pNew;
  290. pNew -> pNext = pRun;
  291.  
  292. }
  293. printf("Type '1' to enter another user. Type '0' to display all users. "); scanf("%d%c", &opt, &cDump);
  294.  
  295. } while (opt == 1);
  296.  
  297. displayAllRecur(pUsers);
  298. //displayAll(pUsers);
  299. break;
  300. }
  301. freeAll(&pUsers);
  302.  
  303. return 0;
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement