Форум языка CPP

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.


Вы здесь » Форум языка CPP » Код домашнего задания » Программа для ввода данных в структуру.


Программа для ввода данных в структуру.

Сообщений 1 страница 3 из 3

1

Код:
// strukt.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string.h>
#include <time.h>
//#include <windows.h>

struct student
{
	char pcStudName[15];
	char pcStudSurName[15];
	int iStudAge;
	int piStudRating[5];
	float iAveRating;

};

int FunctionEnterNumber(int iDecimalNumber);
int FunctionCreatRandomNameOrSurname(char **pP, int i_SIZE, int &iIndex);

void FunctionCreatAgeReating(int *pcRandAge, int *pcRandReating, int i_SIZE);
void FunctionDrawingMenu(void);
void FunctionFullRandomGen(student **ppStudentArrPointers, int i_SIZE);
void FunctionUserRandomGen(	student **ppStudentArrPointers, int i_SIZE, 
            	char **pcRandName,    char **pcRandSurName,
            	int *pcRandAge,    	int *pcRandReating	);

void FunctionEnterAllDataFromStruct( student **ppStudentArrPointers, int i_SIZE);
void FunctionOutputDataStructure( student **ppStudentArrPointers, int i_SIZE);




int _tmain(int argc, _TCHAR* argv[])
{
	const int i_SIZE = 5;
	int iKayChoice;
	int iIndex = 0;
	bool bDataRnd = false;
	char **pcRandName, **pcRandSurName; 
	int *pcRandAge, *pcRandReating;
    	
	FunctionDrawingMenu();

	student **ppStudentArrPointers = new student*[i_SIZE];
	for(int i = 0; i < i_SIZE; ++i)
	{
    ppStudentArrPointers[i] = new student[i_SIZE];
	}	
	
	

	switch(iKayChoice = _getch())
	{
    case 49:
    	FunctionFullRandomGen(ppStudentArrPointers, i_SIZE);
    	
    break;
    case 50:
    	system("cls");
    	
    	bDataRnd = true;
    	pcRandName = new char*[i_SIZE];
    	pcRandSurName = new char*[i_SIZE];
    	pcRandAge = new int[i_SIZE]; 
    	pcRandReating = new int[i_SIZE];
    	std::cout<<"Enter names: "<<std::endl;
    	do
    	{
        std::cout<<"Enter no more than 12 characters and no less than 5 words"<<std::endl;
        std::cout<<"(or words to enter a space)"<<std::endl;
    	}while(!FunctionCreatRandomNameOrSurname(pcRandName, i_SIZE, iIndex));

    	system("cls");
    	
    	std::cout<<"Enter surnames: "<<std::endl;
    	iIndex = 0;
    	do
    	{
        std::cout<<"Enter no more than 12 characters and no less than 5 words"<<std::endl;
        std::cout<<"(or words to enter a space)"<<std::endl;
    	}while(!FunctionCreatRandomNameOrSurname(pcRandSurName, i_SIZE, iIndex));

    	FunctionCreatAgeReating(pcRandAge, pcRandReating, i_SIZE);
    	FunctionUserRandomGen(ppStudentArrPointers, i_SIZE, pcRandName, pcRandSurName, pcRandAge, pcRandReating);
    break;
    case 51:
    FunctionEnterAllDataFromStruct(ppStudentArrPointers, i_SIZE);
    system("cls");
    break;
	}
	system("cls");
	FunctionOutputDataStructure(ppStudentArrPointers, i_SIZE);
	
	//for(register int i = 0; i < i_SIZE; ++i)//NAME
	//{
	//	
	//	std::cout<<ppStudentArrPointers[i]->iAveRating<<std::endl;
	//}
    
	
	
	/*
	*Removing the memory allocated for the structure
	*/
	for(int i = 0; i < i_SIZE; ++i)
	{
    delete[] ppStudentArrPointers[i];
	}
	delete[] ppStudentArrPointers;
	
	/*
	*Removing the memory allocated for the surnames and names
	*/ 
	if(bDataRnd)
	{
    for(int i = 0; i < i_SIZE; ++i)
    {
    	delete[] pcRandSurName[i];
    	delete[] pcRandName[i];
    }
    delete[] pcRandSurName;
    delete[] pcRandName;
    delete[] pcRandAge; 
    delete[] pcRandReating;
	}
    

	std::cout<<"It is work"<<std::endl;
	
	return 0;
}

