gpt4 book ai didi

c - atof 未在单链表中转换

转载 作者:行者123 更新时间:2023-11-30 14:22:48 25 4
gpt4 key购买 nike

输入文件应以后缀顺序指定单个代数表达式,支持正浮点值和负浮点值以及以下运算:加法 (+)、减法 (-)、乘法 (*)、除法 (/)。所有浮点值和操作都应以空格分隔。我必须扫描以下行:

4 5 + 20 3 - * -4.0 +

尝试使用 atof 函数将用于扫描参数的字符串转换为 float 并将其放入链接列表元素时,出现错误。我也使用堆栈结构。这是我的代码:ListElmt.h

#include "bool.h"



#ifndef LISTELMT_H
#define LISTELMT_H



typedef struct ListElmt_ {
float num;
struct ListElmt_ *next;
} ListElmt;


#endif

main.c

#include "bool.h"
#include "listelmt.h"
#include "list.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>



#define BUFSIZE 1000

int stack_push(List *list, int data){

return list_ins_next(list, NULL, data);
}

int stack_pop(List *list, ListElmt *data){
if(list->top->num == data->num){
return list_rem_next(list, data);
}
}

int main ( int argc, char *argv[] )
{
FILE *ifp;
char fileInput[BUFSIZE];
char *newStr;
List *argument_list;
ListElmt *stack_elmt;
int i = 0;
float a=0; float b = 0; int c = 0;

if ( argc != 2 ) /* argc should be 2 for correct execution */
{
/* We print argv[0] assuming it is the program name */
printf( "usage: %s filename", argv[0] );
}
else
{
// We assume argv[1] is a filename to open
ifp = fopen( argv[1], "r" );

if (ifp == 0 ){

printf("Could not open file\n");
}

else{
argument_list = (List *)malloc(sizeof(List));

list_init(argument_list);
stack_elmt = (ListElmt*)malloc(sizeof(ListElmt));



while (fscanf(ifp, "%s", &newStr) != EOF){

if(newStr != "+" || newStr != "-"|| newStr != "*"|| newStr!= "/" ){

stack_elmt->num = atof(newStr);

stack_elmt->next = NULL;

stack_push(argument_list, stack_elmt->num);
}

else{
if(newStr == "+"){
a = argument_list->top->num;
stack_pop(argument_list, argument_list->top);
b = argument_list->top->num;
stack_pop(argument_list, argument_list->top);
stack_elmt->num = a+b;
stack_elmt->next=NULL;

stack_push(argument_list, stack_elmt->num);
}
else if(newStr == "-"){
a = argument_list->top->num;
stack_pop(argument_list, argument_list->top);
b = argument_list->top->num;
stack_pop(argument_list, argument_list->top);
stack_elmt->num = a-b;
stack_elmt->next=NULL;

stack_push(argument_list, stack_elmt->num);
}
else if(newStr == "*"){
a = argument_list->top->num;
stack_pop(argument_list, argument_list->top);
b = argument_list->top->num;
stack_pop(argument_list, argument_list->top);
stack_elmt->num = a*b;
stack_elmt->next=NULL;

stack_push(argument_list, stack_elmt->num);
}
else if(newStr == "/"){
a = argument_list->top->num;
stack_pop(argument_list, argument_list->top);
b = argument_list->top->num;
stack_pop(argument_list, argument_list->top);
stack_elmt->num = a/b;
stack_elmt->next=NULL;

stack_push(argument_list, stack_elmt->num);
}
}

printf("%lf", argument_list->top->num);





}
}
}//list_destroy(argument_list);
getchar();
//return 0;
}

我正在使用 Visual Studio,所以它不会告诉我有错误或任何东西,而是只是中断并使用 atof 转到一些奇怪的文件。

最佳答案

  • 对于字符串比较,请使用 strcmp()strncmp()

    if(newStr != "+"|| newStr != "-"|| newStr != "*"|| newStr!= "/") 不会将字符串进行比较预计。

  • 仅声明了指针。

    char *newStr;

    因此内存应该动态或静态分配。即 char newStr[MAX_STR_LEN];malloc(MAX_STR_LEN); MAX_STR_LEN 是预期的最大字符串长度。

关于c - atof 未在单链表中转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13467742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com