- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- typedef struct variables {
- float variablesp;
- struct variables* next;
- } tvariables;
- typedef tvariables* pvariables;
- typedef struct province {
- char namep[100];
- pvariables variables;
- struct province* succ;
- } tprovince;
- typedef tprovince* pprovince;
- typedef struct paretoset {
- pprovince selection;
- struct paretoset* next;
- } tparetoset;
- typedef tparetoset* pparetoset;
- bool checkdata(char* data) {// it search the 2023 string
- char target[] = "2023";
- int a = strlen(data);
- int b = strlen(target);
- int count = 0;
- while (count != a) {
- int check = 0;
- if (target[0] == data[count]) {
- count++;
- check++;
- for (int i = 1; i < b; i++) {
- if (target[i] == data[count]) {
- check++;
- }
- count++;
- }
- }
- count++;
- if (b == check) {
- return true;
- }
- }
- return false;
- }
- pprovince checkrepeat(pprovince head, char* name) {// it filter for data(2023)
- while (head != NULL) {
- if (strcmp(head->namep, name) == 0) {
- return head;
- }
- head = head->succ;
- }
- return NULL;
- }
- void insertinprovince(pprovince* new, float data) {
- pvariables newvariable = (pvariables)malloc(sizeof(tvariables));
- newvariable->variablesp = data;
- newvariable->next = (*new)->variables;
- (*new)->variables = newvariable;
- }
- void create(pprovince *head, char* name, float data) {// it create the memories for the list and take the first index and sort it
- pprovince prec = NULL;
- pprovince p = *head;
- pprovince new = (pprovince)malloc(sizeof(tprovince));
- if (new == NULL) {
- printf("nothing works anymore");
- exit(EXIT_FAILURE);
- }
- strcpy(new->namep, name);
- new->variables = NULL;
- insertinprovince(&new, data);
- while(p!=NULL){
- if(new->variables->variablesp<p->variables->variablesp){
- break;
- }
- prec = p;
- p = p->succ;
- }
- if (prec == NULL) {
- new->succ = *head;
- *head = new;
- }
- else {
- new->succ = p;
- prec->succ = new;
- }
- }
- pprovince allocatememory(pprovince* head, char* name, float support) {// it fill the struct
- pprovince p;
- p = checkrepeat(*head, name);
- if (p != NULL) {
- insertinprovince(&p, support);
- }
- else {
- create(head, name, support);
- }
- return *head;
- }
- pprovince loadfromfile() {// it take the data from the file, it work properly
- pprovince head = NULL;
- FILE* p = fopen("input.txt", "r");
- if (p == NULL) {
- printf("nothing works anymore'");
- return NULL;
- }
- char name[100];
- float support;
- char data[1000];
- while (!feof(p)) {
- if (fscanf(p, "%99[^,],%*[^,],%*[^,],%*[^,],%f,%999[^\n]\n", name, &support, data) == 3) {
- if (checkdata(data)) {
- head = allocatememory(&head, name, support);
- }
- }
- head->variables->variablesp;
- }
- fclose(p);
- return head;
- }
- pparetoset allocateparetoset(pparetoset head, pprovince p) {
- pparetoset new = (pparetoset)malloc(sizeof(tparetoset));
- new->selection = p;
- new->next = head;
- head = new;
- return head;
- }
- bool domination(pvariables v1, pvariables v2) {
- while (v1 != NULL && v2 != NULL) {
- if (v1->variablesp - v2->variablesp < 0) {
- return false;//v1 non è niente
- }
- v1 = v1->next;
- v2 = v2->next;
- }
- return true;//v1 domina
- }
- pparetoset elimination(pparetoset head,pprovince m){// eliminates
- pparetoset prec = NULL;
- pparetoset p = head;
- while(p!=NULL && domination(p->selection->variables,m->variables)){
- prec = p;
- p = p->next;
- }
- if (p == NULL)return head;
- if (prec==NULL){
- head = head->next;
- free(p);
- }
- else{
- prec->next = p->next;
- free(p);
- }
- return head;
- }
- pparetoset searchparetoset(pprovince head) {// algorithm with problem that search the pareto set trough data
- pparetoset head = NULL;
- pprovince p = head;
- pparetoset support;
- int a =0 ;
- head = allocateparetoset(head, p);
- p = p->succ;
- for(;p!=NULL;p=p->succ){// il primo ciclo for che va avanti tra le provincie
- support = head;
- for(support;support!=NULL;support=support->next){// secondo ciclo for che controlla se nessuna delle province presenti nella pareto set domina la p in monitoraggio
- if(domination(support->selection->variables,p->variables)){
- a++;
- }
- }
- if(a==0){
- head =elimination(head, p);
- head = allocateparetoset(head, p);
- support = head;
- }
- }
- return head;
- }
- void printvalues(pvariables x) {
- while (x != NULL) {
- printf("%f ", x->variablesp);
- x = x->next;
- }
- printf("\n");
- }
- void printparetoset(pparetoset head) {// stampa il risultato
- printf("\n pareto set: \n");
- while (head != NULL) {
- printf("\n***%s***\n", head->selection->namep);
- printf("Values of the variables:\n");
- printvalues(head->selection->variables);
- head = head->next;
- }
- }
- int main() {
- pprovince head = loadfromfile();
- pparetoset head = searchparetoset(head);
- printparetoset(head);
- return 0;
- }
[text] pareto set
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- pareto set
- Paste Code
- 14 Apr-2024
- 5.55 Kb
You can Share it:
Latest Code Pastes