By GokiSoft.com| 20:21 02/10/2023|
Lập Trình C

Quản lý motobike - Quản lý xe cộ - Lập trình C BT1917

1. Data [1.0]

Declare 1 struct:

-struct motorbike_st: char name[20], char manufacturer[10], char madein[12], long int price 

-     In main() function, declare an array of 3 motorbikes named “motorbikeList”. 

2. Write a menu-based program with the following menu: [1.5]

  1. Input data of motorbike

  2. Sort, display details information and statistic of all  motorbikes

  3. Find the  motorbike  of manufacturer

  4. Save to text file motorbike.txt.

  5. Exit.

3.  Input data of motorbikes[ 4.0]

When user chooses 1 from main menu, do the followings:

Example: (text with bold format as data input from user)

Enter data of motorbike 1:

Name: Air Blade

Manufacturer: Honda

Made In: Viet nam

Price: 40000000

This module must be coded in a function which does the followings:

  • Have a motorbike_st struct array arguments  

  • Store value from user’s input into motorbikeList array in main() function.

4. Sort, display details information and statistic of all motorbikes.[ 6.0]

When user chooses 2 from main menu, sort the motorbikes with descending order of manufacturer and display details information of each motorbikes with format below. Display number of motorbikes by manufacturers:

No ||Name ||Manufacturer||Made In||Price||

001|| Sirius||Yamaha||Viet nam||28000000||

002||Air Blade||Honda||Viet nam||40000000||

003||Dyland||Honda||Thai Lan||150000000||

Yamaha has 1 motorbike

Honda has 2 motorbike

This module must be coded in a function which does the followings:

  • Have a motorbike_ststruct array argument 

5. Find the  motorbike of manufacturer   [4.0]

When user chooses 3 from menu, do the followings:

  • Display “Enter manufacturer for search : ” 

  • Find and display detail information of the motorbikes of this manufacturer with format in Question 4. In case you don't find any motorbike of this manufacturer display “There are no motorbike of this manufacturer

This module must be coded in a function which does the followings:

  • Have a motorbike_ststruct array argument 

6. Save to text file motorbike.txt[3.0]

When user chooses 4 from menu, the program save name and manufacturer of each motorbike into a text file  “motorbike.txt”. 

This module must be coded in a function which does the followings:

  • Have a motorbike_st struct pointer argument.

  • Create a file “motorbike.txt” in text mode 

  • Store name, manufacturer of each motorbike into this file  

  • Close the file. 

7. Exit[ 0.5 ]

When user chooses 5 from main menu, the program end.

Liên kết rút gọn:

https://gokisoft.com/1917

Bình luận

avatar
Nguyễn Tiến Đạt [T2008A]
2020-10-03 01:55:38



//Assignment Quan li motor(checked)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct motorbike_st{
	char name[20],manufacturer[10],madein[12];
	long int price;
}Motorbike;

void Menu(){
	printf("\n1.Input data of motorbike");
	printf("\n2.Sort, display details information and statistic of all motorbikes");
	printf("\n3.Find the motorbike of manufacturer");
	printf("\n4.Save to text file motorbike.txt");
	printf("\n5.Exit");
}

void inputmotorbike(Motorbike *p){
	int i;
	for(i=0;i<3;i++){
		fflush(stdin);fflush(stdout);
		printf("\nEnter data of motorbike %d:",i+1);
		fflush(stdin);fflush(stdout);
		printf("\nName: ");
		gets(p[i].name);
		fflush(stdin);fflush(stdout);
		printf("\nManufacturer: ");
		gets(p[i].manufacturer);
		fflush(stdin);fflush(stdout);
		printf("\nMade in: ");
		gets(p[i].madein);
		fflush(stdin);fflush(stdout);
		printf("\nPrice: ");
		scanf("%ld",&p[i].price);
		fflush(stdin);fflush(stdout);
	}
}

void sortmotorbike(Motorbike *p){
	int i,j;
	for(i=0;i<2;i++){
		for(j=i+1;j<3;j++){
			int cmp=strcmp(p[i].manufacturer,p[j].manufacturer);
			if(cmp<0){
				Motorbike temp=p[i];
				p[i]=p[j];
				p[j]=temp;
			}
		}
	}
}

