By GokiSoft.com|
19:48 20/10/2020|
Lập Trình C
[Share Code] Chia sẻ code struct trong C - Lập trình C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef int SO_NGUYEN;
typedef long int SO_NGUYEN_2;
typedef struct Student_ST {
char ten[20];
int tuoi;
char diachi[50];
char email[50];
char sdt[20];
} Student;
typedef struct Animal_ST {
char ten[50];
int tuoi;
} Animal;
int main(int argc, char *argv[]) {
SO_NGUYEN x = 5;
SO_NGUYEN_2 y = 10;
printf("\nx = %d", x);
printf("\nx = %d", y);
//thong tin sinh vien: ten, tuoi, dia chi, email, sdt
char ten[20];
int tuoi;
char diachi[50];
char email[50];
char sdt[20];
//quan thong tin dong vat: ten, tuoi
char tendv[50];
int agedv;
printf("\nNhap ten: ");
fflush(stdin);fflush(stdout);
gets(ten);
printf("\nNhap tuoi: ");
fflush(stdin);fflush(stdout);
scanf("%d", &tuoi);
printf("\nNhap dia chi: ");
fflush(stdin);fflush(stdout);
gets(diachi);
printf("\n%s, %d, %s", ten, tuoi, diachi);
struct Student_ST std;
Student std2;
Animal animal;
//nhap thong tin sinh vien cho struct
printf("\nNhap ten: ");
fflush(stdin);fflush(stdout);
gets(std2.ten);
printf("\nNhap tuoi: ");
fflush(stdin);fflush(stdout);
scanf("%d", &std2.tuoi);
printf("\nTen: %s, tuoi: %d", std2.ten, std2.tuoi);
//nhap thong tin dong vat
printf("\nTendv: ");
gets(animal.ten);
printf("\nTuoidv: ");
scanf("%d",&animal.tuoi);
printf("\nTendv: %s,tuoi: %d", animal.ten, animal.tuoi);
//khai bao dc mang gom 3 sinh vien
Student studentList[3];
//LENGTH: 3
//index: 0 -> 2
int i;
//nhap du lieu
for(i=0;i<3;i++) {
printf("\nNhap thong tin sinh vien thu %d", i + 1);
printf("\nNhap ten: ");
fflush(stdin);fflush(stdout);
gets(studentList[i].ten);
printf("\nNhap tuoi: ");
fflush(stdin);fflush(stdout);
scanf("%d", &studentList[i].tuoi);
}
//Hien thi thong tin sinh vien
for(i=0;i<3;i++) {
printf("\nTen: %s, tuoi: %d", studentList[i].ten, studentList[i].tuoi);
}
// char s1[30], c, s2[30];
// printf("\nNhap s1 = ");
// gets(s1);
// printf("\nNhap s2 = ");
// gets(s2);
// printf("\nNhap c = ");
// scanf("%c", &c);
//
// char *p = strchr(s1, c);
// printf("\np: %s", p);
//
// p = strstr(s1, s2);
// printf("\np: %s", p);
return 0;
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)