Advertisement
Guest User

Untitled

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