kalo93

Untitled

Feb 4th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.56 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <conio.h>
  3. # include <stdlib.h>
  4. # include <locale.h>
  5. #define MAX_nome 500
  6.  
  7. int cont=0;
  8. int u=0;
  9. enum Pri_ut {sim=1,nao=2};
  10.  
  11. typedef struct guard{
  12. int codig_ut;
  13. char nome_ut[MAX_nome];
  14. char CC_ut[8+1];
  15. enum Pri_ut pri_ut;
  16. }n_guard;
  17.  
  18. typedef struct elemento {
  19.  
  20. n_guard utent;
  21. struct elemento *seg;
  22. }Elemento;
  23.  
  24. //******************************************** Menu ***************************************************
  25.  
  26. void menu(){
  27. system("cls");
  28. printf("****************************************\n");
  29. printf("* 1- Inserir Utentes *\n");
  30. printf("* 2- Listar Utentes *\n");
  31. printf("* 3- Atender Utentes *\n"); //Remover//
  32. printf("* 4- Contar Utentes *\n");
  33. printf("* 5- Alterar Utentes *\n");
  34. printf("* 0- Terminar Programa *\n");
  35. printf("****************************************\n");
  36. }
  37.  
  38. //**********************************criacao de um elemento na lista********************************
  39.  
  40. Elemento *criarElemento(Elemento *Base,n_guard utent) {
  41. Elemento *elemento;
  42. elemento=(Elemento*) malloc(sizeof(Elemento)); //alocar espa?o em memoria para o elemento
  43. elemento->utent = utent; //atribuir o valor ao elemento
  44. elemento->seg = NULL; //inicializar o apontador do elemento seguinte para NULL
  45. return elemento;
  46. }
  47.  
  48. //************************************* INSERIR *********************************************************
  49.  
  50. Elemento *opcao1(Elemento *Base) {
  51. system("cls");
  52. printf("****************************************************\n");
  53. printf("* Esta presente na opção de Inserção de Utentes: *\n");
  54. printf("****************************************************\n");
  55.  
  56. if (Base!=NULL) {
  57. Base->seg=opcao1(Base->seg);
  58. } else {
  59. n_guard utent;
  60. cont++;
  61. utent.codig_ut=++u;
  62. printf("Número de utente: %d\n", u);
  63. // printf("Número de utente: ");
  64. // fflush(stdin);scanf("%d",&utent.codig_ut);
  65. printf ("Introduza o nome: ");
  66. fflush(stdin);gets(utent.nome_ut);
  67. printf("introduza o numero do CC do utente: ");
  68. fflush(stdin);gets(utent.CC_ut);
  69. /* do{
  70. printf(" É um utente prioritário (s(1)/n(2)): ");
  71. fflush (stdin);scanf("%d", &utent.pri_ut);
  72. }while( utent.pri_ut != 1 && utent.pri_ut != 2);*/
  73.  
  74. Base=criarElemento(Base,utent);
  75. }
  76. return Base;
  77. }
  78.  
  79. //****************************************** LISTAR ****************************************************
  80.  
  81. void opcao2(Elemento *Base) {
  82.  
  83. printf("***************************************************\n");
  84. printf("* Listagem de Utentes: *\n");
  85. printf("***************************************************\n");
  86.  
  87. if(Base==NULL){
  88. printf("Não existe Utentes registados!!");
  89.  
  90. }
  91.  
  92. if (Base!=NULL) {
  93.  
  94. printf("\nO número do Utente é: %d", Base->utent.codig_ut);
  95. printf("\nO seu nome é: %s",Base->utent.nome_ut);
  96. printf("\nO número de cartão de cidadão: é: %s\n", Base->utent.CC_ut);
  97.  
  98. if (Base->seg != NULL) opcao2(Base->seg);
  99. }
  100. printf("\nPrime uma tecla para continuar!");
  101. getch();
  102.  
  103. }
  104.  
  105. //******************************************** apagar ****************************************************
  106.  
  107. void apagarElemento(Elemento *Base) {
  108. free(Base); //libertar elemento
  109. Base=NULL; //colocar o apontador para NULL
  110. }
  111.  
  112. //******************************************** Atender ***************************************************
  113.  
  114. Elemento *opcao3(Elemento *Base) {
  115. system("cls");
  116. Elemento *aux = Base;
  117. char n;
  118.  
  119. printf("*******************************************************\n");
  120. printf("* Esta presente na opção de Atendimento de Utentes: *\n");
  121. printf("*******************************************************\n");
  122.  
  123. if(Base==NULL){
  124. printf("Não existe Utentes registados!!");
  125.  
  126. }
  127.  
  128. if (Base!=NULL) {
  129.  
  130. printf("-----------------------------------\n\n");
  131. printf("-> Nome: %s \n\n", Base->utent.nome_ut);
  132. printf("-----------------------------------\n");
  133.  
  134. printf("\nPrentende mesmo atender este Utente? (s/n)");
  135. fflush(stdin);scanf("%c",& n);
  136.  
  137. if(n == 's'){
  138. Base = Base->seg;
  139. apagarElemento(aux);
  140.  
  141. printf("\n\n***** O Utente foi atendido com sucesso! ****\n\n");
  142. printf("Prima uma tecla para continuar...");
  143. getch();
  144.  
  145. }else if(n == 'n'){
  146.  
  147. printf("\t************** O utente continuará nas Urgencias. ************\n");
  148. printf("Prima uma tecla para continuar...");
  149.  
  150. }
  151.  
  152. }
  153.  
  154. getch();
  155. return Base;
  156. }
  157.  
  158. //******************************************** CONTAR *****************************************************
  159.  
  160. void opcao4(Elemento *Base){
  161. system("cls");
  162. int i;
  163. n_guard utent;
  164. printf("****************************************************\n");
  165. printf("* Esta presente na opção de Contagem de Utentes: *\n");
  166. printf("****************************************************\n");
  167. // if(Base==NULL){
  168. // printf("Não existe Utentes registados!!");
  169.  
  170. // }
  171. // if(Base!=NULL ){
  172.  
  173. //for (i=1;i<Base->utent.codig_ut;i++){
  174. printf ("\nExistem %d utentes em fila de espera! \n\n\n", cont);
  175. //}
  176. // if (Base->seg != NULL) opcao4(Base->seg,cont);
  177. // }
  178. printf("\n Prime uma tecla para continuar!");
  179. getch();
  180. }
  181.  
  182. //******************************************** Alterar *****************************************************
  183.  
  184. void opcao5(Elemento *Base){
  185. system ("cls");
  186. n_guard utent;
  187. char n;
  188.  
  189. printf("*****************************************************\n");
  190. printf("* Esta presente na opção de Alteração de Utentes: *\n");
  191. printf("*****************************************************\n");
  192.  
  193. if(Base==NULL){
  194. printf("\n\nNão existe Utentes registados!");
  195.  
  196. }
  197.  
  198. if (Base!=NULL) {
  199.  
  200. printf("-----------------------------------\n\n");
  201. printf("-> Nome: %s \n\n", Base->utent.nome_ut);
  202. printf("-----------------------------------\n\n");
  203.  
  204. printf("\nPrentende mesmo alterar este Utente? (s/n)");
  205. fflush(stdin);scanf("%c",& n);
  206.  
  207. if(n == 's'){
  208. printf ("\n->Introduza o novo nome: ");
  209. fflush(stdin);gets(Base->utent.nome_ut);
  210. printf("->Introduza o novo numero do CC do utente: ");
  211. fflush(stdin);gets(utent.CC_ut);
  212.  
  213. printf("\n\nUtente alterado com sucesso!!");
  214. }else if(n == 'n'){
  215. printf("\n\n\n************* O utente continuará com mesmo registos ************");
  216. }
  217.  
  218. }
  219.  
  220. printf("\n\nPrima uma tecla para continuar!");
  221. getch();
  222. }
  223.  
  224.  
  225. //********************************************* Programa principal **************************************************
  226.  
  227.  
  228. main() {
  229. system("color F0");
  230. setlocale (LC_ALL, "Portuguese");
  231. char op;
  232. int n=0;
  233. n_guard utent;
  234. Elemento *Base=NULL;
  235.  
  236. //Ler registos do ficheiro
  237. FILE*fp;
  238.  
  239. //Abrir ficheiros para Leitura
  240. if((fp = fopen("DADOS.DAT","rb"))==NULL) {
  241. printf("\nImpossivel abrir o ficheiro para leitura\n");
  242. getch();
  243. }else{
  244.  
  245. // ler os dados do ficheiro
  246.  
  247. while (fread(&(utent),sizeof (n_guard),1,fp)){
  248. n++;
  249. }
  250. //fechar o ficheiro
  251.  
  252. fclose (fp);
  253. }
  254.  
  255.  
  256. do {
  257. menu();
  258. fflush(stdin);
  259. op=getche();
  260. switch(op){
  261.  
  262. case '1':
  263. system("cls");
  264. Base = opcao1(Base);
  265. break;
  266.  
  267. case '2':
  268. system("cls");
  269. opcao2(Base);
  270. break;
  271.  
  272. case '3':
  273. system("cls");
  274. Base = opcao3(Base);
  275. break;
  276.  
  277. case '4':
  278. opcao4(Base);
  279. break;
  280.  
  281. case '5':
  282. opcao5(Base);
  283. break;
  284.  
  285. case '0':
  286. system("cls");
  287. printf ("Prime uma tecla para terminar o programa");
  288. break;
  289. }
  290.  
  291. }while(op!='0');
  292.  
  293.  
  294. //escrever registos no ficheiro
  295. //Abrir Ficheiro para escrita
  296.  
  297. if ((fp=fopen("DADOS.DAT","wb"))==NULL){
  298. printf("Impossível abrir o ficheiro para a inserçao de filmes\n");
  299. getch();
  300.  
  301. }else{
  302.  
  303. //Escrever os dados no ficheiro
  304. if (fwrite(utent,sizeof( n_guard),n,fp)!=n){
  305. printf("Não foram escritos todos os registos\n");
  306. getch();
  307. }
  308. //fechar ficheiro
  309. fclose(fp);
  310. }
  311.  
  312. }
Advertisement
Add Comment
Please, Sign In to add comment