how can i optimize my code (C)

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 771 Views - Last Post: 29 June 2012 - 02:14 AM Rate Topic: -----

#1 Dragonstorm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 05-June 12

how can i optimize my code (C)

Posted 28 June 2012 - 01:48 PM

I'm in my 4th month of coding in C i have still lots to learn,i decided to make a simple calculator i have successfully done this in addition to two extra functions,yet i am unsure if it is as good as it can be,i am aware that using goto is not the best choice but i am unsure how to write the following using break; and continue;.
i have broke my code up into 4 files so that i can use them in other programs if needed.

MAIN.c
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<windows.h>
#include<time.h>
#include<dos.h>
#include<unistd.h>
//prototype functions FAILSAFE
void DRAWCOLLUMNS_F(void);
int  HYPOTENUSE_F(void);
int  HYPOTENUSER_F(void);
float BASIC_CALCULATOR_F(void);
void main()
{

    //char for choice selection
    char choice,menuopt;

START:


    DRAWCOLLUMNS_F();//draw box
    _flushall();
    choice=getchar();//ask for users choice

    switch(choice)
    {


    case'1':
        clearscreen();
        _flushall();
        HYPOTENUSE_F();
        goto EOS;

    case '2':
        clearscreen();
        _flushall();
        HYPOTENUSER_F();
        goto EOS;
    case '3':
        clearscreen();
        _flushall();
        BASIC_CALCULATOR_F();
        goto EOS;

    case'x':
        goto END;
    default:
     clearscreen();
        printf("Sorry that is an invalid selection\n");

    }
EOS:





        printf("would you like to return to the main menu?(y for yes any other key for no).\n");
        _flushall();
        menuopt=getchar();
        if(menuopt=='y'||menuopt=='Y')
        {
            clearscreen();
            goto START;
        }
        else
            goto END;

END:
    return 0;
}
void DRAWCOLLUMNS_F(void)
{
//ints for drawing the border
    int UL=201,UR=187,F=205,UPT=203,DWNT=202,DL=200,DR=188,VERT=186,LT=204,RT=185;
    //draw collumns
    printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",UL,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,UR);
    printf("%c    welcome to the Dragonstorm multi-function calculator         %c\n",VERT,VERT);
    printf("%c             Please type the number of your selection            %c\n",VERT,VERT);
    printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",LT,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,F,F,F,F,F,F,UPT,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,\
           F,F,F,F,F,F,F,UPT,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,RT);
    // printf("%c                                                                 %c\n",VERT,VERT);
    printf("%c Number to press:  %cHere Are Your Options:%cINFO:                 %c\n",VERT,VERT,VERT,VERT);
    printf("%c1)                 %cHypotenuse calculator %cinsert side a&b legnth%c\n",VERT,VERT,VERT,VERT);
    printf("%c2)                 %cReverse hypot. calc.  %cinsert hypot. &1 side %c\n",VERT,VERT,VERT,VERT);
    printf("%c3)                 %cBasic calculator      %cinsert # and operation%c\n",VERT,VERT,VERT,VERT);
    printf("%c                   %c                      %c                      %c\n",VERT,VERT,VERT,VERT);
    printf("%c   Press  x and  - %c -enter to quit       %c                      %c\n",VERT,VERT,VERT,VERT);
    printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",DL,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,F,F,F,F,F,F,DWNT,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,DWNT,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,DR);
}
void clearscreen()
{
    if (system( "clear" )) system( "cls" );
}



hypotenuse function.c



float HYPOTENUSE_F()
{
float sideC,sideA,sideB,temp;//declare variables used for equation(the theory of pythag.(a+b=c)
char A[2000],B[2000];
int choice;//for menu
STARTHE:
printf("Please do not add cm or m extensions only the number itself.\n\n");
printf("Please give the legnth of a and then press enter.\n");
_flushall();
gets(A);

printf("\nPlease give the legnth of b and then press enter.\n");
_flushall();
gets(B)/>;
sideA=atof(A);
sideB=atof(B)/>;
sideA=(sideA*sideA);
sideB=(sideB*sideB);
temp=sideA+sideB;
sideC=sqrt(temp);
printf("The legnth of the hypotenuse is %f\n",sideC);
printf("\nWould you like to perform the same action with different numbers?(y/n)\n");
 _flushall();
    choice=getchar();
switch (choice)
{


case'y':
clearscreen();
goto STARTHE;
default:
clearscreen();
}

return 0;


}




