Situatie
Sa se creeze un tablou de structuri de tip student (cod matricol, un numar in intervalul 1..999; nume student, un sir de 20 caractere; numar de note, un numar in intervalul 1..15, notele: nota(1), nota(2) … si media calculata) pentru maximum nmax (20) studenti si apoi sa se afiseze un tabel cu mediile studentilor, utilizandu-se declararea unei structuri specifice limbajului C++ care sa includa descrierea structurii, functia de citire si cea de afisare a informatiilor unui student.
Solutie
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<string.h>
#define nmax 20
struct tip_student
{
int cm;
char nume[20];
int nrn;
double nota[15];
double media;
void citeste_student(int wcm,char wnume[20],int wnrn,
double wnota[15],double wmedia)
{
int i;
this->cm=wcm;
strcpy(this->nume,wnume);
this->nrn=wnrn;
this->media=0;
for(i=0;i<wnrn;i++)
{
this->media=this->media+wnota[i];
}
this->media=this->media/this->nrn;
wmedia=this->media;
}
void afisaza_student()
{
printf(“n %5d %-22s %6.2lf”,this->cm,this->nume,this->media);
}
};
void main(void)
{
tip_student student[nmax]; char r=’d’;
int wcm,wnrn;char wnume[20];double wnota[15];
int k,i=0;
while((r==’d’)&&(i<nmax))
{
do
{
printf(“n cod student (1..999):”);
scanf(“%d”,&wcm);
}
while((wcm<1)||(wcm>999));
printf(“n nume student:”);
cin.getline(wnume,20,’n’);
do
{
printf(“n nr note(1..15):”);
scanf(“%d”,&wnrn);
}
while((wnrn<1)||(wnrn>15));
for(k=0;k<wnrn;k++)
{
do
{
printf(“n nota(%d)=”,k);
scanf(“%lf”,&wnota[k]);
}
while((wnota[k]<1)||(wnota[k]>10));
}
student[i].citeste_student(wcm,wnume,wnrn,wnota,0);
i++;
printf(“n continuati?(d/n):”);
r=getche();
}
printf(“n mediile studentilor “);
printf(“n =====================================”);
printf(“n cod nume si prenume media “);
printf(“n =====================================”);
for(k=0;k<i;k++) student[k].afisaza_student();
printf(“n =====================================n”);
}
Leave A Comment?