กุมภาพันธ์ 05, 2012, 08:44:11 PM *
ยินดีต้อนรับคุณ, บุคคลไม่รู้ว่าใคร กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น
   หน้าบอร์ด   เข้าสู่โหมดไร้คอนเซปต์ สมัครสมาชิก  
  
หน้า: [1]   ลงล่าง
  พิมพ์  
ผู้เขียน หัวข้อ: C Language CPE111 KMUTT <TA>  (อ่าน 3457 ครั้ง)
kanmai
บุคคลไม่รู้ว่าใคร
« เมื่อ: สิงหาคม 23, 2008, 02:37:45 AM »

น้องๆ ลองเอาโค้ดไปรันแล้วก้อทำความเข้าใจดู
เพื่อเข้าใจขึ้นบ้าง แต่ถ้ามีปัญหาก้อเมล์มาถามหล่ะกัน

/*
  swap
  This function swap member with in array
  created by TA cpe111
  date 12/08/08
*/

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//this is a loading ;)   just a funny one >>>ei ei
void process()
{
  int i=0,j=0;   // index  i for outside loop ,j for inner loop
 
  for(i=0;i<=60;i++)
  {
    printf("\r"); // put cursor back one position
    switch(i%4)   //i%4 the range of result is 0-3
   {
     case 0:
         printf("|");
         break;
     case 1:
         printf("/");
         break;   
     case 2:
         printf("-");
         break;
     case 3:
         printf("\\");// when u wanna print \ in C, u should put "\\"
         break;
   }
   // this loop for delay. Time delay is 100000, u can change it. try it out !!!
   for(j=0;j<=100000;j++)
   {
      printf("");// print nothing
   }
  }
  printf("\r");// put cursor back one position
}

// function for sent message to user and wait until user press any key
void message(char message[])
{
  printf("%s",message);
  getchar();
}

//function to print list
void printList(int size,int list[],int tag[])
{
  int i=0;
 
  printf("the list ");
  for(i=0;i<size;i++)
  {
     if((i)%5==0)
      printf("\n:");
   printf("  %.2d",list[ tag[ i ] ]);
  }
  printf("\n");
}

// function for swap
void swap(int tag[],int index)
{
  int tmp;
 
  tmp=tag[index];
  tag[index]=tag[index+1];
  tag[index+1]=tmp;
}

// funtion for sorting
void mySort(int size,int list[],int tag[])
{
  int i=0,j=0;
 
  for(i=0;i<size;i++)
  {
    for(j=0;j<size-i;j++)
   {
     if(list[tag[j]]>list[ tag[j+1] ])
      swap(tag,j);
   }
  }
}

int main()
{   
  char inputTmp[128];
  int i=0;
  int check=0;
 
  int list[20]={0}; // hold the number
  int tag[20]={0};  // hold the order
  int size=0;       // hold the size of set

  srand ( time(NULL) );
  printf("this is starting of program\n");
 
  while(!check)
  {
    printf("plz enter the number of list :");
    fgets(inputTmp,128,stdin);
    sscanf(inputTmp,"%d",&size);
   if(size>1&&size<=20)// check for size; 1<size<=20
   {
     check=1;
   }
  }
 
  message("plz press any key to random the list of number\n");
  process();
 
  for(i=0;i<size;i++)
  {
   list[ i ] = rand() % 100 + 1; // generate the number; range is 1-100
   tag[ i ]=i;// assige order for tag 
  }
  printList(size,list,tag);
 
  message("\nplz press any key to sort the list\n");
  process();
  mySort(size,list,tag);
 
  printList(size,list,tag);
  getchar(); // for window. To hold the result screen
 
  return 0;
}

แล้วจาเอาโค้ดมาโพสให้เรื่อยๆๆอ่ะ
หรืออยากได้โค้ดเรื่องไรก้อเมล์มาหล่ะกัน
บันทึกการเข้า
kanmai
บุคคลไม่รู้ว่าใคร
« ตอบ #1 เมื่อ: สิงหาคม 23, 2008, 02:48:40 AM »

ส่วนแลป พี่จาเขียนโค้ดที่เปงแนวเฉลยให้
คือ อาจจะทำได้หลายอย่าง แต่ต้องประมานนี้
แล้วจามาโพสให้ทุกวันพฤหัสตอนกลางคืนประมาน
สองถึงสามทุ่ม
บันทึกการเข้า
kanmai
บุคคลไม่รู้ว่าใคร
« ตอบ #2 เมื่อ: สิงหาคม 23, 2008, 12:18:07 PM »

อ้อ ขอบคุนเจ้าของเวบ (เพื่อนอาต) ด้วยที่ให้พื้นที่
บวกอำนวยความสะดวก ขอบใจหลายๆๆ
บันทึกการเข้า
kanmai
บุคคลไม่รู้ว่าใคร
« ตอบ #3 เมื่อ: สิงหาคม 24, 2008, 07:01:08 AM »

พี่ขอไม่คอมเม้นนะ เพราะว่าพี่ไม่ค่อยว่างเท่าไหร่
ไม่เข้าใจตรงไหนก้อเมล์มาถามนะ

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void saveArray(char names1[20][32],char names2[20][32],int count,char* outputFile)
{
  int i=0;
  FILE* pOutfile;
  pOutfile = fopen(outputFile,"w");
  if(pOutfile==NULL)
  {
    printf("Error: can not open output file");
   exit(1);
  }
 
  fprintf(pOutfile,">>>List 1<<<");
  for(i=0;i<count;i++)
  {
    fprintf(pOutfile,"%s\n",names1[ i ] );
  }
  fprintf(pOutfile,"\n>>>List 2<<<");
  for(i=0;i<count;i++)
  {
    fprintf(pOutfile,"%s\n",names2[ i ] );
  }
 
  fclose(pOutfile);
}


void sortArray(char names[20][32], int valueCount)
{
  int i;
  char temp[32];   /* temp string to use for swapping */
  int swapCount = 1;
 
  while (swapCount > 0)
  {
    swapCount = 0;
    for (i = 0; i < valueCount-1; i++)
    {
      /* if names[ i ] should be later than names [i+1] */
     if (strcmp(names[ i ], names[i+1]) > 0)
     {
       strcpy(temp,names[ i ]);
       strcpy(names[ i ],names[i+1]);
       strcpy(names[i+1],temp);
       swapCount = swapCount + 1;
     }
   }   
  }
}

int main(int argc, char* argv[])
{
  int count=0;
  //char inputFile[128]="input1.txt";
  //char outputFile[128]="output1.txt";
  char* inputFile=NULL;
  char* outputFile=NULL;
  char inputBuffer[128]={'\0'};
  char names1[20][32]={'\0'};  /* values to be sorted */
  char names2[20][32]={'\0'};  /* values to be sorted */
  FILE *pInfile = NULL;
  if(argc!=3)
  {
    printf("Error with input command!!!\n");
   exit(1);
  }
  inputFile = strdup(argv[1]);
  outputFile = strdup(argv[2]);
  if((inputFile == NULL) || (outputFile == NULL))
  {
    printf("Memory allocation error in strdup!!!\n");
    exit(1);
  }
  pInfile = fopen(inputFile,"r");
  if (pInfile == NULL)
  {
    printf("Error: can not open input file!!");
    exit(1);
  }
 
  while(fgets(inputBuffer,128,pInfile)!=NULL)
  {
    if(strlen(inputBuffer)!=0)
    {
      sscanf(inputBuffer,"%s %s",names1[count],names2[count]);
     printf("%s %s\n",names1[count],names2[count]);
     count++;
    }
  }
  fclose(pInfile);
 
  sortArray(names1,count);
  sortArray(names2,count);
 
  saveArray(names1,names2,count,outputFile);
  //getchar();
}
บันทึกการเข้า
numchut
ขั้น 4 : แฟนคลับบอร์ด
***

ความนิยม 134
ออฟไลน์ ออฟไลน์

พลังสะสม: 672


« ตอบ #4 เมื่อ: มกราคม 25, 2009, 01:49:14 AM »

มาขอบคุณอ่ะครับ ว่าแต่ ที่พี่โพสมานี่ มันโคดโปรแกรมอะไรช่วยบอกด้วยนะครับ +เป้นกำลังใจไห้ครับ
บันทึกการเข้า
crack
บุคคลไม่รู้ว่าใคร
« ตอบ #5 เมื่อ: มิถุนายน 03, 2009, 10:17:27 AM »

ดีมากเลยคับ ขอบคุณคับ
บันทึกการเข้า
chan88221
ขั้น 2 : วัยรุ่นไฟแรง
**

ความนิยม 2
ออฟไลน์ ออฟไลน์

พลังสะสม: 53


« ตอบ #6 เมื่อ: มิถุนายน 07, 2009, 11:43:48 PM »

thank thank thank thank  ส่งสายตาหวาน ส่งสายตาหวาน ส่งสายตาหวาน
บันทึกการเข้า
alexnormal
ขั้น 3 : ซุปเปอร์สตาร์
**

ความนิยม 27
ออฟไลน์ ออฟไลน์

เพศ: ชาย
พลังสะสม: 245

~ : ก ร : ท่ o ม น้ o e : ~


เว็บไซต์
« ตอบ #7 เมื่อ: มีนาคม 14, 2010, 09:59:16 PM »

ขอบคุณมากครับ ^^ +1
บันทึกการเข้า

~ : ก ร : ท่ o ม น้ o e : ~ 

แ ม้ วั น นี้ เ ห ม่ ง จ า ไ ม่ รั ก อ้ ว น เ ห มื อ น เ ดิ ม

แ ล : วั น นี้ ใ น ใ จ ข O ง เ ห ม่ ง จ า มี แ ต่ บุ ค เ ท่ า นั้ น

แ ต่ ใ น ใ จ ข อ ง อ้ ว น ทุ ก ล ม ห า ย จั ย มี แ ต่ เ ห ม่ ง ค น เ ดี ย ว เ ท่ า

นั้ น 

ป ล อ้วนรักเหม่งนะ ~
oamosed
ขั้น 3 : ซุปเปอร์สตาร์
**

ความนิยม 15
ออฟไลน์ ออฟไลน์

พลังสะสม: 156


« ตอบ #8 เมื่อ: พฤศจิกายน 26, 2010, 01:11:34 PM »

ขอบคุณครับ
บันทึกการเข้า
หน้า: [1]   ขึ้นบน
  พิมพ์  
 
กระโดดไป:  



art2bempire.com DekNaiMor.com Mayahol.com Mayahol.com 
เว็บไซต์ไร้คอนเซปต์ : หาเพื่อนแก้เหงา แบ่งปันเรื่องราว ความรู้ คอมพิวเตอร์ มือถือ ดาวน์โหลดโปรแกรม หนัง เพลง ฯลฯ เสบียงบุญ sumbydee.com:ชุมชนคนรักโทรศัพท์ DJ-THAI-REMIX Dance Hiphop House สังคมสร้างสรรค์เสียงเพลง Shoppingforlady.com

Powered by MySQL Powered by PHP Powered by SMF 1.1.15 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!