Chuyển giúp code C++ sang code C

#include<iostream>
#include<string.h>
using namespace std;
typedef struct sinhvien
{
	string ten;
	float diem;
	int mssv;
};
void nhapsinhvien(sinhvien &x)
{
	fflush(stdin);
	cout<<"\nnhap ten sinh vien:";
	getline(cin,x.ten);
	fflush(stdin);
	cout<<"\nnhap diem:";cin>>x.diem;
	fflush(stdin);
	cout<<"\nnhap ma sinh vien:";cin>>x.mssv;
}
void xuatmotsinhvien(sinhvien x)
{
	cout<<"\nten:"<<x.ten;
	cout<<"\ndiem:"<<x.diem;
	cout<<"\nmssv:"<<x.mssv;
}
typedef struct danhsach
{
	int n;
	sinhvien *arr;
};
void nhapnhieusinhvien(danhsach ds)
{
	int i;
	for(i=0;i<ds.n;i++)
	{
		nhapsinhvien(ds.arr[i]);
	}
}
void xuatnhieusinhvien(danhsach ds)
{
	
	int i;
	for(i=0;i<ds.n;i++)
	{
		xuatmotsinhvien(ds.arr[i]);
	}
}
void themmotsinhvien(int soluong,danhsach &ds)
{
	int i,z,k;
	z=(ds.n)+soluong;
	k=ds.n;
	// do tung sinh vien sang mot mang moi 
	 sinhvien *brr= new sinhvien[ds.n];
	 for(i=0;i<ds.n;i++)
	 {
	 	brr[i]=ds.arr[i];
	 }
	 // cap phat co vung nho cua mang a du bang voi sl input
	 ds.arr=new sinhvien[(ds.n)+soluong];
	 // do mang b sang lai mang dau
	 for(i=0;i<ds.n;i++)
	 {
	 	ds.arr[i]=brr[i];
	 }
	 for(i=k;i<z;i++)
	 {
	 	nhapsinhvien(ds.arr[i]);
	 	ds.n++;
	 }
		
}
int main()
{
	int soluong;
	sinhvien x;
	danhsach ds;
	cout<<"nhap so luong sinh vien:";cin>>ds.n;
	ds.arr=new sinhvien[ds.n];
	nhapnhieusinhvien(ds);
	cout<<"\nmang sau khi them sinh vien:";
	cout<<"\nnhap so luong sinh vien can them:";cin>>soluong;
	themmotsinhvien(soluong,ds);
	xuatnhieusinhvien(ds);
}

Đổi references thành pointers, new thành malloc(), cout thành printf(), cin thành scanf().

3 Likes

chỗ new chuyển thành malloc() lm s, e ko hiểu chỗ đó

có thể chuyển giúp e chỗ new thành malloc đc ko ạ?

Ví dụ

sinhvien *brr = new sinhvien[ds.n];

malloc():

sinhvien *brr = (sinhvien *) malloc(sizeof(sinhvien) * ds.n);

malloc(n) sẽ allocate n bytes cho brr, trong trường hợp này là kích thước của struct sinhvien nhân với số lượng sinh viên. new thì bớt được khoản không cần phải request số bytes.

2 Likes

tks a code chạy đc r mà hình như còn lỗi ở phần xuất a xem dùm e đc ko?

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

