//area
#include <stdio.h>
int length, width;
long area;
struct coord{
int x;
int y;
};
struct rectangle{
struct coord topleft;
struct coord bottomrt;
}mybox;
main()
{
printf("n Enter the top left x coordinate: ");
scanf("%d", &mybox.topleft.x);
printf("n Enter the top left y coordinate: ");
scanf("%d", &mybox.topleft.y);
printf("n Enter the bottom right x coordinate: ");
scanf("%d", &mybox.bottomrt.x);
printf("n Enter the botto right y coordinate: ");
scanf("%d", &mybox.bottomrt.y);
width = mybox.bottomrt.x-mybox.topleft.x;
length = mybox.bottomrt.y-mybox.topleft.y;
area=width*length;
printf("n The area is %ld units.n",area);
return 0;
}
//구조체랑 배열
#include <stdio.h>
struct data{
float amount;
char fname[30];
char lname[30];
}rec;
main()
{
printf("Enter the donor's first and last name,n");
printf("separates by a space: ");
scanf("%s %s",rec.fname,rec.lname);
printf("nEnter the donation amount: ");
scanf("%f", &rec.amount);
printf("nDonor %s %s gave $%.2f.n", rec.fname, rec.lname, rec.amount);
return 0;
}
//전화번호부
#include <stdio.h>
struct entry{
char fname[20];
char lname[20];
char phone[10];
};
struct entry list[4];
int i;
main()
{
for(i=0;i<4;i++)
{
printf("nEnter first name: ");
scanf("%s",list[i].fname);
printf("nEnter last name: ");
scanf("%s",list[i].lname);
printf("nEnter phine: ");
scanf("%s",list[i].phone);
}
printf("nn");
for(i=0;i<4;i++)
{
printf("name: %s %s",list[i].fname, list[i].lname);
printf("ttphone: %sn", list[i].phone);
}
return 0;
}
//포인터 증가 배열요소 구하기
#include <stdio.h>
#define MAX 4
struct part{
int number;
char name[10];
}data[MAX] = {1,"smith",2,"jones",3,"Adams",4,"wilson"};
struct part *p_part;
int count;
main()
{
p_part=data; //p_part=&data[0];
for(count = 0;count<MAX ; count++)
{
printf("n At address %d: %d %s",p_part,p_part->number
,p_part->name);
p_part++;
}
return 0;
}
//함수의 인수로 구조체를 전달
#include <stdio.h>
struct data{
float amount;
char fname[30];
char lname[30];
}rec;
void print_rec(struct data x);
main()
{
printf("Enter the donor's first and last names,n");
printf("separated by a space: ");
scanf("%s %s", rec.fname,rec.lname);
printf("nEnter the donation amount: ");
scanf("%f",&rec.amount);
print_rec( rec );
return 0;
}
void print_rec(struct data x)
{
printf("nDonor %s %s gave $%.2f.n",x.fname,x.lname,x.amount);
}
#include <stdio.h>
int length, width;
long area;
struct coord{
int x;
int y;
};
struct rectangle{
struct coord topleft;
struct coord bottomrt;
}mybox;
main()
{
printf("n Enter the top left x coordinate: ");
scanf("%d", &mybox.topleft.x);
printf("n Enter the top left y coordinate: ");
scanf("%d", &mybox.topleft.y);
printf("n Enter the bottom right x coordinate: ");
scanf("%d", &mybox.bottomrt.x);
printf("n Enter the botto right y coordinate: ");
scanf("%d", &mybox.bottomrt.y);
width = mybox.bottomrt.x-mybox.topleft.x;
length = mybox.bottomrt.y-mybox.topleft.y;
area=width*length;
printf("n The area is %ld units.n",area);
return 0;
}
//구조체랑 배열
#include <stdio.h>
struct data{
float amount;
char fname[30];
char lname[30];
}rec;
main()
{
printf("Enter the donor's first and last name,n");
printf("separates by a space: ");
scanf("%s %s",rec.fname,rec.lname);
printf("nEnter the donation amount: ");
scanf("%f", &rec.amount);
printf("nDonor %s %s gave $%.2f.n", rec.fname, rec.lname, rec.amount);
return 0;
}
//전화번호부
#include <stdio.h>
struct entry{
char fname[20];
char lname[20];
char phone[10];
};
struct entry list[4];
int i;
main()
{
for(i=0;i<4;i++)
{
printf("nEnter first name: ");
scanf("%s",list[i].fname);
printf("nEnter last name: ");
scanf("%s",list[i].lname);
printf("nEnter phine: ");
scanf("%s",list[i].phone);
}
printf("nn");
for(i=0;i<4;i++)
{
printf("name: %s %s",list[i].fname, list[i].lname);
printf("ttphone: %sn", list[i].phone);
}
return 0;
}
//포인터 증가 배열요소 구하기
#include <stdio.h>
#define MAX 4
struct part{
int number;
char name[10];
}data[MAX] = {1,"smith",2,"jones",3,"Adams",4,"wilson"};
struct part *p_part;
int count;
main()
{
p_part=data; //p_part=&data[0];
for(count = 0;count<MAX ; count++)
{
printf("n At address %d: %d %s",p_part,p_part->number
,p_part->name);
p_part++;
}
return 0;
}
//함수의 인수로 구조체를 전달
#include <stdio.h>
struct data{
float amount;
char fname[30];
char lname[30];
}rec;
void print_rec(struct data x);
main()
{
printf("Enter the donor's first and last names,n");
printf("separated by a space: ");
scanf("%s %s", rec.fname,rec.lname);
printf("nEnter the donation amount: ");
scanf("%f",&rec.amount);
print_rec( rec );
return 0;
}
void print_rec(struct data x)
{
printf("nDonor %s %s gave $%.2f.n",x.fname,x.lname,x.amount);
}