程序运行时没有任何错误,它会提示用户输入行号和列号,以便它可以构建一个2D数组。如果为行输入2,为列输入2,则进入下一步但如果输入3及以上,则会以错误结束
"导致此程序停止工作的问题......"
这是我的代码
#include <stdio.h>
#include <string.h>
#include <malloc.h>
//declaring structure called student
struct student {
//members of the structure
char last_name[30];
char first_name[30];
};
//declaring structure called classroom_seating
struct classroom_seating {
struct student **seating ;
};
//Functions implementation for struct student.
void student_init_default(struct student *s) {
//initializing the data members to string "???"
strcpy(s->first_name, "???");
strcpy(s->last_name, "???");
}
void student_init(struct student *s, char *info) {
/* Use the strtok function to extract first name and
last name from the variable student, then assign
them to each instance variable of the student
structure*/
strcpy(s->first_name, strtok(info, "/"));
strcpy(s->last_name, strtok(NULL, "/"));
}
void student_to_string(struct student *s) {
//printing the initial character of the first name, a period, the initial character of the last name, and a
//period
printf("%c.%c.", s->first_name[0], s->last_name[0]);
}
//end of step1
//step2
//Functions implementation for struct classro classroom_seating_init(int rowNum, int columnNum, struct classroom_seating *a){
//It instantiates a two-dimensional array of the size "rowNum" by "columnNum" specified by the
//parameters inside the struct a. Then it initializes each student element of this array using the student_init_default function
void classroom_seating_init(int rowNum, int columnNum, struct classroom_seating *a){
a->seating=malloc((sizeof(a->seating[rowNum][columnNum])));
//initializing the counters
int i = 0;
int j = 0;
for (i = 0; i < rowNum; i++) {
for (j = 0; j < columnNum; j++) {
student_init_default(&a->seating[i][j]);
}
printf("\n");
}
}
int assign_student_at(int row, int col,struct classroom_seating *a,struct student *s) {
char str1[30];
strcpy(str1, a->seating[row][col].first_name);
if (strcmp(str1, "???"))
{
strcpy(a->seating[row][col].first_name,s->first_name);
strcpy(a->seating[row][col].last_name,s->last_name);
return 1;
}
else {
return 0;
}
}
int check_boundaries(int row, int col, struct classroom_seating *a) {
if (row < 0 || col < 0) {
return 0;
}
else if (row > sizeof(a->seating) || col > sizeof(a->seating[0])) {
return 0;
}
else {
return 1;
}
}
void classroom_seating_to_string(struct classroom_seating *a) {
printf("The Current Seating\n");
printf("-------------------\n");
int i = 0;
int j = 0;
for (i = 0; i < sizeof(a->seating); i++) {
for (j = 0; j < sizeof(a->seating[0]); j++) {
student_to_string(&a->seating[i][j]);
}
printf("\n");
}
}
//end of functions implementation.
//main function
int main(){
//variable declaration
struct student s;
struct classroom_seating a;
char info[30];
int rowNum,colNum,row,col;
//taking the inputs from the user,,,enter number of rows and columns that the array will have
printf("Please enter a number of rows for an classroom seating.");
scanf("%d",&rowNum);
printf("Please enter a number of columns for an classroom seating.");
scanf("%d",&colNum);
//
classroom_seating_init(rowNum,colNum,&a);//calling method to create a 2D array for class arrangement.
printf("Please enter a student information or enter \"Q\" to quit.");
scanf("%s",&info);
student_init(&s, info);
while (1) {
printf("\nA student information is read.\n");
// printing student information.
student_to_string(&s);
printf("\n");
// Ask a user to decide where to seat a student by asking for row and column of a seat
printf("Please enter a row number where the student wants to sit.");
scanf("%d",&row);
printf("Please enter a column number where the student wants to sit.");
scanf("%d", &col);
// Checking if the row number and column number are valid
if (check_boundaries(row, col, &a) == 0) {
printf("\nrow or column number is not valid.");
printf("A student %s %s is not assigned a seat.\n",s.first_name, s.last_name);
}
else {
// Assigning a seat for a student
if (assign_student_at(row, col, &a, &s)== 1) {
printf("\nThe seat at row %d and column %d is assigned to the student\n", row, col);
student_to_string(&s);
classroom_seating_to_string(&a);
}
else {
printf("\nThe seat at row %d and column %d is taken.\n", row, col);
}
}
// Read the next studentInfo
printf("Please enter a student information or enter \"Q\" to quit.");
// reading a student's information
scanf("%s", info);
}
return 0;
}
答案 0 :(得分:2)
鉴于
struct student {
//members of the structure
char last_name[30];
char first_name[30];
};
//declaring structure called classroom_seating
struct classroom_seating {
struct student **seating ;
};
然后
a->seating=malloc((sizeof(a->seating[rowNum][columnNum])));
不是你想要的。当前的问题是,您的结构定义无法模拟现实世界的座位问题。首先,您需要填补总座位数。
您可以将定义更改为
#define ROW_MAX 45
// Say you have 45 rows
#define COL_MAX 50
// Say you have 30 cols
//declaring structure called classroom_seating
struct classroom_seating {
struct student seating[ROW_MAX][COLUMN_MAX] ; //maximum seating
};
然后classroom_seating_init
可以是
void classroom_seating_init(void classroom_seating_init(int rowNum, int columnNum, struct classroom_seating *a){
// You should check the boundaries before reaching this point
// Do something with a->seating[rowNum][columnNum]
// Say add First name scanf("%s",(a->seating[rowNum][columnNum]).first_name)
}
您可以选择在struct student
struct student {
//members of the structure
unsigned char isfilled;
// set it to say 'f' when filling a particular spot is filled
char last_name[30];
char first_name[30];
};