Advertisement
utroz

Estudo C

Jul 7th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX 10 // Espécie de constante
  5.  
  6. char str[MAX]; // Váriavel Global
  7.  
  8. // Troca a palava-chave char por string[80]
  9. typedef char string[80];
  10.  
  11. int count = 0;
  12.  
  13. // Essa função envia um vetor por parametro.
  14. print_Array(void)
  15. {
  16.     int numbers[10] = {4,3,2,1,5,6,7,8,9,0};
  17.     int length = sizeof(numbers) / sizeof(numbers[0]);
  18.     receive_Array(numbers, length);
  19.  
  20. }
  21.  
  22. // Essa função recebe um vetor por argumento sendo(matriz, tamanho da matriz)
  23. receive_Array(int *args, int arg)
  24. {
  25.     int i;
  26.     for(i = 0; i < arg; i++){
  27.         printf("%d\n", args[i]);
  28.     }
  29. }  
  30.  
  31. // Padrão de MENUS.
  32. void menu(){
  33.     int menu_Option = 0;
  34.  
  35.     printf("\nPlease choose a Option:\n");
  36.     scanf("%d", &menu_Option);
  37.  
  38.     switch(menu_Option)
  39.     {
  40.         case 0:
  41.             //implement.
  42.             break;
  43.  
  44.         case 1:
  45.             //implement.
  46.             break;
  47.  
  48.         default:
  49.             printf("Please input a valid option!\n");
  50.             break;
  51.        
  52.     }
  53. }
  54.  
  55. // Contagem de números 1 a 10 com ','.
  56. void count_Num()
  57. {
  58.     int i;
  59.     for(i = 0; i <= 10; i++)
  60.     {
  61.         printf("%d", count);
  62.         if(i == 10) break;
  63.  
  64.         count++;
  65.         putchar(',');
  66.  
  67.     }
  68.  
  69.     putchar('.');
  70.     printf("\n");
  71.     count = 0;
  72. }
  73.  
  74. int increm()
  75. {
  76.     {
  77.         static int i = 0; // static Value is salved in all calls.
  78.         i++;
  79.  
  80.         return i;
  81.     }
  82.  
  83. }
  84.  
  85. // Função para armazenar um valor em uma string.
  86. void input(void)
  87. {
  88.  
  89.     printf("Enter with a name:");
  90.  
  91.     fgets(str ,sizeof(MAX),stdin);
  92.     fflush(stdin);
  93.     fgets(str ,sizeof(MAX),stdin);
  94.  
  95.  
  96.     printf("%s\n", str);
  97. }
  98.  
  99. // Espécie de Menu sem switch.
  100. void f (void)
  101. {
  102.     int t;
  103.     string s;   /* This is created only in cube enter. */
  104.  
  105.     while(t != 1)
  106.     {
  107.         printf("Please input a option:");
  108.         scanf("%d", &t);
  109.         fflush(stdin); // Clean the read buffer.
  110.  
  111.         if(t == 1)
  112.         {                  
  113.             input();
  114.  
  115.  
  116.         }
  117.         else
  118.         {
  119.             printf("Enter with option 1.");
  120.         }
  121.     }
  122. }
  123.  
  124. // Verificar se existe um caractere na palavra.
  125. is_in(char *s, char c)
  126. {
  127.     while(*s){
  128.         if(*s == c) return 1; // Check if contains char on char*(string).
  129.         else *s++;
  130.     }
  131.  
  132.     return 0;
  133. }
  134.  
  135. call_is_in(void)
  136. {
  137.     char *str = "Word";
  138.     char c = 'W';
  139.  
  140.     int ret = is_in(str,c);
  141.  
  142.     if(ret == 1) printf("true\n");
  143.     else printf("false\n");
  144. }
  145.  
  146. //  Uma função de deixar um argumento como constante.
  147. void sp_to_dash(const char *str);
  148.  
  149. void sp_to_dash(const char *str) // Sobreescrevi o método anterior.
  150. {
  151.     while(*str)
  152.     {
  153.         if(*str == '-') printf("%c", ' ');
  154.         else printf("%c", *str);
  155.         str++;
  156.     }
  157.     printf("\n");
  158. }  
  159.  
  160. // Método main, ele é a rotina principal, o programa inicia por ele.
  161. int main (void)
  162. {
  163.     sp_to_dash("Raphael-Santana-Carvalho");
  164.  
  165.     printf("Press a key to exit!...");
  166.     getchar(); // Wait to any key.
  167.  
  168.     return 0;
  169.  
  170. }
  171.  
  172. // Trabalhando com ponteiros.
  173.     // Work with pointers:
  174.     /*int *pX;
  175.     int x = 3;
  176.     pX = &x;
  177.  
  178.     printf("Print the variable value: %d \n", *pX);
  179.     printf("Print the address value input in pointer %p\n", &pX);
  180.     printf("Print the pointer address: %p\n", pX);
  181.  
  182.     int *pY;
  183.     int y = 3;
  184.     pY = &y;
  185.  
  186.     printf("\nrint the variable value: %d \n", *pY);
  187.     printf("Print the address value input in pointer %p\n", &pY);
  188.     printf("Print the pointer address: %p\n", pY);
  189.  
  190.  
  191.     *pY = *pX;
  192.     printf("\nVariable Y receive value of X: %d \n", *pY);
  193.     */
  194. //-------- ^
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement