By GokiSoft.com|
20:22 25/09/2023|
Lập Trình C
Bài tập - Quản lý hình chữ nhật - struct trong C - Lập trình C
1. Tạo 1 struct gồm các thuộc tính sau -> float : dientich, float chieudai, float chieurong
2. Tạo mảng list gồm 5 HCN
3. Nhập thông tin chiều dài và chiều rộng cho 5 HCN trên
4. Tính diện tích của từng HCN và lưu vào biến dientich tương ứng
5. Tính tổng diện tích 5 HCN trên
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2020-09-23 09:05:04
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct HCN_ST{
float hight, width, s;
}HCN;
int main(int argc, char *argv[]) {
int i;
HCN lish[5];
for(i=0;i<5;i++){
printf("\nHCn %d", i);
printf("\nNhap chieu dai: ");
scanf("%d", &lish[i].hight);
printf("\nNhap chieu rong: ");
scanf("%d", &lish[i].width);
}
for(i=0;i<5;i++){
lish[i].s = lish[i].hight * lish[i].width;
printf("\ndien tich HCN la ");
printf("\%d", lish[i].s);
}
int k =0;
for(i=1;i<5;i++){
k+=lish[i].s;
}
printf("\nTong: %d", k);
return 0;
}
![Nguyễn Xuân Mai [T2008A]](https://www.gravatar.com/avatar/d3d863d6f47708501814fb41e9c38f31.jpg?s=80&d=mm&r=g)
Nguyễn Xuân Mai
2020-09-23 08:51:40
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct rectangle{
float height,width,area;
}rectangle;
int main(int argc, char *argv[]) {
rectangle rectangleList[5];
int i;
for(i=0;i<5;i++) {
printf("\nNhap hinh chu nhat thu: %d", i);
printf("\nNhap chieu dai: ");
scanf("%f",&rectangleList[i].height);
printf("\nNhap chieu rong: ");
scanf(" %f",&rectangleList[i].width);
rectangleList[i].area=rectangleList[i].height*rectangleList[i].width;
printf("\nDien tich = %f",rectangleList[i].area);
}
float tong=0;
for(i=0;i<5;i++) {
tong+=rectangleList[i].area;
}printf("\nTong dien tich = %f",tong);
return 0;
}
![An Văn Minh [T2008A]](https://www.gravatar.com/avatar/e0f14efe4b11f7d9d5901e8802319c92.jpg?s=80&d=mm&r=g)
An Văn Minh
2020-09-23 08:51:39
#include<stdio.h>
#include<stdlib.h>
typedef struct HCN{
float dientich,dai,rong;
}HCN;
int main(){
HCN stdlist[5];
int i;
for(i=0;i<5;i++){
printf("\nNhap HCN thu %d", i+1);
printf("\nNhap chieu dai: ");
scanf("\n%f", &stdlist[i].dai);
printf("\nNhap chieu rong: ");
scanf("%f", &stdlist[i].rong);
stdlist[i].dientich=stdlist[i].dai*stdlist[i].rong;
printf("\nDien tich HCN la= %f", stdlist[i].dientich);
}
float sum=0;
for(i=0;i<5;i++){
sum+=stdlist[i].dientich;
}
printf("\ndien tich=%f", sum);
return 0;
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2020-09-23 08:18:51
#include<stdio.h>
typedef struct hcn_st{
int dientich,chieudai,chieurong;
}Hcn;
void nhapdulieu(Hcn *p){
int i;
for(i=0;i<5;i++){
printf("\nHinh chu nhat thu %d:",i+1);
printf("\nChieu dai: ");
scanf("%d",&p[i].chieudai);
printf("\nChieu rong: ");
scanf("%d",&p[i].chieurong);
}
}
void dientichhcn(Hcn *p){
int i;
for(i=0;i<5;i++){
p[i].dientich=p[i].chieudai*p[i].chieurong;
printf("\nDien tich hinh chu nhat thu %d = %d",i+1,p[i].dientich);
}
}
void tongdientich5hcn(Hcn *p){
int tong=0;
int i;
for(i=0;i<5;i++){
tong+=p[i].dientich;
}
printf("\nTong dien tich cua 5 hinh chu nhat = %d",tong);
}
int main(){
Hcn hcnlist[5];
nhapdulieu(hcnlist);
dientichhcn(hcnlist);
tongdientich5hcn(hcnlist);
getch();
return 0;
}
![Nguyễn Hữu Hiếu [T2008A]](https://www.gravatar.com/avatar/ca2884508b617fee77f000c7d99c219d.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Hiếu
2020-09-23 08:08:59
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct rectang_ST {
float dai, rong, dientich;
} rectang;
int main(int argc, char *argv[]) {
int i, n;
float tongdientich = 0;
printf("Nhap so Hinh chu nhat:");
scanf("%d", &n);
rectang rectangList[n];
for(i=0;i<n;i++) {
printf("\nNhap thong so Hinh chu nhat %d: ", i);
printf("\nNhap chieu dai: ");
scanf("%f", &rectangList[i].dai);
printf("\nNhap chieu rong: ");
scanf("%f", &rectangList[i].rong);
rectangList[i].dientich = rectangList[i].dai * rectangList[i].rong;
tongdientich = tongdientich + rectangList[i].dientich;
}
printf("Tong dien tich cac HCN la: %f", tongdientich);
return 0;
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2020-09-23 08:01:10
#include <stdio.h>
#include <stdlib.h>
typedef struct HCN_st {
float length, width, area;
} HCN;
int main() {
HCN hcn[5];
float sums = 0;
for (int i = 0; i < 5; i++) {
printf("\nNhap CD %d: ", i + 1); scanf("%f", &hcn[i].length);
printf("\nNhap CR %d: ", i + 1); scanf("%f", &hcn[i].width);
hcn[i].area = hcn[i].width * hcn[i].length;
printf("\ns%d = %f", i + 1, hcn[i].area);
sums += hcn[i].area;
}
printf("\nSUM S = %f", sums);
}