我的程序有问题:在数据采集过程中,当我输入第二个数据时,程序停止运行并发生SIGSEGV错误(p.s.我正在使用结构)。
代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int codice;
char nom[20];
char cogn[20];
int n_sin_trat;
int n_sin_conc;
int compenso;
} Dip;
int main()
{
int n_dip;
int i,p,sin_no_conc1, sin_no_conc2, comp_tot;
int scambio;
do{
printf("Inserire numero dipendenti: ");
scanf("%d", &n_dip);
}while(n_dip<=0);
Dip tab_dip[n_dip];
Dip temp;
do{
i=i+1;
do{
printf("Inserire il codice del dipendente: ");
scanf("%dn", &tab_dip[i].codice);
}while(tab_dip[i].codice<0);
printf("Inserire il nome del dipendente: ");
scanf("%sn", tab_dip[i].nom);
printf("Inserire il cognome del dipendente: ");
scanf("%sn", tab_dip[i].cogn);
do{
printf("Inserire il numero di sinistri trattati: ");
scanf("%dn", &tab_dip[i].n_sin_trat);
}while(tab_dip[i].n_sin_trat<0);
do{
printf("Inerire il numero di sinistri conclusi");
scanf("%dn", &tab_dip[i].n_sin_conc);
}while(tab_dip[i].n_sin_conc>=tab_dip[i].n_sin_trat);
do{
printf("Immettere il compenso per sinistro");
scanf("%dn", &tab_dip[i].compenso);
}while(tab_dip[i].compenso>0);
}while(i<n_dip);
p=n_dip;
do{
scambio=0;
i=0;
do{
i=i+1;
sin_no_conc1=tab_dip[i].n_sin_trat-tab_dip[i].n_sin_conc;
sin_no_conc2=tab_dip[i+1].n_sin_trat-tab_dip[i+1].n_sin_conc;
if(sin_no_conc1>sin_no_conc2){
temp=tab_dip[i];
tab_dip[i]=tab_dip[i+1];
tab_dip[i+1]=temp;
scambio=1;
}
}while(i<p-1);
p=p-1;
}while(scambio==0);
i=0;
do{
i=i+1;
printf("%dn",tab_dip[i].codice);
printf("%sn",tab_dip[i].nom);
sin_no_conc1=tab_dip[i].n_sin_trat-tab_dip[i].n_sin_conc;
printf("%dn", sin_no_conc1);
comp_tot=sin_no_conc1*tab_dip[i].compenso;
printf("%dn", comp_tot);
}while(i<n_dip);
return 0;
}
编辑
现在输入阶段有效,但是当我尝试使用气泡排序对数据进行排序时(我必须使用气泡排序),程序停止工作。代码如下:
for (p = n_dip, scambio = 0; scambio==0; p--) {
scambio=0;
for (i = 0; i < p - 1; i++) {
sin_no_conc1=tab_dip[i].n_sin_trat-tab_dip[i].n_sin_conc;
sin_no_conc2=tab_dip[i+1].n_sin_trat-tab_dip[i+1].n_sin_conc;
if(sin_no_conc1>sin_no_conc2){
temp=tab_dip[i];
tab_dip[i]=tab_dip[i+1];
tab_dip[i+1]=temp;
scambio=1;
}
}
}
这更适合作为代码审查问题:
#include <stdio.h>
#include <stdlib.h>
// Note: Indent and use spaces around punctuation
typedef struct {
int codice;
char nom[20];
char cogn[20];
int n_sin_trat;
int n_sin_conc;
int compenso;
} Dip;
int main()
{
int n_dip;
int i,p,sin_no_conc1, sin_no_conc2, comp_tot;
int scambio;
do {
printf("Inserire numero dipendenti: ");
// Note: Check the result of scanf
// Note: Scanf doesn't change any variables if it didn't
// read them. You've likely been lucky that you randomly had
// negative values in these uninitialized variables.
} while(scanf("%d", &n_dip) != 1 || n_dip < 0);
// Note: Use vertical space to separate sections
// c99 dynamic array size
Dip tab_dip[n_dip];
Dip temp;
// Previously, this code started with do { i = i + 1;
// Which would cause an off-by-one error in the code leading to
// a crash. C arrays are indexed from 0, so an array of n_dip
// values contains tab_dip[0] ... tab_dip[n_dip - 1]
//
// Your code would start the iteration, then increment i at the top
// which even if i was initialized, would have indexed past the
// last array element. By convention, code usually increments
// indexes at the end. Note that the for loop already does that
// and reminds one to initialize the loop variable.
for (i = 0; i < n_dip; i++) {
do {
printf("Inserire il codice del dipendente: ");
// Again, check the result of scanf and ensure that it
// read a value before checking the value
} while(scanf("%d", &tab_dip[i].codice) != 1 || tab_dip[i].codice < 0);
printf("Inserire il nome del dipendente: ");
scanf("%s", tab_dip[i].nom);
printf("Inserire il cognome del dipendente: ");
scanf("%s", tab_dip[i].cogn);
do {
printf("Inserire il numero di sinistri trattati: ");
} while(scanf("%d", &tab_dip[i].n_sin_trat) != 1 || tab_dip[i].n_sin_trat < 0);
do {
printf("Inerire il numero di sinistri conclusi");
} while(scanf("%d", &tab_dip[i].n_sin_conc) != 1 || tab_dip[i].n_sin_conc >= tab_dip[i].n_sin_trat);
do{
printf("Immettere il compenso per sinistro");
}while(scanf("%d", &tab_dip[i].compenso) != 1 || tab_dip[i].compenso > 0);
}
// The comma operator in C can be used to do multiple
// initializations or increments in the for loop prelude.
// Note that the condition can be any condition. Here, we're
// emulating do-while by satisfying the condition on the first
// pass.
for (p = n_dip, scambio = 0; scambio==0; p--) {
scambio=0;
for (i = 0; i < p - 1; i++) {
sin_no_conc1=tab_dip[i].n_sin_trat-tab_dip[i].n_sin_conc;
sin_no_conc2=tab_dip[i+1].n_sin_trat-tab_dip[i+1].n_sin_conc;
if(sin_no_conc1>sin_no_conc2){
temp=tab_dip[i];
tab_dip[i]=tab_dip[i+1];
tab_dip[i+1]=temp;
scambio=1;
}
}
}
for (i = 0; i < n_dip; i++) {
printf("%dn",tab_dip[i].codice);
printf("%sn",tab_dip[i].nom);
sin_no_conc1=tab_dip[i].n_sin_trat-tab_dip[i].n_sin_conc;
printf("%dn", sin_no_conc1);
comp_tot=sin_no_conc1*tab_dip[i].compenso;
printf("%dn", comp_tot);
}
return 0;
}