Unix中的C解析错误
就我的一生而言,我无法理解我的程序正在解析而不是编译。我所有的括号似乎都在这一点上。谢谢 我手头的任务是: 您需要编写一个接受两个十进制整数的C程序,比如d和r。你 可以假设第一个十进制整数d为非负,第二个十进制整数r为非负 它代表基数,将是2,3,4,15,16中的一个。程序应转换rst 将十进制整数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将rst整数转换为第二个整数指定的基数表示形式。 4.打印表示并停止。 您的程序必须从stdin读取两个整数,并将答案写入stdout。你 可能会假设在提示时,用户将键入两个由一个或多个空格分隔的整数。 注意:回想一下,对于任何基数r2,要使用的数字是0,1,:,r1。使用字母A, B、 C、D、E和F分别表示10、11、12、13、14和15,如十六进制所示 系统。因此,基数11中的表示可以使用数字0、1、:、9、A;基数表示法 12可以使用0、1、9、A、B等。� 大宗报价 到目前为止,我的代码是:Unix中的C解析错误,c,parsing,variable-assignment,C,Parsing,Variable Assignment,就我的一生而言,我无法理解我的程序正在解析而不是编译。我所有的括号似乎都在这一点上。谢谢 我手头的任务是: 您需要编写一个接受两个十进制整数的C程序,比如d和r。你 可以假设第一个十进制整数d为非负,第二个十进制整数r为非负 它代表基数,将是2,3,4,15,16中的一个。程序应转换rst 将十进制整数d转换为基数r的表示形式,并打印结果。 示例: (i) 假设第一个十进制整数是138,第二个十进制整数是16。在这种情况下,, 程序产生的输出应该是8A,即十六进制(基数16) 十进制整数138的
#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;
}
#包括
#包括
#财务主任55
#定义OFF0 48
结构charNode{
字符数字;
结构charNode*next;};结构charNode*head;
int NextParse(int*,int);
charint(int);char-pop();
无效推送(char);
结构charNode*newCharNode();
内部主(空)
{int十进制,基数;
printf(“需要两个数值:\n”);
scanf(“%d%d”、&十进制和基数);
do{push(charint(nextpasse(&十进制,基数));}
而(十进制>0);
while(head!=NULL)
{printf(“%c”,pop());}
printf(“\n”);返回0;}
整数下一个字符(整数*小数,整数基数)
{int digit=*小数%基数;*小数=*小数/基数;
返回数字;
字符(整数x){
如果(x>35)
{
printf(“此基数太大,请重试”);退出(1);
}
否则,如果(x>=10)返回(字符)(x+OFFA);
否则返回字符(x+OFF0);
}
无效推送(字符数字){
结构charNode*temp;
if(head==NULL){
head=newCharNode();head->digit=digit;head->next=NULL;}
else{temp=newCharNode();temp->digit=digit;temp->next=head;head=temp;}
}
字符弹出(无效){
字符数字;结构charNode*temp;
if(head==NULL){
printf(“无法完成此任务。s tack为空”);
出口(2);}
否则{
温度=头部;数字=头部->数字;
head=head->next;空闲(临时);返回数字;}
结构charNode*newCharNode(无效){
struct charNode*memory=(struct charNode*)malloc(sizeof(struct charNode));//新节点的内存地址
if(内存==NULL){
printf(“无法分配内存”);退出(3);
}
否则返回内存;
}
感谢您的帮助。假设上面发布的代码是正确的,下面是一个更好的代码格式版本。我绝对不能保证它能满足您的需要,只是编译器在编译时不会立即呕吐
/*
* 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;
}
/*
*按照pbond的建议,选择一种编码风格并坚持下去
*最初的代码看起来像是您使用的某个IDE
*故意对您隐藏格式问题。以下是
*代码(某种程度上)已更正为许多标准之一。
*/
#包括
#包括
#财务主任55
#定义OFF0 48
结构charNode{
字符数字;
结构charNode*next;
};
结构charNode*head;
int NextParse(int*,int);
charint(int);
char-pop();
无效推送(char);
结构charNode*newCharNode();
内部主(空)
{
整数小数,基数;
printf(“需要两个数值:\n”);
scanf(“%d%d”、&十进制和基数);
做{
push(charint(下一个字符(&decimal,radix));
}而(十进制>0);
while(head!=NULL){
printf(“%c”,pop());
}
printf(“\n”);
返回0;
}
整数下一个字符(整数*小数,整数基数)
{
整数位数=*小数%基数;
*十进制=*十进制/基数;
返回数字;
}
/*
*这里是其中一个错误所在。“下一个通行证”尚未终止。我已经
*增加了结束括号。
*/
/*
*这是另一个错误,看起来应该是这样的
*前面声明的'charint'函数。但是
*“char(intx)”不会通过编译器。这两者结合在一起
*无法终止上述“NextPass”,并且可能
*编译器没有什么帮助,导致语法错误消息混乱。
*“gcc”编译器正确地发现了“char(int)”错误,并且
*理顺风格后的检查显示“下一页”
*上面的错误。
*/
charcharint(int x)
{
如果(x>35)
{
printf(“此基数太大,请重试”);退出(1);
}
如果(x>=10),则为else
返回(字符)(x+OFFA);
其他的
返回(字符)(x+OFF0);
}
无效推送(字符数字)
{
结构charNode*temp;
if(head==NULL)
{
/*
*连接行可能看起来“整洁”,但它使维护变得更加困难。
*/
head=newCharNode();
头部->数字=数字;
head->next=NULL;
}
否则{
temp=newCharNode();
温度->