/*
*Function input names and surnames
*/

int FunctionCreatRandomNameOrSurname(char **pP, int i_SIZE, int &iIndex)
{
	char *pArrName = new char[i_SIZE*15];
	char *pNextName, *pName;
	int iLeng;
	
	std::cin.getline(pArrName, i_SIZE*15);
	
	pName = strtok_s(pArrName, " ", &pNextName);	
	
	
	while(iIndex < i_SIZE && pName != NULL)//The breakdown input text into words
	{
    iLeng = (int)strlen(pName)+1;
        
    if(13 < iLeng)//Check the length of the name
    {
    	delete[] pArrName;
    	return 0;
    }
    pP[iIndex] = new char[iLeng];
    
    strcpy_s(pP[iIndex++], iLeng, pName);
        
    if(pName != NULL)
    {
    	pName = strtok_s(NULL, " ", &pNextName);	
    }
    
    iLeng = 0;
	}
	if(iIndex < i_SIZE)//Check whether all the names entered
    {
    	delete[] pArrName;
    	return 0;
    }
	
	delete[] pArrName;
	return 1;
}

/*
*Function drawing menu
*/
void FunctionDrawingMenu(void)
{
	char pMessageOne[] = "1) Full generate a list of students";
	char pMessageTwo[] = "2) Generate a list of students using the input data";
	char pMessageThree[] = "3) Use the data to the list of students, without generating a random method";
	
	int iLengOne, iLengTwo, iLengThree;
	
	/*
	*Constant dimensions of the frame
	*/
	const int iScrenSizeX = 78;
	const int iScrenSizeY = 6;
	
	/*
	*Dimensions for the alignment of text in the center
	*/
	iLengOne = (iScrenSizeX - (int)strlen(pMessageOne)) / 2;
	iLengTwo = (iScrenSizeX - (int)strlen(pMessageTwo)) / 2;
	iLengThree = (iScrenSizeX - (int)strlen(pMessageThree)) / 2;
	
	/*
	*Draw a border around the text
	*/
	for(register int i = 0; i <= iScrenSizeY; ++i)//Draw a box around the menu text
	{
    switch(i)
    {
    	case 0:
    	case iScrenSizeY:
        for(register int j = 0; j <= iScrenSizeX; ++j)
        {
        	std::cout<<'#';
        }
    	break;
    	case 1:
    	case (iScrenSizeY-1):
        std::cout<<'#';
        for(register int j = 0; j < iScrenSizeX-1; ++j)
        {
        	std::cout<<' ';
        }
        std::cout<<'#';
    	break;
    	case 2:
        std::cout<<'#';
        for(register int j = 0; j < iLengOne; ++j)
        {
        	std::cout<<' ';
        }
        std::cout<<pMessageOne;
        for(register int j = 0; j < iLengOne; ++j)
        {
        	std::cout<<' ';
        }
        std::cout<<'#';
    	break;
    	case 3:
        std::cout<<'#';
        for(register int j = 0; j < iLengTwo; ++j)
        {
        	std::cout<<' ';
        }
        std::cout<<pMessageTwo;
        for(register int j = 0; j < iLengTwo; ++j)
        {
        	std::cout<<' ';
        }
        std::cout<<'#';
    	break;
    	case 4:
        std::cout<<'#';
        for(register int j = 0; j < iLengThree; ++j)
        {
        	std::cout<<' ';
        }
        std::cout<<pMessageThree;
        for(register int j = 0; j < iLengThree; ++j)
        {
        	std::cout<<' ';
        }
        std::cout<<'#';
    	break;
    }
    std::cout<<std::endl;
	}
	std::cout<<"Make your choice: "<<std::endl;
	

}