void displaymotorbike(Motorbike *p){
	int i;
	printf("\n%-5s||%-20s||%-15s||%-12s||%-15s||","No","Name","Manufacturer","Made in","Price");
	for(i=0;i<3;i++){
		printf("\n00%d  ||%-20s||%-15s||%-12s||%-15ld||",i+1,p[i].name,p[i].manufacturer,p[i].madein,p[i].price);
	}
	printf("\n");
	int count[3]={1,1,1};
	int j;
	for(i=0;i<2;i++){
		if(count[i]==0){
			continue;
		}
		for(j=i+1;j<3;j++){
			if(count[j]==0){
				continue;
			}
			int cmp=strcmp(p[i].manufacturer,p[j].manufacturer);
			if(cmp==0){
				count[i]++;
				count[j]--;
			}
		}
	}
	for(i=0;i<3;i++){
		if(count[i]>0){
			printf("\n%s has %d motorbike",p[i].manufacturer,count[i]);
		}
	}
	printf("\n");
}

void manufacturersearch(Motorbike *p){
	int i;
	int count=0;
	char company[15];
	fflush(stdin);fflush(stdout);
	printf("\nEnter manufacturer for search: ");
	gets(company);
	fflush(stdin);fflush(stdout);
	for(i=0;i<3;i++){
		int cmp=strcmp(company,p[i].manufacturer);
		if(cmp==0){
			count++;
		}
	}
	if(count!=0){
		printf("\n%-5s||%-20s||%-15s||%-12s||%-15s||","No","Name","Manufacturer","Made in","Price");
	}
	count=0;
	for(i=0;i<3;i++){
		int cmp=strcmp(company,p[i].manufacturer);
		if(cmp==0){
			count++;
			printf("\n00%d  ||%-20s||%-15s||%-12s||%-15ld||",i+1,p[i].name,p[i].manufacturer,p[i].madein,p[i].price);
		}
	}
	if(count==0){
		printf("\nThere are no motorbike of this manufacturer");
	}
	printf("\n");
}

void savefile(Motorbike *p){
	FILE *fp;
	fp=fopen("motorbike.txt","w");
	int i;
	fprintf(fp,"%-5s||%-20s||%-15s||%-12s||%-15s||","No","Name","Manufacturer","Made in","Price");
	for(i=0;i<3;i++){
		fprintf(fp,"\n00%d  ||%-20s||%-15s||%-12s||%-15ld||",i+1,p[i].name,p[i].manufacturer,p[i].madein,p[i].price);
	}
	fclose(fp);
}

int main(){
	Motorbike motorbikeList[3];
	int choose;
	while(1){
		Menu();
		printf("\nSelect your program: ");
		scanf("%d",&choose);
		switch(choose){
			case 1:
				inputmotorbike(motorbikeList);
			break;
			case 2:
				sortmotorbike(motorbikeList);
				displaymotorbike(motorbikeList);
			break;
			case 3:
				manufacturersearch(motorbikeList);
			break;
			case 4:
				savefile(motorbikeList);
				printf("\nSuccessfully saved!!\n");
			break;
			case 5:
				printf("\nGoodbye!!\n");
				exit(0);
			break;	
			default:
				printf("\nWrong command!!\n");
			break;	
		}
	}
}


avatar
Đặng Trần Nhật Minh [T2008A]
2020-10-02 09:55:47



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

int n = 3;

typedef struct motorbike_st {
	
	char name[20], manufacturer[10], madein[12];
	long int price;
	
} mt;

void swap(mt &a, mt &b) {
	
	mt temp = a;
	a = b;
	b = temp;
	
}

