軟體實驗: C-Lab5 虞台文. Lab5 Problem Write a program which accepts a text file as input and outputs how many characters, lines, and words the file contains.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Standard I/O Lesson Outline
Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Copyright  Hannu Laine C++-programming Part 5 Strings.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Character and String definitions, algorithms, library functions Characters and Strings.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Case studies over control structures and iterative structures Instructor – Gokcen Cilingir Cpt S 121 (July 6, 2011) Washington State University.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
Strings and Dynamic Memory Allocation CS-2301, B-Term Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming.
Programming Review: Functions, pointers and strings.
Exercise 10 Review: pointers, strings and recursion.
C Programming. C vs C++ C syntax and C++ syntax are the same but... C is not object oriented * There is no string class * There are no stream objects.
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
CS1061 C Programming Lecture 15: More on Characters and Strings A. O’Riordan, 2004.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
File IO and command line input CSE 2451 Rong Shi.
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Lin Chen 09/13/2011. Count words number in the file arrayUtils.h include the adding functions template int addInOrder (T* array, int& size, T value);
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
Introduction to Programming
CS-1030 Dr. Mark L. Hornick 1 Java Review Interactive.
Chapter 2 part #1 C++ Program Structure
Flex Fast LEX analyzer CMPS 450. Lexical analysis terms + A token is a group of characters having collective meaning. + A lexeme is an actual character.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
CSC Programming for Science Lecture 4: Beginning Programming.
軟體實驗 Perfect Number Enumerating 虞台文. Perfect Numbers A perfect number is such that it is equal to the sum of its proper divisors, which are any divisors.
Minimal standard C program int main(void) { return 0 ; }
Cryptography.
CSE 332: STL case study STL Case Study: Building a Meeting Schedule Goals –Read in list of (course) meeting times Days of the week are read in as characters.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 11 : September 18.
軟體實驗: C-Lab7 虞台文. Lab7 Issues Lab 7-1  On-Off bits counting Lab 7-2  ACSII File Verification.
Today’s Material Strings Definition Representation Initialization
軟體實驗:實作以二分法開根號 虞台文. The Lab Problem Write a function to compute by bisection the square root of a positive integer n given by the caller. The function’s.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Tutorial On Lex & Yacc.
Chapter 2 part #1 C++ Program Structure
Command line arguments
Types and Values.
Unit-4, Chapter-2 Managing Input and Output Operations
Getting Started with C.
KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY
CS111 Computer Programming
Arrays, For loop While loop Do while loop
Structured Programming (Top Down Step Refinement)
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Command-line arguments
Introduction to C++ Programming
Command-line arguments
Strings and Pointer Arrays
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files Part 2
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

軟體實驗: C-Lab5 虞台文

Lab5 Problem Write a program which accepts a text file as input and outputs how many characters, lines, and words the file contains.

What is a word? A word is a sequence of alphabets terminated by a blank, a period, a comma, the newline character, or the EOF character. : space : tab \t : newline \n : EOF blank The definition is incomplete here.

Tai-Wen Yue35Taipei, Taiwan Example I am young. My data is as follows. Name 36c w AgeOffice 19c3w 32c5w : space : tab \t : newline \n : EOF blank There are 3 lines, 87 characters, and 16 words in the article.

Lab5 Problem Write a program which accepts a text file as input and outputs how many characters, lines, and words the file contains. A word is a sequence of alphabets terminated by a blank, a period, a comma, the newline character, or the EOF character. You can be assured that the input file contains nothing other than alphanumeric characters and the punctuation marks mentioned above.

軟體實驗: C-Lab5 Algorithm

Type of Character Encountered ’ ’, ’\t’ : blank ’,’, ’;’ ’.’, ’!’, ’:’ : punctuation ’\n’ : newline EOF : end of file Those not described below : normal

Type of Character Encountered ’ ’, ’\t’ : blank ’,’, ’;’ ’.’, ’!’, ’:’ : punctuation ’\n’ : newline EOF : end of file Those not described below : normal #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4

Helper Function int CharType(char c) Returns CT_NORMAL, CT_BLANK, CT_PUNCTUATION, CT_NEWLINE, or CT_EOF according to the value of c described above.

States I am young. My data is as follows. Newline Start In Word Not In Word In Word Not In Word Newline Start Newline Start Newline Start In Word Not In Word Not In Word CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NEWLINE CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NORMAL

States Finish CT_EOF #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; Newline Start Newline Start In Word Not In Word Not In Word CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NEWLINE CT_NORMAL CT_BLANK CT_PUNCTUATIOM CT_NEWLINE

The Program Outline /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0; /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0;

The Program Outline /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0; /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0; main(int argc, char** argv) { Open the file described in (argc, argv) while(state != FINISH){ Get the next character, say, c, from the file. Call type=CharType(c) to get c’s type. Parsing the character just read … - change state - update counters } Print out the parsing result. } main(int argc, char** argv) { Open the file described in (argc, argv) while(state != FINISH){ Get the next character, say, c, from the file. Call type=CharType(c) to get c’s type. Parsing the character just read … - change state - update counters } Print out the parsing result. }

The Program Outline /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0; /* character type declaration */ #defineCT_NORMAL0 #defineCT_BLANK1 #defineCT_PUNCTUATION 2 #defineCT_NEWLINE3 #defineCT_EOF4 /* state declaration */ #defineNEW_LINE_START0 #defineIN_WORD1 #defineNOT_IN_WORD 2 #defineFINISH3 /* initial state is set as */ intstate = NEW_LINE_START; /* counters */ intlines=0, chars=0, words=0; main(int argc, char** argv) { Open the file described in (argc, argv) while(state != FINISH){ Get the next character, say, c, from the file. Call type=CharType(c) to get c’s type. Parsing the character just read … - change state - update counters } Print out the parsing result. } main(int argc, char** argv) { Open the file described in (argc, argv) while(state != FINISH){ Get the next character, say, c, from the file. Call type=CharType(c) to get c’s type. Parsing the character just read … - change state - update counters } Print out the parsing result. } Done by Parse(int type);