Em khả năng code của em còn kém mà trình độ tiếng anh của em cũng thấp, mong mọi người xem dùm em lỗi này phải sửa sao ạ :(.
Đây là CODE của em, em đang test thử để hoàn thiện bài thì xảy ra lỗi, mong mọi người xem dùm:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#define MAX 100
typedef struct Node {
int a[MAX];
int size;
};
void init(Node *head)
{
Node *p = head;
p->size = 0;
}
int isfull(Node *head)
{
if(head->size==MAX) return 1;
else return 0;
}
int isempty(Node *head)
{
if(head->size==0) return 1;
else return 0;
}
void input(Node *head,FILE *f)
{
f = fopen("D:\\test.txt", "wb");
Node *p = head;
int n;
printf("so luong phan tu nhap vao danh sach la:");
fscanf_s(f,"%d", &n);
p->size = n;
printf("\nnhap phan tu ghi vao:");
for (int i = 0; i <= p->size-1; i++)
{
printf("\nphan tu thu %d:\t");
scanf_s("%d", &p->a[i]);
}
for (int i = 0; i <= p->size - 1; i++)
{
fwrite(p->a, sizeof(p->a), 1, f);
}
}
int insert(int k, Node *head,FILE *f)
{
Node *p = head;
if (isfull(p))
{
printf("\ndanh sach day");
return 0;
}
if (isempty(p)==1)
{
int k;
printf("\ndanh sach rong");
printf("\n1: nhap lai danh sach \n");
printf("\n2: Exit");
scanf_s("%d", &k);
switch (k)
{
case 1: input(p,f); break;
case 2: break;
default:
break;
}
}
if (k<0 || k>p->size)
{
printf("\ngia tri K khong hop le");
}
//them phan tu k vao 1 vi tri trong danh sach
p->size++;
for (int i = p->size-1; i >= k; i--)
{
p->a[i] = p->a[i - 1];
}
int x;
printf("\nphan tu them vao:\t");
scanf_s("%d", &x);
p->a[k - 1] = x;
return 1;
}
int remove_k(Node *head, int k)
{
Node *p = head;
if (isempty(p)==1)
{
printf("\ndanh sach rong");
return 0;
}
p->a[k - 1] = 0;
for (int i = k - 1; i <= p->size-2; i++)
{
p->a[i + 1] = p->a[i];
}
p->size--;
return 1;
}
int output(Node *head,FILE *f)
{
f = fopen("D:\\test.txt", "rb");
Node *p = head;
/*if (isempty(head)) {
printf("\ndanh sach rong");
return 0;
}*/
printf("\ndanh sach LK:");
for (int i = 0; i <= p->size-1; i++)
{
fread(p->a,sizeof(p->a),1,f);
}
return 1;
}
int search(Node *head,int x)
{
Node *p = head; int *b; int m=0;
b = (int*)malloc(sizeof(int));
if (isempty(head)==1)
{
printf("\ndanh sach rong");
return 0;
}
for (int i = 0; i <= p->size - 1; i++)
{
if (p->a[i] == x)
{
m++;
*(b+m) = i + 1;
}
}
if(m==1) printf("\nx thuoc phan tu thu: %d", *(b+m));
if (m >= 2)
{
printf("\nco %d phan tu trung nhau:", m);
for (int j = 1; j <= m; j++)
{
printf("\nphan tu %d",*(b+j));
}
}
if (m = 0) printf("\nkhong co phan tu ban muon tim");
return 1;
}
void select(Node *head,FILE *f)
{
printf("\nnhap lua chon ban muon:");
int luachon;
scanf_s("%d", &luachon);
Node *p = head;
switch (luachon) {
case 1: {
if (isempty(head)==1) printf("\ndanh sach rong");
else printf("\nso phan tu cua DS : %d", p->size);
output(head,f);
select(head,f);
}
case 2:
{
int k;
printf("\nchon vi tri ban muon chen:\t");
scanf_s("%d", &k);
insert(k, head,f);
select(head,f);
}
case 3:
{
int x;
printf("\nnhap gia tri ban muon tim:");
scanf_s("%d", &x);
search(head, x);
select(head,f);
}
case 4:
{
int k;
printf("\nphan tu ban muon xoa:");
scanf_s("%d", &k);
remove_k(head, k);
select(head,f);
}
case 5:
{
printf("\nchọn 4 để tìm ô chứa giá trị");
printf("\nchọn 5 để xóa ô chứa đó ");
}
case 6:
{
input(head,f);
select(head,f);
}
case 7:
break;
}
}
int main()
{
FILE *f;
Node *head;
head = (Node*)malloc(sizeof(Node));
init(head);
input(head,f);
output(head,f);
printf("Moi ban chon phep toan voi DS LKD:");
printf("\n1: Kiem tra DS");
printf("\n2: Chen phan tu x vao vi tri k trong DS");
printf("\n3: Tim mot phan tu trong DS");
printf("\n4: Xoa phan tu tai vi tri k");
printf("\n5: Xoa phan tu x trong DS");
printf("\n6: nhap lai danh sach");
printf("\n7: Thoat");
select(head,f);
_getch();
return 0;
}
**
## Còn đây là lỗi xảy ra ạ:
**
em thay fopen thành fopen_s thì nó lại sinh ra lỗi C2660:
Em xin cảm ơn trước
