- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我要修复这段代码有点疯狂。我已经处理了将近两个小时,但找不到问题所在。
这是一个简单的程序,它应该将一些文本作为输入并返回相同的文本,所有数字都从十进制转换为十六进制。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int revNumber(int);
typedef enum { STATE_WORD, STATE_BLANK, STATE_NUMBER } state_t;
int main()
{
int c;
int readNum = 0;
int writeNum = 0;
int digit = 1;
int sign = 1;
state_t state = STATE_BLANK;
while( (c = getchar()) != EOF )
{
switch(state)
{
case STATE_WORD:
if( isspace(c) )
state = STATE_BLANK;
putchar(c);
break;
case STATE_BLANK:
if(c == '-')
{
sign = -1;
state = STATE_NUMBER;
}
else if( isdigit(c) )
{
readNum += (c - '0') * digit;
digit *= 10;
state = STATE_NUMBER;
}
else if( isspace(c) )
putchar(c);
else
{
state = STATE_WORD;
putchar(c);
}
break;
case STATE_NUMBER:
if( isdigit(c) )
{
readNum += (c - '0') * digit;
digit *= 10;
}
else
{
writeNum = revNumber(readNum);
readNum = 0;
if(sign == -1)
putchar('-');
if(digit > 1)
{
if( isspace(c) )
{
printf("%x", writeNum);
state = STATE_BLANK;
}
else
{
printf("%d", writeNum);
state = STATE_WORD;
}
}
digit = 1;
sign = 1;
putchar(c);
}
break;
}
}
}
int revNumber(int n)
{
int revNum = 0;
while(n != 0)
{
revNum += revNum * 10 + n%10;
n /= 10;
}
return revNum;
}
但是,该程序无法正常运行,出于某种原因,我一直无法正确转换为十六进制。为什么会这样?非常感谢。
最佳答案
以下代码实际上并不是 2 的补码,因为十六进制值没有符号,只有大小。
但是,这按预期工作(对于我使用的每个测试用例)
它演示了处理状态机的首选逻辑,包括如何轻松处理状态转换。
以下代码不会反转数字中的数字,您应该能够轻松添加该功能,这将取代对 printf()
的调用在inNumber()
功能
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef enum { STATE_WORD, STATE_BLANK, STATE_NUMBER } state_t;
state_t inWord ( char ch );
state_t inBlank ( char ch );
state_t inNumber( char ch );
int number = 0;
char sign = ' ';
state_t state = STATE_BLANK;
int main( void )
{
int c;
printf( "program will echo sentence with numeric sub strings output in hex format\n");
printf( "Enter a sentence:");
while( (c = getchar()) && (c != EOF) )
{
switch(state)
{
case STATE_WORD:
state = inWord( c );
break;
case STATE_BLANK:
state = inBlank( c );
break;
case STATE_NUMBER:
state = inNumber( c );
break;
default:
printf( "state machine contains invalid state\n");
break;
} // end switch
if( '\n' == c )
{
break;
}
} // end while
putchar( '\n');
return 0;
} // end function: main
state_t inWord( char ch )
{
state_t newState = STATE_WORD;
if( isdigit( ch ) )
{
newState = inNumber( ch );
}
else
{
putchar( ch );
}
return newState;
} // end function: inWord
state_t inBlank( char ch )
{
state_t newState = STATE_BLANK;
if( isdigit( ch ) )
{
newState = inNumber( ch );
}
else if( isalpha( ch ) )
{
newState = inWord( ch );
}
else
{
putchar( ch );
}
return newState;
} // end function: inBlank
state_t inNumber( char ch )
{
state_t newState = STATE_NUMBER;
if( !isdigit( ch ) )
{
printf( "%X ", number );
newState = inBlank( ch );
}
else
{
number *=10;
number += ch-'0';
}
return newState;
} // end function: inNumber
关于c - 代码错误(状态机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880478/
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我刚刚开始使用 Arduino,对更多高级内容几乎一无所知。这看起来很简单。现在我是一个通常喜欢将两个设备集成在一起的人,所以我想知道我是否可以使用计算机的键盘或连接到 Arduino 板上的两个硬件
Hadoop上是否有支持 vector 机的实现? 我正在寻找使我能够训练然后在Hadoop之上测试SVM模型的代码。 最佳答案 有人正在使用BSP在Apache Hama之上进行实现,该实现基本上是
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我想用 Java 编写一个“智能监视器”,它会在它检测到即将出现的性能问题时发出警报。我的 Java 应用正在将结构化格式的数据写入日志文件: | | 因此,例如,如果我有一个执行时间为 812
我在 Cappuccino 中遇到这种错误 TypeError: Result of expression '_2b' [undefined] is not an object. TypeError:
我想编写一个简单的 bean 机程序。该程序将接受用户输入的球数和槽数,并计算每个球的路径。每个槽中的球数也将打印为直方图。 我尽力让代码保持简短和简洁,但我能做到的最好的长度是 112 行。当我运行
我正在开发一个 Plone 模板。目前我正在对模板实现搜索。模板上有一个表单提交到同一模板,即同一页面。 我需要启用页面的某些部分,即结果 DIV,仅当页面中的表单已提交时。我不确定如何检查页面是否已
我是机器学习的新手。我正在使用 opencv 开源库做一个项目。我的问题是我没有机器学习方面的经验。我从不同的图像中提取了特征并对其进行了评估,现在我想使用 SVM 对这些图像中的对象进行分类,但我不
我有一个来自自定义抽象对象的数据集和一个自定义距离函数。是否有任何好的 SVM 库允许我训练我的自定义对象(不是 2d 点)和我的自定义距离函数? 我在 this similar stackoverf
我正在尝试编写 SVM 或 KNN 程序来对文本文档进行分类。我掌握了两者的概念,但我希望看到一些很好的代码示例,它们专门演示了如何将文本/单词表示为 vector 。有人知道好的教程/文章/讲座/任
我想知道支持 vector 机 (SVM) 对于大型/超大型数据集(5-15M+ 行)具有非线性决策边界(例如高斯核)? 我知道两种特殊的方法:一方面,这个使用随机梯度下降等的调查: http://c
我试图在 Raspberry Pi 中使用 python 控制连续伺服(DF15RSMG),但连续伺服无法停止。代码如下: import RPi.GPIO as GPIO import time
我们在 .net 中有银行项目。在那个项目中我们需要实现存折打印。 - 存折背面会有磁条。 - 当客户将存折插入自助服务终端时,自助服务终端将生成 ISO 8583 信息。 - 为响应此消息,CBS(
我想使用 svm 分类器进行面部表情检测。我知道 opencv 有一个 svm api,但我不知道训练分类器的输入应该是什么。到现在看了很多论文,都是说人脸特征检测后训练分类器。 到目前为止我做了什么
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
为了学习 ArchLinux,我在 VirtualBox 机器上安装并使用了 ArchLinux。它运行完美。现在我在我的电脑上安装了 Docker(Windows 环境)。在安装 Docker 的过
我找不到很多相关信息,但是我知道可以通过命令提示符electron-packager .创建一个 Electron 应用。但是,可以通过 Node 快速路由在前端执行它吗? 所以我想做这样的事情: r
我正在使用 MS Visual Studio 2008、OpenCV、C++ 和 SVM 继续我的 OCR 项目。我生成了一个超过 2000 个机印字符样本的数据集。当我用线性核测试时,我总是得到 9
我通过UART接口(interface)(RX-TX,GND-GND)将伺服(TowerPro sg90)连接到32伺服 Controller (this one)。 卖家提供了如何连接和控制我的伺服
我是一名优秀的程序员,十分优秀!