修复我无法在 C 中的 main() 中发现的错误(停留数小时 - 短程序)

fixing an error i cannot spot in main() in C (stuck for hours on it - short program)

我的代码真的很难过,我的截止日期是今天。我得到一个错误“main.c:在函数‘main’中: main.c:186:7: 错误:输入末尾需要声明或语句 }”。 我已经尝试了好几个小时来玩支架并修复它,但没有运气。我希望你能帮我解决它,因为你比我更有经验。

它基本上是一个简单的程序,从标准输入(可能来自键盘或使用重定向的文件)获取输入并应用以下内容:

1) 在句子之间换行。 2)不打印数字。 3)如果在括号内,则字母必须大写(粗体)。 4)将大写字母放在句子的第一个字符上。 5)如果不在括号中,也不在句首,则应将其设为小写。

备注: a) 输入的长度没有限制,每个句子可以写在几行(输入)上。 b)如果一个点(.)在括号内,它不会使它成为一个新句子(不需要写一个换行符)。 c)如果给两个点,就是空句,应该像例子那样写。

基本上,我只是请你帮我修复我的代码,所以它会 运行,因为我已经完成了所有这些(想到我错过了一些东西,你可以帮助我改进它 - 我会很高兴的!)

示例:

如果给定输入:

我喜欢打曲棍球..我需要打曲棍球.."hockey is life 3333".

所需的输出将是:

我喜欢打曲棍球。

。我需要打曲棍球。

.

"HOCKEY IS LIFE"

"temp.h"中的代码是:

#define go 0
#define endOfASentence 1
#define isAFirstQuotation 2
#define isASecondQuotation 3
#define inAQuotation 4
#define beginningOfSentence 5
#define middleOfSentence 6

主程序中的代码是:

#include <stdio.h>
#include <ctype.h>
#include "letters.h"

int checkIfLetter (char a); /*checking if char is a letter */
int checkIfnumber (char a); /*checking if char is a number */
int checkIfUpperCase (char a);  /*checking if char is upper case */
int checkIfLowerCase (char a);  /*checking if char is lower case */
int checkIfQuotation (char a);  /*check if char is a quotation */
int checkIfDot (char a);    /*check if char is a dot */





int
main (int argc, const char *argv[])
{
  int status = go;
  char a;
  int beginning = 0;        /*if beginning equals 0 it's the beginning of a sentence, if it's 1 then it's the middle of it */
  int secondQuote = 0;      /*if second quote equals 1, then it's inside of a quotation */
  while ((a = getchar ()) != EOF)
    {
      switch (status)
    {
    case go:
      if (a == '.')
        {
          status = endOfASentence;
        }
      else if (a == '"' && secondQuote == '0')
        {
          status = isAFirstQuotation;
        }
      else if (a == '"' && secondQuote == '1')
        {
          status = isASecondQuotation;
        }
      else if (checkIfLetter (a) == '1' && secondQuote == '1')
        {
          status = inAQuotation;
        }
      else if (checkIfnumber (a) == '1')
        {
          continue;
        }           /*a number should not be on the output, so we just ignore it and not using it */
      else if (checkIfLetter (a) == '1' && beginning == '0')
        {
          status = beginningOfSentence;
        }           /*i tried to differentiate between beginning and middle of the sentence using int beginning */
      else if (checkIfLetter (a) == '1' && beginning == '1')
        {
          status = middleOfSentence;
        }
    case beginningOfSentence:
      if (checkIfQuotation (a) && checkIfDot (a)
         && checkIfnumber (a) != 1)
        {
          if (checkIfUpperCase (a) == '1')
        {
          printf ("%c", toupper (a));
          beginning = 1;
          status = go;
        }
        } break;            /*set to upper and return to go */
    case middleOfSentence:
      if (checkIfQuotation (a) && checkIfDot (a)
         && checkIfnumber (a) != 1)
        {
          if (checkIfLowerCase (a) == '1')
          {
            printf ("%c", tolower (a));
            status = go;
          }
        } break;
    case endOfASentence:
          if (checkIfDot (a) == '1')
        {
          printf ("%c/n", a);
          beginning = 0;
          status = go;
        }break;     /*i tried to append(add) a new line after the dot and to set beginning to 0, to signify that after it's a beginning of a sentence */
    case isAFirstQuotation: /*if it's a quotation, continue to the next char and make it upper case as long as it's a lower case, until you get another quotation */
          while (checkIfLowerCase (a) == '1')
        {
          secondQuote == '1';
          status = go;
        }break;
    case isASecondQuotation:
          if (checkIfQuotation (a) == '1' && secondQuote == '1')
        {
          secondQuote = 0;
          status = go;
        }break;
    case inAQuotation:
          if (secondQuote == '1' && checkIfLetter (a) == '1')
        {
          printf ("%c", toupper (a));
          status = go;
        } break;
    }
    }
      return 0;
    }



  int checkIfLetter (char a)
  {
    if (isalpha (a))
      {
    return 1;
      }
    else
      {
    return 0;
      }
  }

    int checkIfnumber (char a)
    {
      if (isdigit (a))
    {
      return 1;
    }
      else
    {
      return 0;
    }
    }

    int checkIfUpperCase (char a)
    {
      if (checkIfLetter (a) == '1')
    {
      if (isupper (a))
        {
          return 1;
        }
      else
        {
          return 0;
        }
    }
    }

  int checkIfLowerCase (char a)
  {
    if (checkIfLetter (a) == '1')
      {
    if (islower (a))
      {
        return 1;
      }
    else
      {
        return 0;
      }
      }
  }

  int checkIfQuotation (char a)
  {
    if (a == '"')
      {
    return 1;
      }
    else
      {
    return 0;
      }
  }

  int checkIfDot (char a)
  {
    if (a == '.')
      {
    return 1;
      }
    else
      {
    return 0;
      }
  }  

我不知道如何修复它,我已经花了好几个小时。如果您能提供帮助,将不胜感激。

我努力做到非常详尽并遵守规则

您可以尝试一下,看看它是否产生了您想要的结果。
不是字母、space、换行符或点的字符在 while 的顶部被拒绝,所有字母都设置为小写。
然后选择是在句子的开头打印一个大写字母还是在双引号内打印所有大写字母。
没有中断,因为 oneUpper 需要下降到 allUpper。 allUpper 需要下降到默认值。
getchar returns 一个 int 所以 int a 而不是 char a

#include <stdio.h>
#include <ctype.h>

#define oneUpper 1
#define allUpper 2

int main (int argc, const char *argv[])
{
    int status = oneUpper;
    int a;
    while ( EOF != (a = getchar ()))
    {
        //discard non letter except space, newline and .
        if ( !isalpha ( a) && a != ' ' && a != '\"' && a != '.') {
            continue;
        }
        //set all to lower and let oneUpper or allUpper do the rest.
        a = tolower ( a);
        switch (status)
        {
            case oneUpper:
                if ( a == ' ' || a == '\n') {
                    putchar ( a);//print leading space and continue
                    continue;
                }
            case allUpper:
                a = toupper ( a);
            default:
                putchar ( a);
                if ( a == '\"') {
                    if ( status == allUpper) {
                        status = 0;//turn all upper off
                    }
                    else {
                        status = allUpper;//turn all upper on
                    }
                }
                if ( status == oneUpper) {
                    status = 0;//after printing one upper turn it off
                }
                if ( a == '.') {
                    if ( status != allUpper) {
                        putchar ( '\n');
                        status = oneUpper;//after a . turn one upper on
                    }
                }
        }
    }
    return 0;
}