【问题标题】:C Parse Errors In UnixUnix 中的 C 解析错误
【发布时间】:2013-10-04 22:50:55
【问题描述】:

对于我的一生,我无法弄清楚我的程序是在解析而不是编译。我所有的括号似乎都是正确的。谢谢!

我手头的任务是:

您需要编写一个接受两个十进制整数(例如 d 和 r)的 C 程序。你 可以假设第一个十进制整数 d 是非负数,而第二个十进制整数 r 表示基数,将是 2, 3, 4, : : :, 15, 16 之一。程序应转换第一个 十进制整数 d 以基数 r 表示并打印结果。 例子: (i) 假设第一个十进制整数是 138,第二个十进制整数是 16。在这种情况下, 您的程序产生的输出应该是 8A,即十六进制(基数 16) 十进制整数 138 的表示。 (ii) 假设第一个十进制整数是 284,第二个十进制整数是 13。在这种情况下, 你的程序产生的输出应该是 18B,它是基数 13 的表示形式 十进制整数 284。(以 13 为底,使用的数字是 0、1、2、: : :、9、A、B 和 C,其中 A、B 和 C 分别代表 10、11 和 12。) 你的程序应该写成只处理一对整数。因此,大纲为 你的程序如下。 (请注意,不需要错误检查。) 1. 提示用户输入两个十进制整数。 2. 读取两个整数。 13. 将第一个整数转换为第二个整数指定的基数中的表示形式。 4. 打印表示并停止。 您的程序必须从标准输入读取两个整数并将答案写入标准输出。你 可以假设当提示时,用户将键入由一个或多个空格分隔的两个整数。 注意:回想一下,对于任何基数 r 2,要使用的数字是 0、1、: : :、r 1。使用字母 A, B、C、D、E 和 F 分别代表 10、11、12、13、14 和 15,如十六进制中所做的那样 系统。因此,基数 11 中的表示可以使用数字 0、1、: : :、9、A;基数表示 12 可以使用 0、1、: : :、9、A、B 等等。 块引用

到目前为止我的代码是:

#include <stdio.h>
#include <stdlib.h>
#define OFFA 55
#define OFF0 48

struct charNode{
  char digit;


struct charNode *next;}; struct charNode *head;
int NextParse(int *, int);
char charint( int ); char pop();
void push( char );



struct charNode *newCharNode( );
int main(void)

{int decimal,radix;

  printf( "Two num required:\n" );
  scanf( "%d%d", &decimal, &radix );
  do{push( charint( NextParse( &decimal, radix )));}



  while( decimal >0 );
  while( head != NULL )

  {printf( "%c", pop( ) );}
  printf( "\n" ); return 0;}



