After learning the compilation technology, the teacher gave us a question: Give us a simple PascalCodeLet's use the C language to identify the words in the Code and analyze the word category. I did not learn C. I wrote a copy directly with C ++!
When I use C ++ to write code, I feel depressed because I have no compilation skills! What did the teacher ask!
Paste the code and hope to provide some small help to friends who are interested in getting started!
Pascal code:
Begin
F: = 1;
Lastf: = 0;
N: = 0;
For I: = 1 to 20 do
Begin
If (N mod 4 = 0) Then writeln;
End
End
C ++ProgramSource code:
# Include <iostream>
# Include <fstream>
# Include <string>
# Include <cctype>
Using namespace STD;
// Global VaR
Const char * keywords [9] = {"begin", "end", "for", "to", "do", "if", "then ", "writeln", "Mod"}; // keyword
Const char op_signs [2] = {':', '='}; // Operator
Const char divid_signs [3] = {';', '(', ')'}; // delimiter
Const char * szpath = "D: \ code.txt"; // file path
// Function
Bool isend (char ch );
Bool isalpha (char ch );
Bool isdigit (char ch );
Bool isdivid (char ch );
Bool isoprate (char ch );
Void back (fstream * F );
Char * tolower (char * Str );
Bool iskeyword (string strword );
String append (string STR, char ch );
Void strtochararr (char * Parr, string Str );
Void main ()
{
Fstream fi (szpath, IOS: In); // read Style
If (! FI)
{
Cout <"file open faild! "<Endl;
Return;
}
Char ch;
String strword = "";
Char word [100];
Ch = Fi. Get ();
While (Ch! = EOF)
{
If (isalpha (CH) // letter
{
While (! (Isend (CH) | isoprate (CH) | isdivid (CH )))
{
Strword = append (strword, CH );
Ch = Fi. Get ();
If (CH = EOF)
Return;
}
If (iskeyword (strword ))
Cout <strword + "keyword" <Endl;
Else
Cout <strword + "identifier" <Endl;
If (isoprate (CH) | isdivid (CH ))
Back (& FI );
Strword = "";
}
Else if (isoprate (CH) // Operator
{
While (! (Isend (CH) | isdigit (CH) | isdivid (CH) | isalpha (CH )))
{
Strword = append (strword, CH );
Ch = Fi. Get ();
If (CH = EOF)
Return;
}
Cout <strword + "operator" <Endl;
If (isdivid (CH) | isalpha (CH) | isdigit (CH ))
Back (& FI );
Strword = "";
}
Else if (isdigit (CH) // Numerical Value
{
While (! (Isend (CH) | isoprate (CH) | isdivid (CH) | isalpha (CH )))
{
Strword = append (strword, CH );
Ch = Fi. Get ();
If (CH = EOF)
Return;
}
Cout <strword + "value" <Endl;
If (isdivid (CH) | isalpha (CH) | isoprate (CH ))
Back (& FI );
Strword = "";
}
Else if (isdivid (CH) // delimiter
{
While (! (Isend (CH) | isoprate (CH) | isdigit (CH) | isalpha (CH )))
{
Strword = append (strword, CH );
Ch = Fi. Get ();
If (CH = EOF)
Return;
}
Cout <strword + "boundary" <Endl;
If (isdigit (CH) | isalpha (CH) | isoprate (CH ))
Back (& FI );
Strword = "";
}
Ch = Fi. Get ();
}
}
// Determine whether it is a letter
Bool isalpha (char ch)
{
If (CH> = 65 & Ch <= 122)
Return true;
Else
Return false;
}
// Determine whether it is a number
Bool isdigit (char ch)
{
If (CH> = 48 & Ch <= 57)
Return true;
Else
Return false;
}
// Judge whether it is a space or line break
Bool isend (char ch)
{
If (CH = 10 | CH = 32)
Return true;
Else
Return false;
}
// Determine whether it is a boundary operator
Bool isdivid (char ch)
{
For (INT I = 0; I <3; I ++)
{
If (CH = divid_signs [I])
Return true;
}
Return false;
}
// Determine if it is a keyword
Bool iskeyword (string strword)
{
Char pword [100];
Strtochararr (pword, strword );
For (INT I = 0; I <9; I ++)
{
Tolower (pword );
If (0 = strcmp (keywords [I], pword ))
Return true;
Else
Continue;
}
Return false;
}
// Determine whether it is an operator
Bool isoprate (char ch)
{
For (INT I = 0; I <2; I ++)
{
If (CH = op_signs [I])
Return true;
}
Return false;
}
// Move the file pointer back
Void back (fstream * F)
{
F-> seekg (-1, IOS: cur );
}
// Assemble words
String append (string STR, char ch)
{
STR + = CH;
Return STR;
}
// Convert the string to lowercase
Char * tolower (char * Str)
{
Int I = 0;
While (STR [I]! = '\ 0 ')
{
STR [I] = tolower (STR [I]);
I ++;
}
Return STR;
}
// Convert a string to a character array
Void strtochararr (char * Parr, string Str)
{
For (INT I = 0; I <= Str. Length (); I ++)
{
Parr [I] = STR [I];
}
}