Advertisement
Guest User

mp bullshit

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