Hypotenuse reverse function.c




float HYPOTENUSER_F()
{
float continue2;//declare continuation variable
float sideC,sideA,sideB;//declare variables used for equation(the theory of pythag.(a+b=c)
char C[2000],B[2000];
int choice;//for menu
STARTHE:
printf("Please do not add cm or m extensions only the number itself.\n\n");
printf("Please give the legnth of the hypotenuse.\n");
_flushall();
gets(C);


printf("\nPlease give the legnth of the known side.\n");
_flushall();
gets(B)/>;


sideC=atof(C);
sideB=atof(B)/>;
sideC=(sideC*sideC);
sideB=(sideB*sideB);
sideA=sideC-sideB;
sideA=sqrt(sideA);

printf("The legnth of the side is %f\n",sideA);
printf("\nWould you like to perform the same action with different numbers?(y/n)\n");
 _flushall();
    choice=getchar();
switch (choice)
{


case'y':
clearscreen();
goto STARTHE;
default:
clearscreen();
}

return 0;


}



normal calculator.c


float BASIC_CALCULATOR_F(void)
{

    char op,choice,A[2000],B[2000];
    int operation;
    float answer;
    float A2,B2;
STARTS:
    A2=0;
    B2=0;
    printf("Please enter the first number\n\n");
    _flushall();
    gets(A);

    printf("Please enter operation\n");
    _flushall();
    operation=getchar();

    printf("Please enter the second number\n");
    _flushall();
    gets(B)/>;


    A2=atof(A);
    B2=atof(B)/>;

    switch(operation)
    {


    case'+':
        answer=A2+B2;

        goto PANS;

    case '-':

        answer=A2-B2;
        goto PANS;
    case 'x':
        answer=A2*B2;
        goto PANS;

    case'*':
        answer=A2*B2;
        goto PANS;
    case'^':
        answer=pow(A2,B2);
        goto PANS;
    default:
        printf("Sorry that is an invalid selection\n");

    }
    PANS:
    printf("the answer is %f\n",answer);
printf("\nWould you like to perform the same action with different numbers?(y/n)\n");
 _flushall();
    choice=getchar();
switch (choice)
{

case'y':
clearscreen();
goto STARTS;
case'Y':
clearscreen();
goto STARTS;
default:
clearscreen();
}

}


...and that's it,can anyone help me to improve and optimize this script?,thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: how can i optimize my code (C)

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: how can i optimize my code (C)

Posted 28 June 2012 - 02:06 PM

So, so many problems you need to fix before you even CONSIDER optimization.

1. Forget you ever heard about goto, at least until you've been programming in C for quite a while.
2. Do NOT use gets().
3. Indentation; pick a style and use it.
4. _flushall is non-standard, and therefore may result in undefined behavior.
5. It's int main(), not void main().
6. Don't use ALL CAPS except for constants.
7. What the hell is this?
if (system( "clear" )) system( "cls" );

Was This Post Helpful? 3
  • +
  • -

#3 Dragonstorm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 05-June 12

Re: how can i optimize my code (C)

Posted 28 June 2012 - 02:18 PM

i know about gets() and _flushall i used _flushall to flush the input stream,yes i know it is not the best thats why im looking for alternatives,for some reason if i dont use gets() it does not work properly and
if (system( "clear" )) system( "cls" );


is to clear the screen,there are 2 ways one for unix-like systems and one for windows,doing it this way will allow it to compile under linux,etc but i can just call ( i think) system("clear"),but i am unsure, as for indentation i copied from different source files and some have different styles, i am using code::blocks 10.05 ,so i just select all , right click and select the style (Astyle)
and i used void main() because i was not returning any value in main().So when i said optimize i really meant improve,anyway thank you for the reply.
Was This Post Helpful? 0
  • +
  • -

