01/10/2018, 15:26
Cách gọi tên một chương trình con trong lệnh switch
Mong mọi người giúp đỡ ạ! Em cảm ơn
Sau đây là code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include <iostream>
#define MAX_SIZE 255
//dinh nghia cau truc mat hang
typedef struct mathang {
char tenhang[MAX_SIZE];
int gia;
int soluong;
struct mathang* next;
}mathang_t;
typedef
mathang_t* mathang_ptr;
mathang_ptr create_mathang(char* tenhang_, int gia_, int soluong_) {
mathang_ptr mathang = (mathang_ptr)malloc(sizeof(mathang_t));
strcpy(mathang->tenhang, tenhang_);
mathang->gia = gia_;
mathang->soluong = soluong_;
mathang->next = NULL;
return mathang;
}
mathang_t* enter() {
int soluong, gia;
char kq1;
char tenhang[MAX_SIZE];
system("cls");
printf("chon mat hang moi
");
while(getchar()!= '
');
printf ("ten cua mat hang
");
gets(tenhang);
printf("gia:");
scanf("%d", &gia);
printf("so luong:");
scanf ("%d", &soluong);
return create_mathang(tenhang, gia, soluong);
}
void in(mathang_ptr dau){
mathang_ptr p=dau;
while(p){
printf(" %20s %10d %8d
",p->tenhang, p->gia, p->soluong);
p=p->next;
}
}
void them_dau(mathang_ptr *dau, mathang_ptr no){
if(*dau){
no->next=*dau;
*dau=no;
}
else{
*dau=no;
}
}
void them_duoi (mathang_ptr* dau, mathang_ptr no){
if(*dau){
mathang_t *p=*dau;
while(p->next){
p=p->next;
}
p->next=no;
}
else{
*dau=no;
}
}
void chen(mathang_ptr* dau, mathang_ptr no, int k){
if(k<=0){
them_dau(dau,no);
}
else{
if(*dau){
mathang_t *p=*dau;
int i=1;
while(p->next && i<k){
p=p->next;
i++;
}
no->next=p->next;
p->next=no;
}
else{
*dau=no;
}
}
}
void remove_dau(mathang_ptr* dau){
mathang_ptr p=*dau;
if (p){
*dau=p->next;
free(p);
p=NULL;
}
}
void remove_duoi(mathang_ptr* dau){
mathang_ptr p=*dau;
if(p){
if (p->next ==NULL)
{
free(p);
p=NULL;
*dau=NULL;
}
else{
mathang_ptr r=p->next;
while(r->next){
p=r;
r=r->next;
}
free(r);
r=NULL;
p->next=NULL;
}
}
}
int menu(){
system("cls");
printf("1. Nhap mat hang moi
");
printf("2. In tat ca cac mat hang
");
printf("3. Chen vao dau mot mat hang moi
");
printf("4. Chen vao duoi mot mat hang moi
");
printf("5. Chen vao mot mat hang moi o k
");
printf("6. Dem mat hang co gia lon hon x
");
printf("7. Tro ve mat hang dau
");
printf("8. Tro ve mat hang duoi
");
printf("9. Tim san pham bang code
");
printf("0. Thoat
");
printf("Chon so cua ban:");
int luachon;
scanf ("%d", &luachon);
return luachon;
}
int main()
{
char tenhang;
int tl, gia, soluong;
mathang_t mathang;
menu:;
tl= menu();
switch (tl)
{
case 1:
*enter();
break;
case 2:
mathang = *enter();
in(mathang);
break;
}
goto menu;
getch();
return 0;
}

Bài liên quan
Hi Trần Phụng.
Bạn đọc thông báo lỗi xem.
làm sao để khắc phục lỗi đó bây giờ ạ =(((