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];
usuario.id[11] = id[11];
usuario.id[12] = id[12];
usuario.id[13] = id[13];
usuario.id[14] = '\0';
}
if (strlen(id) == CPF_MIN) {
usuario.id[0] = id[0];
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];
usuario.id[11] = '\0';
}
//Preparing for database (mistake)
user_db_add(usuario);
clean();
printf("User registered with sucess.");
}
void r_contrat(){ //Function for make the contract registration.
CONTRACT c;
POWERP pp;
char cnpj[SIZ_ID],id[SIZ_ID],data[SIZ_DATE];
int n, aux;
float mw;
clean();
for(;;){
printf("\n Register contract:\n\n");
printf(" Type the CNPJ of power plant. ");
scanf(" %s",cnpj);
n = id_check(cnpj); //Checking cnpj
if(n == FALSE){ //If cnnpj is not valid.
clean();
printf("\nInsert a valid identification\n");
}
else{
n = id_check_database(cnpj,DB_plant);
if(n == FALSE){ //If cnpj not finded.
clean();
printf("\nPower plant not finded.\n");
}else{n = id_check_database(cnpj,DB_USER);
if(n == TRUE){ //If identification is from a user, and not from a power plant.
clean();
printf("\nThe identification is from a user\n");
}else{
clean();
break;
}
}
}
}
if(strlen(cnpj) == CNPJ_MIN){ //Same mistake, again.
c.cnpj[0] = cnpj[0];
c.cnpj[1] = cnpj[1];
c.cnpj[2] = cnpj[2];
c.cnpj[3] = cnpj[3];
c.cnpj[4] = cnpj[4];
c.cnpj[5] = cnpj[5];
c.cnpj[6] = cnpj[6];
c.cnpj[7] = cnpj[7];
c.cnpj[8] = cnpj[8];
c.cnpj[9] = cnpj[9];
c.cnpj[10] = cnpj[10];
c.cnpj[11] = cnpj[11];
c.cnpj[12] = cnpj[12];
c.cnpj[13] = cnpj[13];
c.cnpj[14] = '\0';
}
clean();
for(;;){ //Looping for register contrat.
printf("\n Register contract:\n\n");
printf(" Type cpf or cnpj from the user: ");
scanf(" %s",id);
n = id_check(id);
if(n == FALSE){
clean();
printf("\nInsert a valid identification.\n");
}
else{
n = id_check_database(id,DB_USER);
if(n == FALSE){
clean();
printf("\nThe user not exist.\n");
}else{
n = id_check_database(id,DB_plant);
if(n == TRUE){
clean();
printf("\nIdentification is from a power plant.\n");
}else{
clean();
break;
}
}
}
}
if(strlen(id) == CNPJ_MIN && (id[3] >= NUMERO_MIN && id[3]<=NUMERO_MAX)){ // arrumando cnpj
c.id[0] = id[0];
c.id[1] = id[1];
c.id[2] = id[2];
c.id[3] = id[3];
c.id[4] = id[4];
c.id[5] = id[5];
c.id[6] = id[6];
c.id[7] = id[7];
c.id[8] = id[8];
c.id[9] = id[9];
c.id[10] = id[10];
c.id[11] = id[11];
c.id[12] = id[12];
c.id[13] = id[13];
c.id[14] = '\0';
}
if (strlen(id) == CPF_MIN) {
c.id[0] = id[0];
c.id[1] = id[1];
c.id[2] = id[2];
c.id[3] = id[3];
c.id[4] = id[4];
c.id[5] = id[5];
c.id[6] = id[6];
c.id[7] = id[7];
c.id[8] = id[8];
c.id[9] = id[9];
c.id[10] = id[10];
c.id[11] = '\0';
}
clean();
for(;;){ //Register date from contract.
printf("\n Register contract:\n\n");
printf(" Date from the begginning of the contrat(DDMMAAAA): ");
scanf(" %s",data);
printf(" %s",data);
n = date_check(data);
if(n == FALSE){
//clean();
printf("\nInsert a valid start date.\n");
}
else{
if (strlen(data) == DATE_MIN) { //Same mistake
c.date[0] = data[0];
c.date[1] = data[1];
c.date[2] = data[2];
c.date[3] = data[3];
c.date[4] = data[4];
c.date[5] = data[5];
c.date[6] = data[6];
c.date[7] = data[7];
c.date[8] = '\0';
}
int day,day1, month,month1, year,year1;
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);
pp = power_plant_check(cnpj,0,FALSE);
day1 = (pp.date[0] - NUMERO_MIN) * 10;
day1 += (pp.date[1] - NUMERO_MIN);
month1 = (pp.date[2] - NUMERO_MIN) * 10;
month1 += (pp.date[3] - NUMERO_MIN);
year1 = (pp.date[4] - NUMERO_MIN) * 1000;
year1 += (pp.date[5] - NUMERO_MIN) * 100;
year1 += (pp.date[6] - NUMERO_MIN) * 10;
year1 += (pp.date[7] - NUMERO_MIN);
if(year < year1){
clean();
printf("\nInsert a valid start date.\n");
}
else{
if(year == year1 && month < month1){
clean();
printf("\nInsert a valid start date.\n");
}
else{
if(year == year1 && month == month1 && day < day1){
clean();
printf("\nInsert a valid start date.\n");
}
else{
clean();
break;
}
}
}
}
}
clean();
for(;;){ //Register of usable MWz
printf("\n Register contract:\n\n");
printf(" Type the estimated potency in MWz: ");
scanf(" %f",&mw);
if(mw <= 0){
clean();
printf("\nInsert a valid potency.\n");
}
else{
pp.mw = power_plant_mw_info(cnpj);
printf("%f\n\n",pp.mw);
if(pp.mw <= mw){
clean();
printf("\nInsert a valid potency.\n");
}
else{
clean();
break;
}
}
}
c.mw = mw;
contract_db_add(c);
//Adding at database.
printf("Register with sucess!");
}
void c_u(){ //Function responsable for check power plant.
char cnpj[SIZ_ID];
int n;
clean();
for(;;){
printf("\n Consult power plant:\n\n");
printf(" Type power plant cnpj: ");
scanf(" %s",cnpj);
n = id_check(cnpj);
if(n == 0){
clean();
printf("\nInsert a valid identification.\n");
}
else{
n = id_check_database(cnpj,DB_plant); //Checking/consulting on database.
if(n == FALSE){
clean();
printf("\nThis power plant does not exist.\n");
}else{
clean();
break;
}
}
}
clean();
//Taking to min.
info_plant(cnpj); //Consulting cnpj
printf("Type 1 for exit: ");
scanf(" %d",&n);
clean();
return;
}
void c_c(){ //Consulting consumer.
USER usr;
char id[SIZ_ID],name[SIZ_NAME];
int n, aux;
clean();
for(;;){
printf("\n Consulting user: n\n");
printf(" Type user CPF or CNPJ: ");
scanf(" %s",id);
n = id_check(id);
if(n == FALSE){
clean();
printf("\nInsert a valid identification.\n");
}
else{
n = id_check_database(id,DB_USER);
if(n == FALSE){
clean();
printf("\nUser does not exist.\n");
}else{
clean();
break;
}
}
}
//Taking to min
info_user(id);// consulta o consumidor e mosta os contratos com as usinas
printf("-----------------//-----------------\n");
printf("digite 1 para EXIT: ");
scanf(" %d",&n);
clean();
return;
}
void d_u(){ // Deleting contracts vinculated with the consumer.
char cnpj[SIZ_ID];
int aux, n;
float mw;
clean();
for(;;){
printf("\n Delete power plant: \n\n");
printf(" Type power plant cnpj: ");
scanf(" %s",cnpj);
n = id_check(cnpj);
if(n == FALSE){
clean();
printf("\nInsert a valid identification.\n");
}else{
n = id_check_database(cnpj,DB_plant);
if(n == FALSE){
clean();
printf("\nIdentification does not exist.\n");
}else{
clean();
break;
}
}
}
delete_database(cnpj,POWERP_OPT);
clean();
printf("Power plant deleted with sucess.");
}
void d_c(){ //Deleting user contrats too.
USER usr;
char id[SIZ_ID],name[SIZ_NAME];
int n, aux;
clean();
for(;;){
printf("\n Delete user:\n\n");
printf(" Type user CPF or CNPJ: ");
scanf(" %s",id);
n = id_check(id);
if(n == FALSE){
clean();
printf("\nInsert a valid identification.\n");
}
else{
n = id_check_database(id,DB_USER);
if(n == FALSE){
clean();
printf("\nUser does not exist.\n");
}else{
clean();
break;
}
}
}
delete_database(id,USER_OPT);
clean();
printf("User deleted.");
}
// Nice functions:
void info_plant(char id[SIZ_ID]){
CONTRACT c;
USER usr;
POWERP pp;
int aux = 0;
pp = power_plant_check(id,0,FALSE);
pp.mw = power_plant_mw_info(id);
clean();
printf("-----------------//-----------------\n");
printf("\nPower Plant: %s\n - Start date: %c%c/%c%c/%c%c%c%c\n - MWs: %f\n",pp.name,pp.date[0],pp.date[1],pp.date[2],pp.date[3],pp.date[4],pp.date[5],pp.date[6],pp.date[7],pp.mw);
printf("-----------------//-----------------\n\n");
aux = 0;
for(;;){
c = contract_check(id,aux,POWERP_OPT,FALSE);
//printf("%s,\n%s,\n%s,\n%f,\n",c.cnpj,c.id,c.date,c.mw);
aux++;
if(c.id[0] == 'x'){
break;
}
usr = user_check(c.id,0, FALSE);
printf("\nUser: %s\n - User ID: %s\n - Start date: %c%c/%c%c/%c%c%c%c\n - MWs used: %f Mw\n",usr.name,c.id,c.date[0],c.date[1],c.date[2],c.date[3],c.date[4],c.date[5],c.date[6],c.date[7],c.mw);
}
return;
}
void info_user(char id[SIZ_ID]){
CONTRACT c;
USER usr;
POWERP pp;
int aux = 0;
usr = user_check(id,0,FALSE);
clean();
printf("-----------------//-----------------\n");
printf("\nUser: %s\n\n",usr.name);
printf("-----------------//-----------------\n\n");
aux = 0;
for(;;){
c = contract_check(id,aux,USER_OPT,FALSE);
aux++;
if(c.id[0] == 'x'){
break;
}
pp = power_plant_check(c.cnpj,0,FALSE);
printf("\nContrat start date: %s\n - Plant ID: %s\n - Contrat start date: %c%c/%c%c/%c%c%c%c\n - Potency used: %.3f\n\n",pp.name,pp.cnpj,c.date[0],c.date[1],c.date[2],c.date[3],c.date[4],c.date[5],c.date[6],c.date[7],c.mw);
}
return;
}
And this is the header file that i want put on the .c file:
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:
And this is the header file that i want put on the .c file:
My problem is making this files in one ".c". Can someone help me? :)