/*
*Function of filling the structure with random data
*/
void FunctionFullRandomGen(student **ppStudentArrPointers, int i_SIZE)
{
	srand((unsigned)time(NULL));

	char pWiredNames[5][10] = {"Andrew", "Nicholas", "Alexander", "Vladislav", "Gennady"};
	char pWiredSurNames[5][13] = {"Gabdulkhaev", "Kadomsky", "Savateykin", "Khaimov", "Babahin"};
	
	for(register int i = 0, rnd; i < i_SIZE; ++i)//NAME
	{
    rnd = rand()%5;
    
    strcpy_s(ppStudentArrPointers[i]->pcStudName, pWiredNames[rnd]);
	}

	for(register int i = 0, rnd; i < i_SIZE; ++i)//SURNAME
	{
    rnd = rand()%5;
    
    strcpy_s(ppStudentArrPointers[i]->pcStudSurName, pWiredSurNames[rnd]);
	}

	for(register int i = 0, rnd; i < i_SIZE; ++i)//AGE
	{
    rnd = rand()%5 + 16;
    ppStudentArrPointers[i]->iStudAge = rnd;
	}
	
	int i_SSARR = sizeof(ppStudentArrPointers[0]->piStudRating)/sizeof(int);

	for(register int i = 0, rnd = 0; i < i_SIZE; ++i)//RATING
	{
    for(register int j = 0; j < i_SSARR; ++j)
    {
    	rnd = rand()%5 + 1;
    	ppStudentArrPointers[i]->piStudRating[j] = rnd;
    }
    
	}

	for(register int i = 0, sum = 0; i < i_SIZE; ++i)//RATING
	{
    for(register int j = 0; j < i_SSARR; ++j)
    {
    	sum += ppStudentArrPointers[i]->piStudRating[j];
    }
    ppStudentArrPointers[i]->iAveRating = ((float)sum) / i_SSARR;
    sum = 0;
	}

}

/*
*Function of filling the structure with random user data
*/
void FunctionUserRandomGen(	student **ppStudentArrPointers, int i_SIZE, 
            	char **pcRandName,    char **pcRandSurName,
            	int *pcRandAge,    int *pcRandReating	)
{
	srand((unsigned)time(NULL));

	for(register int i = 0, rnd; i < i_SIZE; ++i)//NAME
	{
    rnd = rand()%5;
    
    strcpy_s(ppStudentArrPointers[i]->pcStudName, pcRandName[rnd]);
	}

	for(register int i = 0, rnd; i < i_SIZE; ++i)//SURNAME
	{
    rnd = rand()%5;
    
    strcpy_s(ppStudentArrPointers[i]->pcStudSurName, pcRandSurName[rnd]);
	}

	for(register int i = 0, rnd; i < i_SIZE; ++i)//AGE
	{
    rnd = rand()%5;
    ppStudentArrPointers[i]->iStudAge = pcRandAge[rnd];
	}
	
	int i_SSARR = sizeof(ppStudentArrPointers[0]->piStudRating)/sizeof(int);

	for(register int i = 0, rnd = 0; i < i_SIZE; ++i)//RATING
	{
    for(register int j = 0; j < i_SSARR; ++j)
    {
    	rnd = rand()%5;
    	ppStudentArrPointers[i]->piStudRating[j] = pcRandReating[rnd];
    }
    
	}

	for(register int i = 0, sum = 0; i < i_SIZE; ++i)//RATING
	{
    for(register int j = 0; j < i_SSARR; ++j)
    {
    	sum += ppStudentArrPointers[i]->piStudRating[j];
    }
    ppStudentArrPointers[i]->iAveRating = ((float)sum) / i_SSARR;
    sum = 0;
	}

}

