網頁

2021年3月3日 星期三

Unix System Programming (1092)-exer1

exercise 1: a simple C program
  • Write a C program which reads strings from stdin and if a string is consisted of digits, it is treated as a number. Sum all the numbers and write sum to stdout.
  • The reading is terminated by end of file, that is, a ctrl-D.
  • The length of each string does not exceed 16.
  • You may assume the numbers and the sum can be handled with int.
  • For converting a string to a number, you may consult strtol().
  • For example:
    $ gcc ex1-sum-stdin.c 
    $ ./a.out 
    I have 1000
    You have 200
    I give you 50
    Now You have 250.   <---- ctrl-D pressed
    1250                <---- only 1000, 200, 50 are summed
    $ ./a.out 
    100 + 23 = 123
    $100 - 23 = 77.     <---- ctrl-D pressed
    269                 <---- 100, 23, 123, 23 are summed
     
    My program:
    https://ideone.com/V3fc8b
    https://ideone.com/hFVjyY
     

沒有留言:

張貼留言