我是一名地震学家,我对C很新。我一直试图写一些代码,试图学习C并同时完成我的工作。这是我写的代码。 :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
char *token,station[100],earthquake[100],*stn[50],*eq[50],*a="abc";
float lat_st,long_st,lat_eq,long_eq,e=0.0000001;
int c=1;
//--------------------------------------------------------This code block takes the input and checks whether the input has only two values - one for latitude and one for longitude and processes the input string for further analysis-----------------------------------------------------------------------//
a: printf("Input Station Lat-Long: ");
scanf("%s",station);
token=strtok(station,", ");
int i=0;
while(token!=NULL)
{
stn[i]=token;
token=strtok(NULL,", ");
i++;
}
if (atoi(stn[2])){
printf("More than two values for latitude and longitude entered. Please enter again.\n\n");
stn[2]=a;
goto a;
}
b: printf("Input Earthquake Lat-Long: ");
scanf("%s",earthquake);
token=strtok(earthquake,", ");
i=0;
while(token!=NULL)
{
eq[i]=token;
token=strtok(NULL,", ");
i++;
}
if (atoi(eq[2])){
printf("More than two values for latitude and longitude entered. Please enter again.\n\n");
eq[2]=a;
goto b;
}
//---------------------------------------------------End of first check. Next check is to find out whether the input is in quadrant (23N,45W) or azimuthal (23,-45) convention and convert quadrant to azimuthal if required-----------------------------------------------------------------------------------------//
else{
for(i=0;stn[0][i]!='\0';i++)
{
if(stn[0][i]>57)
{
switch (stn[0][i]){
case 'N':
c=1;
break;
case 'n':
c=1;
break;
case 'S':
c=-1;
break;
case 's':
c=-1;
break;
default:
printf("Wrong input. Please enter again.\n\n");
goto a;
}
stn[0][i]='\0';
}
}
lat_st=c*atoi(stn[0]);
c=1;
for(i=0;stn[1][i]!='\0';i++)
{
if(stn[1][i]>57)
{
switch (stn[1][i]){
case 'E':
c=1;
break;
case 'e':
c=1;
break;
case 'W':
c=-1;
break;
case 'w':
c=-1;
break;
default:
printf("Wrong input. Please enter again.\n\n");
goto a;
}
stn[1][i]='\0';
}
}
long_st=c*atoi(stn[1]);
c=1;
for(i=0;eq[0][i]!='\0';i++)
{
if(eq[0][i]>57)
{
switch (eq[0][i]){
case 'N':
c=1;
break;
case 'n':
c=1;
break;
case 'S':
c=-1;
break;
case 's':
c=-1;
break;
default:
printf("Wrong input. Please enter again.\n\n");
goto b;
}
eq[0][i]='\0';
}
}
lat_eq=c*atoi(eq[0]);
c=1;
for(i=0;eq[1][i]!='\0';i++)
{
if(eq[1][i]>57)
{
switch (eq[1][i]){
case 'E':
c=1;
break;
case 'e':
c=1;
break;
case 'W':
c=-1;
break;
case 'w':
c=-1;
break;
default:
printf("Wrong input. Please enter again.\n\n");
goto b;
}
eq[1][i]='\0';
}
}
long_eq=c*atoi(eq[1]);
}
//------------------------------------End of second check. Third check is to determine if any lat value is too close to 90 to cause any problems---------------------//
printf("%f %f %f %f",lat_st,long_st,lat_eq,long_eq);
return 0;
}
代码中已经完成的所有工作就是找出用户可以搞砸的方法。但由于某些原因,我有时会因同一输入而出现分段错误,有时我也不会。样本终端输出:
$ ./a
Input Station Lat-Long: 23N,45E
Input Earthquake Lat-Long: 34N,56E
23.000000 45.000000 34.000000 56.000000
$ ./a
Input Station Lat-Long: 23,45
Input Earthquake Lat-Long: 34,56
23.000000 45.000000 34.000000 56.000000
$ ./a
Input Station Lat-Long: 23S,45
Input Earthquake Lat-Long: 34,54W
-23.000000 45.000000 34.000000 -54.000000
$ ./a
Input Station Lat-Long: 23,45
Input Earthquake Lat-Long: 45,56
Segmentation fault (core dumped)
我几乎可以理解我的代码很糟糕,但我看不到任何错误,或者我错过了什么?
谢谢
编辑:@BLUEPIXY的解决方案似乎有效,但我的问题仍然是为什么在不同的程序运行中输出会有所不同?