CC Lab Task
Lab Task 1
===================Screen
Short===============
===============code====================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
namespace lab2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btncomp_Click(object sender, EventArgs e)
{
int x = 0;
string input = richtxtedit.Text;
foreach (var item in input)
{
string s = item.ToString();
if
(Regex.IsMatch(s, "^[a-zA-Z0-9]$"))
{
richTxtError.Text += s;
}
else if (s == " ")
{
richTxtError.Text += Environment.NewLine;
}
else if (s == ",")
{
richTxtError.Text += "" + Environment.NewLine;
richTxtError.Text += ",";
}
else if (s == ";")
{
richTxtError.Text += "" + Environment.NewLine;
richTxtError.Text += ";";
}
else if (s == "\"")
{
richTxtError.Text += "" + Environment.NewLine;
richTxtError.Text += "\"";
}
else if (s == "\"")
{
richTxtError.Text += "" + Environment.NewLine;
richTxtError.Text += "\"" ;
}
else if (s == "=")
{
richTxtError.Text += "" + Environment.NewLine;
richTxtError.Text += "=" + Environment.NewLine;
}
else if (s == "<")
{
if (x == 0)
{
x++;
richTxtError.Text += "" + Environment.NewLine;
richTxtError.Text += "<<" ;
}
}
}
}
}
}
===============End=====================
Lab Task 2
===================Screen
Short===============
===============code====================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
namespace lab2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btncomp_Click(object sender, EventArgs e)
{
Regex reg2 = new Regex("^cin>>\"[a-zA-Z0-9]+;$");
Regex reg3 = new Regex("^int [a-zA-Z0-9]+=[0-9]+[+|-|*|/]*[0-9]*;$");
Regex reg4 = new Regex("^float [a-zA-Z0-9]+=[0-9]+[.][0-9]+;$");
Regex reg5 = new Regex("^short [a-zA-Z0-9]+=[0-9]+[0-9]+;$");
String[] readtext = richtxtedit.Lines;
int
linenumber=1;
Boolean check = false;
foreach (var v in readtext)
{
if ( reg2.IsMatch(v) || reg3.IsMatch(v) ||
reg4.IsMatch(v) || reg5.IsMatch(v) )
{
check = true;
linenumber++;
}
}
if (check)
{
richTxtError.Clear ();
richTxtError.Text += "Secceeded";
richTxtError.Text += Environment.NewLine;
}
else
{
richTxtError.Text = "faild"+ Environment.NewLine;
richTxtError.ForeColor = Color.Black;
richTxtError.Text += "Line
Number " + linenumber;
}
}
}
}
===============End=====================
Lab Task 3
===================Screen
Short===============
===============code====================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
namespace week5_lab1
{
public partial class Form1 : Form
{
String[] code;
Hashtable var = null;
String d = null;
//Regex
reg = new
Regex("^(int|[sS]tring|float|char)\\s+\\w+\\s*(=\\s*[^,]+)?(,\\s*\\w+\\s*(=\\s*[^,]+)?)*;$");
Regex reg5 = new Regex("^int\\s+\\w+\\s*=\\s*[0-9]+;$");
Regex reg5NotInt = new Regex("^int\\s+\\w+\\s*=\\s*[0-9]+[.][[0-9]+;$");
Regex reg2 = new Regex("^cout<<\"[a-zA-Z0-9]*\";$");
Regex reg3 = new Regex("^cout<<[a-zA-Z0-9]*;$");
Regex reg4 = new Regex("^float\\s+\\w+\\s*=\\s*[0-9]+[.][0-9]+;$");
Regex reg4NotFloat = new Regex("^float\\s+\\w+\\s*=\\s*[0-9];$");
public Form1()
{
InitializeComponent();
}
bool checkvariable = false;
public void variable(string code, int i)
{
string[] t = code.Split(' ');
t[1].Trim();
t = t[1].Split(',');
for (int z = 0; z < t.Length; z++)
{
try
{
string s = t[z];
if (!(s[0] >= 48 &&
s[0] <= 57))
{
if (!t[z].Contains('='))
{
var.Add(t[z], -1);
}
else
{
string[] da = t[z].Split('=');
da[1] =
da[1].Substring(0, da[1].Length - 1);
var.Add(da[0],
da[1]);
}
}
else
{
richTextError.Text = "error in line "
+ (i + 1) + "\n";
checkvariable = true;
}
}
catch (Exception)
{
checkvariable = true;
richTextError.Text = "Error in line "
+ (i + 1) + "\n";
}
}
}
public void output_String(string Code)
{
for (int z = 7; z < Code.Length - 2; z++)
{
richTxtoutput.Text += Code[z];
}
}
public void output_Variable(string Code)
{
for (int z = 6; z < Code.Length - 1; z++)
{
d += Code[z];
}
foreach (var v in var.Keys)
{
if (v.Equals(d))
{
richTxtoutput.Text +=
var[v];
d = null;
}
}
}
private void btncomp_Click(object sender, EventArgs e)
{
var = new Hashtable();
//richTxtoutput.Clear();
int count = richtxtedit.Lines.Length;
code = new String[count];
TextReader read = new
System.IO.StringReader(richtxtedit.Text);
richTextError.Clear();
for (int i = 0; i < count; i++)
{
code[i] = read.ReadLine();
if (reg5.IsMatch(code[i]))
{
//MessageBox.Show("");
variable(code[i], i);
}
else if
(reg5NotInt.IsMatch(code[i]))
{
richTextError.Text += "float value not convert to int \n";
int n = i + 1;
richTextError.Text += "error in line "
+ n + "\n";
}
else if (reg2.IsMatch(code[i]))
{
output_String(code[i]);
}
else if (reg4.IsMatch(code[i]))
{
//MessageBox.Show("in");
variable(code[i], i);
}
else if
(reg4NotFloat.IsMatch(code[i]))
{
richTextError.Text += "int value not convert to float \n";
int n = i + 1;
richTextError.Text += "error in line "
+ n + "\n";
}
else if (reg3.IsMatch(code[i]))
{
output_Variable(code[i]);
}
else
{
int n = i + 1;
richTextError.Text += "error in line "
+ n + "\n";
}
}
}
}
}
===============End=====================
|
------------------------------------------end
input-----------------------
------------------------------------------code
run-----------------------
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
using
System.Collections;
using
System.IO;
using
System.Text.RegularExpressions;
using lab2;
namespace
week5_lab1
{
public partial class Form1 : Form
{
String[] code;
Hashtable var = null;
String d = null;
//Regex reg = new
Regex("^(int|[sS]tring|float|char)\\s+\\w+\\s*(=\\s*[^,]+)?(,\\s*\\w+\\s*(=\\s*[^,]+)?)*;$");
Regex reg5 = new
Regex("^int\\s+\\w+\\s*=\\s*[0-9]+;$");
Regex reg5NotInt = new
Regex("^int\\s+\\w+\\s*=\\s*[0-9]+[.][[0-9]+;$");
Regex reg2 = new
Regex("^cout<<\"[a-zA-Z0-9]*\";$");
Regex reg3 = new
Regex("^cout<<[a-zA-Z0-9]*;$");
Regex Cin_reg = new
Regex("^cin>>[a-zA-Z0-9]*;$");
Regex Cout_Double_reg3 = new
Regex("^cout<<[a-zA-Z0-9]*<<\"[a-zA-Z0-9]*\";$");
Regex reg4 = new
Regex("^float\\s+\\w+\\s*=\\s*[0-9]+[.][0-9]+;$");
Regex reg6String = new
Regex("^String\\s\\w+(=\"\\w*\")*;$");
Regex reg4NotFloat = new
Regex("^float\\s+\\w+\\s*=\\s*[0-9];$");
Regex if_reg7 = new Regex("^if
\\([a-z0-9]*>[a-zA-Z0-9]*\\){$");
Regex is_alphabat_reg = new
Regex("^[a-zA-Z]$");
Regex is_dight_reg = new
Regex("^[0-9]$");
Regex end_Barakat_reg = new
Regex("^}$");
Regex Else_reg = new Regex("^else
{$");
int valueCin;
public Form1(int c)
{
InitializeComponent();
valueCin = c;
}
bool checkvariable = false;
public void variable(string code, int
i)
{
string[] t = code.Split(' ');
t[1].Trim();
t = t[1].Split(',');
for (int z = 0; z < t.Length;
z++)
{
try
{
string s = t[z];
if (!(s[0] >= 48
&& s[0] <= 57))
{
if
(!t[z].Contains('='))
{
var.Add(t[z], -1);
}
else
{
string[] da =
t[z].Split('=');
da[1] =
da[1].Substring(0, da[1].Length - 1);
var.Add(da[0],
da[1]);
}
}
else
{
richTextError.Text =
"error in line " + (i + 1) + "\n";
checkvariable = true;
}
}
catch (Exception)
{
checkvariable = true;
richTextError.Text =
"Error in line " + (i + 1) + "\n";
}
}
}
public void output_String(string Code)
{
for (int z = 7; z < Code.Length
- 2; z++)
{
richTxtoutput.Text += Code[z] +
"\n";
}
}
public void output_Variable(string
Code)
{
d = "";
for (int z = 6; z < Code.Length
- 1; z++)
{
d += Code[z];
}
foreach (var v in var.Keys)
{
if (v.Equals(d))
{
richTxtoutput.Text +=
var[v];
d = null;
}
}
}
public bool Singalcondition(String con)
{
String f = "";
String l = "";
String[] b = new String[] {
"<", ">", "==", "<=",
">=", "!=" };
String opt = "";
for (int i = 0; i < b.Length;
i++)
{
if (con.Contains(b[i]))
{
if (i == 1)
{
f =
con.Split('>')[0];
l =
con.Split('>')[1];
if
(is_alphabat_reg.IsMatch(f))
{
if
(is_dight_reg.IsMatch(l))
{
int val =
int.Parse(var[f] + "");
if (val >
int.Parse(l))
{
//MessageBox.Show("if
true");
return
false;
}
}else if
(is_alphabat_reg.IsMatch(l))
{
int variable1 =
int.Parse(var[f] + "");
int varaiable2
= int.Parse(var[l] + "");
if
(variable1>varaiable2)
{
return false;
}
}
}
else if
(is_dight_reg.IsMatch(f))
{
if
(is_dight_reg.IsMatch(l))
{
if
(int.Parse(f) > int.Parse(l))
{
//MessageBox.Show("if true");
return
false;
}
}
else if
(is_alphabat_reg.IsMatch(l))
{
int varaiable2
= int.Parse(var[l] + "");
if
(int.Parse(f) > varaiable2)
{
return
false;
}
}
}
else if (int.Parse(f)
> int.Parse(l))
{
//richTxtoutput.Text += "#iftrue";
return true;
}
}
}
}
return true;
}
bool Condition = false;
bool ifCondition = false;
private async void btncomp_Click(object
sender, EventArgs e)
{
richTxtoutput.Clear();
var = new Hashtable();
//richTxtoutput.Clear();
int count =
richtxtedit.Lines.Length;
code = new String[count];
TextReader read = new
System.IO.StringReader(richtxtedit.Text);
richTextError.Clear();
for (int i = 0; i < count; i++)
{
code[i] = read.ReadLine();
String p = code[i];
if (Condition)
{
if
(end_Barakat_reg.IsMatch(code[i]))
{
Condition = false;
}
else if (count==i)
{
richTextError.Text +=
"Missing the brakat }";
}
}
else if
(if_reg7.IsMatch(code[i]))
{
String[] s =
code[i].Split('(');
s = s[1].Split(')');
// MessageBox.Show(s[0]);
Condition =
Singalcondition(s[0]);
ifCondition = Condition;
//richTxtoutput.Text =
"test";
}
else if
(Else_reg.IsMatch(code[i]))
{
if (!ifCondition)
{
Condition = true;
}
//MessageBox.Show("else");
}
else if
(end_Barakat_reg.IsMatch(code[i]))
{
//MessageBox.Show("end Barakat");
}
else if (p[0] == 'i' &&
p[1] == 'n' && p[2] == 't' && p[3] == ' ')
{
//MessageBox.Show("");
variable(code[i], i);
}
else if
(Cin_reg.IsMatch(code[i]))
{
for (int r = 5; r <
p.Length - 1; r++)
{
d += p[r];
}
Form2 f = new Form2();
f.labal_Cin.Text =
"Enter value " + d;
//this.Hide();
f.ShowDialog();
// variable(code[i] +
"=" + f.txt_cin.Text, i);
if (var.ContainsKey(d))
{
var[d] =
f.txt_cin.Text;
}
else
{
var.Add(d, f.txt_cin.Text);
}
//
variable(code[i]+"=" + f.txt_cin.Text, i);
// richTxtoutput.Text += "cin"+
f.txt_cin.Text;
}
else if (reg5NotInt.IsMatch(code[i]))
{
richTextError.Text +=
"float value not convert to int \n";
int n = i + 1;
richTextError.Text +=
"error in line " + n + "\n";
}
else if (p[0] == 'f' &&
p[1] == 'l' && p[2] == 'o' && p[3] == 'a' && p[2] ==
't' && p[3] == ' ')
{
output_String(code[i]);
}
else if
(Cout_Double_reg3.IsMatch(code[i]))
{
String key = "";
int u =
code[i].IndexOf("<<") + 2;
int l =
code[i].LastIndexOf("<<");
for (int r = u; r < l; r++)
{
key += p[r];
}
richTxtoutput.Text +=
var[key];
// String m=
code[i].Substring(code[i].IndexOf("<<")+2);
// var first =
code[i].Substring(u, code[i].LastIndexOf("<<"));
key = "";
l =
code[i].LastIndexOf("<<") + 3;
for (int r = l; r <
p.Length - 2; r++)
{
key += p[r];
}
richTxtoutput.Text += key;
//output_String(first);
MessageBox.Show(key);
//
MessageBox.Show(code[i].Substring(0,
code[i].LastIndexOf("<<")));
//m =
m.Substring(m.IndexOf("<<") + 3);
// MessageBox.Show(m);
//String m =
code[i].Where(s=>s.In)
//MessageBox.Show(m);
}
else if
(reg6String.IsMatch(code[i]))
{
//MessageBox.Show("in");
variable(code[i], i);
}
else if (reg4.IsMatch(code[i]))
{
//MessageBox.Show("in");
variable(code[i], i);
}
else if
(reg4NotFloat.IsMatch(code[i]))
{
richTextError.Text +=
"int value not convert to float \n";
int n = i + 1;
richTextError.Text += "error
in line " + n + "\n";
}
else if (reg3.IsMatch(code[i]))
{
output_Variable(code[i]);
}
else
{
int n = i + 1;
richTextError.Text +=
"error in line " + n + "\n";
}
}
}
}
}
------------------------------------------end
in-----------------------
Comments
Post a Comment