int NextParse( int *decimal,int radix )
{ int digit=*decimal%radix; *decimal = *decimal/radix;

  return digit;

char (int x) {
  if ( x > 35 )
  {
    printf("This radix is too large, try again"); exit(1);
  }
  else if (x > = 10) return (char) (x + OFFA);
  else return (char) (x + OFF0);
  }

void push(char digit) {
  struct charNode *temp; 
  if (head == NULL) {
    head = newCharNode(); head -> digit = digit; head -> next = NULL;}
  else {temp= newCharNode( ); temp -> digit=digit; temp -> next=head;head=temp;}
}
char pop(void) {
  char digit; struct charNode *temp;
  if (head == NULL) {
    printf("Cannot complete this task. Empty s tack");
    exit(2);}
  else {
    temp = head; digit = head -> digit;
    head = head -> next; free(temp); return digit;}}

struct charNode *newCharNode(void) {
 struct charNode *memory = (struct charNode *) malloc(sizeof(struct charNode)); //memory adress for new node
 if (memory == NULL) {
   printf("Could not allocate memory"); exit(3);
 }
 else return memory;
}

感谢您的帮助。

【问题讨论】:

  • 你能发布你得到的编译错误吗?另外,尝试应用编码约定并坚持下去,我发现您的编码风格存在各种不一致,这使得它难以阅读。
  • 为什么被标记为java
  • 您在第 41 行“char (int x) {”周围有一个相当明显的错误。你确定你复制的代码正确吗?看起来“NextParse”没有被正确终止(}),并且下一个函数没有被命名。 pbond 也是正确的,找到一种编码风格并坚持下去。这将使您和我们更容易找到此类问题。这是此类样式列表的一个链接:maultech.com/chrislott/resources/cstyle
  • @pbond 我收到以下错误:: In function NextParse'::40: parse error before char' : At top level: :44: parse error before else'` 抱歉标记 java。我的错。

标签: c parsing variable-assignment


【解决方案1】:

假设上面发布的代码是正确的,这里有一个格式更好的代码版本。我绝对不保证它会做你想要的,除了编译器在我编译它时不会立即出错。

/*
 * As suggested by pbond, pick a coding style and stick with it.  The
 * original code rather looked like you were using some IDE that was
 * deliberately hiding the formatting issues from you.  Here's the
 * code (sort of) corrected to one of many standards.
 */
#include <stdio.h>
#include <stdlib.h>
#define OFFA 55
#define OFF0 48

struct charNode {
  char digit;
  struct charNode *next;
};

struct charNode *head;

int NextParse(int *, int);
char charint( int );
char pop();
void push( char );
struct charNode *newCharNode( );

int main(void)
{
  int decimal,radix;

  printf( "Two num required:\n" );
  scanf( "%d%d", &decimal, &radix );
  do {
    push( charint( NextParse( &decimal, radix )));
  } while( decimal >0 );

  while( head != NULL ) {
    printf( "%c", pop( ) );
  }

  printf( "\n" );
  return 0;
}



int NextParse( int *decimal,int radix )
{
  int digit= *decimal % radix;
  *decimal = *decimal / radix;

  return digit;
}

/*
 * Here's where one of the errors was.  'NextParse' had not been terminated.  I've
 * added the closing bracket.
 */

/*
 * This is the other error.  It looked like it was supposed to be
 * the 'charint' function declared earlier.  However, the original
 * "char (int x)" wouldn't have passed the compiler.  That combined
 * with the failure to terminate 'NextParse' above, and a possibly
 * less than helpful compiler, lead to confusing syntax error messages.
 * The 'gcc' compiler correctly spotted the "char (int" error, and visual
 * examination after straightening out the style showed the 'NextParse'
 * error above.
 */
char charint (int x)
{
  if ( x > 35 )
    {
      printf("This radix is too large, try again"); exit(1);
    }
  else if (x >= 10)
    return (char) (x + OFFA);
  else
    return (char) (x + OFF0);
}

void push(char digit)
{
  struct charNode *temp; 
  if (head == NULL)
    {
      /*
       * Concatenating lines may look 'neat', but it makes maintenance a lot harder.
       */
      head = newCharNode();
      head -> digit = digit;
      head -> next = NULL;
    }
  else {
    temp= newCharNode( );
    temp -> digit=digit;
    temp -> next=head;
    head=temp;
  }
}

char pop(void) {
  char digit;
  struct charNode *temp;
  if (head == NULL)
    {
      printf("Cannot complete this task. Empty s tack");
      exit(2);
    }
  else
    {
      temp = head;
      digit = head -> digit;
      head = head -> next;
      free(temp);
      return digit;
    }
}

struct charNode *newCharNode(void) {
  struct charNode *memory
    = (struct charNode *) malloc(sizeof(struct charNode)); //memory adress for new node
  if (memory == NULL)
    {
      printf("Could not allocate memory");
      exit(3);
    }
  else
    return memory;
}

如果您使用的是 IDE,我建议您选择以下两种方法之一:

1) 了解如何调整 IDE 以将您的代码置于标准样式中。

2) 选择一个不同的 IDE,这个隐藏的太多了,而且你报告的错误信息也不是很丰富。

如果您没有使用 IDE,我建议您购买一个并使用上面的建议 (1)。

您选择哪种 IDE 取决于您的环境以及您的个人偏好/风格。 IDE 争论本质上几乎是宗教性的,所以我无意就使用哪个 IDE 提出任何建议。只是你学会了如何正确使用它。

一个调整不当的 IDE 可能会给你带来更多的痛苦。

【讨论】:

  • 顺便说一句:我最近遇到了 Visual C++ 用户的问题,其中编译器的库存配置默默地隐藏了过多的警告。警告实际上是代码中非语法错误的关键指针。 'gcc' 编译器解决了这些问题。这导致了关于“(a) Visual C++ 没有抱怨,(b) 这只是一个警告,并不重要”的争论。
  • 首先,我不关心 (a) 和 (b) 没有警告是不重要的,直到您了解它为什么会发生!修复代码中的所有警告。如果您无法修复警告,请在代码中记录。见stackoverflow.com/questions/14288603/…
猜你喜欢
  • 2018-09-10
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 2012-04-26
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
相关资源
最近更新 更多