Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. /*
  2. * 이번 시간에는 형변환에 대해서 알아보도록 한다.
  3. * 여러가지 변환들이 있다.
  4. * 1. 문자열에서 숫자로.
  5. * 2. 문자 타입을 변경. double -> int...
  6. * 3. 부호가 있는 것 <--> 부호가 없는 것.
  7. *
  8. *
  9. * 산술 연산에서는 묵시적 형변환이 일어난다.
  10. * - 만약 연산대상 중에서 하나라도 long double 형이 있다면, 다른 것들은 long double형으로 변환.
  11. * - 그렇지 않다면, 만약 연산대상 중에서 하나라도 double 형이 있다면, 다른 것들은 double형으로 변환.
  12. * - 그렇지 않다면 하나로도 float 형이 있다면 다른 것들은 float 형으로 변환.
  13. * - 그렇지 않다면, char 와 short는 int으로 변환된다.
  14. * - 그런다음, 연산대상 중에서 하나라도 long형이 있다면, 다른 것들은 long형으로 변환된다.
  15. *
  16. * * 한가지 유의할 점은 float 형은 자동적으로 double 형으로 변환되지 않는다.
  17. */
  18.  
  19. #define RAND 0
  20. #define ATOI 1
  21. #if RAND
  22. /*
  23. * Generate a random number.
  24. * The example of rand.
  25. */
  26. #include <stdio.h>
  27. #define KEY 1103515245L
  28. #define SOLT 12345L
  29.  
  30. unsigned long int next = 1;
  31.  
  32. int rand (void)
  33. {
  34. next = next * KEY + SOLT;
  35. /* the return type of sizeof could be unsigned long int.*/
  36. printf("%lu\n", sizeof(unsigned int));
  37.  
  38. return (unsigned int) (next/65536) % 32768;
  39. }
  40.  
  41. int main (int argc, char** argv)
  42. {
  43. unsigned int val;
  44. val = rand();
  45.  
  46. printf("%u\n", val);
  47. return 0;
  48. }
  49.  
  50. // the water is wild.
  51. #endif
  52.  
  53. #if ATOI
  54.  
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <stdlib.h>
  58. #include <assert.h>
  59.  
  60. #define MAX_LEN 6
  61. /*
  62. * Let's make atoi function.
  63. */
  64.  
  65. int is_digit_me (char c)
  66. {
  67. return (c <= '9') && (c >= '0') ? 1 : 0;
  68. }
  69.  
  70. int atoi_me (const char* s)
  71. {
  72. int i;
  73. int total = 0;
  74. char c;
  75.  
  76. for (i = 0; i < strlen(s); i++) {
  77. c = s[i];
  78. if (is_digit_me(c)) {
  79. total *= 10;
  80. total += c - '0';
  81. }
  82. else {
  83. assert(0);
  84. }
  85. }
  86. return total;
  87. }
  88.  
  89. char* input_msg(void)
  90. {
  91. int i = 0;
  92. char c;
  93. char* buf = NULL;
  94.  
  95. // memset
  96. buf = (char*)malloc(sizeof(char) * MAX_LEN);
  97. memset(buf, 0, MAX_LEN);
  98.  
  99. // while ((c = getchar()) != '\n') {
  100. for (i = 0; ((c = getchar()) != '\n'); i++) {
  101. if (i == MAX_LEN) {
  102. break;
  103. }
  104. if (is_digit_me(c) == 0){
  105. goto free_buf;
  106.  
  107. }
  108. buf[i] = c;
  109. }
  110. return buf;
  111. free_buf:
  112. free(buf);
  113. return NULL;
  114.  
  115. }
  116.  
  117. int main (int argc, char** argv)
  118. {
  119. const char* string_num = "30213";
  120. char* buf = NULL;
  121. int ret = 0;
  122.  
  123. // mem alloc from input_msg()
  124. buf = input_msg();
  125. if (buf == NULL) {
  126. assert(0);
  127. }
  128.  
  129. ret = atoi_me(buf);
  130. printf("%d\n", ret);
  131.  
  132. // free
  133. free(buf);
  134. return 0;
  135. }
  136. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement