Преобразование кода из с++ в с# - C#
Формулировка задачи:
немного знаком с с# и тут появилась необходимость переписать старый код, написанный мной на с++ в с#
#include <stdio.h> #include <stdlib.h> #include <string.h> #define bool char #define true 1 #define false 0 #define MAXCOUNT 1024 #define stdLen 256 typedef struct Date { int day; int month; int year;}Date; void saveDate (Date d, FILE *f) { fprintf(f,"%d\n%d\n%d\n",d.day,d.month,d.year);} Date loadDate(FILE *f) { Date d; fscanf(f,"%d\n%d\n%d\n",&d.day,&d.month,&d.year); return d;} void printDate(Date d) { printf("Day - %d\nMonth - %d\nYear - %d\n",d.day,d.month,d.year);} Date scanDate() { Date d; printf("Enter Date\n"); printf("Enter a day\n"); scanf("%d",&d.day); printf("Enter a month\n"); scanf("%d",&d.month); printf("Enter a year\n"); scanf("%d",&d.year); return d;} typedef struct Car { Date dateofinspection; int consumption; char *brand; char *number; int id; int owner; int mileage;} Car; typedef struct BaseCar { Car base[MAXCOUNT]; int count; int lastId; } BaseCar; typedef struct Driver { char* name; int id; int cost; int bid; } Driver; typedef struct BaseDriver { Driver base[MAXCOUNT]; int count; int lastId; } BaseDriver; void saveBaseCar(BaseCar bd) { FILE *f; f = fopen("carbase.dat","wb"); fprintf(f,"%d\n",bd.count); fprintf(f,"%d\n",bd.lastId); for (int i=0;i<bd.count;i++) { saveDate(bd.base[i].dateofinspection,f); fprintf(f,"%d\n",bd.base[i].consumption); fprintf(f,"%s\n",bd.base[i].brand); fprintf(f,"%s\n",bd.base[i].number); fprintf(f,"%d\n",bd.base[i].id); fprintf(f,"%d\n",bd.base[i].owner); fprintf(f,"%d\n",bd.base[i].mileage); } fclose(f); } BaseCar loadBaseCar() { FILE *f; BaseCar bd; f = fopen("carbase.dat","rb"); if(f!=NULL) { fscanf(f,"%d\n",&bd.count); fscanf(f,"%d\n",&bd.lastId); for (int i=0;i<bd.count;i++) { bd.base[i].dateofinspection = loadDate(f); fscanf(f,"%d\n",&bd.base[i].consumption); bd.base[i].brand= (char*)malloc(stdLen*sizeof(char)); fscanf(f,"%s\n",bd.base[i].brand); bd.base[i].number= (char*)malloc(stdLen*sizeof(char)); fscanf(f,"%s\n",bd.base[i].number); fscanf(f,"%d\n",&bd.base[i].id); fscanf(f,"%d\n",&bd.base[i].owner); fscanf(f,"%d\n",&bd.base[i].mileage); } fclose(f); return bd; } else { bd.count=0; bd.lastId=0; return bd; } } void saveBaseDriver(BaseDriver bd) { FILE *f; f = fopen("BaseDriver.dat","wb"); fprintf(f,"%d\n",bd.count); fprintf(f,"%d\n",bd.lastId); for (int i=0;i<bd.count;i++) { fprintf(f,"%s\n",bd.base[i].name); fprintf(f,"%d\n",bd.base[i].id); fprintf(f,"%d\n",bd.base[i].cost); fprintf(f,"%d\n",bd.base[i].bid); } fclose(f); } BaseDriver loadBaseDriver() { FILE *f; BaseDriver bd; f = fopen("BaseDriver.dat","rb"); if(f==NULL) { fscanf(f,"%d\n",&bd.count); fscanf(f,"%d\n",&bd.lastId); for (int i=0;i<bd.count;i++) { bd.base[i].name= (char*)malloc(stdLen*sizeof(char)); fscanf(f,"%s\n",bd.base[i].name); fscanf(f,"%d\n",&bd.base[i].id); fscanf(f,"%d\n",&bd.base[i].cost); fscanf(f,"%d\n",&bd.base[i].bid); } fclose(f); return bd; } else { bd.count=0; bd.lastId=0; return bd; } } void addDriver(BaseDriver *bd) { if (bd->count != MAXCOUNT) { Driver d; d.id = bd->lastId; bd->lastId++; printf("Enter name of new driver\n"); d.name = (char*)malloc(stdLen*sizeof(char)); scanf("%s",d.name); printf("enter the cost of petrol\n"); scanf("%d",&d.cost); printf("enter the number of bid\n"); scanf("%d",&d.bid); bd->base[bd->count] = d; bd->count++; printf("New driver is added with name %s\n\n",d.name); } else printf("Base of driver overflowed\n"); } void addCar(BaseCar *bd, BaseDriver *basedriver) { if (bd->count != MAXCOUNT) { printf("\nNew Car:\n"); Car p; printf("Enter a number of car\n"); p.number = (char*)malloc(stdLen*sizeof(char)); scanf("%s",p.number); printf("Enter a consumption of car\n"); scanf("%d",&p.consumption); printf("Enter a brand of car\n"); p.brand = (char*)malloc(stdLen*sizeof(char)); scanf("%s",p.brand); printf("Enter date of inspection\n"); p.dateofinspection = scanDate(); printf("Enter a mileage of car\n"); scanf("%d",&p.mileage); p.id = bd->lastId; bd->lastId++; p.owner = -1; char * nameofdriver; do { printf("Enter a name of driver\n"); nameofdriver = (char*)malloc(stdLen*sizeof(char)); scanf("%s",nameofdriver); for (int i=0;i<basedriver->count;i++) if (strcmp(nameofdriver,basedriver->base[i].name) == 0) { p.owner = basedriver->base[i].id; } if (p.owner == -1) { printf("This driver doesn't exist\n"); } } while (p.owner == -1); bd->base[bd->count] = p; bd->count++; printf("Car is added with id = %d\n\n",p.id); } else printf("Base of car overflowed\n"); } int findCarById(BaseCar* bd,int id) { int i=0; while (i<bd->count&&bd->base[i].id!=id) i++; if(i>=bd->count) return -1; return i; } int findDriverById(BaseDriver* bd,int id) { int i=0; while (i<bd->count&&bd->base[i].id!=id) i++; if(i>=bd->count) return -1; return i; } void removeCar(BaseCar *bd, int id) { for(int i = id;i<(bd->count-1);i++) bd->base[i] = bd->base[i+1]; bd->count--; } void removeDriver(BaseDriver *bd, BaseCar *bp) { printf("\nDelete a driver:\n"); printf("Enter name of driver\n"); char *name = (char*)malloc(stdLen*sizeof(char)); scanf("%s",name); for (int i=0;i<bd->count;i++) { if(strcmp(name,bd->base[i].name) == 0) { for (int j = 0; j < bp->count; j++) { if (bp->base[j].owner == bd->base[i].id) removeCar(bp,j); } for(;i<(bd->count-1);i++) bd->base[i] = bd->base[i+1]; bd->count--; break; } } printf("Driver is deleted\n\n"); } void removeCarbyId(BaseCar *bp) { printf("\nDelete a car:\n"); printf("Enter a id of car\n"); int id; scanf("%d",&id); int index=findCarById(bp,id); if (index!=-1) { removeCar(bp,index); printf("Car is deleted\n\n"); } else printf("Car doesn't exist\n\n"); } void showCar(Car *p) { printf("Id - %d\n",p->id); printf("Number - %s \n",p->number); printDate(p->dateofinspection); printf("ID of driver - %d\n",p->owner); printf("Brand - %s\n",p->brand); printf("Consumption - %d\n",p->consumption); printf("Mileage - %d\n",p->mileage); } void showDriver(Driver *d) { printf("Id - %d\n",d->id); printf("Name - %s \n",d->name); printf("Bid - %d\n",d->bid); printf("Cost - %d\n",d->cost); } void showCarbyBrand(BaseCar *bp) { printf("\nShow car by Brand:\n"); printf("Enter brand\n"); char *brand = (char*)malloc(stdLen*sizeof(char)); scanf("%s",brand); for (int i=0;i<bp->count;i++) if (!strcmp(brand,bp->base[i].brand)) { showCar(&bp->base[i]); printf("\n"); } printf("\n"); } void showByOwner(BaseCar *bp) { printf("Enter id Driver\n"); int id; scanf("%d",&id); for (int i=0;i<bp->count;i++) { if(id==bp->base[i].owner) showCar(&bp->base[i]); } } void showBaseDriver(BaseDriver *bd) { printf("\nBase of Drivers\n"); printf("Found - %d\n",bd->count); for(int i = 0;i<bd->count;i++) { printf("ID - %d\n",bd->base[i].id); printf("Name - %s\n\n",bd->base[i].name); } printf("\n"); } void showBaseCar(BaseCar *bp) { printf("\nBase of Drivers\n"); printf("Found - %d\n",bp->count); for(int i = 0;i<bp->count;i++) { showCar(&bp->base[i]); printf("\n"); } printf("\n"); } void showDriverByBid(BaseDriver *bd) { printf("Enter the number of bid\n"); int bid; scanf("%d",&bid); for(int i = 0;i<bd->count;i++) { if(bid==bd->base[i].bid) showDriver(&bd->base[i]); } printf("\n"); } void showDriverByCost(BaseDriver *bd) { printf("\nEnter amount cost of petrol\n"); int cost; scanf("%d",&cost); for(int i = 0;i<bd->count;i++) { if(cost==bd->base[i].cost) showDriver(&bd->base[i]); } printf("\n"); } void showCarById(BaseCar *bp) { printf("\nShow a car:\n"); printf("Enter a id of car\n"); int id; scanf("%d",&id); int index=findCarById(bp,id); if (index!=-1) { showCar(&bp->base[index]); } else printf("Car doesn't exist\n\n"); } void showDriverById(BaseDriver *bd) { printf("\nShow a driver:\n"); printf("Enter a id of driver\n"); int id; scanf("%d",&id); int index=findDriverById(bd,id); if (index!=-1) { showDriver(&bd->base[index]); } else printf("Driver doesn't exist\n\n"); } void addDriverbid(BaseDriver *bd) { printf("Enter a id of driver\n"); int id, i=0; scanf("%d",&id); int index=findDriverById(bd,id); if (index!=-1) { Driver d; printf("enter the number of bid\n"); scanf("%d",&bd->base[i].bid); } else printf("Driver doesn't exist\n\n"); } void addDrivercost(BaseDriver *bd) { printf("Enter a id of driver\n"); int id, i=0; scanf("%d",&id); int index=findDriverById(bd,id); if (index!=-1) { Driver d; printf("issuance of money to spend\n"); scanf("%d",&bd->base[i].cost); } else printf("Driver doesn't exist\n\n"); } void addCarmileage(BaseCar *bp) { printf("Enter a id of car\n"); int id,z1, i=0,z=0,p=0; scanf("%d",&id); int index=findCarById(bp,id); if (index!=-1) { printf("Enter the number of spent fuel\n"); scanf("%d",&z); p=z*5; printf("Brand - %s\n",bp->base[i].brand); printf("mileage - %d\n",p); z1=bp->base[i].mileage+p; printf("actual mileage - %d\n\n", z1); } else printf("Driver doesn't exist\n\n"); }
Решение задачи: «Преобразование кода из с++ в с#»
textual
Листинг программы
using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication6 { class Program { struct Date { public int day; public int month; public int year; } void saveDate (Date d) { FileInfo fi = new FileInfo(@"C:\Users\Виталий\Desktop\MyTest.txt"); using (StreamWriter sw = fi.AppendText()) sw.WriteLine("{0}\n {1}\n {2}\n",d.day, d.month, d.year); } Date loadDate(Date d) { FileInfo fi = new FileInfo(@"C:\Users\Виталий\Desktop\MyTest.txt"); using (StreamReader sr = fi.OpenText()) { string s = ""; while ((s = sr.ReadLine(/*d.day, d.month, d.year*/)) != null) { Console.WriteLine(s); } return d; } } void printDate(Date d) { Console.Write("Day - {0}\nMonth - {1}\nYear - {2}\n",d.day,d.month,d.year); } Date scanDate(Date d) { Console.Write("Enter Date\n"); Console.Write("Enter a day\n"); d.day = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter a month\n"); d.month=Convert.ToInt32(Console.ReadLine()); Console.Write("Enter a year\n"); d.year = Convert.ToInt32(Console.ReadLine()); return d; } struct Car { Date dateofinspection; int consumption; char brand; char number; int id; int owner; int mileage; } struct BaseCar { int count; int lastId; } struct Driver { char name; int id; int cost; int bid; } struct BaseDriver { int count; int lastId; }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д