开发者

how to make this program efficient in c? [closed]

开发者 https://www.devze.com 2023-03-18 06:36 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

here is the code

void main()
{
    int x,y,i=0,o,p;


    printf("enter the command");
    scanf("%s",dir);
    printf("enter the limit");
    scanf("%d%d",&o,&p);
    printf("enter the place");
    scanf("%d%d",&x,&y);
    clrscr();
    if(((x>0)&&(x<o))&&((y>0)&&(y<p)))
    {
        while(dir[i]!='\0')
        {
            for(i=0;i<strlen(dir);i++)
            {
                if(dir[i]=='l')
                {
                    if(a=='n')
                    a='w';
                    else if(a=='w')
                    a='s';
                    else if(a=='s')
                    a='e';
                    else
                    a='n';
                }
                else if(dir[i]=='r')
                {
                    if(a=='n')
                    a='e';
                    else if(a=='e')
                    a='s';
                    else if(a=='s')
                    a='w';
                    else
                    a='n';
                }

            }
        }
        printf("%d %d %c",x,开发者_运维知识库y,a);
    }
    else printf("out of area");
    getch();
}

how to make the above program efficient using some oops concept in c or by some way?


You should try to improve the program stepwise, and as it's written in C making it OOPSy is probably not the highest priority. Eg start out by improving the selection logic by introducing a switch() statement:

switch (dir[i]) {
    case 'l':
        ...
        break;
    case 'r':
        ...
        break;
    default:
        ....
        break;
}

You can also replace the inner if-chain with a switch, but that would get pretty messy. So, break that inner construction out in a function.

char newa (char olda) {
    if(olda=='n') return 'w';
    if(olda=='w') return 's';
    if(olda=='s') return 'e';
    return 'n';
}

See? Easier on the eye even with plain if's :-) That way the inner block becomes

a = newa(a);

Bonus points if you figure out how to fold the 3 very similar but not entirely identical inner blocks into 1 function!

And so on. Just keep on improving step-wise, making your program more readable at every iteration.

Ah yes, and while you're at it, put some useful comments here and there, especially comments that will help you figure out in 3 months what the &!@%#& you tried to achieve with a certain function :-)

Edit: according to Wikipedia indentation is

The placement of text farther to the right to separate it from surrounding text.

And I'm incredibly puzzled that this concept seems foreign to you.


One that's small enough that it's often easy to miss is:

for(i=0;i<strlen(dir);i++)

The compiler doesn't (at least normally) have any way of knowing that strlen will return the same result from every call, so it'll end up being called on every iteration of the loop. To prevent that, you want to do something like:

size_t len = strlen(dir);
for (i=0; i<len; i++) /* ... */

You're currently doing the conversion of (apparently) directions using cascaded if statements. I'd probably build a small table, and just do a table lookup:

char results[2]['z'-'a'];

results[0]['n'-'a'] = 'w';
results[0]['w'-'a'] = 's';
results[0]['s'-'a'] = 'e';
results[0]['e'-'a'] = 'n';
results[1]['n'-'a'] = 'e';
results[1]['e'-'a'] = 's';
results[1]['s'-'a'] = 'w';
results[1]['w'-'a'] = 'n';

a = results[dir[i]=='r'][a-'a'];
0

精彩评论

暂无评论...
验证码 换一张
取 消