NNLTBC 2013 2014 D1

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Câu 1

#include <stdio.h>
#include <conio.h>
#include <string.h>

struct Sach
{
char Ten[100];
int SoLuong;
int SoTien;
};

int TongSoTien(Sach *ds, int n)


{
int s=0, i;
for (i=0; i<n; i++)
{
s += ds[i].SoTien;
}
return s;
}

void HienThi(Sach *ds, int n)


{
int max = -1;
for (int i=0; i<n; i++)
if (ds[i].SoTien > max)
{
max = ds[i].SoTien;
}
for (int i=0; i<n; i++)
if (ds[i].SoTien == max)
{
printf("- Ten sach: %s\n", ds[i].Ten);
printf(" So luong hien co: %d.000d\n", ds[i].SoLuong);
}
printf("* So tien ma moi dau sach dong gop: %d.000d\n", max);
}

int Xoa(Sach *ds, int &n)


{
int dem = 0, i = 0, j;
do{
if (ds[i].SoLuong==0)
{
for (j=i; j<n-1; j++)
ds[j] = ds[j+1];
n--; dem++;
}
else i++;
} while (i<n);

return dem;
}

void main()
{
Sach ds[100];
int n=3;

strcpy(ds[0].Ten, "Lap trinh C co ban");


ds[0].SoLuong = 0;
ds[0].SoTien = 60;

strcpy(ds[1].Ten, "Lap trinh C nang cao");


ds[1].SoLuong = 5;
ds[1].SoTien = 60;

strcpy(ds[2].Ten, "Thuat toan");


ds[2].SoLuong = 0;
ds[2].SoTien = 30;

printf("a. Cho den thoi diem nay, tong so tien da dong gop cho Quy khuyen hoc
la: “);
printf(“%d.000d\n\n", TongSoTien(ds,n));

printf("b. Cac dau sach co dong gop nhieu nhat cho Quy khuyen hoc:\n");

HienThi(ds,n);

printf("c. Cac dau sach hien co trong Thu vien:\n");


Xoa(ds,n);
for (int i=0; i<n; i++)
{
printf("- Ten sach: %s\n", ds[i].Ten);
printf(" So luong hien con: %d\n", ds[i].SoLuong);
printf(" So tien dong gop: %d.000d\n\n", ds[i].SoTien);
}

getch();
}

Câu 2
#include <stdio.h>
#include <conio.h>
struct ThongKe
{
int ch;
long SoKyTu;
}ds[256];
int n;
//cau a
int TimKiem(ThongKe ds[256],int n, int ch)
{
for(int i=0;i<n;i++)
if(ds[i].ch==ch)
return i;
return -1;
}

//Cau b
void ThongKeKyTu(ThongKe ds[256],int *n)
{
FILE *f=fopen("Data.txt","r");

if(f==NULL)
{ printf("Khong mo duoc file");
Return; }
*n=0;

while(!feof(f))
{
int ch=fgetc(f);
int vt=TimKiem(ds,*n,ch);
if(vt==-1)
{
ds[*n].ch=ch;
ds[*n].SoKyTu=1;
(*n)++;
}
else
ds[vt].SoKyTu= ds[vt].SoKyTu+1;

}
fclose(f);
}

//Cau c
void HienThiMax(ThongKe ds[256],int n)
{ int max=ds[0].SoKyTu;int vt=0;

for(int i=0;i<n;i++)
if(ds[i].ch>='0' && ds[i].ch<='9' &&
max<ds[i].SoKyTu)
{
max=ds[i].SoKyTu;vt=i;
}

printf(" Ky tu so dau tien %c dat max: %d ",


ds[vt].ch,ds[vt].SoKyTu);
}

//Cau d
void SoDong(ThongKe ds[256],int n)
{
int d=0,d2=0;

for(int i=0;i<n;i++)
{
if(ds[i].ch==10) d=ds[i].SoKyTu;
d2=d2+ds[i].SoKyTu;
}

printf("So dong : %d\n Dung luong %d", d+1,d2+d-1);


}

Câu 3
//Cau a.
char *left(char *st,int n)
{ char *p;
int i,j;
if (strlen(st)<=n) return(st);
i=0;
while (i<n)
{p[i]=st[i];
i++;}
p[i]='\0';
return(p);
}

//Cau b.
# define nhonhat(a,b) ((a) > (b) ? (b) : (a))
char *trich(char *nguon, char n, char so)
{
char k,*chuoi;
so=nhonhat(so,strlen(nguon));
chuoi=(char *)malloc(so+1);
if (chuoi == NULL)
return(" ");
for (k=0; k<so && nguon[k+n-1];k++)
chuoi[k]=nguon[k+n-1];
chuoi[k]='\0';
return(chuoi);
}

You might also like