Advertisement
alertsfail

task_task

May 25th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.32 KB | None | 0 0
  1. // task_task.cpp : Defines the entry point for the console application.
  2. //
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include "stdafx.h"
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <locale.h>
  11. #include <ctype.h>
  12.  
  13.  
  14. const int this_year=2015,min_year=1965,fullname_len=40,work_len=30,num_workers=2;
  15.  
  16. struct workers
  17.     {
  18.         char fullname[fullname_len];
  19.         char work[work_len];
  20.         int year;
  21.     };
  22.  
  23.  
  24. int handle_word(char string[],int len_string)
  25. {
  26.     if(len_string<2)
  27.         return 1;
  28.     int hyphen=0,hyphen_pos=-1;
  29.     for(int j=0;j<len_string;j++)
  30.     {
  31.         if(string[j]=='-')
  32.         {
  33.             hyphen_pos=j;
  34.             hyphen++;
  35.         }
  36.         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]!='ё')
  37.             return 1;
  38.         if(!(hyphen_pos<0)&&(hyphen_pos<2||hyphen_pos>=len_string-2)||hyphen>1)
  39.             return 1;
  40.     }
  41.     return 0;
  42. }
  43.  
  44.  
  45.  
  46. int handle_fullname(char string[],int limit_len)
  47. {
  48.     int len=strlen(string);
  49.     if(len<2)
  50.         return 1;
  51.     int position=0;
  52.     while(position<len&&string[position]==' ')
  53.         position++;
  54.     int copy_start_pos=position;
  55.     position=len;
  56.     while(position>copy_start_pos&&string[position]==' ')
  57.         position--;
  58.     int copy_end_pos=position;
  59.     char temp[40];
  60.     int len_temp=copy_end_pos-copy_start_pos;
  61.     if(len_temp>=2)
  62.         if(len_temp<=limit_len)
  63.             for(int i=0;i<len_temp;i++)
  64.                 temp[i]=string[copy_start_pos+i];
  65.         else for(int i=0;i<limit_len;i++)
  66.         {
  67.                 temp[i]=string[copy_start_pos+i];
  68.                 len_temp=limit_len;
  69.         }
  70.     position=0;
  71.     int i,handle_word_code=0;
  72.     char word[40];
  73.     copy_start_pos=0;
  74.     copy_end_pos=15;
  75.     int num_words=0;
  76.     while(position<len_temp)
  77.     {
  78.         for(int j=copy_start_pos;j<copy_end_pos;j++)
  79.             word[j]=NULL;
  80.         copy_start_pos=position;
  81.         i=0;
  82.         while(position<len_temp&&temp[position]!=' ')
  83.         {
  84.             word[i]=temp[position];
  85.             position++;
  86.             i++;
  87.         }
  88.         copy_end_pos=position;
  89.         if(handle_word(word,copy_end_pos-copy_start_pos)!=0)
  90.             return 1;
  91.         else num_words++;
  92.         while(position<len_temp&&temp[position]==' ')
  93.             position++;
  94.     }
  95.     if(num_words!=3)
  96.         return 1;
  97.     else return 0;
  98. }
  99.  
  100. int handle_year(char number[],int min_year,int max_year)
  101. {
  102.     int len=strlen(number);
  103.     int i=0;
  104.     while(i<len&&number[i]==' ')
  105.         i++;
  106.     int copy_start_pos=i;
  107.     i=len;
  108.     while(i>copy_start_pos&&number[i]==' ')
  109.         i--;
  110.     int copy_end_pos=i;
  111.     char temp[4];
  112.     if(copy_end_pos-copy_start_pos!=4)
  113.         return 1;
  114.     else
  115.         for(int j=0;j<4;j++)
  116.             if(number[copy_start_pos+j]<'0'||number[copy_start_pos+j]>'9')
  117.                 return 1;
  118.             else temp[j]=number[copy_start_pos+j];
  119.     int result=atoi(temp);
  120.     if(result<min_year||result>max_year)
  121.         return 1;
  122.     else return result;
  123. }
  124.  
  125. void add_worker(workers* worker)
  126. {
  127.         printf("Введите ФИО:\n");
  128.         getchar();
  129.         gets(worker->fullname);
  130.         fflush(stdin);
  131.         int handle_fullname_code=handle_fullname(worker->fullname,fullname_len);
  132.         while(handle_fullname_code!=0)
  133.         {
  134.             printf("ФИО введено неверно. Попробуйте ещё раз:\n");
  135.             gets(worker->fullname);
  136.             fflush(stdin);
  137.             handle_fullname_code=handle_fullname(worker->fullname,fullname_len);
  138.         }
  139.  
  140.         printf("Введите должность: ");
  141.         scanf("%s",worker->work);
  142.         fflush(stdin);
  143.         int handle_word_code=handle_word(worker->work,strlen(worker->work));
  144.         while(handle_word_code!=0)
  145.         {
  146.             printf("Должность введена неправильно. Попробуйте ещё раз: \n");
  147.             scanf("%s",worker->work);
  148.             fflush(stdin);
  149.             handle_word_code=handle_word(worker->work,strlen(worker->work));
  150.         }
  151.  
  152.         char year[10];
  153.         printf("Введите год начала работы: ");
  154.         scanf("%s",year);
  155.         fflush(stdin);
  156.         int handle_year_result=handle_year(year,min_year,this_year);
  157.         while(handle_year_result==1)
  158.         {
  159.             printf("Год начала работы введен неправильно. Попробуйте ещё раз: ");
  160.             scanf("%s",year);
  161.             fflush(stdin);
  162.             handle_year_result=handle_year(year,min_year,this_year);
  163.         }
  164.         worker->year=handle_year_result;
  165. }
  166.  
  167. void print_worker(workers* worker)
  168. {
  169.     printf("ФИО: %s\n",worker->fullname);
  170.     printf("Должность: %s\n",worker->work);
  171.     printf("Год начала работы: %d\n",worker->year);
  172.     printf("\n");
  173. }
  174.  
  175.  
  176. int _tmain(int argc, _TCHAR* argv[])
  177. {
  178.     setlocale(LC_ALL,"rus");
  179.     SetConsoleCP(1251);
  180.     SetConsoleOutputCP(1251);
  181.    
  182.     workers worker[5];
  183.     workers* pntr=&worker[0];
  184.     pntr=(workers*) malloc(sizeof(char)*74);
  185.     printf("1-Ввести данные о %d работниках\n2-Распечатать данные о работниках (не более 5)\n3-Вывести на экран имена работников со стажем большим, чем введенный с клавиатуры(стаж не более 60 лет)\n0-Выход\n\n",num_workers);
  186.     int code;
  187.     printf("Введите код: ");
  188.     scanf("%d",&code);
  189.     fflush(stdin);
  190.     do
  191.     {
  192.         switch(code){
  193.             case 1:{
  194.                 for(int i=0;i<num_workers;i++)
  195.                 {
  196.                     printf("%d работник:\n",i+1);
  197.                     add_worker(&pntr[i]);
  198.                     printf("\n");
  199.                 }
  200.             }
  201.             break;
  202.  
  203.             case 2:{
  204.                 int i=0;
  205.                 while(i<num_workers)
  206.                 {
  207.                     if((&pntr[i])->fullname!=NULL)
  208.                     {
  209.                         printf("%d работник:\n",i+1);
  210.                         print_worker(&pntr[i]);
  211.                     }
  212.                     i++;
  213.                 }
  214.             }
  215.             break;
  216.  
  217.             case 3:{
  218.                 printf("Введите год стаж работника - не более 60 лет: ");
  219.                 int stage;
  220.                 scanf("%d",&stage);
  221.                 while(stage<0||stage>60)
  222.                 {
  223.                     printf("Стаж введен неверно. Попробуйте ещё раз: ");
  224.                     scanf("%d",&stage);
  225.                 }
  226.                 int desired_workers=0;
  227.                 int i=0;
  228.                 while(i<num_workers)
  229.                 {
  230.                     if((this_year-(&pntr[i])->year)>stage)
  231.                     {
  232.                         print_worker(&pntr[i]);
  233.                         desired_workers++;
  234.                     }
  235.                     i++;
  236.                 }
  237.                 if(desired_workers==0)
  238.                     printf("Работники со стажем более %d лет отсутствуют\n",stage);
  239.             }
  240.             break;
  241.  
  242.             case 0:{
  243.                 free(pntr);
  244.                 return 0;
  245.             }
  246.             break;
  247.  
  248.             default:
  249.                 printf("Введен неверный код\n");
  250.             break;
  251.         }//switch
  252.         printf("Введите код: ");
  253.         scanf("%d",&code);
  254.         fflush(stdin);
  255.     }while(code!=0);
  256.  
  257.     getchar();
  258.     free(pntr);
  259.     return 0;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement