我为我的大学编程课编写了一个代码,它用于使用名为ds4rd.exe的程序从Dualshock 4控制器收集数据。但是,代码似乎编译,但每当我尝试运行它时,我都会收到分段错误错误。我该如何解决这个问题?
这是我的代码
// 185 lab6.c
//
// This is the outline for your program
// Please implement the functions given by the prototypes below and
// complete the main function to make the program complete.
// You must implement the functions which are prototyped below exactly
// as they are requested.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.141592653589
//NO GLOBAL VARIABLES ALLOWED
//PRE: Arguments must point to double variables or int variables as appropriate
//This function scans a line of DS4 data, and returns
// True when the square button is pressed
// False Otherwise
//This function is the ONLY place scanf is allowed to be used
//POST: it modifies its arguments to return values read from the input line.
int read_line(double* g_x, double* g_y, double* g_z, int* time, int* Button_T, int* Button_X, int* Button_S, int* Button_C);
// PRE: -1.0 <= x_mag <= 1.0
// This function computes the roll of the DS4 in radians
// if x_mag outside of -1 to 1, treat it as if it were -1 or 1
// POST: -PI/2 <= return value <= PI/2
double roll(double x_mag);
// PRE: -1.0 <= y_mag <= 1.0
// This function computes the pitch of the DS4 in radians
// if y_mag outside of -1 to 1, treat it as if it were -1 or 1
// POST: -PI/2 <= return value <= PI/2
double pitch(double y_mag);
// PRE: -PI/2 <= rad <= PI/2
// This function scales the roll value to fit on the screen
// POST: -39 <= return value <= 39
int scaleRadsForScreen(double rad);
// PRE: num >= 0
// This function prints the character use to the screen num times
// This function is the ONLY place printf is allowed to be used
// POST: nothing is returned, but use has been printed num times
void print_chars(int num, char use);
//PRE: -39 <= number <=39
// Uses print_chars to graph a number from -39 to 39 on the screen.
// You may assume that the screen is 80 characters wide.
void graph_line(int number);
int main() {
double x, y, z; // magnitude values of x, y, and z
int b_Triangle, b_X, b_Square, b_Circle; // variables to hold the button statuses
double roll_rad, pitch_rad; // value of the roll measured in radians
int scaled_value; // value of the roll adjusted to fit screen display
int time = 0;
int run_Roll, run_Pitch;
//insert any beginning needed code here
do
{
// Get line of input
read_line(&x, &y, &z, &time, &b_Triangle, &b_X, &b_Square, &b_Circle);
// calculate roll and pitch. Use the buttons to set the condition for roll and pitch
roll_rad = roll(x);
pitch_rad = pitch(y);
if (b_Triangle == 1) {
run_Roll = 1;
run_Pitch = 0;
}
if (b_X == 1) {
run_Roll = 0;
run_Pitch = 1;
}
// switch between roll and pitch(up vs. down button)
if (b_Triangle == 1) {
run_Roll = 1;
}
if (b_X == 1) {
run_Pitch = 1;
}
// Scale your output value
roll_rad = scaleRadsForScreen(roll_rad);
pitch_rad = scaleRadsForScreen(pitch_rad);
// Output your graph line
if (run_Roll == 1) {
graph_line(roll_rad);
}
if (run_Pitch == 1) {
graph_line(pitch_rad);
}
fflush(stdout);
} while (b_Square != 1); // Modify to stop when the square button is pressed
return 0;
}
int read_line(double* g_x, double* g_y, double* g_z, int* time, int* Button_T, int* Button_X, int* Button_S, int* Button_C) {
double x, y, z;
int b_Triangle, b_X, b_Square, b_Circle;
scanf("%d, %lf, %lf, %lf, %d, %d, %d, %d", &time, &g_x, &g_y, &g_z, &Button_T, &Button_X, &Button_S, &Button_C);
if (*Button_S == 1) {
return(1);
}
else {
return(0);
}
}
double roll(double x_mag) {
if (x_mag <= -1.0) {
x_mag = -1.0;
}
if (x_mag >= 1.0) {
x_mag = 1.0;
}
return(asin(x_mag));
}
double pitch(double y_mag) {
if (y_mag <= -1.0) {
y_mag = -1.0;
}
if (y_mag >= 1.0) {
y_mag = 1.0;
}
return(asin(y_mag));
}
int scaleRadsForScreen(double rad) {
return (int)((78 * rad) / PI);
}
void print_chars(int num, char use) {
int i = 0;
for (i = 0; i < num; i++) {
printf("%c", use);
}
return;
}
void graph_line(int number) {
if(number < 0) {
print_chars(number + 39, ' ');
print_chars(abs(number), 'l');
}
else if(number > 0) {
print_chars(40, ' ');
print_chars(number, 'r');
}
else if(number == 0) {
print_chars(39, ' ');
print_chars(1, '0');
}
}
答案 0 :(得分:1)
我怀疑问题发生在scanf
的{{1}}。您已将所有参数传递到该函数,其地址为&符号read_line
,但是,这些变量已经是指针,您需要在没有&符号的情况下传递它们。您还应该检查&
和scanf
的返回值。