NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Ch3: Regular expressions for c declaration statements

Content
In C programming language, you can declare an integer/float variable as

int a ;
int bx_082679 ;
int xyz_213_Them ;
float a2387287382 ;
Sometimes, you can also initialize the variable with a value, such as

int x = 1000 ;
float z878787 = 0.11233 ;
Please write two regular expressions that can recognize such declaration statements.
One for integer and one for float.

Format for float is as same as you learned in high school. Ex. 1.234 or 567.899
Your expression don't need to handle scientific expression(科學表達式) and other programming-language-specified format.

Revised Answer
integer: ^[ ]*int[ ]+[a-zA-Z_][a-zA-Z_0-9]*[ ]+=[ ]+-?[0-9]+[ ]+;[ ]*$
float: ^[ ]*float[ ]+[a-zA-Z_][a-zA-Z_0-9]*[ ]+=[ ]+-?[0-9]+.[0-9]+[ ]+;[ ]*$

Log #2136 Reject
integer: ^int(s)+[a-zA-Z_][a-zA-Z_0-9]*(s)+=(s)+-?[0-9]+(s)+;$
float: ^float(s)+[a-zA-Z_][a-zA-Z_0-9]*(s)+=(s)+-?[0-9]+.[0-9]+(s)+;$

TA:
不錯!!!不過你需要注意一下
1. 你的空白字元(s)有可能出現在很多地方,包含整個statement的最前或最後,所以需要注意點處理
2. 在整數的RE中你有處理正路號的問題很棒,不過要把他們以方括號([])包起來,否則後面的?只對-產生作用
3. 整數該怎麼表達?往兩個方向去想

整數為0的時候
整數不為0的時候
浮點數的整數部分其實就是整數的方式處理,小數點再以小數點的方式處理,所以你的浮點數要分成這兩個部分
加油^^

Log #1712 Reject
integer: ^ints[a-zA-Z][a-zA-Z_0-9]s=s[0-9]+s;.$
float: ^floats[a-zA-Z][a-zA-Z_0-9]s=s[0-9]+.[0-9]+s;.$

TA:
consider conditions below:
1. more than one space before or after "=", "int" and "float"
2. longer variable name, like abc_def
3. variable name starts with dash: _local
4. negative int and float
5. "." matches "any character", "." matches "."

Log #1668 Reject
(int )[a-z A-Z]+[a-z A-Z _ 0-9]+( = )[0-9]+( ;)
(float )[a-z A-Z]+[a-z A-Z _ 0-9]+( = )[0-9]+.[0-9]+( ;)

TA: 請妥善處理statement中出現的空白字元(s)

Log #1655 Reject
(int )[a-z A-Z _ 0-9]+( = )[0-9]+( ;)
(float )[a-z A-Z _ 0-9]+( = )[0-9]+.[0-9]+( ;)

TA: id 不可以數字做為開頭

Log #1544 Reject
(int )(a-z A-Z _ 0-9)( = [0-9]+ ;)
(float )(a-z A-Z _ 0-9)( = [0-9]+.[0-9]+ ;)

TA: (a-z A-Z _ 0-9) 麻煩請寫標準的 regex喔 還有兩個辨識數值的地方也怪怪的

Log #1470 Reject
integer: [0-9]+
float: [0-9]+.[0-9]+

TA: 請看清楚題目
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.