Тема: Перепишіть код з С++ на С
Дуже потрібна допомога в переправлені код з С++ на С. Буду дуже вдячна!)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "string.h"
using namespace std;
struct Car {
char manufacturer[50];
char model[50];
int price;
int power;
int year;
};
void input_one(Car *cars, int n) {
getchar();
cout << "Car: " << n + 1 << endl;
cout << "Manufacturer: ";
gets_s(cars[n].manufacturer);
cout << "Model: ";
gets_s(cars[n].model);
cout << "Price: ";
cin >> cars[n].price;
cout << "Power: ";
cin >> cars[n].power;
cout << "Year: ";
cin >> cars[n].year;
cout << endl;
}
void input(Car *cars, int n) {
for (int i = 0; i < n; i++) {
input_one(cars, i);
}
}
void print_car(Car *cars, int n) {
cout << "Car " << n + 1 << endl;
cout << "Manufacturer: " << cars[n].manufacturer << endl;;
cout << "Model: " << cars[n].model << endl;;
cout << "Price: " << cars[n].price << endl;;
cout << "Power: " << cars[n].power << endl;
cout << "Year: " << cars[n].year << endl;
cout << endl;
}
void print_cars(Car *cars, int n) {
cout << endl;
for (int i = 0; i < n; i++) {
print_car(cars, i);
}
cout << endl;
}
void sort_by_price(Car *cars, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
if (cars[i].price < cars[j].price) {
Car temp = cars[i];
cars[i] = cars[j];
cars[j] = temp;
}
}
}
void sort_by_power(Car *cars, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
if (cars[i].power < cars[j].power) {
Car temp = cars[i];
cars[i] = cars[j];
cars[j] = temp;
}
}
}
void sort_by_year(Car *cars, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
if (cars[i].year < cars[j].year) {
Car temp = cars[i];
cars[i] = cars[j];
cars[j] = temp;
}
}
}
void sort_cars(Car *cars, int n) {
cout << "Choose how do you want to sort cars: " << endl;
cout << "1 - price" << endl;
cout << "2 - power" << endl;
cout << "3 - year" << endl;
int a;
cout << "Your choice: ";
cin >> a;
switch (a) {
case 1:
sort_by_price(cars, n);
break;
case 2:
sort_by_power(cars, n);
break;
case 3:
sort_by_year(cars, n);
break;
default:
return;
}
print_cars(cars, n);
}
Car *delete_element(Car *cars, int *n) {
print_cars(cars, *n);
int el;
getchar();
cout << "Which car do you want to delete? ";
cin >> el;
el--;
Car *temp = new Car[(*n) - 1];
int p = 0;
for (int i = 0; i < *n; i++) {
if (el != i) {
temp[p] = cars[i];
p++;
}
}
(*n)--;
cars = new Car[*n];
for (int i = 0; i < (*n); i++) {
cars[i] = temp[i];
}
return cars;
}
Car *add_element(Car *cars, int *n) {
Car *temp = new Car[*n];
for (int i = 0; i < *n; i++) {
temp[i] = cars[i];
}
(*n)++;
cars = new Car[*n];
for (int i = 0; i < (*n) - 1; i++) {
cars[i] = temp[i];
}
input_one(cars, (*n) - 1);
return cars;
}
void modify_element(Car *cars, int n) {
print_cars(cars, n);
int el;
getchar();
cout << "which car do you want to modify? ";
cin >> el;
input_one(cars, el - 1);
}
void search_car(Car *cars, int n) {
char str[50];
getchar();
cout << "Which manufacturer of car do you want to find: ";
gets_s(str);
for (int i = 0; i < n; i++) {
if (strcmp(str, cars[i].manufacturer) == 0) {
cout << endl;
print_car(cars, i);
cout << endl;
}
}
}
int menu() {
int a;
cout << endl;
cout << "1 - to add car" << endl;
cout << "2 - to find car" << endl;
cout << "3 - to watch cars" << endl;
cout << "4 - to modify car" << endl;
cout << "5 - to delete car" << endl;
cout << "6 - to sort cars" << endl;
cout << "0 - to exit" << endl;
cout << endl;
cout << "Your choice: ";
cin >> a;
cout << endl;
return a;
}
void print_one_to_file(FILE *fptr, Car *cars, int n) {
fprintf(fptr, "Car %d\n", n + 1);
fprintf(fptr, "Manufacturer: %s\n", cars[n].manufacturer);
fprintf(fptr, "Model: %s\n", cars[n].model);
fprintf(fptr, "Price: %d\n", cars[n].price);
fprintf(fptr, "Power: %d\n", cars[n].power);
fprintf(fptr, "Year: %d\n\n", cars[n].year);
}
void print_to_file(FILE *fptr, Car *cars, int n) {
fprintf(fptr, "\n");
for (int i = 0; i < n; i++) {
print_one_to_file(fptr, cars, i);
}
fprintf(fptr, "\n");
}
int main() {
int n;
cout << "How many cars do you want to add: ";
cin >> n;
Car *cars = new Car[n];
input(cars, n);
FILE *fPtr;
fPtr = fopen("file1.txt", "wt");
if (fPtr == NULL)
{
printf("Unable to create file.\n");
exit(EXIT_FAILURE);
}
//fputs(data, fPtr);
fprintf(fPtr, "\nAll the cars");
print_to_file(fPtr, cars, n);
while (true) {
switch (menu()) {
case 1:
cars = add_element(cars, &n);
fprintf(fPtr, "\nAdded new element");
print_to_file(fPtr, cars, n);
break;
case 2:
search_car(cars, n);
break;
case 3:
print_cars(cars, n);
fprintf(fPtr, "\nWatch cars");
print_to_file(fPtr, cars, n);
break;
case 4:
modify_element(cars, n);
fprintf(fPtr, "\nModified element");
print_to_file(fPtr, cars, n);
break;
case 5:
cars = delete_element(cars, &n);
fprintf(fPtr, "\nDeleted an element");
print_to_file(fPtr, cars, n);
break;
case 6:
sort_cars(cars, n);
fprintf(fPtr, "\nSorted cars");
print_to_file(fPtr, cars, n);
break;
case 0:
return 0;
default:
return 0;
}
}
}