PDA

View Full Version : Programmers out there?


Crispy Critters
10-11-2006, 04:17 PM
I don't even remember the last time I posted here, been so busy. I'm taking a C programming class and I was wondering if anyone here could give me some tips or advice on what I've made. It's a simple crappy little calculator basically.


#include <stdio.h>

int main ()
{

char opt;
float num1;
float num2;
float num3;
float ans;

printf("\n\n\n\n\n== What operation would you like to perform? == \n\n");
printf(" A = Add three numbers \n");
printf(" S = Subtract two numbers \n");
printf(" M = Multiply three numbers \n");
printf(" D = Divide two numbers \n");

printf(" E = Exit program\n\n\n \n");
printf("\n\n\n\n\n\nNOTE: Please use capital letters");
printf("\n\n\n\n:");
scanf("%c",&opt);


if (opt == 'A') /* Performs addition of three numbers */
{
printf("\n\n\n\n== You chose option 'A', add three numbers ==");
printf("\n\nPlease enter the first number: ");
scanf("%f" , &num1);
printf("\nPlease enter the second number: ");
scanf("%f" , &num2);
printf("\nPlease enter the third number: ");
scanf("%f" , &num3);

ans = num1 + num2 + num3;

printf("Answer: %.01f\n " , ans);
}

else if (opt == 'S') /* Performs subtraction of two numbers */

{
printf("\n\n\n\n== You chose option 'S', subtract two numbers ==");
printf("\n\nPlease enter the first number: \n");
scanf("%f" , &num1);
printf("\nPlease enter the second number: ");
scanf("%f" , &num2);

ans = num1 - num2;

printf("Answer: %.01f\n " , ans);
}

else if (opt == 'M') /* Performs multiplication of three numbers */

{
printf("\n\n\n\n== You have chosen option 'M', multiply three numbers ==");
printf("\n\nPlease enter the first number: ");
scanf("%f" , &num1);
printf("\nPlease enter the second number: ");
scanf("%f" , &num2);
printf("\nPlease enter the third number: ");
scanf("%f" , &num3);

ans = num1 * num2 * num3;

printf("Answer: %.01f\n" , ans);
}
else if (opt == 'D') /* Performs division of two numbers */

{
printf("\n\n\n\n== You have chosen option 'D', divide two numbers ==");
printf("\n\nPlease enter the first number: ");
scanf("%f" , &num1);
printf("\nPlease enter the second number: ");
scanf("%f" , &num2);

ans = num1 / num2;

printf("Answer: %.01f\n" , ans);
}


return 0;
}

Orochi Avlis
10-11-2006, 05:11 PM
Switch cases and sub functions make it much neater.
And maybe a Do While to keep the program looping until the user decides to exit. :)

Crispy Critters
10-11-2006, 08:53 PM
I started out using Switch Cases, but I was getting frustrated with the syntax errors, so I just stuck with what I knew. I would've read up on it more, but I only had about a half hour to get everything done and scripted.

Do While? My class hasn't gone over this yet, we're only up to chapter 4 in the book we're reading (A First Book of ANSI C; 4th Edition). I looked in the book real quick, and I'm assuming that it will just bring the main menu back up once a calculation has been completed.

Eugenides
10-11-2006, 10:38 PM
do while is basicly a loop with some conditions

Jiminator
10-11-2006, 11:06 PM
wow, i hate programs like that. make a real calculator that take expressions like 3 + 3 *5 - 1 and then print 17.

It'd be a somewhat complex little project making the recursive expression parser, but it would beat the hell out of what you are doing now....

DudeMiester
10-11-2006, 11:46 PM
Better yet, learn something more useful then just syntax and write it using SSE instructions, lol.

phreak
10-12-2006, 12:04 AM
Comments:

You call printf redundantely. You can call it just once for the initial message and still have readable code, like so:

printf("\n\n\n\n\n== What operation would you like to perform? == \n\n"
"A = Add three numbers \n"
"S = Subtract two numbers \n"
"M = Multiply three numbers \n"
"D = Divide two numbers \n"

"E = Exit program\n\n\n \n"

"\n\n\n\n\n\nNOTE: Please use capital letters"
"\n\n\n\n:");


The reason this works is that strings in C are automatically concatanated. As in "blah1" "blah2" is the same as "blah1blah2". Also, 99% compilers ignore whitespace, so you can add new lines as you wish. But you still have to have complete statements(meaning closed quotes on each line).

Here's some info on swich case. First, switch works with integer values only. That is, char, int, long int, short int, signed, unsinged, etc, but not strings, floats or doubles.

the syntax is

switch(opt){
case('A'): /* Performs addition of three numbers */
{
...//code i'm too lazy to paste
break;
}
case('S'): /* Performs addition of three numbers */
{
...//code i'm too lazy to paste
break;
}
...
//etc
}

switch(variable){} says test that variable
case(value): {} says if variable=value, then do following code
break; says to break OUT of the switch. If you don't do the break; then after doing all the stuff in case('A') the program will proceed to directly executing the code in case('S') without even testing whether variable=value.

Also, you can do some optimization, you can reduce the number of variables. Essentially, for all operations you can use 2 variable, ans and temp.

Example of how to do it would be

printf("\n\n\n\n== You chose option 'A', add three numbers ==");
printf("\n\nPlease enter the first number: ");
scanf("%f" , &test);
ans=test; //Let ans be the first number
printf("\nPlease enter the second number: ");
scanf("%f" , &test);
ans=ans+test; // Add to ans(the first number) the second number
printf("\nPlease enter the third number: ");
scanf("%f" , &test);

ans = ans + num3 //Add to and(the first number+second number) the third number

printf("Answer: %.01f\n " , ans);


Those are my 2 cents so far. An advice I can give you if you seriously want to learn C and know C after this course is to find yourself the book "The C Programming Language, Second Edition" That is the best book on C in existence and if you work with it, you'd come out of it knowing the language to its core. (it's written by the creator of the language after all). It is very well-written, and also serves as a great reference book. I can honestly say I learned C completely just by reading it straight for 8 hours. It filled in all the gaps I had and definitely gave me a solid understanding of the language.

Also, a slightly more positive rewrite of jiminathare's comment(come on jiminathare, be respectful and supportive of the beginners willing to learn). Never, NEVER be afraid of making your programs better, faster, richer. Be willing to ask questions no how things are done, be willing to search for info on the internet, in the "The C Programming Language, Second Edition". Programming is wonderfully rich field, that has the benefit of instant gratification, and is just plain fun.

P.S. Have you covered pointers already? You seem to be using them frivously in scanf(). I'd be amazed if you had.

P.P.S. Continuing the helping comments, you should just drop C completely and do everything in assembly. You shouldn't even have an OS.:doh: :cool: ;) :love: :dopefish:

Orochi Avlis
10-12-2006, 04:17 PM
I looked in the book real quick, and I'm assuming that it will just bring the main menu back up once a calculation has been completed.
Right. It executes first, then checks a certain condition set by you.
If it doesn't match it, it repeats until it does.