编译原理实验模拟DFA.
- 格式:pdf
- 大小:159.75 KB
- 文档页数:3
一. 算法2.1 模拟DFA(用C#) using System;
using System.Collections.Generic; using System.Text;
namespace ConsoleApplication1
{
class Program {
static void Main(string[] args)
{
int start=0;
int[] end={3}; Console.WriteLine("**********本程序用于识别正规式为(a|b)*abb的字
符序列*******"); String input=Console.ReadLine().Trim();
char[] inArray=input.ToCharArray();
int s=start; int length=inArray.Length;
int flag=0;
char a=inArray[0];
while(flag!=length)
{ a=inArray[flag];
s=move(s,a);
flag+=1;
}
bool temp=false; for(int i=0;i { if(s==end[i]) temp=true; } if(temp) Console.WriteLine("接受"); else Console.WriteLine("不接受"); } public static int move(int state, char c) { if (state == 0 && c == 'a') { return 1; } if (state == 0 && c == 'b') { return 0; } if (state == 1 && c == 'a') { return 1; } if (state == 1 && c == 'b') { return 2; } if (state == 2 && c == 'a') { return 1; } if (state == 2 && c == 'b') { return 3; } if (state == 3 && c == 'a') { return 1; } if (state == 3 && c == 'b') { return 0; } else return -1; } } } 运行结果: 四.P186:用C语言实现值调用和引用调用的参数传递 #include void swap1(int x,int y) { int temp; temp=x; x=y; y=temp; } void swap2(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; } int main() { int a=1, b=2; swap1(a,b); printf("值调用的参数传递:\n"); printf("a=%d,b=%d\n",a,b); swap2(&a,&b); printf("引用调用的参数传递:\n"); printf("a=%d,b=%d\n",a,b); } 运行结果: