[數隻]WorldV

看板NTUCHE-98-HW作者 (睡眠松。)時間18年前 (2005/11/03 13:19), 編輯推噓4(400)
留言4則, 4人參與, 最新討論串1/1
作業五。 大家隨便抄沒關係,因為我不是要交這一篇。 /************************************\ * * * 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
文章代碼(AID): #13QPtdUL (NTUCHE-98-HW)