-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab11b2.c
More file actions
78 lines (61 loc) · 2.16 KB
/
lab11b2.c
File metadata and controls
78 lines (61 loc) · 2.16 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
#include<string.h>
#include<ctype.h>
struct Product
{
int stt;
char name[100];
int soluong;
int gia;
int tong;
int thanhtoan ;
};
typedef struct Product product;
int main()
{
int n;
printf("Nhap so luong san pham: ");
scanf("%d",&n);
product a[n];
for(int i =0;i<n;i++)
{
getchar();
printf("Thong tin san pham: %d:\n",i+1);
a[i].stt=i+1;
printf("Ten san pham : "); gets(a[i].name);
printf("So luong : "); scanf("%d",&a[i].soluong);
printf("Don gia <$> : "); scanf("%d",&a[i].gia);
printf("Tong tien <$>: "); scanf("%d",&a[i].tong);;
}
int thanhtoan =0;
for(int i =0;i<n;i++)
{thanhtoan += a[i].tong;}
printf("\nDanh sach san pham vua nhap: \n");
printf("+-------+-----------------------------------------------------+----------------------+----------------------+----------------------+\n");
printf("| %-5s | %-51s | %-20s | %-20s | %-20s |\n","STT","Ten san pham","So luong","Don gia","Tong tien" );
printf("+-------+-----------------------------------------------------+----------------------+----------------------+----------------------+\n");
for(int i=0;i<n;i++)
{
printf("| %-5d | %-51s | %20d | %20d | %20d |\n",a[i].stt,a[i].name,a[i].soluong,a[i].gia,a[i].tong);
}
printf("+----------------------------------------------------------------------------------------------------------------------------------+\n");
printf("| %128d |\n",thanhtoan);
printf("+----------------------------------------------------------------------------------------------------------------------------------+\n");
FILE *f;
f=fopen("product.txt","w");
if(f == NULL)
{
printf("khong the mo file.\n");
return 1;
}
for(int i =0;i<n;i++)
{
fprintf(f,"ten san pham: %s\n",a[i].name);
fprintf(f,"so luong: %d\n",a[i].soluong);
fprintf(f,"don gia: %d\n",a[i].gia);
fprintf(f,"tong tien: %d\n",a[i].tong);
fprintf(f,"\n");
}
fclose(f);
return 0;
}