#4 Dragonstorm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 05-June 12

Re: how can i optimize my code (C)

Posted 28 June 2012 - 02:42 PM

can anyone help improve it,as in using better functions,looping in different ways,etc.I am now going to rewrite my code using fgets() as i have found through the internet is a better idea,and have also found that main() is an int function and must be called as int main(); sorry i did not really understand why not to call it as void main() but the link that you have provided did help,Thank you JackOfAllTrades,and on a side note when i said " i copied from different source files" i was referring
to the code that i have written. :yes:
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,461
  • Joined: 05-May 12

Re: how can i optimize my code (C)

Posted 28 June 2012 - 04:06 PM

View PostDragonstorm, on 28 June 2012 - 02:18 PM, said:

if (system( "clear" )) system( "cls" );


is to clear the screen,there are 2 ways one for unix-like systems and one for windows,doing it this way will allow it to compile under linux,etc but i can just call ( i think) system("clear"),but i am unsure


If you want it to compile on Linux systems, then you shouldn't be including <windows.h> or <dos.h>.

Additionally, all your ASCII character drawing may or may not work depending on what terminal or terminal emulator is running.

This post has been edited by Skydiver: 28 June 2012 - 04:10 PM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,461
  • Joined: 05-May 12

Re: how can i optimize my code (C)

Posted 28 June 2012 - 04:31 PM

Follow JackOfAllTrades's advice to not use goto. Use a do-while or while loop instead.

Additionally, refactor out some of the places where you have common prompting needs.

Something like:
do
{

    // Do other program stuff here

} while (PromptUser("Do you want to run again?", "YN") == 'Y');

:
:

char PromptUser(char * prompt, char *valid)
{
    char ch;
    do
    {
        printf("%s [%s]", prompt, valid);
        ch = input_character_with_function_of_your_choice();
    } while(!strchr(valid, ch));
    return ch;
}


Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: how can i optimize my code (C)

Posted 28 June 2012 - 04:40 PM

Quote

is to clear the screen,there are 2 ways one for unix-like systems and one for windows,doing it this way will allow it to compile under linux,etc but i can just call ( i think) system("clear"),but i am unsure, as for indentation i copied from different source files and some have different styles, i am using code::blocks 10.05 ,so i just select all , right click and select the style (Astyle)

Things like this are usually better done using preprocessor commands, rather than trying to determine the platform type during runtime.

i.e.
#include <iostream>

#ifdef __GNUC__  
#define CLEAR system("clear") //linux
#else
#define CLEAR system("cls") //others (windows in your case)
#endif

int main() {
   std::cout<<"some text";
   CLEAR; //clear text
   return 0;
}


Was This Post Helpful? 1
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: how can i optimize my code (C)

Posted 28 June 2012 - 04:40 PM

View PostSkydiver, on 28 June 2012 - 06:06 PM, said:

View PostDragonstorm, on 28 June 2012 - 02:18 PM, said:

if (system( "clear" )) system( "cls" );


is to clear the screen,there are 2 ways one for unix-like systems and one for windows,doing it this way will allow it to compile under linux,etc but i can just call ( i think) system("clear"),but i am unsure


If you want it to compile on Linux systems, then you shouldn't be including <windows.h> or <dos.h>.

Additionally, all your ASCII character drawing may or may not work depending on what terminal or terminal emulator is running.

This is correct, the OP should look into "conditional compilation" using the macro preprocessor so they can figure out which system the program is running on and include files that are needed by that system.
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • Joined: 19-February 09

Re: how can i optimize my code (C)

Posted 28 June 2012 - 04:52 PM

Instead of this to draw columns :

    //draw collumns
    printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",UL,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,UR);



Using a for loop :

  int i;

  //draw columns
  printf("%c", UL);
  for(i=0; i<65; i++)
    printf("%c", F);
  printf("%c\n", UR);




For the conditional compilation something like :


Spoiler

Was This Post Helpful? 0
  • +
  • -

#10 Dragonstorm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 05-June 12

Re: how can i optimize my code (C)

