作者SleepingBun (睡眠松。)
看板NTUCHE-98-HW
標題[數隻]Welt sechster
時間Thu Nov 10 21:48:53 2005
註解比程式還多,大家請認真讀。
(讀不懂可洽詢F。)
/********************************************\
* *
* semidesigned by Seraph' *
* 2005 Autumn in Taipei *
* All rights forsaken. *
* *
\********************************************/
#include<stdio.h>
#include<ctype.h>
/* Here we start a lesson of type defining.
[ Exempli Gratia ]
typedef enum
{ ... , -3, -2, -1, 0, 1, 2, 3, ... }
int;
This means that, from now on, we can define an x as
an integer with "int."
A more conspicuous example would go this way:
typedef enum
{ Peugeot, Volks_Wagen, Mercedez_Benz, Porche }
brand_of_Veh;
Thereupon, we can write such in main function:
brand_of_Veh ya_Veh;
And ya_Veh can then be Peugeot, Volks_Wagen or some. */
typedef enum
{start, build_num, number, build_id, identifier, stop}
state_type;
/* Such user_defined type could be used on functions, too.
Following is a good example. */
state_type transition(state_type, char);
int main(void)
{
state_type current_state; //An user-defined variable
char transition_char; //An antithetic typical one
/* Learning how to program, we shall have a good habit of
initializing everything in the beginning. */
current_state = start;
/* Here comes a do-while Statement. Its syntax :
do STATEMENT while ( EXPRESSION ) ;
Thy program ought to iterate the STATEMENT while
the EXPRESSION stay tenable. By the way, we use braces,
I mean {} ←these, when the STATEMENT hath more than
one line. */
do
{
if ( current_state == identifier )
{
printf(" - Identifier\n");
current_state = start;
}
else if ( current_state == number )
{
printf(" - Number\n");
current_state = start;
}
scanf("%c", &transition_char);
/* In fact, scanf Routine is worth our while discussing.
Its syntax :
int scanf( *FORMAT [,ARGUMENT]... );
The "int" ahead infers that this function would return
an integer representative of the fields successfully
converted and assigned.
[ Exempli Gratia ] ( Adapted from MSDN Library)
#include <stdio.h>
void main( void )
{
int i, result;
float fp;
char c, s[81];
wchar_t wc, ws[81];
printf( "\n\nEnter an int, a float, two chars and two strings\n");
result = scanf( "%d %f %c %C %s %S", &i, &fp, &c, &wc, s, ws );
printf( "\nThe number of fields input is %d\n", result );
printf( "The contents are: %d %f %c %C %s %S\n", i, fp, c, wc, s, ws);
}
==========================Output==========================
Enter an int, a float, two chars and two strings
71
98.6
h
z
Byte characters
The number of fields input is 6
The contents are: 71 98.599998 h z Byte characters
========================================================== */
if ( transition_char != ' ' && transition_char != '.'
&& transition_char != '\n' )
printf("%c", transition_char);
current_state = transition(current_state, transition_char);
} while ( current_state != stop );
printf("\n");
}
state_type transition(state_type current_state, char transition_char)
{
/* [ switch, case, default Statements ]
syntax :
switch( EXPRESSION )
{
[case VARIABLE:]
. . .
[STATEMENT]
. . .
[default:
STATEMENT]
}
We use this statement when a variable allows only integer,
that is, in the following case, start corresponding to 0,
build_num to 1, number to 2, et cetera. ( These were
spontaneously pre-defined through the typedef declaration. )
Another pivot is the "break Statement", we use it to
terminate and exit the closest do, for, switch, or while
statement. */
switch (current_state)
{
case start:
if ( !isdigit(transition_char) )
current_state = build_id;
else if ( transition_char == '.' )
current_state = stop;
else
current_state = build_num;
break;
case build_num:
if ( transition_char == ' ' )
current_state = number;
else if ( transition_char == '\n' )
current_state = number;
else if ( transition_char == '.' )
{
printf(" - Number\n");
current_state = stop;
}
else if ( !isdigit(transition_char) )
current_state = build_id;
break;
case number:
current_state = start;
break;
case build_id:
if ( transition_char == ' ' )
current_state = identifier;
else if ( transition_char == '\n' )
current_state = identifier;
else if ( transition_char == '.' )
{
printf(" - Identifier\n");
current_state = stop;
}
break;
case identifier:
current_state = start;
break;
default:
break;
}
return current_state;
}
--
推 SleeplessBun:[偽‧鼠吱]頭推~~~鼠吱是一定要推鼠吱的啦~ 吱吱~ 11/07 22:19
推 AzureBun: [真.鼠吱]二推~~~鼠吱是一定要推鼠吱的啦~ 吱吱~ 11/15 16:24
※ 編輯: SleepingBun 來自: 61.62.27.54 (11/15 23:39)