หัวข้อ: ใครเก่งภาษา C ช่วยผมหน่อยครับ จนปัญญา เริ่มหัวข้อโดย: donclub ที่ 30 พฤศจิกายน 2011, 06:45:59 ช่วยแยกเป็นfunctionให้หน่อยครับ
หรือแนะมาเป็นแนวทางก็ได้ แยกเป็นฟังก์ชัน ตรงที่ commentเป็นตัวอ่ะเลขครับ (//1, //2, //3, //4, //5, //6) จนปัญญาจริงๆครับ นั่งทำทั้งคืนแล้ว พอแก้แล้วมันก็มีerrorเต็มหมด ไม่รู้จะให้ส่งค่าไป ส่งค่ากลับยังงัย อ้างถึง #include<stdio.h> #include<time.h> #include<stdlib.h> #include<string.h> FILE *fp; int main(void){ int randomNumber; int inputNumber; int temp; bool isSame = false; bool isInputSame = false; bool isTopScore = false; int randomArray[4]; int inputArray[4]; int loop; int i,j; int turn = 0; int rightPosition = 0; int rightOnlyNumber = 0; int score1 = 0; int score2 = 0; int totalScore = 0; int turnScore = 0; int score3 = 0; int hightScore[10]; char name[10][20]; char player[20]; char ch; loop = 0; // 1. fp = fopen("hight.txt","r"); if(fp != NULL){ while(!feof(fp) && (loop<10)){ fscanf(fp,"%s%d%c",name[loop],&hightScore[loop],&ch); printf("%d :: %s \t %d \n",loop+1,name[loop],hightScore[loop]); loop++; } } else{ printf("ERROR\n"); exit(0); } fclose(fp); // 2. srand(time(NULL)); do{ isSame = false; randomNumber = (rand() % 8853) + 1023; temp = randomNumber; for(loop = 0; loop < 4; loop++){ randomArray[loop] = temp % 10; temp = temp / 10; } for(i = 0; i < 4; i++){ for(j = 0; j < 4; j++){ if(i != j){ if(randomArray == randomArray[j]){ isSame = true; break; } } } if(isSame){ break; } } }while(isSame); // printf("%d \n",randomNumber); printf("Pls. input your name :: "); scanf("%s",player); turn = 0; // input number and compare // 3. totalScore = 0; do{ rightPosition = 0; rightOnlyNumber = 0; do{ isInputSame = false; printf("Turn %d your quess :: ",turn+1); scanf("%d",&inputNumber); temp = inputNumber; for(loop = 0; loop < 4; loop++){ inputArray[loop] = temp % 10; temp = temp / 10; } for(i = 0; i < 4; i++){ for(j = 0; j < 4; j++){ if(i != j){ if(inputArray == inputArray[j]){ isInputSame = true; printf("Invalid Input Pls. input again \n"); break; } } } if(isInputSame){ break; } } }while(isInputSame); // compare for(i = 0; i < 4; i++){ if(randomArray == inputArray){ rightPosition++; } for(j = 0; j < 4; j++){ if(i != j){ if(randomArray == inputArray[j]){ rightOnlyNumber++; } } } } printf(" %d \t %d\n",rightPosition,rightOnlyNumber); turn++; score1 = rightOnlyNumber * 12; score2 = rightPosition * 25; turnScore = score1 + score2; totalScore = totalScore + turnScore; printf("Score1 :: %d \t Score2 :: %d \t totalScore :: %d \n",score1,score2,totalScore); }while( (rightPosition < 4) && (turn < 10) ); // 4. score3 = 0; score3 = (10 - (turn+1)) * 100; totalScore = totalScore + score3; printf("Your score :: %d \n", totalScore); printf("Random number :: %d \n",randomNumber); // 5. isTopScore = false; for(loop = 0; loop < 10; loop++){ if(totalScore > hightScore[loop]){ for(i = 9; i < loop; i--){ hightScore = hightScore[i-1]; } hightScore[loop] = totalScore; strcpy(name[loop],player); isTopScore = true; } if(isTopScore){ break; } } // 6. // write to file loop = 0; fp = fopen("hight.txt","w"); if(fp != NULL){ while(loop < 10){ fprintf(fp,"%s %d \n",name[loop],hightScore[loop]); loop++; } } else{ printf("ERROR \n"); exit(0); } printf("END Program \n"); } // end main function หัวข้อ: Re: ใครเก่งภาษา C ช่วยผมหน่อยครับ จนปัญญา เริ่มหัวข้อโดย: mSkyline ที่ 30 พฤศจิกายน 2011, 08:00:12 ก้อแค่ยก คำสั่งใน // มาแยกเป็นฟัง์ชั่น
อันไหนที่ต้องส่งตัวแปรเข้ามา ก็สร้างเป็นอากิวเม้น หรือว่ามันส่งข้อมูลไรออกมา ก็ return ออกไป ก็จบแล้ว หัวข้อ: Re: ใครเก่งภาษา C ช่วยผมหน่อยครับ จนปัญญา เริ่มหัวข้อโดย: bonshington ที่ 30 พฤศจิกายน 2011, 10:47:21 ไม่คิดจะใส่ comment หน่อยหรอ? ว่าตรงไหน อะไรยังไง
เห็นแบบบนี้แล้วเข้าใจว่า เอาการบ้านมาหรอ? วิธีการเรียกใช้ function ของ c มันต้องดูว่าเป็น pointer รึเปล่า เช่น โค๊ด: // 1. ตย โค๊ด: void ReadFile(void *path, void *name, void *highscroe){ |