Posted 29 June 2012 - 01:00 AM

Thank you for the replies,i know that this cannot compile under linux,but the specific line of code:
1	if (system( "clear" )) system( "cls" );

i got from the net,and it works ,when i use system("clear") it does nothing. :oops: :oops:
Was This Post Helpful? 0
  • +
  • -

#11 Dragonstorm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 05-June 12

Re: how can i optimize my code (C)

Posted 29 June 2012 - 01:08 AM

Great,now im going to rewrite my code with the given help,but i have run into a snag,how will i go about accepting multiple numbers and operations?
i was thinking maybe a loop,but in reallity i have no idea.
Was This Post Helpful? 0
  • +
  • -

#12 Dragonstorm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 05-June 12

Re: how can i optimize my code (C)

Posted 29 June 2012 - 01:14 AM

ok...after replacing void main() with int main,NON of the calculations made return the correct value,and yes my function main is returning 0,
as i have inserted
return 0;
is there anyone who knows how to fix this?
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,461
  • Joined: 05-May 12

Re: how can i optimize my code (C)

Posted 29 June 2012 - 01:17 AM

Show us your new code. The changes you described in post #12 should not have affected how you program runs.
Was This Post Helpful? 0
  • +
  • -

#14 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: how can i optimize my code (C)

Posted 29 June 2012 - 01:23 AM

Please post your current code. And yeah, use loops for anything that has to be done many times, be it calculation or taking input or cursing your teacher/boss. :)

This post has been edited by aresh: 29 June 2012 - 01:24 AM

Was This Post Helpful? 0
  • +
  • -

#15 Dragonstorm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 05-June 12

Re: how can i optimize my code (C)

Posted 29 June 2012 - 01:36 AM

