- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
#include<string.h>
#include<stdio.h>
int firstState(char s[], int length);
int secondState(char s[], int length);
int thirdState(char s[], int length);
int forthState(char s[], int length);
int main()
{
char string[10];
gets(string);
if( firstState(string, 0) )
printf("Accept\n");
else
printf( "Not accept\n" );
return 0;
}
int firstState(char s[], int length)
{
if(s[length] == 'a')
return (secondState(s, length++));
else if(s[length] == 'b')
return firstState(s, length++);
else
return 0;
}
int secondState(char s[], int length)
{
if(s[length] == 'a')
return secondState(s, length++);
else if(s[length] == 'b')
return thirdState(s, length++);
else
return 0;
}
int thirdState(char s[], int length)
{
if(s[length] == 'a')
return secondState(s, length++);
else if(s[length] == 'b')
return forthState(s, length++);
else
return 0;
}
int forthState(char s[], int length)
{
if(s[length] == 'a')
return secondState(s, length++);
else if(s[length] == 'b')
return firstState(s, length++);
else
return 0;
}
它给了我一个段错误或核心转储,我很困惑!!!有人可以解释为什么它给了我这种错误吗????并告诉我如何调试才能让我的代码运行的非常清晰!!
我真的厌倦了这个:(
抱歉我的英语不好
最佳答案
你有一个无限递归,
return (secondState(s, length++));
传递的 length
参数是递增前 length
的值,因此您只需查看第一个 char
。
将 length
参数作为 length + 1
传递,并检查 length
是否小于 10(char 的长度
数组 string
).
另外,
gets(string);
是非常不安全的,如果输入超过九个字符,你就写在分配的内存之外。使用
fgets(string, sizeof string, stdin);
相反。
嗯,由于只需要上述修复和改变一个返回值,所以大部分逻辑是正确的,修复代码:
// #include<string.h> <- We don't use that
#include<stdio.h>
// Match the grammar (a+b)*abb
int firstState(char s[], int length); // nothing of the suffix matched
int secondState(char s[], int length); // matched one character of the suffix
int thirdState(char s[], int length); // matched two
int forthState(char s[], int length); // matched the complete suffix
int main()
{
char string[10];
// Get a 0-terminated string into the buffer.
fgets(string, sizeof string, stdin);
if( firstState(string, 0) )
printf("Accept\n");
else
printf( "Not accept\n" );
return 0;
}
int firstState(char s[], int length)
{
if(s[length] == 'a') // first character of suffix matched
return (secondState(s, length+1));
else if(s[length] == 'b') // nothing matched
return firstState(s, length+1);
else // end of string in not-accepting state
return 0;
}
int secondState(char s[], int length)
{
if(s[length] == 'a') // the old matched 'a' wasn't part of the suffix, the new may be
return secondState(s, length+1);
else if(s[length] == 'b') // now matched two characters of the suffix
return thirdState(s, length+1);
else // end of string in not-accepting state
return 0;
}
int thirdState(char s[], int length)
{
if(s[length] == 'a') // last three chars aba, the last 'a' could be part of the suffix
return secondState(s, length+1);
else if(s[length] == 'b') // full suffix matched
return forthState(s, length+1);
else // end of string in not-accepting state
return 0;
}
int forthState(char s[], int length)
{
if(s[length] == 'a') // another char, start a new candidate for the suffix
return secondState(s, length+1);
else if(s[length] == 'b') // another char, can't be part of the suffix, start over
return firstState(s, length+1);
else // end of string in accepting state, yay!
return 1;
// return s[length] == '\0';
// if characters other than 'a' and 'b' need not signal the end of the string
}
关于c - 我的 C 代码中的错误 "segmentation fault",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13575851/
我在一个俄罗斯编程论坛上遇到了这个问题,但还没有想出一个优雅的解决方案。 问题: 你有一个包含N个正整数的数组,你需要将它分成M个连续的段,使得最大段的和是可能的最小值。通过段的总数,我的意思是它所有
我正在从文本“从 0 到 1 的操作系统”中学习链接脚本,并且在文本中他们展示了一个使用关键字 PHDRS 的示例; ENTRY(main); PHDRS { headers PT_P
一个小型测试程序在 64 位 Linux 上使用 gfortran (4.4.5) 返回段错误。 n=2_8**22_8 时不存在故障。 gdb 指示在循环的第一次迭代期间函数 mylen 中发生段错
我正在尝试计算一个比率,当我的分子数组充满 0 时它可以工作,但是当我在分子数组中有值时会中断程序。 223 Double_t *ratio_calculations(int bin_numbers,
我正在研究组合分页/分段系统,在我的书中有两种方法: 1.paged segmentation 2.segmented paging 我无法弄清楚两者之间的区别。我认为在分页分段中,段被分成页面,在分
语义分割只是一种重复,还是“语义分割”和“分割”之间有区别? “场景标记”和“场景解析”有区别吗? 像素级分割和像素级分割有什么区别? (附带问题:当您拥有这种逐像素注释时,您是否可以免费获得对象检测
我遇到了一个非常奇怪的行为,我正在遵循在线文档,并且在创建一个段来与我的子段一起使用时,lambda 按以下顺序执行: xray_recorder.begin_segment('segment-nam
我们正在我们的服务器上进行一些负载测试,我正在使用 tshark 将一些数据捕获到 pcap 文件,然后使用 wireshark GUI 通过转到 Analyze -> expert Info 来查看
我是一名高中生,今年开始学习汇编。 我目前正在制作 Pacman 克隆作为我的最终项目。 我遇到的唯一问题是我的代码很大,*.exe 文件几乎有 64KB。 所以我的问题是,如果我转向模型介质,我需要
前言 本系列文章是博主在工作中使用SAM模型时的学习笔记,包含三部分: SAM初步理解,简单介绍模型框架,不涉及细节和代码 SAM细节理解,对各模块结合代码进一步分析 SAM微调实例
我很困惑进程在使用虚拟内存时如何可能出现段错误。据我了解,“虚拟”内存允许进程访问所有可用内存,然后将其映射到“实际”硬件内存。通过这种转换,进程怎么可能尝试访问不允许访问的内存部分? 最佳答案 听起
Tritwise操作(向右旋转和疯狂操作)无法正确运行,并在Malbolge编译器/解释器中引发分段错误。 在看到有关Coding Challenges和Code Golf的惊人答案之后,我决定开始在
只是有关DirectX11镶嵌的问题。 在“船体着色器”中,可以设置的最大镶嵌因子为64(不确定原因)。现在,尽管对于小型飞机已经足够了,但是对于大型飞机来说,这还远远不够,所以我想知道如何渲染大型飞
我需要在 matlab 中手动分割图像。我很想使用“impoly”,但我主要需要分割圆形对象。 我在 Youtube 上的 Ali Pashaei 视频中找到了我需要的东西,在“FlowImagePr
我有下面的代码。当我取消注释 temperature(i,j) = anode_temperature 时,我会出现 SegFault。 forall(i=0:Cells(1), j=0:Cells(
我是 C 编程新手,我的代码中出现了段错误。该程序使用返回函数来询问用户他们的银行帐户中有多少钱。稍后我将添加代码来计算利息。感谢您为我查看此内容,因为我很难找出为什么会出现此错误。 #include
为了测试,我编写了一个代码来计算 #include int main(void) { int p, i, primes[50], index; boo
这个问题已经有答案了: What is a segmentation fault? (17 个回答) 已关闭 5 年前。 最近我开始研究内置函数,但遇到了一个错误,那就是: 为什么我会遇到此程序段错误
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
注意:我们有很多段错误,基本相同 答案,所以我试图将其分解为一个规范的问题,例如 我们有undefined reference。 尽管我们有一个关于what a segmentati
我是一名优秀的程序员,十分优秀!