-2
我的代码已被反复校对,我仍然无法弄清楚为什么这些代码中的一部分会发出错误消息。只有一个'}'的行被调用为错误。这怎么可能?额外的错误?他们在哪里?他们是什么? C编程
#include <stdio.h>
#include <getword.h>
#define STORAGE 255
int getword(char *w) {
char *word = w; /*points to first char in array */
char iochar; /*holds current var being processed */
int counter = 0; /* counts number of characters in a word */
char *first = w; /* points to first character in array */
int bslashflag = 0; /* 0 if no backslash seen, 1 otherwise */
int negateornah = 0; /* if set to 1, return value is negated */
while((iochar = getchar()) != EOF) { /*runs until EOF is encountered */
if(iochar =='\\') /* if a slash char is found in the word*/
{ bslashflag = 1; /*acknowledge that*/
/*backslash was seen*/
iochar = getchar(); /* skip it & go to the next char*/
if(iochar == '\'){ /*and if it ends up being another*/
/*slash, slash designated to be added in */
*word = iochar; /* so go ahead and add it in */
*word++; /* and go to the next empty slot */
counter++; /*and increment your counter */
iochar = getchar(); /* *ALERT << may or may not beneccessary */
} else if(iochar == '<' || iochar == '>' || iochar == '|' || iochar == '$' || iochar == '&')
{ /* if a metacharacter is found after a bslash, */
/*just treat it like any other character*/
*word = iochar;
*word++;
counter+=1;
}
}
else if (iochar == ' ')
{
if(counter == 0) //*f the first char is a space */
{
while((iochar = getchar()) == ' ') { } /*skips leading blanks*/
} else if(bslashflag == 1) { /*if there was bslash before space*/
*word++ = iochar; /*add the space as part of string*/
counter++; /*increments counter*/
bslashflag--; /*resets backslash flag for next char check*/
}
else
{ /* until alphanum char is found*/
*word = '\0'; /*if not leading blank, its the end of word*/
if(negateornah == 1)
{
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /* return word size */
}
}
else if(iochar == '\n' || iochar == '<' || iochar == '>' || iochar == '|' || iochar == '$' || iochar == '&')
{ /*if a metacharacter is encountered*/
if(iochar == '\n' && counter == 0) /* if the first char in the word is \n */
{
*word = '\0'; /* and null terminate the array; TRY *first INCASE OF ERROR*/
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /* null terminate the array and return count so scan can continue*/
}
else if(iochar == '\n' && counter != 0)
{
*word = '\0' ;
ungetc('\n',stdin); /* put it back where you got it*/
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /*and return a number so the scan can continue*/
}
else if(iochar == '$')
{
if(counter ==0) {
negateornah = 1; } /*means $ was first char in word, set negate count flag*/
if(bslashflag == 1) /* if a backslash was seen before this character*/
{
*word = iochar; /* just add it into the word*/
*word++; /* as part of the string*/
counter++; /* and count it as a letter*/
bslashflag=0; /*reset flag for next check*/
}
*word++ = iochar; /*add it into word*/
counter++; /*and account for addition*/
return counter;
}
else if(iochar == '|') {
if(counter==0){
*word = iochar;
*word++;
counter++
return counter;
}
if(bslashflag == 1) {
*word = iochar;
*word++;
counter++;
int bslashflag = 0;
}
if(negateornah == 1) {
negateornah= 0; //reset flag for next run*/
counter *= -1; //negate the value in counter*/
return counter;
}
return counter;
}
else if(iochar == '&') {
if (counter ==0) {
*word++ = iochar;
*word = '\0';
counter++;
return counter;
}
if(bslashflag == 1) {
*word = iochar;
*word++;
counter++;
bslashflag = 0;
}
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter;
}
else if(iochar == '<' || iochar == '>') {
if(counter==0){
*word++ = iochar;
*word = '\0';
return counter;
}
if(bslashflag==1) {
*word = iochar;
*word++;
counter++;
bslashflag = 0;
}
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter;
}
else if (iochar == ' ') /* if space is encountered*/
{
if(bslashflag == 1){ /* if a backslash was seen before this*/
*word = iochar;
*word++;
counter++;
bslashflag = 0;
}
else if(counter == 0) /* as the first character */
{
while((iochar = getchar()) == ' ') { } /*keep going until*/
}
else { /* iochar is not a space*/
*word = '\0'; /* this block handles the word ending space*/
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /* by null terminating the word at the end*/
} /* and returning the count and printed word*/
}
*word++ = iochar; /* add iochar into the array if its an alphanum char*/
counter+=1; /* increment counter to reflect added char*/
}
*word = '\0'; /* if EOF is encountered*/
*first = '\0'; /* null terminate entire word, clearing it from the array*/
if(counter != 0){
if(negateornah == 1) {
negateornah= 0; /*reset flag for next run*/
counter *= -1; /*negate the value in counter*/
return counter;
}
return counter; /*ensure word continues in next run*/
}
return -1; /*and return -1 for EOF*/
}
iochar =='\'应该是iochar =='\\'。但还有其他问题。 –
'counter ++' - >'counter ++;' – BLUEPIXY
一次处理一个错误。或者,处理报告的第一个错误。看看下一个是否有意义。如果没有,也许这是第一个错误的后果,只是尝试重新编译。如果下一个错误有意义,请修复它,然后继续下一个错误等。重复,直到错误全部解决。 (然后返回并向编译器选项添加更多警告,并重复整个练习。)请不要显示错误消息的图片。在你的问题中包括他们作为'代码'。并且'getword.h'头文件是否自行编译?我们需要知道它里面有什么吗? –