untuk menggunakan 1 struct untuk beberapa kali, beberapa cara :
1.Deklarasi manual
#include <stdio.h>
typedef struct Mahasiswa {
char NIM[8];
char nama[50];
float ipk;
};
main()
{
Mahasiswa a,b,c;
……
……
……
}
artinya struct mahasiswa digunakan untuk 3 variabel, yaitu a,b,c.
2.Struct of array 1
#include <stdio.h>
typedef struct Mahasiswa {
char NIM[8];
char nama[50];
float ipk;
}mhs;
void main()
{
mhs biodata[3];
……
……
……
}
3.Struct of array 2
#include <stdio.h>
typedef struct Mahasiswa {
char NIM[8];
char nama[50];
float ipk;
}mhs[3];
void main()
{
mhs biodata[3];
……
……
……
}
4.Struct of array 3
#include <stdio.h>
struct Mahasiswa {
char NIM[8];
char nama[50];
float ipk;
};
void main()
{
struct mhs biodata[3];
……
……
……
}
Contoh Program :
#include <iostream.h>
#include <conio.h>
struct mhs
{ char nama[20],nim[10],jurusan[2];
int sks,program; };
struct mhs bayar[2];
main ()
{
int bts,var,tetap;
for(int i=0;i<2;i++)
{
//input data
cout<<"\nNama mhs = ";cin>>bayar[i].nama;
cout<<"NIM = ";cin>>bayar[i].nim;
cout<<"Jurusan[TI,MI,SI] = ";
cin>>bayar[i].jurusan;
input:
cout<<"Program[1=D3,2=S1]= ";
cin>>bayar[i].program;
}
if (bayar[i].program < 0 || bayar[i].program > 2)
{cout<<"Program tidak sesuai\n";
goto input;}
cout<<"Jumlah sks = ";cin>>bayar[i].sks;
if (bayar[i].program==1)
{tetap=500000;
var=bayar[i].sks*25000;}
else if (bayar[i].program==2)
{tetap=750000;
var=bayar[i].sks*50000;}
cout<<"“;
//output data
cout<<"\n\n-----------------------\n";
cout<<" Output ";
cout<<"\n-----------------------\n";
cout<<"\nNama mhs = "<<bayar[i].nama;
cout<<"\nNIM = "<<bayar[i].nim;
cout<<"\nJurusan = "<<bayar[i].jurusan;
cout<<"\nProgram = "<<bayar[i].program;
cout<<"\nJumlah sks = "<<bayar[i].sks;
cout<<"\nSpp tetap = "<<tetap;
cout<<"\nSpp variabel = "<<var;
cout<<endl<<endl;
} getch();}
0 Komentar untuk "Struct of array"