Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // task_task.cpp : Defines the entry point for the console application.
- //
- #define _CRT_SECURE_NO_WARNINGS
- #include "stdafx.h"
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <locale.h>
- #include <ctype.h>
- const int this_year=2015,min_year=1965,fullname_len=40,work_len=30,num_workers=2;
- struct workers
- {
- char fullname[fullname_len];
- char work[work_len];
- int year;
- };
- int handle_word(char string[],int len_string)
- {
- if(len_string<2)
- return 1;
- int hyphen=0,hyphen_pos=-1;
- for(int j=0;j<len_string;j++)
- {
- if(string[j]=='-')
- {
- hyphen_pos=j;
- hyphen++;
- }
- else if(!(string[j]>='A'&&string[j]<='Z')&&!(string[j]>='a'&&string[j]<='z')&&!(string[j]>='А'&&string[j]<='Я')&&!(string[j]>='а'&&string[j]<='я')&&string[j]!='Ё'&&string[j]!='ё')
- return 1;
- if(!(hyphen_pos<0)&&(hyphen_pos<2||hyphen_pos>=len_string-2)||hyphen>1)
- return 1;
- }
- return 0;
- }
- int handle_fullname(char string[],int limit_len)
- {
- int len=strlen(string);
- if(len<2)
- return 1;
- int position=0;
- while(position<len&&string[position]==' ')
- position++;
- int copy_start_pos=position;
- position=len;
- while(position>copy_start_pos&&string[position]==' ')
- position--;
- int copy_end_pos=position;
- char temp[40];
- int len_temp=copy_end_pos-copy_start_pos;
- if(len_temp>=2)
- if(len_temp<=limit_len)
- for(int i=0;i<len_temp;i++)
- temp[i]=string[copy_start_pos+i];
- else for(int i=0;i<limit_len;i++)
- {
- temp[i]=string[copy_start_pos+i];
- len_temp=limit_len;
- }
- position=0;
- int i,handle_word_code=0;
- char word[40];
- copy_start_pos=0;
- copy_end_pos=15;
- int num_words=0;
- while(position<len_temp)
- {
- for(int j=copy_start_pos;j<copy_end_pos;j++)
- word[j]=NULL;
- copy_start_pos=position;
- i=0;
- while(position<len_temp&&temp[position]!=' ')
- {
- word[i]=temp[position];
- position++;
- i++;
- }
- copy_end_pos=position;
- if(handle_word(word,copy_end_pos-copy_start_pos)!=0)
- return 1;
- else num_words++;
- while(position<len_temp&&temp[position]==' ')
- position++;
- }
- if(num_words!=3)
- return 1;
- else return 0;
- }
- int handle_year(char number[],int min_year,int max_year)
- {
- int len=strlen(number);
- int i=0;
- while(i<len&&number[i]==' ')
- i++;
- int copy_start_pos=i;
- i=len;
- while(i>copy_start_pos&&number[i]==' ')
- i--;
- int copy_end_pos=i;
- char temp[4];
- if(copy_end_pos-copy_start_pos!=4)
- return 1;
- else
- for(int j=0;j<4;j++)
- if(number[copy_start_pos+j]<'0'||number[copy_start_pos+j]>'9')
- return 1;
- else temp[j]=number[copy_start_pos+j];
- int result=atoi(temp);
- if(result<min_year||result>max_year)
- return 1;
- else return result;
- }
- void add_worker(workers* worker)
- {
- printf("Введите ФИО:\n");
- getchar();
- gets(worker->fullname);
- fflush(stdin);
- int handle_fullname_code=handle_fullname(worker->fullname,fullname_len);
- while(handle_fullname_code!=0)
- {
- printf("ФИО введено неверно. Попробуйте ещё раз:\n");
- gets(worker->fullname);
- fflush(stdin);
- handle_fullname_code=handle_fullname(worker->fullname,fullname_len);
- }
- printf("Введите должность: ");
- scanf("%s",worker->work);
- fflush(stdin);
- int handle_word_code=handle_word(worker->work,strlen(worker->work));
- while(handle_word_code!=0)
- {
- printf("Должность введена неправильно. Попробуйте ещё раз: \n");
- scanf("%s",worker->work);
- fflush(stdin);
- handle_word_code=handle_word(worker->work,strlen(worker->work));
- }
- char year[10];
- printf("Введите год начала работы: ");
- scanf("%s",year);
- fflush(stdin);
- int handle_year_result=handle_year(year,min_year,this_year);
- while(handle_year_result==1)
- {
- printf("Год начала работы введен неправильно. Попробуйте ещё раз: ");
- scanf("%s",year);
- fflush(stdin);
- handle_year_result=handle_year(year,min_year,this_year);
- }
- worker->year=handle_year_result;
- }
- void print_worker(workers* worker)
- {
- printf("ФИО: %s\n",worker->fullname);
- printf("Должность: %s\n",worker->work);
- printf("Год начала работы: %d\n",worker->year);
- printf("\n");
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL,"rus");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- workers worker[5];
- workers* pntr=&worker[0];
- pntr=(workers*) malloc(sizeof(char)*74);
- printf("1-Ввести данные о %d работниках\n2-Распечатать данные о работниках (не более 5)\n3-Вывести на экран имена работников со стажем большим, чем введенный с клавиатуры(стаж не более 60 лет)\n0-Выход\n\n",num_workers);
- int code;
- printf("Введите код: ");
- scanf("%d",&code);
- fflush(stdin);
- do
- {
- switch(code){
- case 1:{
- for(int i=0;i<num_workers;i++)
- {
- printf("%d работник:\n",i+1);
- add_worker(&pntr[i]);
- printf("\n");
- }
- }
- break;
- case 2:{
- int i=0;
- while(i<num_workers)
- {
- if((&pntr[i])->fullname!=NULL)
- {
- printf("%d работник:\n",i+1);
- print_worker(&pntr[i]);
- }
- i++;
- }
- }
- break;
- case 3:{
- printf("Введите год стаж работника - не более 60 лет: ");
- int stage;
- scanf("%d",&stage);
- while(stage<0||stage>60)
- {
- printf("Стаж введен неверно. Попробуйте ещё раз: ");
- scanf("%d",&stage);
- }
- int desired_workers=0;
- int i=0;
- while(i<num_workers)
- {
- if((this_year-(&pntr[i])->year)>stage)
- {
- print_worker(&pntr[i]);
- desired_workers++;
- }
- i++;
- }
- if(desired_workers==0)
- printf("Работники со стажем более %d лет отсутствуют\n",stage);
- }
- break;
- case 0:{
- free(pntr);
- return 0;
- }
- break;
- default:
- printf("Введен неверный код\n");
- break;
- }//switch
- printf("Введите код: ");
- scanf("%d",&code);
- fflush(stdin);
- }while(code!=0);
- getchar();
- free(pntr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement