Advertisement
Guest User

Asri Code

a guest
Mar 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <cstring>
  5.  
  6. struct Student {
  7.     char nim[11];
  8.     char name[31];
  9.     int id;
  10. };
  11.  
  12. struct Node {
  13.     struct Student *student;
  14.     struct Node* next;
  15. };
  16.  
  17. void initNode(struct Node *head,struct Student *student){
  18.     head->student = student;
  19.     head->next =NULL;
  20. }
  21.  
  22.  
  23. void tambahMahasiswa(struct Node *head, struct Student *student) {
  24.     Node *newNode = new Node;
  25.     newNode->student = student;
  26.  
  27.     newNode->next = NULL;
  28.     Node *cur = head;
  29.     while(cur) {
  30.         if(cur->next == NULL) {
  31.             cur->next = newNode;
  32.             return;
  33.         }
  34.         cur = cur->next;
  35.     }
  36. }
  37.  
  38. bool Convert(const char* arr, int &arrInt) {
  39.     errno = 0;
  40.     char *strend;
  41.  
  42.     arrInt = strtol(arr, &strend, 10);
  43.     if(errno == 0 && *strend == '\0')
  44.         return true;
  45.  
  46.     return false;
  47. }
  48.  
  49. int main() {
  50.     int i = 0;
  51.     int total_student = 0;
  52.  
  53.     struct Node *student_linked_list = new Node;
  54.  
  55.     std::cout << "Input Data Siswa : " << std::endl;
  56.  
  57.     for(i = 0; i < 10; i++) {
  58.         std::cout << "Data Ke - " << i + 1 << std::endl;
  59.         Student *student = new Student;
  60.  
  61.         while(true) {
  62.             char name_temp[50];
  63.             std::cout << "Masukan Nama : ";
  64.  
  65.             std::cin.getline(name_temp,50);
  66.  
  67.             if(strnlen(name_temp, 50) > 30) {
  68.                 std::cout << "Nama tidak boleh lebih dari 30 karakter" << std::endl;
  69.                 continue;
  70.             }
  71.  
  72.             if(strnlen(name_temp, 50) < 3) {
  73.                 std::cout << "Nama tidak boleh kurang dari 3 karakter" << std::endl;
  74.                 continue;
  75.             }
  76.  
  77.             strncpy(student->name, name_temp, 30);
  78.             break;
  79.         }
  80.  
  81.         while(true) {
  82.             char nim_temp[20];
  83.             std::cout << "Masukan NIM : ";
  84.  
  85.             std::cin.getline(nim_temp,20);
  86.  
  87.             if(strnlen(nim_temp, 20) != 10) {
  88.                 std::cout << "Panjang NIM harus 10 karakter" << std::endl;
  89.                 continue;
  90.             }
  91.  
  92.             int nim_int;
  93.             if(!Convert(nim_temp, nim_int)) {
  94.                 std::cout << "NIM harus angka" << std::endl;
  95.                 continue;
  96.             }
  97.  
  98.             strncpy(student->nim, nim_temp, 10);
  99.             break;
  100.         }
  101.  
  102.  
  103.         while(true) {
  104.             char id_temp[10];
  105.             std::cout << "Masukan Absensi : ";
  106.  
  107.             std::cin.getline(id_temp,10);
  108.  
  109.             int absensi_int;
  110.             if(!Convert(id_temp, absensi_int)) {
  111.                 std::cout << "Absensi harus angka" << std::endl;
  112.                 continue;
  113.             }
  114.  
  115.             student->id = absensi_int;
  116.             break;
  117.         }
  118.  
  119.         bool end_input = false;
  120.         while(true) {
  121.             char reinput[10];
  122.             std::cout << "Lanjutkan ? (y/n) : ";
  123.  
  124.             std::cin.getline(reinput,10);
  125.  
  126.             if(strncmp(reinput, "y", 10) == 0) {
  127.                 break;
  128.             }
  129.             if(strncmp(reinput, "n", 10) == 0) {
  130.                 end_input = true;
  131.                 break;
  132.             }
  133.         }
  134.  
  135.         if(i == 0) {
  136.             initNode(student_linked_list, student);
  137.         } else {
  138.             tambahMahasiswa(student_linked_list, student);
  139.         }
  140.  
  141.         total_student = total_student + 1;
  142.         std::cout << std::endl;
  143.  
  144.         if(end_input) {
  145.             break;
  146.         }
  147.     }
  148.  
  149.  
  150.     std::cout << std::endl << "Menampilkan Data 1 -" << (total_student) << std::endl;
  151.  
  152.     Node *list = student_linked_list;
  153.     int loop = 1;
  154.     while(list) {
  155.         std::cout << loop << ". " << list->student->name << " (" << list->student->nim << "): " << list->student->id << std::endl;
  156.         list = list->next;
  157.         loop = loop + 1;
  158.     }
  159.  
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement