Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 해시
- 다익스트라
- 에라토스테네스의 체
- 다이나믹 프로그래밍
- 구현
- DFS
- 그리디
- 그래프 탐색
- join
- 다시
- GROUP BY
- 브루트포스
- 누적합
- 서브쿼리
- MST
- 투포인터
- 그래프 이론
- 트리
- 우선순위큐
- 재귀
- 분할정복
- BFS
- 시뮬레이션
- DP
- 크루스칼
- 다이나믹프로그래밍
- 플로이드-워셜
- 수학
- 백트래킹
- 자료구조
Archives
- Today
- Total
기록하고 까먹지 말기
과제10 - StructUnion (1 ~ 7) 본문
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// creating structure
typedef struct Student{
char name[20];
int id;
double salary;
};
int main(){
struct Student s; // declaring structure
// initializing structure's contents
strcpy(s.name, "Henry");
s.id = 1112;
s.salary = 95743;
// printing output
printf("Name: %s\n", s.name);
printf("Id: %d\n", s.id);
printf("Salary: %f", s.salary);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// creating structures
struct birth{
int day;
int month;
int year;
};
struct Student{
char name[20];
int rollnum;
struct birth date; // it has object of birth(nested structure)
};
int main(){
struct Student s; // declaring structure
// initializing structure's contents with entering from user
printf("Enter name: ");
scanf("%s", s.name); getchar(); // inputting name from user
printf("Enter roll number: ");
scanf("%d", &s.rollnum); // inputting roll number from user
printf("Enter Date of Birth [DD MM YY] format:");
scanf("%d %d %d", &s.date.day, &s.date.month, &s.date.year); // inputting date from user
printf("\n");
// printing output
printf("Name: %s\n", s.name);
printf("RollNo : %d\n", s.rollnum);
printf("Date of birth : %02d/%02d/%d", s.date.day, s.date.month, s.date.year);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// making structure
typedef struct {
char name[20];
double quantity;
double price;
double amount;
} Fancy;
int main(){
// declaring and initializing structure
Fancy my;
Fancy* ptr; // pointer of structure Fancy
ptr = &my;
// entering contents from user
printf("Enter product name: ");
scanf("%s", ptr->name); getchar(); // to erase new line
printf("Enter price: ");
scanf("%lf", &(ptr->price));
printf("Enter quantity: ");
scanf("%lf", &(ptr->quantity));
ptr->amount = ptr->price * ptr->quantity; // calculating amount of item
// print output
printf("\nName: %s\n", ptr->name);
printf("Price: %f\n", ptr->price);
printf("Quantity: %d\n", (int)ptr->quantity); // casting from double to integer to print like sample
printf("Total Amount: %f\n", ptr->amount);
return 0;
}
#include <stdio.h>
// declaring union
union prac{
char *name;
char names[20];
};
int main()
{
// initializing unions
union prac p1,*ptr;
p1.name="Henry Wong";//assigning string to null character
ptr=&p1; // pointing p1
// printing output
printf("\n\n Pointer : Show a pointer to union :\n");
printf("----------------------------------------\n");
printf(" %s %s\n\n",ptr->name,(*ptr).name);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// declaring union with 3 variable, and array size 32
union u1{
int n1;
short n2, n3;
char arr[32];
};
// declaring structure with 3 variable, and array size 32
struct s1{
int n1;
short n2, n3;
char arr[32];
};
int main(){
// creatin union and structure
union u1 u;
struct s1 s;
// printing output with size of union and structure
printf("size of union = %d bytes\n", sizeof(u));
printf("size of structure = %d bytes", sizeof(s));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// creating structure
struct score{
int id;
double gpa;
};
int main(){
// declaring structures and pointer
struct score s1;
struct score s2;
struct score* ptr;
// assigning valus into variables of structures
s1.id = 1;
s1.gpa = 2.9;
s2.id = 2;
s2.gpa = 3.7;
ptr = &s1; // pointing s1
// printing output with pointer
printf("Student1: ID =%d\n", ptr->id);
printf("Student1: GPA =%lf\n", ptr->gpa);
ptr = &s2; // pointing s2
// printing output with pointer
printf("Student2: ID =%d\n", ptr->id);
printf("Student2: GPA =%lf", ptr->gpa);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// creating structure
union score{
int id;
double gpa;
};
int main(){
// declaring structures and pointer
union score s1;
union score* ptr;
// assigning valus into variables of structures
ptr = &s1; // pointing s1
// printing output with pointer
// pointing id and gpa whenever printing output due to union
s1.id = 1;
printf("Student: ID =%d", ptr->id);
s1.gpa = 2.9;
printf(" and Student: GPA =%lf\n", ptr->gpa);
s1.id = 2;
printf("Student: ID =%d", ptr->id);
s1.gpa = 3.7;
printf(" and Student: GPA =%lf", ptr->gpa);
return 0;
}
'전공 > C' 카테고리의 다른 글
과제11 - final (2) (0) | 2021.06.22 |
---|---|
과제11 - final (1) (0) | 2021.06.22 |
과제9 - Pointer Dynamic (6 ~ 9) (0) | 2021.06.22 |
과제9 - Pointer Dynamic (1 ~ 5) (0) | 2021.06.22 |
과제8 - Pointer Array (6 ~ 10) (0) | 2021.06.22 |