/*
*Function input data from struct student
*/
void FunctionEnterAllDataFromStruct( student **ppStudentArrPointers, int i_SIZE)
{
	char **pcRandName = new char*[i_SIZE];
	char **pcRandSurName = new char*[i_SIZE];
	int iIndex = 0;
	system("cls");
	std::cout<<"Enter names: "<<std::endl;
	
	do//Enter a name
	{
    std::cout<<"Enter no more than 12 characters and no less than 5 words"<<std::endl;
    std::cout<<"(or words to enter a space)"<<std::endl;
	}while(!FunctionCreatRandomNameOrSurname(pcRandName, i_SIZE, iIndex));

	system("cls");
	std::cout<<"Enter surnames: "<<std::endl;
	iIndex = 0;
	do//Enter a surname
	{
    std::cout<<"Enter no more than 12 characters and no less than 5 words"<<std::endl;
    std::cout<<"(or words to enter a space)"<<std::endl;
	}while(!FunctionCreatRandomNameOrSurname(pcRandSurName, i_SIZE, iIndex));
	system("cls");
	
	for(register int i = 0; i < i_SIZE; ++i)//entry into the structure NAME
	{
    strcpy_s(ppStudentArrPointers[i]->pcStudName, pcRandName[i]);
	}

	for(register int i = 0; i < i_SIZE; ++i)//entry into the structure SURNAME
	{
    strcpy_s(ppStudentArrPointers[i]->pcStudSurName, pcRandSurName[i]);
	}
	
	for(register int i = 0; i < i_SIZE; ++i)
    {
    	delete[] pcRandSurName[i];
    	delete[] pcRandName[i];
    }
    delete[] pcRandSurName;
    delete[] pcRandName;

	std::cout<<"Please enter the age of "<<i_SIZE<<" students"<<std::endl;
	for(register int i = 0; i < i_SIZE; ++i)//entry into the structure AGE
	{
    std::cout<<"Student "<< i <<std::endl;
    ppStudentArrPointers[i]->iStudAge = FunctionEnterNumber(2);
	}
	
	int i_SSARR = sizeof(ppStudentArrPointers[0]->piStudRating)/sizeof(int);

	std::cout<<"Enter 5 ratings for each of the "<<i_SIZE<<" students"<<std::endl;
	for(register int i = 0; i < i_SIZE; ++i)//entry into the structure RATING
	{
    for(register int j = 0; j < i_SSARR; ++j)
    {
    	std::cout<<"Student "<< i <<" rating "<< j <<std::endl;
    	ppStudentArrPointers[i]->piStudRating[j] = FunctionEnterNumber(1);
    }
    
	}

	for(register int i = 0, sum = 0; i < i_SIZE; ++i)//entry into the structure EV RATING
	{
    for(register int j = 0; j < i_SSARR; ++j)
    {
    	sum += ppStudentArrPointers[i]->piStudRating[j];
    }
    ppStudentArrPointers[i]->iAveRating = ((float)sum) / i_SSARR;
    sum = 0;
	}
}


/*
*Function input age and reating student
*/
void FunctionCreatAgeReating(int *pcRandAge, int *pcRandReating, int i_SIZE)
{
	system("cls");
	std::cout<<"Please enter the age of 5 students"<<std::endl;
	for(register int i = 0; i < i_SIZE; ++i)//AGE
	{
    std::cout<<"Student "<< i <<std::endl;
    pcRandAge[i] = FunctionEnterNumber(2);
	}
	system("cls");
	std::cout<<"Please enter the age of 5 students"<<std::endl;
	for(register int i = 0; i < i_SIZE; ++i)//REATING
	{
    std::cout<<"Student "<< i <<std::endl;
    pcRandReating[i] = FunctionEnterNumber(1);
	}
	system("cls");
	
}


/*
*The function convert a string to number
*/

