- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是一个使用堆栈的后缀计算器的简单程序,但是 atoi() 导致它崩溃。为什么会这样?我已经尝试使用 ch-'0' 将 char 转换为字符串并且它有效,但是在这种情况下用于 char 到 int 转换的 atoi() 函数似乎不起作用。
是因为ch不是char也不是string例如。炭黑;而不是 char ch[20];
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int num[MAX],tos=-1;
push(int x)
{
if(tos==MAX)
{
printf("the stack is full");
}
else
{
printf(" l");
tos++;
num[tos]=x;
}
}
int pop()
{
if(tos<0)
{
printf("stack underflow");
}
else
return num[tos--];
}
int main()
{
char postfix[MAX],exp[MAX],ch,val;
int a,b;
printf("enter the postfix expression");
fgets(postfix,MAX,stdin);
strcpy(exp,postfix);
for(int i=0;i<strlen(postfix);i++)
{
printf(" xox ");
ch=postfix[i];
if(isdigit(ch))
{
push(ch - '0');
printf(" %d ",atoi(ch));
}
else
{
printf("%d",tos);
a=pop();
b=pop();
switch(ch)
{
case '+':
val=a+b;
break;
case '-':
val=a-b;
break;
case '*':
val=a*b;
break;
case '/':
val=a/b;
break;
}
printf("%d",val);
push(val);
}
}
printf("the result of the expression %s = %d",exp,num[0]);
return 0;
}
最佳答案
Is it because ch is a char nor string eg. char ch; and not char ch[20];
是的。 atoi(ch)
甚至不是有效的 C,不允许干净地编译。
在这种情况下,您可以根据 ch
和空终止符创建一个临时字符串。例如通过复合文字:(char[2]){ch, '\0'}
。
并且您永远不应该出于任何目的使用atoi
,因为它的错误处理能力很差,而且是一个完全多余的函数。请改用 strtol
系列函数。
你可以这样调用strtol
:
strtol( (char[2]){ch, '\0'}, // string to convert from
NULL, // end pointer, not used, set to NULL
10 ); // base 10 = decimal
例子:
printf(" %d ", (int)strtol( (char[2]){ch, '\0'}, NULL, 10) );
这完全等同于更具可读性:
char tmp[2] = { ch, '\0' };
int result = (int) strtol(tmp, NULL, 10);
printf(" %d ", result);
关于c - atoi 似乎不适用于我的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54330130/
本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,Python, C++, Java 题目地址:https://leetcode-cn.com/problems/string-to
我正在读取通过 RS485 发送的值,这是编码器的值我首先检查它是否已返回 E 字符(编码器报告错误),如果没有则执行以下操作 *position = atoi( buffer );
我有一个问题,我的 C 程序只为小于 5 的值正确分配了输入数据。我发现在创建保存值的 int 数组时出错:我使用了 atoi(var-1) 而不是 atoi(变种)-1。 当var='5'时,打印出
我试图在字符串 509951644 和 4099516441 上调用 atoi。第一个毫无问题地转换了。第二个是给我十进制值 2,147,483,647 (0x7FFFFFFF)。为什么会这样? 最佳
我编写了一个程序,它从用户那里获取 2 个参数并将它们加在一起,例如,如果用户输入 ./test 12 4,它将打印出总和:16。 令我困惑的是为什么我必须使用 atoi我不能只使用 argv[1]
你好,我有一个函数叫 int SearchKey(char key[]). 我给出的关键是这样的字符串 2014-02-13T23:50:00 在函数中我只保留数字,这意味着我的字符串变成这样:201
我正在制作用于组装的 atoi 函数。 无论我尝试什么都行不通,我也不知道为什么。 有谁知道是什么问题? org 100h mov si, stri ;parameter call atoi
我正在尝试解决家庭作业问题。说明是用 C 语言编写 Vigenere 密码。 C 不喜欢下面的代码: rot = atoi(argv[1][index]) - 'A'; rot 已声明为整数; ind
我试图编写自己的 atoi() 函数实现,并尝试了两种不同的代码: #include #include int myatoi(const char *string); int main(int a
我希望在不使用任何其他函数(如 strtonum())的情况下重现 atoi() 函数的行为。我真的不明白如何将 char 或 char * 转换为 int 而不是将其转换为 ASCII 值。我在 C
我有以下代码: char* input = (char*)malloc(sizeof(char) * BUFFER) // buffer is defined to 100 int digit = a
我想在编译时实现atoi()函数(在C++语言中,使用C++11或C++14标准)。因此它应该能够将双引号中的文本解析为数字,或者报告错误。更具体地说,它是更大系统的一部分,能够在编译时解析类似 pr
以下代码显示了奇怪的行为: int main() { char numArr[] = {'9','8','5'}; int num; printf("%d\n",num);
这是代码: #include /* atoi: convert s to integer; version 2 */ int atoi(char s[]) { int i, n, sign; fo
我在这个简单的循环练习中遇到问题。我认为代码是正确的,并且没有收到任何错误,但是当我运行程序时,我只是一遍又一遍地收到“^C”。请帮忙。 #import #import #include int
我对 atoi() 有一个奇怪的问题:我有一个字符串(比如说 str),类似于“aaaa 1111\0”(我打印并检查过 - 确实是这样)。我尝试在 str+5 上执行 atoi,但程序崩溃了。我在前
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
如何在不使用给定参数中的 atoi 的情况下将字符串转换为整数?这是我尝试过的: int main(int argc, char *argv[]){ for(int i = 1; i < ar
我使用 atoi() 从 header 获取状态代码,但它不适用于以下输入: "404 Not Found\r\n内容类型: text/html\r\n日期: 2013 年 12 月 12 日星期四
printf("Enter a number or type 'Exit' to exit\n"); long val = 0; int y = 3; scanf("%s",
我是一名优秀的程序员,十分优秀!