修复一个错误,我无法在C中的main()中发现(卡住了几个小时 - 短程序)

时间:2017-11-12 12:12:06

标签: c stdin

我的代码真的很难过,我的截止日期是今天。我给出了一个错误" main.c:在函数'main'中: main.c:186:7:错误:输入结束时的预期声明或语句        }&#34 ;. 我已经花了好几个小时玩括号并修复它但没有运气。我希望你能帮助我修复它,因为你比我经验丰富。

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

1)在句子之间添加一条新线。 2)不打印数字。 3)如果在括号内,则字母必须大写(粗体)。 4)在句子的第一个字符上加上大写字母。 5)如果不在括号中或在句子的开头,则应将其设为小写。

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

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

示例:

如果给定输入:

我喜欢打曲棍球..我需要玩HOCKEY ......"曲棍球是生命3333"。

所需的输出将是:

我喜欢打曲棍球。

。我需要打曲棍球。

" HOCKEY是生命"

" 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;
      }
  }  

我不知道如何解决这个问题,而且我已经花了好几个小时。如果你能提供帮助,我将非常感激。

我试图非常精细并遵守规则

1 个答案:

答案 0 :(得分:1)

您可以尝试这样做以查看它是否产生了预期效果 不是字母,空格,换行符或点的字符在while的顶部被拒绝,所有字母都设置为小写字母。
然后选择是在句子开头打印一个大写字母,或在双引号内打印所有大写字母 没有休息,因为o​​neUpper需要通过allUpper。 allUpper需要进入默认状态 getchar返回一个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;
}