0

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

我手头的任务是:

您需要编写一个接受两个十进制整数(例如 d 和 r)的 C 程序。您可以假设第一个十进制整数 d 是非负数,并且表示基数的第二个十进制整数 r 将是 2、3、4、: : :、15、16 之一。程序应该转换第一个十进制整数 d以基数 r 表示并打印结果。示例: (i) 假设第一个十进制整数是 138,第二个十进制整数是 16。在这种情况下,您的程序产生的输出应该是 8A,它是十进制整数 138 的十六进制(基数 16)表示。( ii) 假设第一个十进制整数是 284,第二个十进制整数是 13。在这种情况下,程序产生的输出应该是 18B,它是十进制整数 284 的基数 13 表示。(以 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;
}

感谢您的帮助。

4

1 回答 1

0

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

/*
 * 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 可能会比其他任何事情都更让您感到悲伤。

于 2013-10-04T23:40:27.087 回答