Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. typedef struct Student{
  6. char* studentName; //Dyn. alloc. of stud. name
  7. long id; // ID Number
  8. float mark; // mark
  9. unsigned char subjectStatus;
  10. }Student;
  11.  
  12. typedef struct University{
  13. Student* studentArr; // Dync. Alloc(Realloc) of students
  14. int numOfStudents; //num of students
  15. }University;
  16.  
  17. void getStudents(University *ptr);
  18. unsigned char stringToBinary(unsigned char tmpSubjectStatus[]);
  19.  
  20. void main()
  21. {
  22. printf("Please enter details of student: (a)");
  23. University uni;
  24. getStudents(&uni); //Send address of structure University, because we want to change it not make a local copy of it
  25.  
  26. getch();
  27. }
  28. void getStudents(University *ptr)
  29. {
  30. FILE *op;
  31. char tmpStudentName[20];
  32. long tmpId;
  33. float tmpMark;
  34. char tmpSubjectStatus[6];
  35. ptr->numOfStudents = 0;
  36. if ((op = fopen("input.txt", "r")) == NULL)
  37. {
  38. printf("Failed to open file.");
  39. }
  40. ptr->studentArr = (Student*)malloc(sizeof(Student));
  41. if (ptr->studentArr == NULL){
  42. printf("Error: memory was not allocated.");
  43. exit(1);
  44. }
  45. while (fscanf(op, "%s %ld %f %s", tmpStudentName, &tmpId, &tmpMark, tmpSubjectStatus) != NULL)
  46. {
  47. ptr->numOfStudents++;
  48. ptr->studentArr = (Student*)realloc(ptr->studentArr, sizeof(Student) * ptr->numOfStudents); /*Additional code for Realloc fails - we didn't study!*/
  49. ptr->studentArr[ptr->numOfStudents - 1].studentName = (char*)malloc(sizeof(char)* strlen(tmpStudentName));
  50.  
  51. if (!(ptr->studentArr[ptr->numOfStudents - 1].studentName)) //if we failed to allocate memory for studentName
  52. {
  53. while (ptr->numOfStudents > 0)
  54. {
  55. free(ptr->studentArr[ptr->numOfStudents - 1].studentName); //free student name
  56. ptr->numOfStudents--; // decrease numOfStudents by one
  57. }
  58. free(ptr->studentArr); //if all student names are free, we need to free the array
  59. printf("Student name was not allocated.");
  60. exit(1);
  61. }
  62.  
  63. strcpy(ptr->studentArr[ptr->numOfStudents - 1].studentName, tmpStudentName);
  64. ptr->studentArr[ptr->numOfStudents - 1].id = tmpId;
  65. ptr->studentArr[ptr->numOfStudents - 1].mark = tmpMark;
  66. ptr->studentArr[ptr->numOfStudents - 1].subjectStatus = stringToBinary(tmpSubjectStatus); //atoi: from "11001"(string) to 11001(int),then casting to unsigned char
  67. }
  68.  
  69. fclose(op);
  70. }
  71.  
  72. unsigned char stringToBinary(char tmpSubjectStatus[])
  73. {
  74. int tmpInteger;
  75. int tmpBinary = 0; //convert tmpInteger into binary number
  76. unsigned char tmpBinaryCh;
  77. int i = 0;
  78. int mostRightbit = 16;
  79. tmpInteger = atoi(tmpSubjectStatus); //Convert from string to int
  80. while (i < 5){
  81. if (((16 >> i) & tmpInteger) != 0){
  82. tmpBinary += (16 >> i);
  83. }
  84. i++;
  85. }
  86. tmpBinaryCh = (unsigned char)tmpBinary;
  87. return tmpBinaryCh;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement