fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. // Fungsi untuk menghasilkan gamet dari genotipe induk
  9. vector<char> get_gametes(const string& genotype) {
  10. vector<char> gametes;
  11. gametes.push_back(genotype[0]);
  12. if (genotype[0] != genotype[1]) {
  13. gametes.push_back(genotype[1]);
  14. }
  15. return gametes;
  16. }
  17.  
  18. int main() {
  19. // Input genotipe dari pengguna
  20. string induk1_genotipe, induk2_genotipe;
  21.  
  22. cout << "--- Program Perhitungan Persentase F1 ---" << endl;
  23. cout << "Masukkan genotipe induk 1 (contoh: TT, Tt, tt): ";
  24. cin >> induk1_genotipe;
  25. cout << "Masukkan genotipe induk 2 (contoh: TT, Tt, tt): ";
  26. cin >> induk2_genotipe;
  27.  
  28. // Menghasilkan gamet dari masing-masing induk
  29. vector<char> gamet1 = get_gametes(induk1_genotipe);
  30. vector<char> gamet2 = get_gametes(induk2_genotipe);
  31.  
  32. // Menggunakan map untuk menghitung frekuensi genotipe F1
  33. map<string, int> frekuensi_genotipe;
  34.  
  35. // Melakukan persilangan (tabel Punnett)
  36. for (char g1 : gamet1) {
  37. for (char g2 : gamet2) {
  38. string offspring_genotype;
  39. offspring_genotype += min(g1, g2); // Memastikan urutan alfabetis
  40. offspring_genotype += max(g1, g2);
  41. frekuensi_genotipe[offspring_genotype]++;
  42. }
  43. }
  44.  
  45. // Menghitung total keturunan
  46. int total_keturunan = gamet1.size() * gamet2.size();
  47.  
  48. cout << "\n--- Hasil Persilangan (F1) ---" << endl;
  49.  
  50. // Menampilkan persentase genotipe dan fenotipe
  51. for (auto const& [genotipe, frekuensi] : frekuensi_genotipe) {
  52. double persentase = (static_cast<double>(frekuensi) / total_keturunan) * 100.0;
  53.  
  54. // Menentukan fenotipe berdasarkan genotipe (asumsi dominasi penuh)
  55. string fenotipe;
  56. if (genotipe.find('T') != string::npos || genotipe.find('H') != string::npos) {
  57. fenotipe = "Dominan"; // Contoh: Tinggi, Merah, dll.
  58. } else {
  59. fenotipe = "Resesif"; // Contoh: Pendek, Putih, dll.
  60. }
  61.  
  62. cout << "Genotipe: " << genotipe << " (" << fenotipe << ")" << endl;
  63. cout << "Persentase: " << persentase << "%" << endl << endl;
  64. }
  65.  
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
--- Program Perhitungan Persentase F1 ---
Masukkan genotipe induk 1 (contoh: TT, Tt, tt): Masukkan genotipe induk 2 (contoh: TT, Tt, tt): 
--- Hasil Persilangan (F1) ---
Genotipe:  (Resesif)
Persentase: 25%

Genotipe: O (Resesif)
Persentase: 25%

Genotipe: � (Resesif)
Persentase: 25%

Genotipe: �O (Resesif)
Persentase: 25%