Advertisement
Guest User

Untitled

a guest
May 16th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "estruturas.h"
  5.  
  6.  
  7.  
  8.  
  9.  
  10. void clear(){
  11. system("@cls||clear");
  12. }
  13.  
  14. void voltar_menu(){
  15. printf("Prima Enter para voltar ao menu.");
  16. getchar();
  17. clear();
  18. }
  19.  
  20. /*
  21. MENU
  22. */
  23.  
  24. void menu()
  25. {
  26. int opcao;
  27. printf("MENU PRINCIPAL \n----------------------\n");
  28.  
  29. printf("Escolha a sua opcao \n");
  30. printf("1 - Listagem por ordem alfabetica.\n");
  31. printf("2 - Adicionar preferencias de utilizador.\n");
  32. printf("3 - Remover preferencias de utilizador.\n");
  33. printf("4 - Listagem por popularidade.\n");
  34. printf("5 - Gerar Viagem.\n");
  35. printf("6 - Terminar o programa.\n");
  36.  
  37. scanf("%d",&opcao);
  38. getchar();/*por causa do enter(NAO SEI PQ, mas se n tiver esta merda n da print depois as opcoes)*/
  39. clear();
  40.  
  41. switch(opcao){
  42. case 1:
  43. clear();
  44. printf("Listagem por ordem alfabetica\n -------------\n");
  45. voltar_menu();
  46. break;
  47. case 2:
  48. clear();
  49. printf("Adicionar preferencias de utilizador\n -------------\n");
  50. voltar_menu();
  51. break;
  52. case 3:
  53. clear();
  54. printf("Remover preferencias de utilizador\n -------------\n");
  55. voltar_menu();
  56. break;
  57. case 4:
  58. clear();
  59. printf("Listagem por popularidade\n -------------\n");
  60. voltar_menu();
  61. break;
  62. case 5:
  63. clear();
  64. printf("Gerar Viagem\n -------------\n");
  65. voltar_menu();
  66. break;
  67. case 6:
  68. /*
  69. ESCREVER FICHEIROS
  70. */
  71. clear();
  72. printf("Programa terminado\n");
  73. exit(0);
  74. break;
  75. }
  76. menu();
  77.  
  78. }
  79. /*
  80. LOGIN E REGISTO
  81. */
  82.  
  83.  
  84. user_ptr create_users_list(){
  85.  
  86.  
  87. user_ptr aux;
  88. aux = (user_ptr) malloc (sizeof(USERS));
  89. if (aux != NULL) {
  90. strcpy(aux->username,NULL);
  91. strcpy(aux->password,NULL);
  92. aux->next = NULL;
  93. }
  94. return aux;
  95.  
  96. }
  97.  
  98. void userlogin(user_ptr auxUser){
  99.  
  100. FILE *fp;
  101. char uName[10];
  102. char pWord[10];
  103. int i;
  104.  
  105. //auxUser=(user_ptr) malloc (sizeof(USERS));
  106.  
  107. printf("1. Login \n");
  108. printf("2. Registar\n");
  109. scanf("%d",&i);
  110.  
  111. switch(i){
  112. case 1:
  113. if ( ( fp=fopen("user.txt", "r+")) == NULL) {
  114. if ( ( fp=fopen("user.txt", "w+")) == NULL) {
  115. printf ("ERRO AO ABRIR O FICHEIRO\n");
  116. exit(1);
  117. }
  118. }
  119. printf("Username: ");
  120. scanf("%s",uName);
  121. printf("Password: ");
  122. scanf("%s",pWord);
  123. while ( fread (auxUser, sizeof(struct user), 1, fp) == 1) {
  124. if( strcmp ( auxUser->username, uName) == 0) {
  125. if( strcmp ( auxUser->password, pWord) == 0) {
  126. printf ("Dados Corretos. Login feito.\n");
  127. clear();
  128. menu();
  129.  
  130. }else{
  131. printf("Erro\n");
  132. }
  133. }
  134. }
  135. break;
  136.  
  137. case 2:
  138. if ( ( fp=fopen("user.txt", "a+")) == NULL) {
  139. if ( ( fp=fopen("user.txt", "w+")) == NULL) {
  140. printf ("ERRO AO ABRIR O FICHEIRO\n");
  141. exit ( 1);
  142. }
  143. }
  144. printf("Choose A Username: ");
  145. scanf("%s",auxUser->username);
  146. printf("Choose A Password: ");
  147. scanf("%s",auxUser->password);
  148. fwrite (auxUser, sizeof(struct user), 1, fp);
  149. clear();
  150. menu();
  151. break;
  152. }
  153. free (auxUser);
  154. fclose(fp);
  155. }
  156.  
  157.  
  158.  
  159. /*
  160. PARTE DE EXPERIMENTAR A LISTA
  161. */
  162.  
  163. locals_ptr Ler_Ficheiros(){
  164. locals_ptr L=NULL, aux;
  165. FILE *f;
  166. char buff[50];
  167.  
  168. f=fopen("Locais.txt","r");
  169.  
  170. if(f==NULL){
  171. printf("Erro ao abrir o ficheiro de Locais.\n");
  172. return(NULL);
  173. }
  174.  
  175. while(fscanf (f, "%st", buff)!=EOF){
  176.  
  177. if(L==NULL){
  178.  
  179. L=(locals_ptr) malloc (1*sizeof(LOCALS));
  180. strcpy(L->name,buff);
  181. L->next=NULL;
  182. aux = L;
  183.  
  184. }
  185. else {
  186. aux->next=(locals_ptr)malloc (1*sizeof(LOCALS));
  187. aux=aux->next;
  188. strcpy(aux->name,buff);
  189. aux->next=NULL;
  190. }
  191. }
  192.  
  193. return (L);
  194. }
  195.  
  196.  
  197.  
  198. void Showlist(LOCALS *L){
  199. if(L==NULL){
  200. printf("A Lista esta vazia!n");
  201. return;
  202. }
  203. else {
  204. while(L!=NULL){
  205. printf("%s\n",L->name);
  206. L=L->next;
  207. }
  208. }
  209. return;
  210. }
  211.  
  212.  
  213.  
  214.  
  215. locals_ptr create_locals_list(){
  216.  
  217.  
  218. locals_ptr aux;
  219. aux = (locals_ptr) malloc (sizeof(LOCALS));
  220. if (aux != NULL) {
  221. strcpy(aux->name,NULL);
  222. aux->local_pdi_ptr = NULL;
  223. aux->next = NULL;
  224. }
  225. return aux;
  226.  
  227. }
  228.  
  229. void insert_locals_list (locals_ptr ptr, char* n) {
  230. locals_ptr no;
  231. locals_ptr ant, inutil;
  232. no = (locals_ptr) malloc(sizeof(LOCALS));
  233. if (no != NULL) {
  234. strcpy(no->name, n) ;
  235. //procura_lista(ptr, n, &ant, &inutil);
  236. no->next = ant->next;
  237. ant->next = no;
  238. }
  239. }
  240.  
  241. /*
  242. void procura_lista (locals_ptr lista, int chave, locals_ptr *ant, locals_ptr *actual) {
  243. *ant = lista;
  244. *actual = lista->next;
  245. while ((*actual) != NULL && (*actual)->num_aluno < chave) {
  246.  
  247. *ant = *actual;
  248. *actual = (*actual)->next;
  249. }
  250.  
  251. if ((*actual) != NULL && (*actual)->num_aluno != chave)
  252. *actual = NULL; // Se elemento não encontrado
  253. }
  254. */
  255.  
  256. int main()
  257. {
  258. printf("OLAHAHS");
  259. user_ptr users_aux = create_users_list();
  260.  
  261. userlogin(users_aux);
  262. locals_ptr locals_aux = create_locals_list();
  263. locals_aux = Ler_Ficheiros();
  264.  
  265. /*
  266. LOCAIS *L=NULL;
  267. L=Ler_Ficheiros();
  268. Showlist(L);
  269. */
  270. return 0;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement