Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

CPT Lab 5




// CPT Lab05 Simple text file
/*
ไฟล์ num.txt ประกอบด้วยข้อมูลตัวเลข
  - บรรทัดที่ 1 บอกจำนวนตัวเลขที่ต้องการหาผลรวม
  - บรรทัดต่อไป ตัวเลขจำนวนเต็ม บรรทัดละ 1 ตัวเลข
 
จงเขียนโปรแกรมเพื่อหาผลรวามของตัวเลข ที่อยู่หลังบรรทัดที่ 1
 
ตัวอย่างไฟล์ num.txt
3
7
2
1
 
รูปแบบการแสดงผล
=10
 
 
หากมีการแก้ไขไฟล์ num.txt อาจทำให้ testing failed.
*/
#include<stdio.h>
 
int main(){
  FILE *fp;
int i,input,sum=0,num;
  fp = fopen("num.txt","rb");
fscanf(fp,"%d",&num);
  for(i=0;i<num;i++){
    fscanf(fp,"%d",&input);
  sum+=input;
 }
printf("=%d",sum);
fclose(fp);
  return 0;
}


// Coe CPT lab5 Read Binary File
/*
จงเขียนโปรแกรมที่ทำการอ่านไฟล์ไบนารี ชื่อ employee.bin
โดยไฟล์จะเริ่มต้นจากตัวเลขจำนวนเต็ม บอกจำนวนข้อมูลพนักงานที่บันทึกไว้ในไฟล์ จากนั้นประมวลผลข้อมูลให้อยู่ในรูปของ struct employee ที่กำหนดให้ แล้วทำการแสดงข้อมูลของพนักงานทุกคน พร้อมแสดงผลเงินเดือนรวม
 
***ทำการแสดงผลจากการอ่านออกทางหน้าจอ สำหรับการอ่านไฟล์นั้นจะต้องทำการกดปุ่ม L-Test ก่อน เพื่อ download ไฟล์มาเก็บไว้ในเครื่องโดยอัตโนมัติ แล้วค่อยทำการรันโปรแกรม***
 
รูปแบบการแสดงผล
John Doe:3000.0
Mark Ken:2300.0
Sucy Merc:2000.0
=7300.0
*/
#include <stdio.h>
struct employee {
 
char name[128];
 
float salary;
};
typedef struct employee Employee;
 
int main() {
 
int num;
  Employee e,
*ep=&e;
 
float total = 0.0;
 
FILE *fp;
 
  fp
= fopen("employee.bin","rb");
       
fread(&num,sizeof(num),1,fp);
 
while(num--){
   
fread(&e,sizeof(Employee),1,fp);
   
printf("%s:%.1f\n", e.name, e.salary);
    total
+=e.salary;
   
}
printf("=%.1f\n", total);
fclose(fp);
 
return 0;    
}/*
เขียนโปรแกรม เพื่อดึงตัวเลขจำนวนเต็มที่ถูกบันทึกไว้ในไฟล์ไบนารี num.dat โดยโปรแกรมจะถูกเขียนโดยไม่ทราบล่วงหน้าว่า มีตัวเลขจำนวนกี่ตัวในไฟล์ดังกล่าว และไม่มีการอ่านข้อมูลทั้งหมดขึ้นสู่อาเรย์
 
โปรแกรมจะรับข้อมูลจากผู้ใช้ ว่า ต้องการทราบตัวเลขลำดับใด (ให้ลำดับแรกคือ 1) หลังจากนั้น จะแสดงตัวเลขที่อยู่ ณ. ตำแหน่งที่กำหนด
 
โปรแกรมจะหยุดการทำงานเมื่อผู้ใช้ใส่ตำแหน่งเป็น 0 หรือค่าติดลบ ทั้งนี้ให้ถือว่า ผู้ใช้จะไม่ใส่ตำแหน่งที่ไม่มีอยู่จริงในไฟล์
 
ตัวอย่างการแสดงผล
Enter: &amp;amp;lt;1&amp;amp;gt;
= 40
Enter: &amp;amp;lt;372&amp;amp;gt;
= 68
Enter: &amp;amp;lt;250&amp;amp;gt;
= 12
Enter: &amp;amp;lt;0&amp;amp;gt;
Done.
 
***ทำการแสดงผลจากการอ่านออกทางหน้าจอ สำหรับการอ่านไฟล์นั้นจะต้องทำการกดปุ่ม L-Test ก่อน เพื่อ download ไฟล์มาเก็บไว้ในเครื่องโดยอัตโนมัติ แล้วค่อยทำการรันโปรแกรม***
*/

//CPT lab5 part3

#include&amp;amp;lt;stdio.h&amp;amp;gt;
int main(){
   
FILE *fp;
   
int d,position;
    fp
= fopen("num.dat","rb");
       
while(1){
           
printf("Enter: ");
           
scanf("%d",&amp;amp;amp;position);
           
if(position==0)
               
break;
           
fseek(fp,(position-1)*sizeof(int),SEEK_SET);
           
fread(&amp;amp;amp;d,sizeof(d),1,fp);
           
printf("= %d\n", d);
           
       
}
printf("Done.\n");
   
fclose(fp);
   
return 0;
}

Bubble Sort

#include<stdio.h>

int main(){
int a[]={5,1,4,3,2};
int i,k,m,tmp,len = 5;
for(m=0;m<len;m++)
printf("%d ",a[m]);
printf("\n");

for(i=0;i<len;i++)
for(k=0;k<len-1;k++){

if(a[k]>a[k+1]){

tmp = a[k];
a[k]=a[k+1];
a[k+1]=tmp;
printf("Round %d : Swap [%d,%d] ",i+1,a[k],a[k+1]);
for(m=0;m<len;m++)
printf("%d ",a[m]);
printf("\n");
}
}
for(m=0;m<len;m++)
printf("%d ",a[m]);
return 0;
}

cpt2

#include<stdio.h>
#include<math.h>
#define MAX 5

typedef struct{
  int x;
  int y;
} Point;

typedef struct{
  Point a;
  Point b;
} Line;

float distance(Point *p1, Point *p2);
void input(Line *lptr);
float longest(Line p[], int len);

int main(){
  int i;
  Line lines[MAX];
  float d;
  for(i = 0; i < MAX; i++){
    printf("L%02d: ", i + 1);
    input(&lines[i]);
  }    
  d=longest(lines,MAX);
  printf("=%.2f",d);
  return 0;
}
void input(Line *lptr){
    scanf("%d %d %d %d",&lptr->a.x,&lptr->a.y,&lptr->b.x,&lptr->b.y);
}
float distance(Point *p1, Point *p2){
   
    return sqrt(pow(p1->x - p2->x,2)+pow(p1->y - p2->y,2));
}
float longest(Line p[], int len){
    float l,max=0 ;
    int i;
    for(i = 0; i < MAX; i++){
        if(distance(&p[i].a,&p[i].b)>=max){
               max=distance(&p[i].a,&p[i].b);
        }
    }
    return max;
}

The Star for Introcom


#include<stdio.h>
#include<stdlib.h>
int finput()
{int x,i=8;
printf("\nEnter Value:");
scanf("%d",&x);
return x;
}
void f6()
{int in,i,i2;
in=finput();
 for(i=1;i <= in ;i++) {
for(i2=i;i2 <in;i2++)
 printf(" ");
 for(i2=0;i2 < (i*2)-1 ;i2++)
  printf("*");
 printf("\n");
}
}
void f5()
{int in,i,i2;
in=finput();
 for(i=0;i < in ;i++) {
for(i2=i;i2 >0;i2--)
printf(" ");
 for(i2=0;i2+1<(in-i)*2;i2++)
 printf("*");
printf("\n");
}
}
void f4()
{int in,i,i2;
in=finput();
 for(i=0;i < in ;i++){ 
for(i2=in-i;i2>0;i2--)
printf("*");
printf("\n");
}
}
void f3()
{int in,i,i2;
in=finput();
 for(i=0;i < in ;i++) {
for(i2=i;i2 >0;i2--)
printf(" ");
for(i2=in-i;i2>0;i2--)
printf("*");
printf("\n");
}
}
void f2()
{int in,i,i2;
in=finput();
 for(i=0;i <= in ;i++) {
for(i2=i;i2 <in;i2++)
printf(" ");
for(i2=i;i2 >0;i2--)
printf("*");
printf("\n");
}
}
void f1()
{int in,i,i2;
in=finput();
 for(i=1;i <= in ;i++) {
for(i2=i;i2 >0;i2--)
printf("*");
printf("\n");
}
}
void fmenu()
{int x,y,i;
printf("\n\t MENU By UM B intania ^ ^\n\t1.Triangle1\t");
printf("2.Triangle2\t");
printf("3.Triangle3\t");
printf("4.Triangle4\n\t");
printf("5.DoubleTri-Down\t");
printf("6.DoubleTri-Up\t\n");
scanf("%d",&x);
i=8;
while(i--)
printf("%d-",x);
switch(x)
{case 1:f1();break;
case 2:f2();break;
case 3:f3();break;
case 4:f4();break;
case 5:f5();break;
case 6:f6();break;
case 9:system("PAUSE");exit(0);
}


}
int main()
{while(1)
fmenu();
system("PAUSE");
return 0;
}

summation of array call by reference

#include<stdio.h>

int sum(int a[])
{int x=0;
int i;
for(i=0;i<10;i++)
    {x+=a[i];}   
    x+=4;
   
return x;   
}

int main()
{
    int i,a[30];
    int ans;
    for(i=0;i<10;i++)
    a[i]=i;
   
    ans=sum(a);
    printf("%d\n",ans);
   
return 0;   
}

Lab6-3 3Checkpoint+Homework

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int i,len;
void replaceStr(char str[],char c1, char c2)
{
     len=strlen(str);
     for(i=0;i<len;i++)
                       if(str[i]==c1)
                                       printf("%c",c2);
                       else printf("%c",str[i]);
                       printf("\n");
     }
 int main()
 {int count;
 char ch1,c1,c2;
 char chk3[5][4][40];
 char str1[50], str2[50];
 while(1)
 {
 printf("Enter the first string : ");
 scanf("%s",str1); //fflush(stdin);
 printf("Enter the second string : ");
 scanf("%s",str2); //fflush(stdin);
if(strcmp(str1,str2)<0)
printf("\"%s\" comes before \"%s\" \n------\n",str1,str2);
else printf("\"%s\" comes after \"%s\" \n------\n",str1,str2);
printf("\nCheck Point2\n");
printf("Enter 2 Charactors to replace in First String ex. a->A\n");
scanf("%c %c %c",&c1,&c1,&c2);
replaceStr(str1,c1,c2);
printf("\n---------\n");
printf("\nCheck point3\n");
for(i=0;i<5;i++)
                {printf("Enter Name surname Province Year:");
                scanf("%s %s %s %s",chk3[i][0],chk3[i][1],chk3[i][2],chk3[i][3]);
}
for(i=0;i<5;i++)
                {
                printf("%s %s %s %s\n",chk3[i][0],chk3[i][1],chk3[i][2],chk3[i][3]);
}/*
printf("\nHomework 1\n");
for(count=i=0;i<len;i++)
                  switch(str1[i])
                  {case 'a':case 'A':case'e':case'E':case'i':case'I':case'o':case'O':case'u':case'U': count++;}
printf("%s has %d vowel",str1,count);
printf("\nHomework 2\n");
for(;len!=-1;len--)
printf("%c",str1[len]);

printf("\nHomework 3\n");
len=strlen(str1);
for(i=0;i<len/2;i++)
if(str1[i]!=str1[len-1])break;
if(i==len/2)
printf("%s is parimdrome",str1);
else                   printf("%s is not parimdrome",str1);
*/
}
system("PAUSE");
 return 0;
}