[數隻]WorldV
作業五。
大家隨便抄沒關係,因為我不是要交這一篇。
/************************************\
* *
* by Seraph *
* 2005 Autumn *
* All rights forsaken. *
* *
\************************************/
#include<stdio.h>
// ↙Thou shalt declare a pointer with a sign *.
void scan_data(char *, float *);
float do_next_op(char, float, float);
/*>>>>>>>>>>>>>>>>>>> main <<<<<<<<<<<<<<<<<*/
int main(void)
{
char s_operator = '\0'; //operators such as '+', '-', '*', '/', 'q'
float f_operand = 0, //value to operate
f_accumulator = 0; //accumulated result
//------> Nothing but an example... <-------//
printf("Example :\n");
printf(" + 5.0\n");
printf(" result so far is 5.0\n");
printf(" ^ 2.0\n");
printf(" result so far is 25.0\n");
printf(" / 2.0\n");
printf(" result so far is 12.5\n");
printf(" q 0\n");
printf(" final result is 12.5\n\n\n");
//------------------------------------------//
/* So, we start with a loop that defined
to be terminated when s_operator equals
'q'. ( 'q' is 113 in value. ) */
while( s_operator != 'q' )
{
printf("result so far is %f\n", f_accumulator);
scan_data( &s_operator, &f_operand); //Call the redundant one...
f_accumulator = do_next_op(s_operator, f_operand, f_accumulator);
//After returning the result, the loop goes on.
}
printf("final result is %f\n", f_accumulator); // THE END
}
/*>>> scan_data ( superfluous function ) <<<*/
void scan_data(char *s_operator, float *f_operand)
{
scanf("%s %f", s_operator, f_operand);
}
/*>>>>>>>>>>>>>>>> do_next_op <<<<<<<<<<<<<<*/
float do_next_op(char s_operator, float f_operand, float f_accumulator)
{
float f_temp = f_accumulator; //Temporory power for exponential.
//The s_operator reigns.
switch (s_operator)
{
case '+':
f_accumulator += f_operand;
//Equivalent to : f_accumulator = f_accumulator + f_operand
break;
case '-':
f_accumulator -= f_operand; // ↑
break;
case '*':
f_accumulator *= f_operand; // ↑
break;
case '/':
if ( f_operand == 0 ) //Division fails when denominator is zero.
{
printf("Wrong with the denominator.\n");
break; //Break here without operation.
}
f_accumulator /= f_operand; // ↑
break;
case '^': //applied to natural numbers only...
for ( ; f_operand >= 2; f_operand-- ) f_accumulator *= f_temp;
break;
case 'c': //The "AC function"
case 'C':
f_accumulator = 0;
break;
default:
break;
}
return f_accumulator;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.182.43
推
11/03 13:24, , 1F
11/03 13:24, 1F
推
11/03 22:11, , 2F
11/03 22:11, 2F
推
11/03 22:40, , 3F
11/03 22:40, 3F
推
11/03 22:51, , 4F
11/03 22:51, 4F