#include <stdio.h>
#include <stdlib.h>
// Funzione per scrivere i dati nel file "DatiCompleti.txt"
void write_data_file( int rows) {
FILE
* file
= fopen ( "DatiCompleti.txt" , "w" ) ; if ( ! file) {
printf ( "Errore nell'apertura del file!\n " ) ; return ;
}
double col1, col2, col3, col4;
printf ( "Inserisci i dati per %d righe (4 colonne ciascuna):\n " , rows
) ;
for ( int i = 0 ; i < rows; ++ i) {
fprintf ( file
, "%.2lf\t %.2lf\t %.2lf\t %.2lf\n " , col1
, col2
, col3
, col4
) ; }
printf ( "Dati salvati in 'DatiCompleti.txt'.\n " ) ; }
// Funzione per dividere i dati nei file "Colonne1e3.txt" e "Colonne2e4.txt"
void split_data_files( int rows) {
FILE
* file
= fopen ( "DatiCompleti.txt" , "r" ) ; FILE
* file1
= fopen ( "Colonne1e3.txt" , "w" ) ; FILE
* file2
= fopen ( "Colonne2e4.txt" , "w" ) ;
if ( ! file || ! file1 || ! file2) {
printf ( "Errore nell'apertura dei file!\n " ) ; return ;
}
double col1, col2, col3, col4;
for ( int i = 0 ; i < rows; ++ i) {
fscanf ( file
, "%lf\t %lf\t %lf\t %lf" , & col1
, & col2
, & col3
, & col4
) ; fprintf ( file1
, "%.2lf\t %.2lf\n " , col1
, col3
) ; fprintf ( file2
, "%.2lf\t %.2lf\n " , col2
, col4
) ; }
printf ( "Dati divisi in 'Colonne1e3.txt' e 'Colonne2e4.txt'.\n " ) ; }
// Funzione per generare grafici utilizzando gnuplot
void generate_plots( ) {
FILE * gnuplot = popen( "gnuplot" , "w" ) ;
if ( gnuplot == NULL) {
printf ( "Errore nell'avvio di gnuplot.\n " ) ; return ;
}
// Comandi per il primo grafico (Colonna 1 e Colonna 3)
fprintf ( gnuplot
, "set terminal pngcairo size 800,600\n " ) ; fprintf ( gnuplot
, "set output 'grafico_colonne1e3.png'\n " ) ; fprintf ( gnuplot
, "set title 'Grafico a punti: Colonna 1 vs Colonna 3'\n " ) ; fprintf ( gnuplot
, "set xlabel 'Colonna 1'\n " ) ; fprintf ( gnuplot
, "set ylabel 'Colonna 3'\n " ) ; fprintf ( gnuplot
, "plot 'Colonne1e3.txt' using 1:2 with points pointtype 7 title 'Dati (1,3)'\n " ) ;
// Comandi per il secondo grafico (Colonna 2 e Colonna 4)
fprintf ( gnuplot
, "set output 'grafico_colonne2e4.png'\n " ) ; fprintf ( gnuplot
, "set title 'Grafico con punti e linee: Colonna 2 vs Colonna 4'\n " ) ; fprintf ( gnuplot
, "set xlabel 'Colonna 2'\n " ) ; fprintf ( gnuplot
, "set ylabel 'Colonna 4'\n " ) ; fprintf ( gnuplot
, "plot 'Colonne2e4.txt' using 1:2 with linespoints linewidth 2 title 'Dati (2,4)'\n " ) ;
pclose( gnuplot) ;
printf ( "Grafici generati: 'grafico_colonne1e3.png' e 'grafico_colonne2e4.png'.\n " ) ; }
// Funzione principale
int main( ) {
int rows;
printf ( "Quante righe di dati vuoi inserire? " ) ;
if ( rows <= 0 ) {
printf ( "Il numero di righe deve essere positivo.\n " ) ; return 1 ;
}
write_data_file( rows) ;
split_data_files( rows) ;
generate_plots( ) ;
printf ( "Operazione completata.\n " ) ; return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8vIEZ1bnppb25lIHBlciBzY3JpdmVyZSBpIGRhdGkgbmVsIGZpbGUgIkRhdGlDb21wbGV0aS50eHQiCnZvaWQgd3JpdGVfZGF0YV9maWxlKGludCByb3dzKSB7CiAgICBGSUxFICpmaWxlID0gZm9wZW4oIkRhdGlDb21wbGV0aS50eHQiLCAidyIpOwogICAgaWYgKCFmaWxlKSB7CiAgICAgICAgcHJpbnRmKCJFcnJvcmUgbmVsbCdhcGVydHVyYSBkZWwgZmlsZSFcbiIpOwogICAgICAgIHJldHVybjsKICAgIH0KCiAgICBkb3VibGUgY29sMSwgY29sMiwgY29sMywgY29sNDsKICAgIHByaW50ZigiSW5zZXJpc2NpIGkgZGF0aSBwZXIgJWQgcmlnaGUgKDQgY29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