帮助我的代码/ C / excersise。 [英] Help with my code / C / excersise.

查看:78
本文介绍了帮助我的代码/ C / excersise。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>
#include <math.h>



int main()
{

char ch;
int linecounter = 1;
int anpar = 0;
int klpar = 1;
int aneis = 0;
int kleis = 1;
int anbrack = 0;
int klbrack = 1;

do{
    ch = getchar();
    if (ch == '\n'){
      linecounter++;}

    if (ch == '('){
            anpar++;

      } else if (ch == ')'){
            anpar--;


      } else if (anpar < 0){
          printf("VRETHIKE/AN %d PARENTHESI/EIS,KLEINEI/OUN STH GRAMMI %d ALLA DEN EXEI/OUN ANOIKSEI", abs(anpar),linecounter);
        }



    if(ch == '<'){
            aneis++;

      } else if (ch == '>'){
            aneis--;

      }else if(aneis < 0){
          printf("VRETHIKE/AN %d EISAGOGIKO/A,KLEINEI/OUN STH GRAMMI %d ALLA DEN EXEI/OUN ANOIKSEI \n", abs(aneis),linecounter);

       }

    if(ch == '['){
            anbrack++;

      } else if (ch == ']'){
            anbrack--;

      }else if(anbrack < 0){
          printf("VRETHIKE/AN %d BRACKET/S,KLEINEI/OUN STH GRAMMI %d ALLA DEN EXEI/OUN ANOIKSEI \n", abs(anbrack),linecounter);

       }




} while (ch != EOF);

if (anpar + klpar == 1){
    printf("0 PARENTHESEIS ANOIXTES \n");
} else if (abs(anpar) + klpar >1){
    printf("%d PARENTHESEIS ANOIXTES \n", abs(anpar));
}

if (aneis + kleis == 1){
    printf("0 EISAGOGIKA ANOIXTA \n");
} else if (abs(aneis) + kleis >1){
    printf("%d EISAGOGIKA ANOIXTA \n", abs(aneis));
}

if (anbrack + klbrack == 1){
    printf("0 BRACKETS ANOIXTA \n");
} else if (abs(anbrack) + klbrack >1){
    printf("%d BRACKETS ANOIXTA \n", abs(anbrack));
}
    return 0;

}





或关于pastebin:http://pastebin.com/ZGC5RWbz



这是例外:写一个程序,它将从用户输入一些文本,它将找到(),<> ,[]正在关闭核心。如果程序发现问题,它将打印此消息:我们发现打开]或>或)但没有条目(或<或[...在行x和列y。

当输入结束时写入(),<>,[]的总和这是开放的。

ex:2对[]或()或<>仍然是开放的。

!没有数组或文件的代码。

!行和列的开头应该从1开始而不是0.

它是一个基本的C语言,所以不知道专业的方法或技巧等。



在上面的代码中,我没有包含问题所在的行,因为我无法找到办法。

上述代码的问题在于printf's永远像一个循环,而不是我想要的。

我正在努力想出这个例外,但似乎我找不到正确的方法来制作它。



任何帮助都很好。

先谢谢你的伙计..



or on pastebin : http://pastebin.com/ZGC5RWbz

Here is the excersise : Write a program which will take input from the user some text and it will finds if the () , <> , [] are closing corectlly. If the program find a problem it will print this message: we found an open ] or > or ) but without an entry ( or < or [ ..in the line x and column y.
When the input is over write the sum of the () , <> , [] which are open.
ex: 2 pairs of [] or () or <> are still open.
!do the code without arrays or files.
!the start of the lines and columns should start from 1 and not 0.
Its a basic C excersise so dont know professional ways or tricks etc.

In the code above i havent includes in which row the problem is cause i cant find a way to do it.
The problem of the above code is that the printf's are going forever like a loop and not as i wanted.
I am trying hard to figure this excersise out but it seems that i cant find the right way to make it.

ANY HELP IS GOOD.
Thanks in advance mates..

推荐答案

如果你的问题是循环永不中断,你需要开始看看休息条件。

使调试更容易的一种方法是删除所有不相关的问题代码并输入少量文本。



例如你可以这样开始

If your problem is that the loop never breaks, you need to start to look at the break condition.
One way to make it easier to debug is to remove all irrelevant code for the problem and enter a small amount of text.

For example you can start like this
char ch = '\0';
do
{
    ch = getchar();
    printf("%c", ch);
} while (ch != EOF);
printf("\r\nEnd of file\r\n");



如果你有这个部件工作,你可以继续添加越来越多的代码。



And再次:使用调试器。添加监视,检查变量并查看代码的大部分内容。



提示:您正在从键盘读取用户输入,而不是从文件读取。

如何从键盘输入EOF?



[更新]

确定多一点帮助然后。

1.初始化变量 int klpar = 1; 但你永远不会改变它的值。

什么是这个变量的意图是什么?

2. kleis klbrack 也是如此。

3.使用更易读的变量名并习惯使用英语。例如,如果你想在这里分享你的代码会有所帮助。

4.你应该改变这部分的逻辑


When you have this part working you can continue to add more and more code.

And again: Make use the debugger. Add watches, inspect variables and see what happens in very part of your code.

Hint: You are reading user input from the keyboard, not from a file.
How do you enter EOF from the keyboard?

[UPDATE]
OK a bit more help then.
1. You initialize the variable int klpar = 1; but you never change its value.
What is the intention of this variable?
2. The same goes for kleis and klbrack.
3. Use variable names that is more readable and get used to use English. It helps if you want to share your code here, for example.
4. You should probably change the logic of this part

if (ch == '('){
    anpar++;
 
    } else if (ch == ')'){
        anpar--;
 

    } else if (anpar < 0){
        printf("VRETHIKE/AN %d PARENTHESI/EIS,KLEINEI/OUN STH GRAMMI %d ALLA DEN EXEI/OUN ANOIKSEI", abs(anpar),linecounter);
}



to


to

if (ch == '(')
    anpar++;
else if (ch == ')')
    anpar--;
 
if (anpar < 0)
    printf("VRETHIKE/AN %d PARENTHESI/EIS,KLEINEI/OUN STH GRAMMI %d ALLA DEN EXEI/OUN ANOIKSEI", abs(anpar),linecounter);



5.为 aneis 和<$ c $做同样的事情c> anbrack

或者你可以这样做,然后进行反测试。


5. Do the same for aneis and anbrack
Or you can do it like this and do the counter test afterwards.

if (ch == '\n')
    linecounter++;
else if (ch == '(')
    anpar++;
else if (ch == ')')
    anpar--;
else if(ch == '<')
    aneis++;
else if (ch == '>')
    aneis--;
else if (ch == '[')
    anbrack++;
else if (ch == ']')
    anbrack--;
else
    ; // What to do here? Nothing? Anything?

if (anpar < 0)
    printf("VRETHIKE/AN %d PARENTHESI/EIS,KLEINEI/OUN STH GRAMMI %d ALLA DEN EXEI/OUN ANOIKSEI", abs(anpar),linecounter);
// Do the other tests here
// ...


这篇关于帮助我的代码/ C / excersise。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