typedef struct
{
    char HoTen[30];
    float diemTB;
}
sinhvien;
typedef struct
{
    int n;
    sinhvien *arr;
}
danhsach;
void nhapsv(sinhvien sv)
{
    rewind(stdin);
    size_t len;
    printf("Nhap ten sinh vien: ");
    fgets(sv.HoTen, 30, stdin);
    len = strlen(sv.HoTen)-1;
    if (sv.HoTen[len] == '\n')
        sv.HoTen[len] = '\0';
    printf("Nhap DTB: ");
    scanf("%f",&sv.diemTB);
}
void xuatsv(sinhvien sv)
{
    printf("\n------------------------\n");
    printf("%s\n",sv.HoTen);
    printf("%.2f",sv.diemTB);
}
void nhapnhieusv(danhsach ds)
{
    for (int i = 0; i < ds.n; i++)
    {
        printf("Sinh vien thu %d: ",i+1);
        printf("\n------------------------\n");
        nhapsv(ds.arr[i]);
    }
}
void xuatnhieusv(danhsach ds)
{
    for (int i = 0; i < ds.n; i++)
    {
        xuatsv(ds.arr[i]);
    }
}
void themsinhvien(int soluong, danhsach ds)
{
    int z,k;
    z = (ds.n)+soluong;
    k = ds.n;
    sinhvien* brr = (sinhvien*)malloc(sizeof(sinhvien)*ds.n);
    for (int i = 0; i<ds.n; i++)
    {
        brr[i]=ds.arr[i];
    }
    ds.arr=(sinhvien*)malloc(sizeof(sinhvien)*((ds.n)+soluong));
    for (int i=0; i<ds.n; i++)
    {
        ds.arr[i]=brr[i];
    }
    for (int i =k; i<z; i++)
    {
        nhapsv(ds.arr[i]);
        ds.n++;
    }
}
int main(int argc, const char * argv[])
{
    danhsach ds;
    int soluong;
    printf("Nhap so luong sinh vien: ");
    scanf("%d",&ds.n);
    ds.arr = (sinhvien*) malloc(sizeof(sinhvien)*ds.n);
    nhapnhieusv(ds);
    printf("Nhap so luong sinh vien can them: ");
    scanf("%d",&soluong);
    themsinhvien(soluong, ds);
    xuatnhieusv(ds);
}

Thay reference thành pointer mà bạn lại cho nó thành value thế thì nhập với xuất không ăn khớp là đúng rồi

Chỗ & ở c++ thay thành *
Rồi xem compiler báo lỗi thế nào thì sửa thế đó

2 Likes

Tham số này không phải là con trỏ thì toạch :smiley: dữ liệu nhập sẽ ko đưa ra ngoài được. Tương tự cho những hàm còn lại.

2 Likes

e ra đc r cảm ơn mọi người đã giúp đỡ, đây là code của e

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

typedef struct
{
    char HoTen[30];
    float diemTB;
}
sinhvien;
typedef struct
{
    int n;
    sinhvien *arr;
}
danhsach;
void nhapsv(sinhvien *sv)
{
    rewind(stdin);
    size_t len;
    printf("Nhap ten sinh vien: ");
    fgets(sv->HoTen, 30, stdin);
    len = strlen(sv->HoTen)-1;
    if (sv->HoTen[len] == '\n')
        sv->HoTen[len] = '\0';
    printf("Nhap DTB: ");
    scanf("%f",&sv->diemTB);
}
void xuatsv(sinhvien sv)
{
    printf("\n------------------------\n");
    printf("HoTen: %s\n",sv.HoTen);
    printf("DTB: %.2f",sv.diemTB);
}
void nhapnhieusv(danhsach ds)
{
    for (int i = 0; i < ds.n; i++)
    {
        printf("Sinh vien thu %d: ",i+1);
        printf("\n------------------------\n");
        nhapsv(&ds.arr[i]);
    }
}
void xuatnhieusv(danhsach ds)
{
    for (int i = 0; i < ds.n; i++)
    {
        xuatsv(ds.arr[i]);
    }
}
void themsinhvien(int soluong, danhsach *ds)
{
    int z,k;
    z = (ds->n)+soluong;
    k = ds->n;
    sinhvien *brr = (sinhvien*)malloc(sizeof(sinhvien)*(ds->n));
    for (int i = 0; i<ds->n; i++)
    {
        brr[i]=ds->arr[i];
    }
    ds->arr=(sinhvien*)malloc(sizeof(sinhvien)*((ds->n)+soluong));
    for (int i=0; i<ds->n; i++)
    {
        ds->arr[i]=brr[i];
    }
    for (int i =k; i<z; i++)
    {
        nhapsv(&ds->arr[i]);
        ds->n++;
    }
}
int main(int argc, const char * argv[])
{
    danhsach ds;
    int soluong;
    printf("Nhap so luong sinh vien: ");
    scanf("%d",&ds.n);
    ds.arr = (sinhvien*) malloc(sizeof(sinhvien)*ds.n);
    nhapnhieusv(ds);
    printf("Nhap so luong sinh vien can them: ");
    scanf("%d",&soluong);
    themsinhvien(soluong, &ds);
    xuatnhieusv(ds);
}
1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?