ok,sorry for the late reply my inernet was down:(,
i have not been able to change gets(A) to fgets(A,sizeof(A),stdin)
as it gives me an error saying"stdin not declared in ths function"
heres my code
MAIN.c
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<windows.h>
#include<time.h>
#include<dos.h>
#include<unistd.h>
//prototype functions FAILSAFE
void DRAWCOLLUMNS_F(void);
float  HYPOTENUSE_F(void);
float  HYPOTENUSER_F(void);
float BASIC_CALCULATOR_F(void);
int main()
{

    //char for choice selection
    char choice,menuopt;

START:


    DRAWCOLLUMNS_F();//draw box
    _flushall();
    choice=getchar();//ask for users choice

    switch(choice)
    {


    case'1':
        clearscreen();
        _flushall();
        HYPOTENUSE_F();
        goto EOS;

    case '2':
        clearscreen();
        _flushall();
        HYPOTENUSER_F();
        goto EOS;
    case '3':
        clearscreen();
        _flushall();
        BASIC_CALCULATOR_F();
        goto EOS;

    case'x':
        goto END;
    default:
     clearscreen();
        printf("Sorry that is an invalid selection\n");

    }
EOS:





        printf("would you like to return to the main menu?(y for yes any other key for no).\n");
        _flushall();
        menuopt=getchar();
        if(menuopt=='y'||menuopt=='Y')
        {
            clearscreen();
            goto START;
        }

END:
    return 0;
}
void DRAWCOLLUMNS_F(void)
{
//ints for drawing the border
    int UL=201,UR=187,F=205,UPT=203,DWNT=202,DL=200,DR=188,VERT=186,LT=204,RT=185;
    //draw collumns
    printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",UL,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,UR);
    printf("%c    welcome to the Dragonstorm multi-function calculator         %c\n",VERT,VERT);
    printf("%c             Please type the number of your selection            %c\n",VERT,VERT);
    printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",LT,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,F,F,F,F,F,F,UPT,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,\
           F,F,F,F,F,F,F,UPT,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,RT);
    // printf("%c                                                                 %c\n",VERT,VERT);
    printf("%c Number to press:  %cHere Are Your Options:%cINFO:                 %c\n",VERT,VERT,VERT,VERT);
    printf("%c1)                 %cHypotenuse calculator %cinsert side a&b legnth%c\n",VERT,VERT,VERT,VERT);
    printf("%c2)                 %cReverse hypot. calc.  %cinsert hypot. &1 side %c\n",VERT,VERT,VERT,VERT);
    printf("%c3)                 %cBasic calculator      %cinsert # and operation%c\n",VERT,VERT,VERT,VERT);
    printf("%c                   %c                      %c                      %c\n",VERT,VERT,VERT,VERT);
    printf("%c   Press  x and  - %c -enter to quit       %c                      %c\n",VERT,VERT,VERT,VERT);
    printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",DL,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,F,F,F,F,F,F,DWNT,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F\
           ,F,F,F,F,F,F,F,DWNT,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,DR);
}
void clearscreen()
{
    if (system( "clear" )) system( "cls" );
}



hypotenuse function.c
float HYPOTENUSE_F()
{
float sideC,sideA,sideB,temp;//declare variables used for equation(the theory of pythag.(a+b=c)
char A[2000],B[2000];
int choice;//for menu
STARTHE:
printf("Please do not add cm or m extensions only the number itself.\n\n");
printf("Please give the legnth of a and then press enter.\n");
_flushall();
gets(A);

printf("\nPlease give the legnth of b and then press enter.\n");
_flushall();
gets(B)/>;   //fgets(B, sizeof(B)/>, stdin);
sideA=atof(A);
sideB=atof(B)/>;
sideA=(sideA*sideA);
sideB=(sideB*sideB);
temp=sideA+sideB;
sideC=sqrt(temp);
printf("The legnth of the hypotenuse is %f\n",sideC);
printf("\nWould you like to perform the same action with different numbers?(y/n)\n");
 _flushall();
choice=getchar();
switch (choice)
{


case'y':
clearscreen();
goto STARTHE;
default:
clearscreen();
}

return 0;


}


Hypotenuse reverse function.c

float HYPOTENUSER_F()
{
float continue2;//declare continuation variable
float sideC,sideA,sideB;//declare variables used for equation(the theory of pythag.(a+b=c)
char C[2000],B[2000];
int choice;//for menu
STARTHE:
printf("Please do not add cm or m extensions only the number itself.\n\n");
printf("Please give the legnth of the hypotenuse.\n");
_flushall();
gets(C);


printf("\nPlease give the legnth of the known side.\n");
_flushall();
gets(B)/>;


sideC=atof(C);
sideB=atof(B)/>;
sideC=(sideC*sideC);
sideB=(sideB*sideB);
sideA=sideC-sideB;
sideA=sqrt(sideA);

printf("The legnth of the side is %f\n",sideA);
printf("\nWould you like to perform the same action with different numbers?(y/n)\n");
 _flushall();
    choice=getchar();
switch (choice)
{


case'y':
clearscreen();
goto STARTHE;
default:
clearscreen();
}

return 0;


}


normal calculatorF.c

void BASIC_CALCULATOR_F(void)
{

    char op,choice,A[2000],B[2000];
    int operation;
    float answer;
    float A2,B2;
STARTS:
    A2=0;
    B2=0;
    printf("Please enter the first number\n\n");
    _flushall();
    gets(A);

    printf("Please enter operation\n");
    _flushall();
    operation=getchar();

    printf("Please enter the second number\n");
    _flushall();
    gets(B)/>;


    A2=atof(A);
    B2=atof(B)/>;

    switch(operation)
    {


    case'+':
        answer=A2+B2;

        goto PANS;

    case '-':

        answer=A2-B2;
        goto PANS;
    case 'x':
        answer=A2*B2;
        goto PANS;

    case'*':
        answer=A2*B2;
        goto PANS;
    case'^':
        answer=pow(A2,B2);
        goto PANS;
    default:
        printf("Sorry that is an invalid selection\n");

    }
    PANS:
    printf("the answer is %f\n",answer);
printf("\nWould you like to perform the same action with different numbers?(y/n)\n");
 _flushall();
    choice=getchar();
switch (choice)
{

case'y':
clearscreen();
goto STARTS;
case'Y':
clearscreen();
goto STARTS;
default:
clearscreen();
}
return 0;
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2