void inputMotor(mt *a, int n) {
	
	for (int i = 0; i < n; i++) {
		
		printf("\nEnter data of motorbike %d:", i + 1);
					
		printf("\nName: ");
		fflush(stdin); fflush(stdout);
		gets(a[i].name);
					
		printf("\nManufacturer: ");
		fflush(stdin); fflush(stdout);
		gets(a[i].manufacturer);
					
		printf("\nMade in: ");
		fflush(stdin); fflush(stdout);
		gets(a[i].madein);
		
		printf("\nPrice: ");
		fflush(stdin); fflush(stdout);
		scanf("%d", &a[i].price);
								
	}
	
}

void sortMotor(mt *a, int n) {
	
	int index_MAX;
	
	for (int i = 0; i < n - 1; i++) {
					
		index_MAX = i;
				
		for (int j = i + 1; j < n; j++)
		if (strcmp(a[index_MAX].name, a[j].name) < 0) index_MAX = j;
				
		swap(a[index_MAX], a[i]);
					
	}
	
//	printf("\n==============================================================================================");
	printf("\n|No.|Name                          |Manufacturer        |Made In        |Price               |"); //|3|30|20|15|20|
	printf("\n==============================================================================================");
	
	for (int i = 0; i < n; i++)
		printf("\n|00%d|%-30s|%-20s|%-15s|%-20d|", i + 1, a[i].name, a[i].manufacturer, a[i].madein, a[i].price);
		
	printf("\n===============================================================================================");
	
	char *temp, cnt = 1;
	temp = (char *)malloc(30 * sizeof(char));
	strcpy(temp, a[0].manufacturer);
	for (int i = 1; i < n; i++) {
		
		if(strcmp(a[i].manufacturer, temp) == 0) cnt++;
		else {
			
			printf("\n%s has %d motorbikes.", temp, cnt);
			free(temp);
			strcpy(temp, a[i].manufacturer);
			cnt = 1;
			
		}
		
		if (i == n - 1) printf("\n%s has %d motorbikes.", temp, cnt);
		
	}
	
}

void searchMotor(mt *a, int n) {
	
	printf("\nEnter manufacturer for search: ");
	fflush(stdin); fflush(stdout);
	char ss[30]; gets(ss);
	
	bool check = false;
	
	for (int i = 0; i < n; i++)
		if (strcmp(a[i].manufacturer, ss) == 0) printf("\nName: %s", a[i].name), check = true;
		
	if (!check) printf("\nThere are no motorbike of this manufacturer.");
	
}

void outputMotor(mt *a, int n) {
	
	FILE *fptr;
    fptr = fopen("motorbike.txt","w");
 
    if(fptr == NULL) printf("Error!"), exit(1);
    
    fprintf(fptr, "\n|No.|Name                          |Manufacturer        |Made In        |Price               |"); //|3|30|20|15|20|
	fprintf(fptr, "\n==============================================================================================");
	
	for (int i = 0; i < n; i++) {
		
		fprintf(fptr, "\n|00%d|%-30s|%-20s|%-15s|%-20d|", i + 1, a[i].name, a[i].manufacturer, a[i].madein, a[i].price);
		
	}
	
//	fwrite(a, sizeof(SV), n, fptr);
	
	fclose(fptr);
	
}

int main() {

	mt motorbikeList[n];
	
	int sel;
	
	while (true) {
		
		printf("\n==========Menu=========");
		printf("\n1. Input data of motorbike.");
		printf("\n2. Sort, display details information and statistic of all  motorbikes.");
		printf("\n3. Find the  motorbike  of manufacturer.");
		printf("\n4. Save to text file motorbike.txt.");
		printf("\n5. Exit.");	
		printf("\n========================");
		printf("\nNhap lua chon cua ban: "); 
		fflush(stdin); fflush(stdout);
		scanf("%d", &sel);
		
		switch (sel) {
			
			case 1:
				
				inputMotor(motorbikeList, n);
				
				break;
				
			case 2:
				
				sortMotor(motorbikeList, n);
				
				break;
				
			case 3:
				
				searchMotor(motorbikeList, n);
				
				break;
				
			case 4:
				
				outputMotor(motorbikeList, n);
				
				break;
				
			case 5:
				
				printf("EXIT!");
				
				exit(0);
				
			default:
				
				printf("\nNHAP SAI!");
				
				break;
			
		}
		
	}
	
	return 0;

}