2014-12-06 28 views
-6

解決了我自己的問題,使用了不同於以下建議的方法! :)如何打印C中結構的內容?

感謝您查看我的問題! :)

我一直在學習關於結構和工作在C實踐實驗室,我的代碼似乎沒有正確編譯與我做任何更改。 目前我沒有收到任何輸出,程序崩潰。對於如何在將函數傳遞給函數時如何正確使用'*'和'&'符號,我仍然非常困惑。我對這種做法的目標是:

  • 打印在相同的格式的數據文件
  • 打印用最好的GPA
  • 計算學生的全名數組的內容並打印平均GPA
  • 打印所有學生的名字與平均
  • 打印上面的GPA誰擁有低於平均水平
  • 不大不小的GPA在數組中的結構爲順序從最低到最高最年輕的學生的名字GPA
  • 重新打印陣列(現在是按不同的順序從去年 時間)

如何正確地調用,並從學生結構打印這些項目嗎?我如何訪問gpa值來傳遞一個函數來計算平均值?

#include <stdio.h> 
#include <stdlib.h> 

// define constants 
#define ARR 100 
#define FIRST 7 
#define MIDINIT 1 
#define LAST 9 
#define STREET 16 
#define CITY 11 
#define STATE 2 
#define ZIP 5 
#define AGE 3 
#define GPA 4 
#define START 0 
#define FIRSTID 8 
#define INITID 10 
#define STREETID 20 
#define CITYID 37 
#define STATEID 49 
#define ZIPID 52 
#define AGEID 57 
#define GPAID 64 

// defined structs 

typedef struct { 
    char street[STREET + 1]; 
    char city[CITY + 1]; 
    char state[STATE + 1]; 
    char zip[ZIP + 1]; 
} Address; 

typedef struct { 
    char firstname[FIRST + 1]; 
    char initial[MIDINIT + 1]; 
    char lastname[LAST + 1]; 
    Address ofstudent; 
    int age; 
    double gpa; 
} Student; 

// function prototype 
void strsub(char buf[], char s[], int start, int size); 
void processStudent(int *id, Student students[]); 
void sortStudentGpa(Student *students, int id); 
void maxGpa(Student *students, int id); 

/* lab6student.c: creates an array of student structures and outputs reports */ 
int main(void) 
{ 
    Student students[ARR]; // creates an array of student structures 
    int id = 0; // counter for student 

    processStudent(&id, students); 
    maxGpa(students, id); 
} 
void strsub(char buf[], char s[], int start, int size) { 
    int i; 

    for (i = 0; i < size && buf[start + i] != '\0'; i++) { 
     // loops as long as iterator is less than size 
     // and while string has not run out of characters 
     s[i] = buf[i + start]; 
    } 
    s[i] = '\0'; 
} 
/* void sort(Student *students, int id) { 
    int j, i; 

    for(i = 1; i < n; i++) { 
     for(j = 0; j < id - i; j++) { 
      if(students[j].gpa > students[j + 1].gpa) { 
       Student temp = students[j]; 
       students[j] = students[j + 1]; 
       students[j + 1] = temp; 
      } 
     } 
    } 
} */ 
void processStudent(int *id, Student students[]) { 
    FILE *data; 
    char line[ARR]; 
    *id = 0; // counter for student 

    data = fopen("Students.dat", "r"); 
    if (data == NULL) { 
     printf("Students.dat file not found!\n"); 
     exit(1); 
    } 

    // process file 
    while (!feof(data)) { 
     // organize student info into separate arrays 
     fgets(line, ARR, data); 
     strsub(line, students[*id].firstname, START, FIRST); 
     strsub(line, students[*id].initial, FIRSTID, MIDINIT); 
     strsub(line, students[*id].lastname, INITID, LAST); 
     strsub(line, students[*id].ofstudent.street, STREETID, STREET); 
     strsub(line, students[*id].ofstudent.city, CITYID, CITY); 
     strsub(line, students[*id].ofstudent.state, STATEID, STATE); 
     strsub(line, students[*id].ofstudent.zip, ZIPID, ZIP); 
     students[*id].age = atoi(&line[AGEID]); 
     students[*id].gpa = atoi(&line[GPAID]); 
     (*id)++; 
    } 

    fclose(data); 
} 
//sorts struct student array containing num (gpa) elements into 
//ascending order 
void sortStudentGpa(Student *students, int id) { 
    int i, j; // indexes into unsorted and sorted partitions 
    Student temp; // temporarily holds an element from the array 

    for (i = 1; i < id; ++i) { 
     temp = students[i]; 
     j = i - 1; 
     while (j >= 0 && temp.gpa < students[j].gpa) { 
      students[j + 1] = students[j]; 
      j = j - 1; 
     } 
     students[j + 1] = temp; 
    } 
} 
void maxGpa(Student *students, int id) { 
    int iwithmax, i; 
    float max = 0; 

    for (i = 0 ; i < id ; i++) { 
     if (students -> gpa > max) { 
      max = students -> gpa; 
      iwithmax = i; 
     } 
    } 

    printf("\n\nHighest GPA is done by Student %d with GPA = %f", iwithmax, max); 
} 
+3

使用調試器。編譯啓用警告。我發現不太可能存在編譯器錯誤(「我的代碼似乎沒有正確編譯」)。重新開始並逐步添加部分代碼,以查看錯誤的位置。校園中的某個人可能願意挖掘您的代碼並對其進行調試,而Stackoverflow則不然。 – 2014-12-06 06:00:28

+0

請不要在一個問兩個問題。這是如何幫助其他人搜索的? – 2014-12-06 09:05:49

+0

在進程學生函數中,您將id的地址分配爲0.因此您必須分配值而不是地址。 * id = 0;然後你執行代碼。 – sharon 2014-12-06 10:32:02

回答

5

在maxGpa功能只是改變了定義

 void maxGpa(Student *students, int id); 

然後在maxGpa功能做了以下更改

void maxGpa(Student *students, int id) { 
    int iwithmax, i; 
    float max = 0; 

    for (i = 0 ; i < id ; i++) { 
      if (students -> gpa > max) { 
        max = students -> gpa; 
        iwithmax = i; 
      } 
    } 

試試這個.....

+0

您可能想要解釋何時使用' - >'箭頭運算符,以及具有結構的'.'運算符來確保學習已經發生。 – 2014-12-06 06:56:09

+1

傳遞時我們只會傳遞地址,所以我們會得到指針,所以我們必須使用 - >。如果我們想要使用*,那麼我們已經指定了這樣的(*學生)。因爲結構成員運營商的優先權。高於*。如果我們給* students.gpa這將意味着*(students.gpa),這將是非法的。 – sharon 2014-12-06 07:51:06

+0

這不是一個很好的答案。首先,它只解決最後提出的第二個充滿問題,而不是標題中的問題。然後,請不要在沒有解釋的情況下發布代碼片段。 – 2014-12-06 09:07:23