KyungHee Univ. 1-0 문자열 입 · 출력 프로그램 작성 예. KyungHee Univ. 1-1  모니터에 문자열 출력 하는 프로그램 작성  표준 출력 장치에 문자를 출력 하는 함수 함수의 기본형 : void SCI_OutChar(letter)  문자를.

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

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Strings Testing for equality with strings.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Data Representation Computer Organization &
Introduction to Computers and Programming Lecture 7:
Data Representation COE 205
Review Two’s complement
COMP3221: Microprocessors and Embedded Systems--Lecture 4 1 COMP3221: Microprocessors and Embedded Systems Lecture 4: Number Systems (II)
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
USART and Asynchrono us Communica tion The USART is used for synchronous and asynchronous serial communication. USART = Universal Synchronous/Asynchronous.
COMP3221 lec07-numbers-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 7: Number Systems - III
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
How to use the CCS compiler with the serial port.
1 CS 162 Introduction to Computer Science Chapter 5 ASCII to Integer Conversion Herbert G. Mayer, PSU Status 11/9/2014.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Computers Organization & Assembly Language
Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt.
IT253: Computer Organization
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
System development with Java Lecture 2. Rina Errors A program can have three types of errors: Syntax and semantic errors – called.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
1.11, 1.12, 1.13, 1.14, 1.18, 1.20, 1.23, 1.24, 1.25, 1.32, 1.33.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Corporation
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
Hexadecimal. Overview Hexadecimal (hex) ~ base 16 number system Use 0 through 9 and... A = 10 B = 11 C = 12 D = 13 E = 14 F = 15.
Binary Number Output To display a number in binary format, a program looks at each bit in the number and sends the ASCII equivalent of a ‘1’ (31h) or a.
 All C programs are made up of functions that perform operations on variables.  In this lecture we examine variables  Variables are the basic building.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Agenda Character representation Numerical Conversions ASCII EBCDIC
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
CS 2130 Lecture 23 Data Types.
Chapter 1: Binary Systems
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Integers/Characters Input/Output Integers and Characters Input/Output System Calls. syscall Trap Handler Services for Integers and Characters Read Integer,
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
CS 1704 Introduction to Data Structures and Software Engineering.
Chapter 1 Representing Data in a Computer. 1.1 Binary and Hexadecimal Numbers.
1 Objects Types, Variables, and Constants Chapter 3.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
USER INTERACTION AND VARIABLES Tarik Booker CS 290 California State University, Los Angeles.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
Data Representation COE 308 Computer Architecture
Chip Config & Drivers – Required Drivers:
Do-more Technical Training
NUMBER SYSTEMS.
Data Representation ICS 233
Lec 3: Data Representation
User Interaction and Variables
Data Representation.
ECE Application Programming
COMP3221: Microprocessors and Embedded Systems
Plan of the Day: More on type conversions scanf printf format strings
Input and Output Lecture 4.
Formatting Output.
IDENTIFIERS CSC 111.
UART Protocol Chapter 11 Sepehr Naimi
Data Representation ICS 233
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Fundamental Programming
Data Representation COE 308 Computer Architecture
Presentation transcript:

KyungHee Univ. 1-0 문자열 입 · 출력 프로그램 작성 예

KyungHee Univ. 1-1  모니터에 문자열 출력 하는 프로그램 작성  표준 출력 장치에 문자를 출력 하는 함수 함수의 기본형 : void SCI_OutChar(letter)  문자를 출력 하는 표준 함수  장치에 따라 실제 문자를 출력 하는 함수를 별도로 작성 하여  출력 장치가 변경 되어도 프로그램 변경은 최소가 되도록 한다.  시리얼 모니터에 문자열을 출력하는 함수. 함수의 기본형 : void SCI_OutString(char *pt)  문자열이 저장된 기억장소의 포인터를 인수로 받아 USART 장치에 문자열을 출력하는 한다.  문자열의 끝은 C 의 문자열과 같이 NULL(0) 가 저장되어 있다.  영문 소문자 또는 대문자를 전달 받아 대문자로 변환 하여 되돌려 주는 함수. 함수의 기본형 : void SCI_UpCase(char character)  부호 없는 정수를 출력하는 함수. 함수의 기본형 : void SCI_OutUDec(unsigned short number) 부호 없는 정수 number 의 값을 전달 받아 문자로 변환 하여 USART 장치에 출력 한다.

