標題:

有2條關於C語言的程式題~~~

發問:

希望大家幫下我,唔該哂~~~真係對於C語言唔係好熟,有時寫少左個;號就真係有排煩~~~ Program1: http://www.freewebtown.com/francish/Program/Program%201.txt Program2: http://www.freewebtown.com/francish/Program/Program%202.txt 更新: 我想有人教下我點做呀~~~真係唔識呢~~~ 更新 2: #ifdef DEBUG #endif 這兩個是咩意思呢? 更新 3: fgets(buf,256,stdin) 我想問fgets, buf, stdin這些都係關鍵字, 定還是係自定義的變數呀? 還有這行點解呢~~~

最佳解答:

/* Note: Because of a bug in Yahoo knowledge+ this program code is encoded in order to be properly displayed. You MUST decode it by substituting these characters before compiling/running it: Substitute all 〝〞s with tabs (ASCII 9). Substitute all 〝”〞s with half-width double-quotes (ASCII 34). Substitute all 〝&〞s with half-width ampersands (ASCII 38). Substitute all 〝’〞s with half-width apostrophes (ASCII 39). Substitute all 〝<〞s with half-width less-thans (ASCII 60). Substitute all 〝>〞s with half-width greater-thans (ASCII 62). Or follow these steps to convert it to usable code: 1. Click ”Start” menu -> ”Run”. 2. Type ”cmd.exe” and click ”OK”. 3. Type ”chcp 437” and press ”Enter”. 4. Type ”copy con sqrt.c” and press ”Enter”. 5. Copy & paste the code into the command prompt. 6. Press ”F6” and then ”Enter”. */ #include<stdio.h> double abs(double num) { if(num>=0.0) return num; else return(-num); } double sqrt(double num,double last_guess) { double new_guess=0.5*(last_guess+num/last_guess); #ifdef DEBUG printf(”new_guess: %1.10f\n”,last_guess,new_guess); #endif if(abs(last_guess-new_guess)<0.0000000001) return(new_guess); else return(sqrt(num,new_guess)); } int main(int argc,char **argv) { double num; char buf[256]; while(1) { printf(”Enter a number: ”); fgets(buf,256,stdin); sscanf(buf,”%lf”,&num); if(num<0.0||(num==0.0&&*buf!=48)) printf(”Error: Invalid input!\n\n”); else break; } printf(”The square root of %1.6f is approximately %1.10f\n”,num,sqrt(num,1.0)); return(0); } 2006-11-06 18:27:51 補充: /* calc.c */#include<stdio.h>void scan_data(int *operator,float *operand) {#ifdef DEBUGconst char operators[]=”e+-*/^cq”;#endifchar buf[256];int i;fgets(buf,256,stdin);switch(*buf) {case ’+’:*operator=1;break;case ’-’:*operator=2;break;case ’*’: 2006-11-06 19:03:01 補充: *operator=3;break;case ’/’:*operator=4;break;case ’^’:*operator=5;break;case ’c’:*operator=6;break;case ’q’:*operator=7;break;default:printf(”Error! unsupported operator: %c\n”,*buf);*operator=0;} 2006-11-06 19:06:25 補充: if(*operator>0&&*operator<6) {for(i=1;i<255;i++) {if(buf[i]==0||buf[i]==’-’||buf[i]==’.’||(buf[i]>47&&buf[i]<58)) break;}sscanf((char*)buf+i,”%f”,operand);}#ifdef DEBUGprintf(”operator: %1c\noperand: %12.10f\n”,operators[*operator],*operand);#endif} 2006-11-06 19:08:53 補充: void do_op(float *accu,int operator,float operand) {switch(operator) {case 1:*accu+=operand;break;case 2:*accu-=operand;break;case 3:*accu*=operand;break;case 4:*accu/=operand;break;case 5:*accu=powf(*accu,operand);}} 2006-11-06 19:10:51 補充: int main(int argc,char **argv) {float accu=0.0,operand;int operator,cont=1;printf(”Calculator. Please enter operator and operand:\n”);while(cont) {scan_data(&operator,&operand);switch(operator) {case 1:case 2:case 3:case 4:case 5: 2006-11-06 19:11:54 補充: do_op(&accu,operator,operand);break;case 6:accu=0.0;break;case 7:cont=0;}if(operator) printf(”result: %8.6f\n”,accu);}return(0);}

免費註冊體驗

 

此文章來自奇摩知識+如有不便請留言告知

其他解答:
arrow
arrow

    rlz87tm45t 發表在 痞客邦 留言(0) 人氣()