Recitation 5 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Recitation 5

Description:

Compose a text file containing the desired input, and call it, for example, ... We can do it using if or switch to test whether a character is a digit or not, ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 16
Provided by: eda8
Category:

less

Transcript and Presenter's Notes

Title: Recitation 5


1
Recitation 5
  • Input and Output Files and Redirections.

2
Reading a file from the standard input of your
program
  • Compose a text file containing the desired input,
    and call it, for example, input.txt.
  • Feed the contents of input.txt to your program as
    standard input like this
  • ./myprog lt input.txt
  • The lt redirects the contents of input.txt
    into the standard input of myprog.

3
Writing the standard output of your program to a
file
  • Suppose we want to collect what's going into
    the standard output into a file called, for
    example, output.txt
  • ./myprog gt output.txt The gt
    redirects the contents of myprog's standard
    output into the
  • file output.txt.

4
Writing from a file into a program's standard
input and writing the standard output of the
program into a file
  • ./myprog lt input.txt gt output.txt
  • The contents of input.txt is written to
    myprog and its standard output is written into
    output.txt.

5
Eliminating repetitions -- A problem
  • Write a computer program that reads arbitrarily
    long text from the standard input and prints its
    input to the standard output removing
    repetitions.
  • We say that a character repeats itself when it
    appears more than one time consecutively.

6
Eliminating repetitions An example
  • Running the program
  • ./remove_repetitions lt input.txt gt output.txt
  • input.txt
  • ABCDEFGGGGGGGGGHI
  • JKLMN
  • OPPPPPPPPPPPLMNOPPPQRSTTTTTTT
  • TTUVWXYZZZZZZZZZZZ
  • output.txt
  • ABCDEFGHI
  • JKLMN
  • OPLMNOPQRST
  • TUVWXYZ

7
Eliminating repetitions Source code
  • include ltstdio.hgt
  • int main()
  • int c, previousEOF
  • while ( EOF ! (cgetchar()) )
  • if (c!previous)
  • putchar(c)
  • previous c
  • return 0

8
Counting numbers in the input -- A problem
  • Write a computer program that reads arbitrarily
    long text from the standard input and prints to
    the standard output how many natural numbers were
    seen in the input.
  • A natural number will be represented by a string
    of characters all made by digits.
  • A digit is any character from '0','1','2','3','4',
    '5','6','7','8','9'.
  • We can do it using if or switch to test whether a
    character is a digit or not, or we can use a
    ready made function that does the same.
  • A function like that is isdigit() from ctype.h.

9
Counting numbers in the input -- Example

Commandline ./count_naturals lt input.txt gt
output.txt input.txt This example consists of
some text containing 1. Digits, such as,
'2','7', or '9' 2. Integers, such as, -958, 0, or
1984 3. Real numbers, such as, 3.1415, 0.25, or
-99.82 4. Dates, such as, 2/3/1976, or Fri Nov 8
205355 IST 2002 There are 25 integers in this
text including the number mentioned in this
sentence. output.txt Seen 25 natural numbers in
the input
10
include ltstdio.hgt include ltctype.hgt int main()
int c unsigned int counter 0 char
in0 while( EOF ! (cgetchar()) ) if
(isdigit(c)) if (1 ! in) in
1 counter else in
0 printf("Seen d natural numbers in the
input\n",counter) return 0
/ Counting numbers in the input
/

11
Counting numbers - integers

So, what's the difference? Now we also need to
consider cases where there is a minus sign.
Let's write a program that counts negative
integers and non negative integers. The program
should also sum up the total number of integers
seen.
12
Counting numbers in the input -- Example

Command line ./count_integers lt input.txt gt
output.txt input.txt This example
consists of some text containing 1. Digits,
such as, '2','7', or '9' 2. Integers, such as,
-958, 0, or 1984 3. Real numbers, such as,
3.1415, 0.25, or -99.82 4. Dates, such as,
2/3/1976, or Fri Nov 8 205355 IST 2002 There
are 25 integers in this text including the number
mentioned in this sentence. output.txt Seen a
total of 25 integral numbers in the input 2 of
them are negative integral numbers. And 23 of
them are non negative integral numbers.
13
Counting numbers in the input -- Example
  • Source code

14
C Comments Remover
  • The problem
  • Write a computer program that reads a valid
    ANSI-C program from the standard input and prints
    it to the standard output without the comments.
  • A valid ANSI-C program is a program that compiles
    with no
  • warnings and without any errors using the
    -Wall -ansi -pedantic
  • flags with the gcc compiler. You may assume
    your program receives a valid ANSI-C program.

15
C Comments Remover
  • Input
  • Output
  • Source code
Write a Comment
User Comments (0)
About PowerShow.com