KyungHee Univ. 1-2  모니터에 문자열 출력 하는 프로그램 작성  부호 있는 정수를 출력하는 함수. 함수의 기본형 : void SCI_OutSDec(unsigned short number 부호 있는 정수 number 의 값을 전달 받아 문자로 변환 하여 USART 장치에 출력 한다.  Null 문자로 끝나는 문자열을 전달 받아 대 문자로 변환하여 되돌려 주는 함수. 함수의 기본형 : void SCI_UpCaseString(char *inString)  문자열이 저장된 기억장소의 포인터를 인수로 받아 대문자로 변환 한 후 되돌려 주는 함수  부호 없는 정수를 16 진 수로 변환하여 USART 장치에 출력하는 함수. 함수의 기본형 : void SCI_OutUHex(unsigned short number) 부호 없는 정수 number 의 값을 전달 받아 16 진 문자열로 변환 하여 USART 장치에 출력 한다.

KyungHee Univ. 1-3  Key Board 로 부터 문자열을 입력 하는 프로그램  표준 입력 장치로 부터 한 문자를 입력 받는 함수 함수의 기본형 : unsigned short SCI_InChar()  문자열을 Key Board 로부터 입력하는 함수. 함수의 기본형 : void SCI_InString(char *string, unsigned int max)  부호 없는 10 진 수를 Key Board 로부터 입력하는 함수. 함수의 기본형 : unsigned short SCI_InUDec(void)  부호 있는 10 진 수를 Key Board 로부터 입력하는 함수. 함수의 기본형 : short SCI_InSDec(void)  부호 없는 16 진 수를 Key Board 로부터 입력하는 함수. 함수의 기본형 : unsigned short SCI_InUHex(void)

KyungHee Univ. 1-4  표준 출력 함수 예 // SCI_OutChar to USART void SCI_OutChar(char letter){ tx0_char(letter); } // UART0 을 이용한 출력 void tx0_char(unsigned char data) { // Wait for empty transmit buffer while ( !(UCSR0A & (1<<UDRE))); // Put data into buffer, sends the data UDR0 = data; }

KyungHee Univ. 1-5  표준 입력 함수 예 // SCI_InChar from USART unsigned short SCI_InChar(){ return (rx0_char()); } // UART0 을 이용한 입력 unsigned char rx0_char(void) { // Wait for data to be received while ( !(UCSR0A & (1<<RXC)) ); // Get and return received data from buffer return UDR0; }

KyungHee Univ. 1-6  문자열을 출력하는 함수 // Output String (NULL termination) void SCI_OutString(char *pt){ char letter; while(letter=*pt++){ SCI_OutChar(letter); }

KyungHee Univ. 1-7  영문 소문자 또는 대문자를 전달 받아 대문자로 변환 하여 되돌려 주는 함수 // converts lowercase to uppercase // char by subtracting $20 from lowercase ASCII to make // uppercase ASCII char SCI_UpCase(char character){ return ((character>='a') && (character<='z')) ? character-0x20 : character; }

KyungHee Univ. 1-8  부호 없는 정수를 출력하는 함수 // SCI_OutUDec // Output a 16 bit number in unsigned decimal format // Variable format 1-5 digits with no space before or after // This function uses recursion to convert decimal number // of unspecified length as an ASCII string void SCI_OutUDec(unsigned short n){ if(n >= 10){ SCI_OutUDec(n/10); Recursive Operation n=n % 10; } SCI_OutChar( n + '0‘ ); /* n is between 0 and 9 */ }

KyungHee Univ. 1-9  부호 있는 정수를 출력하는 함수 // Output a 16 bit number in signed decimal format // Variable format (optional sign)1 to 5 digits with no space before or after // This function checks if the input parameter is negative, // If the number is negative, then // 1) it outputs a "-", // 2) negates the number and // 3) outputs it with OutUDec. // Otherwise, it just calls OutUDec (i.e., no "+" sign) void SCI_OutSDec(short number){ if(number < 0){ number = -number; SCI_OutChar('-'); } SCI_OutUDec(number); }

KyungHee Univ  부호 없는 정수를 16 진 수로 변환하여 출력하는 함수 // Output a 32 bit number in unsigned hexadecimal format // Variable format 1 to 8 digits with no space before or after // This function uses recursion to convert the number of // unspecified length as an ASCII string void SCI_OutUHex(unsigned short number){ if(number >= 0x10) { SCI_OutUHex(number / 0x10); SCI_OutUHex(number % 0x10); } else if(number < 0xA){ SCI_OutChar(number + '0'); } else{ SCI_OutChar((number - 0x0A) + 'A'); }

KyungHee Univ  Null 문자로 끝나는 문자열을 전달 받아 대 문자로 변환하여 되돌려 주는 함수 // converts a NULL terminated string to uppercase void SCI_upCaseString(char *inString){ char *pt = inString; // 'a' = 0x61 and 'A' = 0x41, so their difference is 0x20 while(*pt){ // NULL => done if((*pt >= 'a') && (*pt <= 'z')) *pt -= 0x20; pt++; }

KyungHee Univ // SCI_InUDec // InUDec accepts ASCII input in unsigned decimal format // and converts to a 16 bit unsigned number // with a maximum value of // If you enter a number above 65535, it will truncate without reporting the error // Backspace will remove last digit typed unsigned short SCI_InUDec(void){ unsigned short number=0, length=0; unsigned char character; while((character=SCI_InChar())!=CR){ // accepts until carriage return input // The next line checks that the input is a digit, 0-9. // If the character is not 0-9, it is ignored and not echoed if((character>='0') && (character<='9')) { number = 10*number+(character-'0'); // this line overflows if above length++; SCI_OutChar(character); }  부호 없는 10 진 수를 Key Board 로부터 입력하는 함수

KyungHee Univ // If the input is a backspace, then the return number is // changed and a backspace is outputted to the screen else if((character==BS) && length){ number /= 10; length--; SCI_OutChar(character); } return number; }

KyungHee Univ // SCI_InSDec // InSDec accepts ASCII input in signed decimal format // and converts to a signed 16 bit number // with an absolute value up to // If you enter a number above or below , // it will truncate without reporting the error // Backspace will remove last digit typed short SCI_InSDec(void){ short number=0, sign=1;// sign flag 1= positive -1 = negative unsigned int length=0; unsigned char character; while ((character=SCI_InChar())!=CR){ // Check for carriage return if(!length) { // + or - only valid as first char if(character=='-'){ sign = -1; length++; SCI_OutChar('-');// if - inputted, sign is negative } else if(character=='+'){ length++; SCI_OutChar('+');//if + inputted, sign is positive }  부호 있는 10 진 수를 Key Board 로부터 입력하는 함수

KyungHee Univ // The next line checks that the input is a digit, 0-9 // If the character is not 0-9, it is ignored and not echoed if((character>='0') && (character<='9')){ number = number*10+character-'0'; // this line overflows if above length++; SCI_OutChar(character); } // If the input is a backspace, then the return number is changed and a backspace // is outputted to the screen. If the backspace erases a minus, then sign is // reset to positive else if((character==BS) && length){ number /=10; length--; if(!length){ sign = 1; } SCI_OutChar(BS); } return sign*number; }

KyungHee Univ // SCI_InUHex // InUHex accepts ASCII input in unsigned hexadecimal (base 16) format // No '$' or '0x' need be entered, just the 1 to 4 hex digits // It will convert lower case a-f to uppercase A-F // and converts to a 16 bit unsigned number // with a maximum value of FFFF // If you enter a number above FFFF, it will truncate without reporting the error // Backspace will remove last digit typed unsigned short SCI_InUHex(void){ unsigned short number=0, digit, length=0; unsigned char character; while((character=SCI_UpCase(SCI_InChar()))!=CR){ digit = 0x10; // assume bad if((character>='0') && (character<='9')){ digit = character-'0'; } else if((character>='A') && (character<='F')){ digit = (character-'A')+0xA; }  부호 없는 16 진 수를 Key Board 로부터 입력하는 함수.

KyungHee Univ // If the character is not 0-9 or A-F, it is ignored and not echoed if(digit <= 0xF ){ number = number *0x10 + digit; length++; SCI_OutChar(character); } // Backspace outputted and return value changed if a backspace is inputted else if(character == BS && length){ number /= 0x10; length--; SCI_OutChar(character); } return number; }

KyungHee Univ // SCI_InString // This function accepts ASCII characters from the serial port // and adds them to a string until a carriage return is inputted // or until max length of the string is reached. // It echoes each character as it is inputted. // If a backspace is inputted, the string is modified // and the backspace is echoed // InString terminates the string with a null character // -- Modified by Agustinus Darmawan + Mingjie Qiu -- void SCI_InString(char *string, unsigned int max) { unsigned int length=0; unsigned char character;  문자열을 Key Board 로부터 입력하는 함수

KyungHee Univ while((character = SCI_InChar()) != CR){ if(character == BS){ if(length){ string--; length--; SCI_OutChar(BS); } else if(length < max){ *string++ = character; length++; SCI_OutChar(character); } *string = 0; // 문자열의 끝에 Null code 삽입함. }