Hi everyone! This is my very first post. I hope that some can help me.
I'm totally newbie to VB but i need to make a executable file to do this:
Open a given .cpp file, and for every line inside this file check all the words containing only uppercase single words or upper case words seperated by "_" (This is the standard to be accepted as a properly declared constant inside that .cpp file*), finally the code must display a message saying how many constants has been properly declared and how many wasn't.
Let's suppose that this is the code:
#define WEEK_DAYS 7
#define PI 3.141592
#define COUNt 1000
state = EXITO;
i = 0;
while ((string[i] != NULL && (state == SUCCESS))
{
transmitCar(string[i], &state);
}
The program should say:
3 variables found!
2 Variables passed the test
1 variable failed the test.
Score is 0.66
For now, i can open the .cpp file and i can display the code inside on a window, nothing else. My idea is trying to seperate every word or character with line.Split
and store it using an array, and after that use a for sentence to go over the array, find every word with upper case characters and delete the "_" and then count all the word. But that procedure only has to be done if before the word there is a #define.
Thanks in advance. And sorry for my english.
P.D The code doesn't need to be pretty, stable or optimized, but if it works, it's great.
This is my code, i know it's awful.
Imports System.IO
Imports System.Text
Public Class Código
Private Property fileList As ObjectModel.ReadOnlyCollection(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myStream As Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
Dim i As Integer = 0
Dim line As String
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "Archivos de C++ (*.CPP)|*.CPP|Todos los archivos (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
'Algoritmo para evaluar el codigo:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\Codigo.cpp", _
System.Text.Encoding.ASCII)
MsgBox(fileReader)
For Each line In File.ReadAllLines("C:\Codigo.cpp")
' Split line on comma
Dim parts As String() = line.Split(New Char() {" "c})
' Loop over each string received
Dim part As String
For Each part In parts
' Display to Console
Console.WriteLine("{0}:{1}", i, part)
Next
i += 1
Next
End If
Catch Ex As Exception
MessageBox.Show("No se puede leer el archivo." & Ex.Message)
Finally
'Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
End Class