int FunctionEnterNumber(int iDecimalNumber)
{
	if(iDecimalNumber > 9)
	{
    iDecimalNumber = 9;
	}
	int cInputKay;
	int iResult = 0;
	char *cNumbersArr = new char[iDecimalNumber+1];
	*cNumbersArr = '\0';
	for(int i = 0;;i = 0)
	{
    for(bool bFlags = false;;)
    {
        cInputKay = _getch();
        system("cls");
        switch(cInputKay)
        {
        case 48:
        case 49:
        case 50:
        case 51:
        case 52:
        case 53:
        case 54:
        case 55:
        case 56:
        case 57:
            cNumbersArr[i++] = (char)cInputKay;
            cNumbersArr[i] = 0;
            if(i == iDecimalNumber)
            {
            bFlags = true;
            }
            break;
        case 13:
            	cNumbersArr[i] = 0;
            	bFlags = true;
            break;
        case 8:
            	if(i == 0)
            	{
            break;
            	}
            	cNumbersArr[--i] = 0;
            break;
        }
        std::cout<< cNumbersArr;
        if(bFlags)
        {
        break;
        }
    }
    system("cls");
    	iResult = atoi(cNumbersArr);
    	delete[] cNumbersArr;
    	return iResult;
    
	}
	system("cls");
	delete[] cNumbersArr;
	return -1;
}


/*
*Function output data structure
*/
void FunctionOutputDataStructure( student **ppStudentArrPointers, int i_SIZE)
{
	const int iScrenSizeX = 59;
	
	const int iNWords = 5;

	char pTextWords[iNWords][8] = {"Name", "Surname", "Age", "Rating", "AVR"};
	
	/*
	*Dimensions for the alignment of text in the center
	*/
	int iLeng[iNWords] = {0}; 
	for(register int i = 0; i < iNWords; ++i)
	{
    switch(i)
    {
    	case 0:
    	case 1:
    	case 3:
        iLeng[i] = (15 - (int)strlen(pTextWords[i])) / 2;
    	break;
    	case 2:
    	case 4:
        iLeng[i] = (6 - (int)strlen(pTextWords[i])) / 2;
    	break;
    }
	}
	
	/*
	*Frame Header
	*	#########################################
	*	#    #    #    #    #    #
	*	#    #    #    #    #    #	
	*	#    #    #    #    #    #
	*	#########################################
	*
	*
	*/    
	for(register int i = 0; i < 3; ++i)
	{
    switch(i)
    {
    	case 0:
        for(register int j = 0; j < iScrenSizeX; ++j)
        {
        	std::cout<<'#';
        }
    	break;
    	case 1:
        for(register int j = 0; j < iNWords; ++j)
        {
        	switch(j)
        	{
            case 0:
            	std::cout<<'#';
            case 1:
            case 2:
            case 3:
            case 4:
            	for(register int k = 0; k < iLeng[j]; ++k)
            	{
                std::cout<<' ';
            	}
            	std::cout<< pTextWords[j];
            	for(register int k = 0; k < iLeng[j]; ++k)
            	{
                std::cout<<' ';
            	}
            	std::cout<<'#';
            break;
        	}
        }
    	break;
    	case 2:
        for(register int j = 0; j < iScrenSizeX; ++j)
        {
        	std::cout<<'#';
        }    	
    	break;
    }
    std::cout<<std::endl;
	}
	
	/*
	*Formatted output of data and structure
	*/
	for(register int i = 0; i < i_SIZE; ++i)
	{
    std::cout<<std::setw(11)<<ppStudentArrPointers[i]->pcStudName;
    std::cout<<std::setw(17)<<ppStudentArrPointers[i]->pcStudSurName;
    std::cout<<std::setw(7)<<ppStudentArrPointers[i]->iStudAge;
    std::cout<<std::setw(7);
    for(register int j = 0; j < i_SIZE; )
    {
    	std::cout<<ppStudentArrPointers[i]->piStudRating[j];
    	++j;
    	if(j < i_SIZE)    	
    	std::cout<<',';
    }
    std::cout<<std::setw(7)<<ppStudentArrPointers[i]->iAveRating;
    std::cout<<std::endl;
	}
}

0

2

Пишите замечания.

0

3


Вы здесь » Форум языка CPP » Код домашнего задания » Программа для ввода данных в структуру.