I was coding for a college project, and it is practically finished. But my amazing teacher placed the condition that it would only be possible to send 1 .c file, and i have one header that i used to organize some functions of the code. I am having a lot of trouble making this in one file. Here is the code:
#include <string.h> //Defining one variable type, macro, and various functions for manipulating arrays of characters.
#include "221037581.h" //Defining my own header to import especif functions.
#include <stdlib.h> //Defining the standart library.
#define REG_USI 1 //Register plant
#define REG_CONS 2 //Register consumer
#define REG_CONT 3 //Register contract
#define CHECK_USI 4 //Check plant
#define CHECK_CONS 5 //Check consumer
#define DEL_USI 6 //Delete plant
#define DEL_CONS 7 //Delete consumer
#define EXIT 8 //Exit
#define SIZ_NAME 100 //Defining maximum name size
#define CPF_MIN 11 //Defining cpf size
#define SIZ_ID 18 //Defining id size
#define MAX_SIZE 100 //Defining maximum size for database
#define SIZ_DATE 8 //Defining date size
#define SIZ_CNPJ 14 //Defining cnpj size
#define DB_NAME 'n' //Defining name database
#define DB_ID 'i' //Defining ID database
#define DB_USER 'u' //Defining user database
#define DB_plant 'p' //Defining plant database
#define DB_CONTRACT 'c' //Defining contract database
int main_menu(); //Defining menu function
void r_u(); //Defining register plant function
void r_c(); //Defining register consumer function
void r_contrat(); //Defining register contract function
void c_u(); //Defining check plant function
void c_c(); //Defining check consumer function
void d_u(); //Defining delete plant function
void d_c(); //Defining delete consumer function
void info_plant(char id[SIZ_ID]); //Defining function to consult the plant and show how much MWs still is usable.
void info_user(char id[SIZ_ID]); //Defining function to consult user and your contracts
int main() //Main function
{
int n;
FILE *data_base;
data_base = fopen(DATABASE_PP, "r"); //Opening database
if (data_base == NULL) {
data_base = fopen(DATABASE_PP, "w+");
fprintf(data_base,"0xxxxxxxxxxxxx\nName :3\nxxxxxxxx\n1.000000\n");
}
fclose(data_base); //Closing database
data_base = fopen(DATABASE_CONTRACT, "r"); //Opening contract database
if (data_base == NULL) {
data_base = fopen(DATABASE_CONTRACT, "w+");//Opening contract database
fprintf(data_base,"0xxxxxxxxxxxxx\n0xxxxxxxxxx\nxxxxxxxx\n1.000000\n");
}
fclose(data_base); //Closing database
data_base = fopen(DATABASE_USER, "r"); //Opening user database
if (data_base == NULL) {
data_base = fopen(DATABASE_USER, "w+"); //Opening user database
fprintf(data_base,"0xxxxxxxxxxxxx\nName :3\n");
}
fclose(data_base); //Closing database
n = main_menu();
if(n == EXIT){
printf("\nThanks for using my software\n");
}
return 0;
}
int main_menu(){ //Start menu function
int escolha;
clean(); //Clean screen function
for(;;){
printf("\nPHOTOVOLTAIC ENERGY MANAGEMENT SYSTEM - PEMS\n\n 1 -> Register plant.\n 2 -> Register Consumer.\n 3 -> Register a contract between consumer and plant.\n 4 -> Consult registered plant.\n 5 -> Consult registered consumer.\n 6 -> Delete plant.\n 7 -> Delete consumer.\n 8 -> EXIT.\n\nMake your choice: "); //Menu statement.
scanf(" %d",&escolha); //Recieve choice from user.
switch(escolha){ //Options
case REG_USI: //Register plant.
clean();
r_u();
break;
case REG_CONS: //Register consumer.
clean();
r_c();
break;
case REG_CONT: //Register contract.
clean();
r_contrat();
break;
case CHECK_USI: //Consult plant.
clean();
c_u();
break;
case CHECK_CONS: //Consult consumer.
clean();
c_c();
break;
case DEL_USI: //Delete plant.
clean();
d_u();
break;
case DEL_CONS: //Delete consumer.
clean();
d_c();
break;
case EXIT: //Exit from software.
return EXIT;
break;
default:
clean();
printf("caracter inválido!");
break;
}
}
return escolha;
}
void r_u(){ //Register plant function.
char cnpj[SIZ_ID],name[SIZ_NAME],data[SIZ_DATE]; //Defining structs.
int aux, n;
float mw;
clean();
for(;;){ //Function to define the name of the power plant.
printf("\n Register plant:\n\n");
printf(" Type the name of power plant: ");
scanf(" %[^\n]s",name);
n = id_check(name);
if(n == TRUE){
clean();
printf("\nInsert a valid name.\n");
}else{
break;
}
}
clean();
for(;;){ //Function to define CNPJ of power plant.
printf("\n Register plant:\n\n");
printf(" Type the CNPJ of power plant: ");
scanf(" %s",cnpj);
n = id_check(cnpj);
if(n == FALSE){ //CNPJ verifications.
clean();
printf("\nInsert a valid identification.\n");
}
else{ //Consulting if cnpj already on plants database.
n = id_check_database(cnpj,DB_plant);
if(n == TRUE){
clean();
printf("\nIndentification already used.\n");
}else{ //Consulting if cnpj already on users database.
n = id_check_database(cnpj,DB_USER);
if(n == TRUE){
clean();
printf("\nAlready in use by a user.\n");
}else{
clean();
break;
}
}
}
}
clean(); //Clean screen function.
for(;;){ //Registering date from power plant.
printf("\n Register plant:\n\n");
printf("Type the date from the plant beginning operations (DDMMAAAA): ");
scanf(" %s",data);
n = date_check(data); // Date verification function.
if(n == FALSE){
clean();
printf("\nInsert a valid date.\n");
}
else{
clean();
break;
}
}
clean();
for(;;){ //MGw power plant.
printf("\n Register plant:\n\n");
printf(" Type the potency of the power plant in MGw: ");
scanf(" %f",&mw);
if(mw <= 0){ //Checking if the power of the plant is valid.
clean();
printf("\nType a valid potency.\n");
}
else{
clean();
break;
}
}
POWERP usina;
if(strlen(cnpj) == CNPJ_MIN){ //organizing cnpj
usina.cnpj[0] = cnpj[0];
usina.cnpj[1] = cnpj[1];
usina.cnpj[2] = cnpj[2];
usina.cnpj[3] = cnpj[3];
usina.cnpj[4] = cnpj[4];
usina.cnpj[5] = cnpj[5];
usina.cnpj[6] = cnpj[6];
usina.cnpj[7] = cnpj[7];
usina.cnpj[8] = cnpj[8];
usina.cnpj[9] = cnpj[9];
usina.cnpj[10] = cnpj[10];
usina.cnpj[11] = cnpj[11];
usina.cnpj[12] = cnpj[12];
usina.cnpj[13] = cnpj[13];
usina.cnpj[14] = '\0';
}
if (strlen(data) == DATE_MIN) { //ignore this (sad try)
usina.date[0] = data[0];
usina.date[1] = data[1];
usina.date[2] = data[2];
usina.date[3] = data[3];
usina.date[4] = data[4];
usina.date[5] = data[5];
usina.date[6] = data[6];
usina.date[7] = data[7];
usina.date[8] = '\0';
}
// Preparing for implement on database
for(aux = 0; aux <= SIZ_NAME; aux++){
if(name[aux] == '\0'){
usina.name[aux] = '\0';
break;
}
usina.name[aux] = name[aux];
}
usina.mw = mw;
power_plant_db_add(usina);
//printf("%s, %s\n", usina.cnpj, cnpj);
clean();
printf("Power plant registered with sucess!");
}
void r_c(){ //Function for user register.
USER usuario;
char id[SIZ_ID],name[SIZ_NAME];
int n, aux;
clean();
for(;;){ //Looping for recieve username.
printf("\n Register user:\n\n");
printf(" Type the name of the user: ");
scanf(" %[^\n]s",usuario.name); //Recieving the username.
n = id_check(usuario.name); //Fuctions that it is responsable for name validations.
if(n == TRUE){
clean();
printf("\nInsert a valid name\n");
}else{
break;
}
}
clean();
for(;;){ //Looping for recieve cpf or cnpj from user.
printf("\n Register user:\n\n");
printf(" Type cpf or cnpj from user. ");
scanf(" %s",id);
n = id_check(id); //Cpf and cnpj verification.
if(n == FALSE){
clean();
printf("\nInsert a valid identification. \n");
}
else{
n = id_check_database(id,DB_USER); //Check if identification its already in use.
if(n == TRUE){
clean();
printf("\nIndentification already in use.\n");
}else{
n = id_check_database(id,DB_plant);
if(n == TRUE){
clean();
printf("\nIndentification already in use by the power plant.\n"); //Check if identification its already in use.
}else{
clean();
break;
}
}
}
}
//Adding at data base.
if(strlen(id) == CNPJ_MIN && (id[3] >= NUMERO_MIN && id[3]<=NUMERO_MAX)){ //organizing cnpj
usuario.id[0] = id[0]; //Unnecessary
usuario.id[1] = id[1];
usuario.id[2] = id[2];
usuario.id[3] = id[3];
usuario.id[4] = id[4];
usuario.id[5] = id[5];
usuario.id[6] = id[6];
usuario.id[7] = id[7];
usuario.id[8] = id[8];
usuario.id[9] = id[9];
usuario.id[10] = id[10];
And this is the header file that i want put on the .c file:
#define SIZ_ID 18 //Defining id size
#define MAX_SIZE 100 //Defining maximum size for database
#define SIZ_DATE 8 //Defining date size
#define SIZ_CNPJ 14 //Defining cnpj size
#define MAX_SIZE 100
#define FLOAT_SIZE 10
#define FALSE 0
#define TRUE 1
#define CPF_MIN 11 //Defining cpf size
#define CPF_MAX 14 //Defining max cpf size (unusable)
#define CNPJ_MIN 14 //Defining min cnpj size
#define CNPJ_MAX 18 //Defining max cnpj size (unusable)
#define DATE_MIN 8 //Defining min date size
#define DATE_MAX 10 //Defining max date size (unusable)
#define NUMERO_MIN 48 //RAM
#define NUMERO_MAX 57 //RAM
#define DATABASE_TEMP "temp.txt" //Creating txt to be temperoray database
#define DATABASE_USER "users.txt" //Creating txt to be user database
#define DATABASE_CONTRACT "contrats.txt" //Creating txt to be contracts database
#define DATABASE_PP "plants.txt" //Creating txt to be plants database
#define NAME_OPT 'n'//Defining name
#define ID_OPT 'i' //Defining ID
#define USER_OPT 'u' //Defining user
#define POWERP_OPT 'p' //Defining plant
#define CONTRACT_OPT 'c' //Defining contract
typedef struct {
char cnpj[SIZ_CNPJ]; //Defining cnpj struct
char name[SIZ_NAME]; //Defining name struct
char date[SIZ_DATE]; //Defining date struct
float mw; //Defining potency struct
} POWERP;
typedef struct {
char id[SIZ_ID];//Defining id struct
char name[SIZ_NAME]; //Defining name struct
} USER;
typedef struct {
char cnpj[SIZ_CNPJ];
char id[SIZ_ID];
char date[SIZ_DATE];
float mw;
} CONTRACT;
void clean() { //Defininf clean screen function
#if defined(linux) || defined(unix) || defined(APPLE)
system("clear");
#endif
#if defined(_WIN32) || defined(_WIN64)
system("cls");
#endif
return;
}
int id_check(char id[SIZ_ID]) {
int aux, cpf[CPF_MIN], cnpj[CNPJ_MIN], confirmation, aux2, idAux[SIZ_ID];
int tamanhoString = strlen(id);
confirmation = TRUE;
if (tamanhoString == CPF_MIN || (tamanhoString == CPF_MAX && !(id[3] >= NUMERO_MIN && id[3] <= NUMERO_MAX))) {
if (tamanhoString == CPF_MIN) {
for (aux = 0; aux < CPF_MIN; aux++) {
cpf[aux] = id[aux] - NUMERO_MIN;
}
}
if (cpf[1] == cpf[0] && cpf[2] == cpf[1] && cpf[3] == cpf[2] &&
cpf[4] == cpf[3] && cpf[5] == cpf[4] && cpf[6] == cpf[5] &&
cpf[7] == cpf[6] && cpf[8] == cpf[7] && cpf[9] == cpf[8] &&
cpf[10] == cpf[9]) {
confirmation = FALSE;
}
if (confirmation == TRUE) { //First digit verification
for (aux = 0; aux < CPF_MIN; aux++) {
idAux[aux] = cpf[aux];
}
aux2 = 10;
for (aux = 0; aux < (CPF_MIN - 2); aux++) {
idAux[aux] *= aux2;
aux2--;
}
aux2 = 0;
for (aux = 0; aux < (CPF_MIN - 2); aux++) {
aux2 += idAux[aux];
}
aux2 *= 10;
aux2 %= 11;
if (aux2 != cpf[9] && !(aux2 == 10 || aux2 == 11)) {
confirmation = FALSE;
}
if ((aux2 == 10 || aux2 == 11) && cpf[9] != 0) {
confirmation = FALSE;
}
}
if (confirmation == TRUE) { //Second digit verification
for (aux = 0; aux < CPF_MIN; aux++) {
idAux[aux] = cpf[aux];
}
aux2 = 11;
for (aux = 0; aux < (CPF_MIN - 1); aux++) {
idAux[aux] *= aux2;
aux2--;
}
aux2 = 0;
for (aux = 0; aux < (CPF_MIN - 1); aux++) {
aux2 += idAux[aux];
}
aux2 *= 10;
aux2 %= 11;
if (aux2 != cpf[10] && !(aux2 == 10 || aux2 == 11)) {
confirmation = FALSE;
}
if ((aux2 == 10 || aux2 == 11) && cpf[10] != 0) {
confirmation = FALSE;
}
}
} else {
if (tamanhoString == CNPJ_MIN || (tamanhoString == CNPJ_MAX && (id[3] >= NUMERO_MIN && id[3] <= NUMERO_MAX))) {
if (tamanhoString == CNPJ_MIN) {
for (aux = 0; aux < CNPJ_MIN; aux++) {
cnpj[aux] = id[aux] - NUMERO_MIN;
}
}
idAux[0] = cnpj[0] * 5;
idAux[1] = cnpj[1] * 4;
idAux[2] = cnpj[2] * 3;
idAux[3] = cnpj[3] * 2;
idAux[4] = cnpj[4] * 9;
idAux[5] = cnpj[5] * 8;
idAux[6] = cnpj[6] * 7;
idAux[7] = cnpj[7] * 6;
idAux[8] = cnpj[8] * 5;
idAux[9] = cnpj[9] * 4;
idAux[10] = cnpj[10] * 3;
idAux[11] = cnpj[11] * 2;
aux2 = 0;
for (aux = 0; aux < (CNPJ_MIN - 2); aux++) {
aux2 += idAux[aux];
}
aux2 %= 11;
if (!(aux2 < 2 && cnpj[12] == 0)) {
aux2 = 11 - aux2;
if (aux2 != cnpj[12]) {
confirmation = FALSE;
}
} //organizing cnpj
idAux[0] = cnpj[0] * 6;
idAux[1] = cnpj[1] * 5;
idAux[2] = cnpj[2] * 4;
idAux[3] = cnpj[3] * 3;
idAux[4] = cnpj[4] * 2;
idAux[5] = cnpj[5] * 9;
idAux[6] = cnpj[6] * 8;
idAux[7] = cnpj[7] * 7;
idAux[8] = cnpj[8] * 6;
idAux[9] = cnpj[9] * 5;
idAux[10] = cnpj[10] * 4;
idAux[11] = cnpj[11] * 3;
idAux[12] = cnpj[12] * 2;
aux2 = 0;
for (aux = 0; aux < (CNPJ_MIN - 1); aux++) {
aux2 += idAux[aux];
}
aux2 %= 11;
if (!(aux2 < 2 && cnpj[13] == 0)) {
aux2 = 11 - aux2;
if (aux2 != cnpj[13]) {
confirmation = FALSE;
}
}
} else {
confirmation = FALSE;
}
}
return confirmation;
}
int date_check(char data[SIZ_DATE]) { //Get date in int type.
int day, month, year, aux, bissexto;
int confirmation = 1;
aux = strlen(data);
if (aux == DATE_MIN || aux == DATE_MAX) {
if (aux == DATE_MIN) {
day = (data[0] - NUMERO_MIN) * 10;
day += (data[1] - NUMERO_MIN);
month = (data[2] - NUMERO_MIN) * 10;
month += (data[3] - NUMERO_MIN);
year = (data[4] - NUMERO_MIN) * 1000;
year += (data[5] - NUMERO_MIN) * 100;
year += (data[6] - NUMERO_MIN) * 10;
year += (data[7] - NUMERO_MIN);
}
if (aux == DATE_MAX) {
day = (data[0] - NUMERO_MIN) * 10;
day += (data[1] - NUMERO_MIN);
month = (data[3] - NUMERO_MIN) * 10;
month += (data[4] - NUMERO_MIN);
year = (data[6] - NUMERO_MIN) * 1000;
year += (data[7] - NUMERO_MIN) * 100;
year += (data[8] - NUMERO_MIN) * 10;
year += (data[9] - NUMERO_MIN);
}
if (month > 12 || month < 1) {
confirmation = FALSE;
return confirmation;
} else {
if(day >29 && month == 1){
confirmation = FALSE;
return confirmation;
}
if (day == 31) {
switch (month) {
case 1: // january
confirmation = TRUE;
return confirmation;
break;
case 3: // march
confirmation = TRUE;
return confirmation;
break;
case 5: // may
confirmation = TRUE;
return confirmation;
break;
case 7: // july
confirmation = TRUE;
return confirmation;
break;
case 8: // august
confirmation = TRUE;
return confirmation;
break;
case 10: // october
confirmation = TRUE;
return confirmation;
break;
case 12: // december
confirmation = TRUE;
return confirmation;
break;
default:
confirmation = FALSE;
return confirmation;
break;
}
}
if (day > 31 || day < 1) {
confirmation = FALSE;
return confirmation;
}
}
} else {
confirmation = FALSE;
}
return confirmation;
}
int id_check_database(char i[SIZ_ID], char type_base){ //check database
int n, aux,confirmation;
char c[MAX_SIZE];
char info[MAX_SIZE];
FILE *data_base;
switch (type_base) {
case POWERP_OPT:
data_base = fopen(DATABASE_PP, "r");
if (data_base == NULL) {
data_base = fopen(DATABASE_PP, "w+");
}
fprintf(data_base,"0xxxxxxxxxxxxx\nnome :3\nxxxxxxxx\n1.000000\n");
break;
case USER_OPT:
data_base = fopen(DATABASE_USER, "r");
if (data_base == NULL) {
data_base = fopen(DATABASE_USER, "w+");
}
fprintf(data_base,"0xxxxxxxxxxxxx\nnome :3\n");
break;
case CONTRACT_OPT:
data_base = fopen(DATABASE_CONTRACT, "r");
if (data_base == NULL) {
data_base = fopen(DATABASE_CONTRACT, "w+");
}
fprintf(data_base,"0xxxxxxxxxxxxx\n0xxxxxxxxxx\nxxxxxxxx\n1.000000\n");
break;
default:
return -1;
break;
}
if(strlen(i) == CPF_MIN){
i[CPF_MIN] = '\0';
// for(aux = 0; aux<CPF_MIN;aux++){
// i[aux] -= NUMERO_MIN;
// }
}
if(strlen(i) == CPF_MAX && !(i[3]>=NUMERO_MIN && i[3]<=NUMERO_MAX)){
i[0] = i[0];
i[1] = i[1];
i[2] = i[2];
i[3] = i[4];
i[4] = i[5];
i[5] = i[6];
i[6] = i[8];
i[7] = i[9];
i[8] = i[10];
i[9] = i[12];
i[10] = i[13];
i[11] = '\0';
}
if(strlen(i) == CNPJ_MIN && (i[3]>=NUMERO_MIN && i[3]<=NUMERO_MAX)){
i[CNPJ_MIN] = '\0';
// for(aux = 0; aux<CNPJ_MIN; aux++){
// i[aux] -= NUMERO_MIN;
// }
}
if(strlen(i) == CNPJ_MAX){
i[0] = i[0];
i[1] = i[1];
i[2] = i[3];
i[3] = i[4];
i[4] = i[5];
i[5] = i[7];
i[6] = i[8];
i[7] = i[9];
i[8] = i[11];
i[9] = i[12];
i[10] = i[13];
i[11] = i[14];
i[12] = i[16];
i[13] = i[17];
i[14] = '\0';
}
while(!feof(data_base)){
fgets(c,100,data_base);
//printf("\n\"%s\", \"%s\"\n",c,i);
n = FALSE;
for(aux = 0; aux < strlen(i); aux++){
if(i[aux] == '\0'){
break;
}
if(i[aux] != c[aux]){
n = FALSE;
break;
}else{
n = TRUE;
}
}
if(n == TRUE){
break;
}
}
if(n == FALSE){
return FALSE;
}
return n;
}
int power_plant_db_add(POWERP pp){ //powerplant add in database
FILE *data_base;
data_base = fopen(DATABASE_PP,"a+");
fprintf(data_base,"%s\n",pp.cnpj);
fprintf(data_base,"%s\n",pp.name);
fprintf(data_base,"%s\n",pp.date);
fprintf(data_base,"%f\n",pp.mw);
fclose(data_base);
puts(":)");
return 0;
}
int contract_db_add(CONTRACT c){ //add contract to db
FILE *data_base;
data_base = fopen(DATABASE_CONTRACT,"a+");
fprintf(data_base,"%s\n",c.cnpj);
fprintf(data_base,"%s\n",c.id);
fprintf(data_base,"%s\n",c.date);
fprintf(data_base,"%f\n",c.mw);
fclose(data_base);
return 0;
}
int user_db_add(USER u){ //add user to db
FILE *data_base;
data_base = fopen(DATABASE_USER,"a+");
fprintf(data_base,"%s\n",u.id);
fprintf(data_base,"%s\n",u.name);
fclose(data_base);
return 0;
}
CONTRACT contract_check(char id[SIZ_ID],int index,char type_check,int reverse){ //check contract
FILE *data_base;
CONTRACT con;
int n, aux,ind;
data_base = fopen(DATABASE_CONTRACT, "r");
if (data_base == NULL) {
data_base = fopen(DATABASE_CONTRACT, "w+");
}
ind = 0;
n = FALSE;
while(!feof(data_base)){
fscanf(data_base,"%s\n%s\n%s\n%f\n", con.cnpj, con.id, con.date, &con.mw);
// power plant id check
if(type_check == POWERP_OPT){
for(aux = 0; aux < CNPJ_MIN; aux++){
if(id[aux] == con.cnpj[aux]){
n = TRUE;
}else{
n = FALSE;
break;
}
}
}
// user id check
if(type_check == USER_OPT){
for(aux = 0; aux < strlen(id); aux++){
if((id[aux] == '\0' || id[aux] == '\n') && con.id[aux-1] == id[aux-1]){
break;
}else{
if(id[aux] == con.id[aux]){
n = TRUE;
}else{
n = FALSE;
break;
}
}
}
}
if(reverse == FALSE){
if(n == TRUE && ind == index){
fclose(data_base);
return con;
}else{
if(n == TRUE){
ind++;
}
}
}else{
if(n == FALSE && ind == index){
fclose(data_base);
return con;
}else{
if(n == FALSE){
ind++;
}
}
}
if(feof(data_base)){
break;
}
}
con.id[0] = 'x';
con.cnpj[0] = 'x';
fclose(data_base);
return con;
}
POWERP power_plant_check(char id[SIZ_ID], int index, int reverse){ //powerplant check
FILE *data_base;
POWERP pp;
char c[MAX_SIZE];
char mw[FLOAT_SIZE];
int n, aux, aux1,ind;
data_base = fopen(DATABASE_PP, "r");
if (data_base == NULL) {
data_base = fopen(DATABASE_PP, "w+");
}
n = FALSE;
ind = 0;
while(!feof(data_base)){
fflush(stdin);
fflush(stdout);
//fscanf(data_base,"%s\n%[^\n]s\n%s\n%f\n", pp.cnpj, pp.name, pp.date, &pp.mw);
fscanf(data_base,"%s\n", pp.cnpj);
fscanf(data_base,"%[^\n]s\n", pp.name);
fscanf(data_base,"%s\n", pp.date);
fscanf(data_base,"%f\n", &pp.mw);
printf("%s\n%s\n%s\n%f\n\n", pp.cnpj, pp.name, pp.date, pp.mw);
for(aux = 0; aux < CNPJ_MIN; aux++){
if(id[aux] == pp.cnpj[aux]){
n = TRUE;
}else{
n = FALSE;
break;
}
}
if(reverse == FALSE){
if(n == TRUE && ind == index){
fclose(data_base);
return pp;
}else{
if(n == TRUE){
ind++;
}
}
}else{
if(n == FALSE && ind == index){
fclose(data_base);
return pp;
}else{
if(n == FALSE){
ind++;
}
}
}
if(feof(data_base)){
break;
}
}
fclose(data_base);
clean();
pp.cnpj[0] = 'x';
return pp;
}
USER user_check(char id[SIZ_ID], int index, int reverse){ //Check user in database functions
FILE *data_base;
USER usr;
char c[MAX_SIZE];
int n, aux, aux1,ind;
data_base = fopen(DATABASE_USER, "r");
if (data_base == NULL) {
data_base = fopen(DATABASE_USER, "w+");
}
ind = 0;
n = FALSE;
while(!feof(data_base)){
fflush(stdin); //Cleaning keyboard
fflush(stdout);//Cleaning keyboard
fscanf(data_base,"%s\n", usr.id);
fscanf(data_base,"%[^\n]s\n", usr.name);
for(aux = 0; aux < strlen(id); aux++){
if(id[aux] == usr.id[aux]){
n = TRUE;
}else{
n = FALSE;
break;
}
}
if(feof(data_base)){
break;
}
if(reverse == FALSE){
if(n == TRUE && ind == index){
fclose(data_base);
return usr;
}else{
if(n == TRUE){
ind++;
}
}
}else{
if(n == FALSE && ind == index){
fclose(data_base);
return usr;
}else{
if(n == FALSE){
ind++;
}
}
}
}
fclose(data_base);
usr.id[0] = 'x';
return usr;
}
float power_plant_mw_info(char id[SIZ_ID]){
CONTRACT c;
POWERP pp;
int aux = 0;
pp = power_plant_check(id,0,FALSE);
float f = pp.mw;
for(;;){
c = contract_check(id,aux,POWERP_OPT,FALSE);
aux++;
if(c.id[0] == 'x'){
break;
}
pp.mw -= c.mw;
printf("a %s\n%f\n",c.id,pp.mw);
clean();
}
return pp.mw;
}
void delete_database(char id[SIZ_ID], char type_delete){ //Functions to delete database
FILE* data_base,*temp;
CONTRACT con;
USER usr;
POWERP pp;
int aux;
char c[SIZ_ID],n[MAX_SIZE];
puts("fase0\n");
if(type_delete == USER_OPT){
data_base = fopen(DATABASE_USER,"r");
temp = fopen(DATABASE_TEMP,"w+");
aux = 0;
for(;;){
usr = user_check(id,aux,TRUE);
printf("%s\n",usr.id);
if(usr.id[0] == 'x'){
break;
}
puts("fase1\n");
aux++;
fprintf(temp,"%s\n%s\n",usr.id,usr.name);
}
fclose(data_base);
fclose(temp);
remove(DATABASE_USER);
data_base = fopen(DATABASE_USER,"a+");
temp = fopen(DATABASE_TEMP,"r");
for(;;){
fscanf(temp,"%s\n%[^\n]s\n",usr.id,usr.name);
if(feof(temp)){
break;
}
puts("fase2\n");
fprintf(data_base,"%s\n%s\n",usr.id,usr.name);
}
fclose(data_base);
fclose(temp);
remove(DATABASE_TEMP);
}else{
data_base = fopen(DATABASE_PP,"r");
temp = fopen(DATABASE_TEMP,"w+");
aux = 0;
for(;;){
pp = power_plant_check(id,aux,TRUE);
printf("%s\n",pp.cnpj);
if(pp.cnpj[0] == 'x'){
break;
}
puts("fase1\n");
aux++;
fprintf(temp,"%s\n%s\n%s\n%f\n",pp.cnpj,pp.name,pp.date,pp.mw);
}
fclose(data_base);
fclose(temp);
remove(DATABASE_PP);
data_base = fopen(DATABASE_PP,"a+");
temp = fopen(DATABASE_TEMP,"r");
for(;;){
fscanf(temp,"%s\n", pp.cnpj);
fscanf(temp,"%[^\n]s\n", pp.name);
fscanf(temp,"%s\n", pp.date);
fscanf(temp,"%f\n", &pp.mw);
puts("fase2\n");
fprintf(data_base,"%s\n%s\n%s\n%f\n",pp.cnpj,pp.name,pp.date,pp.mw);
if(feof(temp)){
break;
}
}
fclose(data_base);
fclose(temp);
remove(DATABASE_TEMP);
}
//Removing contracts
if(type_delete == USER_OPT){
data_base = fopen(DATABASE_CONTRACT,"r");
temp = fopen(DATABASE_TEMP,"w+");
aux = 0;
for(;;){
con = contract_check(id,aux,USER_OPT,TRUE);
printf("%s\n",con.cnpj);
if(con.id[0] == 'x'){
break;
}
puts("fase1\n");
aux++;
fprintf(temp,"%s\n%s\n%s\n%f\n",con.cnpj,con.id,con.date,con.mw);
}
fclose(data_base);
fclose(temp);
remove(DATABASE_CONTRACT);
data_base = fopen(DATABASE_CONTRACT,"a+");
temp = fopen(DATABASE_TEMP,"r");
for(;;){
fscanf(temp,"%s\n%s\n%s\n%f\n",con.cnpj,con.id,con.date,&con.mw);
if(feof(temp)){
break;
}
puts("fase2\n");
fprintf(data_base,"%s\n%s\n%s\n%f\n",con.cnpj,con.id,con.date,con.mw);
}
fclose(data_base);
fclose(temp);
remove(DATABASE_TEMP);
return;
}else{
data_base = fopen(DATABASE_CONTRACT,"r");
temp = fopen(DATABASE_TEMP,"w+");
aux = 0;
for(;;){
con = contract_check(id,aux,POWERP_OPT,TRUE);
printf("%s\n",con.cnpj);
if(con.id[0] == 'x'){
break;
}
puts("fase1\n");
aux++;
fprintf(temp,"%s\n%s\n%s\n%f\n",con.cnpj,con.id,con.date,con.mw);
}
fclose(data_base);
fclose(temp);
remove(DATABASE_CONTRACT);
data_base = fopen(DATABASE_CONTRACT,"a+");
temp = fopen(DATABASE_TEMP,"r");
for(;;){
fscanf(temp,"%s\n%s\n%s\n%f\n",con.cnpj,con.id,con.date,&con.mw);
puts("fase2\n");
fprintf(data_base,"%s\n%s\n%s\n%f\n",con.cnpj,con.id,con.date,con.mw);
if(feof(temp)){
break;
}
}
fclose(data_base);
fclose(temp);
remove(DATABASE_TEMP);
return;
}
}
My problem is making this files in one ".c" Can someone help me? :)
Replace the #include with the content of the file. Credits: @KamilCuk :D