Робочий зошит
Київ-2014 рік Класна робота: Умова: Текст програми: #include <conio.h> #include <stdio.h> #include <stdlib.h>
struct store{ int num; int asort[4]; };
struct list{ struct store* el; };
struct list lofst[20];
main (){
int n=0,i,j; int ap[4]; char c1; int ip[4];
for(i=1;i<5;i++) ap[i]=0;
do{ lofst[n].el=(struct store *)malloc(sizeof(struct store)); lofst[n].el->num=n+1; for (i=1;i<5;i++){ printf("\nHave store N%i good N%i?(y)\t",n+1,i); c1=getch(); if (c1=='y') lofst[n].el->asort[i]=1; printf("%c",c1); puts("\n"); } n++; printf("Want to add new?(y)\t"); c1=getch(); printf("%c",c1); puts("\n"); }while ((c1=='y')&&(n<20));
i=1; for (i;i<5;i++){ printf("\nWant to search for good N%i?(y)\t",i); c1=getch(); if (c1=='y') ip[i]=1; printf("%c",c1); puts("\n"); }
for (j=0;j<n;j++){ for (i=1;i<5;i++){ if (lofst[j].el->asort[i]==1) ap[i]=1; } }
int z=0;
for (j=1;j<5;j++){ if ((ap[j]!=1)&&(ip[j]==1)) { printf("Noone store has good N%i\n",j); z++;} }
if (z==0) printf("All goods are in stores\n");
return 0;
} Домашня робота: Умова: 2) Ввести дані в табл. 2. Відсортувати перелік студентів за належністю до кафедри. В межах кафедри розташувати інформацію за абеткою. Вивести результати сортування у табл. 2.
Текст програми: #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <windows.h> #include <string.h>
#define MAXLENN 20 #define MAXRECORDS 100
struct specialisation{ char code[MAXLENN]; char name[MAXLENN]; };
struct sname{ char firstn[MAXLENN]; char surn[MAXLENN]; char ftrn[MAXLENN]; };
struct student{ char faculty[MAXLENN]; char department[MAXLENN]; struct sname stname; struct specialisation spec; int curse; char mark; char group[MAXLENN]; };
struct list{ struct student* prev; struct student* next; };
void gotoxy(short x, short y) { COORD pos = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); }
void sort(struct list *[],int num); void output(struct student*,FILE *); void input(struct student*); int finput(FILE* fp); void strstruct(FILE *,char *,struct student *); void spacedel(char [],int*,char*);
struct list* listst[MAXRECORDS]; char * rec[100]; int an,n=0; char path[30]; char fuc[20]="Faculty",dep[20]="Department",sn[20]="Surname",fnam[20]="First name",fn[20]="Father's name"; char sc[20]="Spec.code",snm[20]="Spec. name",co[20]="Course",mar[20]="Mark",g[20]="Group";
main(){
FILE *fp;
system("mode con cols=80 lines=60"); //system("color f0");
gotoxy(10,0); printf("///If you want to rewrite or create new file press \"n\" \\\\\\\n "); gotoxy(8,1); printf("///If you want to open existing file and sort it press \"s\" \\\\\\\n "); gotoxy(13,2); printf("///If you want to work without files press \"f\" \\\\\\\n"); gotoxy(35,3);
char c; c=getch();
if (c=='s'){
do{ puts("\nInput path to file\n"); gets(path); if ((fp=fopen(path,"r"))==NULL){ printf("Can't open file %s\n",path); printf("Want to try again? (y)\n"); if (getch()!='y') return 1; else (an=1); } else an=0;
}while(an!=0);
n=finput(fp); int i;
for(i=0;i<n;i++){ listst[i]=(struct list *)malloc((sizeof(struct list))); listst[i]->next=(struct student *)malloc(sizeof(struct student)); strstruct(fp,rec[i],listst[i]->next); }
sort(listst,n-1);
fclose(fp);
int y;
for (y=0;(*(path+y)!='\0')&&(*(path+y)!='.');y++);
if (*(path+y)=='.') *(path+y)='\0'; strcat(path," sorted.txt");
fp=fopen(path,"w");
i=0;
fprintf(fp,"%10s|%15s|%15s|%15s|%15s|%15s|%15s|%7s|%10s|",fuc,dep,sn,fnam,fn,sc,snm,co,g); fprintf(fp,"%7s|\n",mar);
for (i;i<134;i++) fputs("-",fp);
while (i<n){ output(listst[i]->next,fp); puts("\n"); i++; }
fclose(fp);
return 0;
}
if ((c=='n')||(c=='f')) {
if (c=='n'){ do{ puts("\nInput path to file\n"); gets(path); if ((fp=fopen(path,"w"))==NULL){ printf("Can't open file %s\n",path); printf("Want to try again? (y)\n"); if (getch()!='y') return 1; else (an=1); } else an=0;
}while(an!=0);
puts("///File opened\\\\\\\n"); } else { puts("\n"); fp=fopen("RES.txt","w"); }
do{ printf("///Student N%i\\\\\\\n",n+1); listst[n]=(struct list *)malloc((sizeof(struct list))); (n>0)? (listst[n]->prev=listst[n-1]->next): (listst[n]->prev=NULL); listst[n]->next=(struct student *)malloc(sizeof(struct student)); input(listst[n]->next); puts("\n\tWant to add more\n\t///Press y\\\\\\"); n++; }while (getch()=='y');
listst[n]=(struct list *)malloc(sizeof(struct list)); listst[n]->next=NULL;
sort(listst,n-1); puts("\t\t///List was sorted\\\\\\\n");
n=0; int i=0;
fprintf(fp,"%10s|%15s|%15s|%15s|%15s|%15s|%15s|%7s|%10s|",fuc,dep,sn,fnam,fn,sc,snm,co,g); fprintf(fp,"%7s|\n",mar);
for (i;i<134;i++) fputs("-",fp);
//fputs("\n",fp);
while (listst[n]->next!=NULL){ output(listst[n]->next,fp); puts("\n"); n++; }
fclose(fp);
return 0; }
return 0; }
void input (struct student* st1){
gets(st1->faculty); printf("\nEnter faculty\t"); gets(st1->faculty); printf("\nEnter department\t"); gets(st1->department); printf("\nEnter surname\t"); gets(st1->stname.surn); printf("\nEnter first name\t"); gets(st1->stname.firstn); printf("\nEnter second name\t"); gets(st1->stname.ftrn); printf("\nEnter specialisation code\t"); gets(st1->spec.code); printf("\nEnter specialisation name\t"); gets(st1->spec.name); printf("\nEnter group\t"); gets(st1->group); printf("\nEnter course\t"); scanf("%d",&st1->curse); printf("\nEnter mark\t"); st1->mark=getch(); printf("%c",st1->mark); }
void sort(struct list* lt1[],int num){
int k,i,p,j; struct student* temp;
for(i = 0; i <= num - 1; ++i) { for(j = 0; j <= num - 1; ++j) if (strcmp(((lt1[j]->next)->department),((lt1[j+1])->next)->department)>0) { temp=lt1[j]->next; lt1[j]->next=lt1[j+1]->next; lt1[j+1]->next=temp; } }
for (k=0;k<=num-1;++k) { if (strcmp(((lt1[k]->next)->department),((lt1[k+1])->next)->department)==0){ for (p=k;(strcmp(((lt1[p]->next)->department),((lt1[k]->next)->department))==0)&&(p<num);++p);
for(i = k; i <= p - 1; ++i) { for(j = k; j <= p - 1; ++j) if (strcmp(((lt1[j]->next)->stname.surn),((lt1[j+1])->next)->stname.surn)>0) { temp=lt1[j]->next; lt1[j]->next=lt1[j+1]->next; lt1[j+1]->next=temp; } else if ((strcmp(((lt1[j]->next)->stname.surn),((lt1[j+1])->next)->stname.surn)==0)){ if (strcmp(((lt1[j]->next)->stname.firstn),((lt1[j+1])->next)->stname.firstn)>0) { temp=lt1[j]->next; lt1[j]->next=lt1[j+1]->next; lt1[j+1]->next=temp; } else if ((strcmp(((lt1[j]->next)->stname.firstn),((lt1[j+1])->next)->stname.firstn)==0)){ if (strcmp(((lt1[j]->next)->stname.ftrn),((lt1[j+1])->next)->stname.ftrn)>0) { temp=lt1[j]->next; lt1[j]->next=lt1[j+1]->next; lt1[j+1]->next=temp; } } }
}
k=p;
} } }
void output(struct student* st1,FILE * fp){
char c[2];
printf("\nFaculty %s\t",st1->faculty); printf("\nDepartment %s\t",st1->department); printf("\nSurname %s\t",st1->stname.surn); printf("\nFirst name %s\t",st1->stname.firstn); printf("\nSecond name %s\t",st1->stname.ftrn); printf("\nSpecialisation code %s\t",st1->spec.code); printf("\nSpecialisation name %s\t",st1->spec.name); printf("\nCourse %d\t",st1->curse); printf("\nGroup %s\t",st1->group); printf("\nMark %c\t",st1->mark);
fputs("\n",fp); fprintf(fp,"%10s|",st1->faculty); fprintf(fp,"%15s|",st1->department); fprintf(fp,"%15s|",st1->stname.surn); fprintf(fp,"%15s|",st1->stname.firstn); fprintf(fp,"%15s|",st1->stname.ftrn); fprintf(fp,"%15s|",st1->spec.code); fprintf(fp,"%15s|",st1->spec.name,fp); itoa(st1->curse,c,10); fprintf(fp,"%7s|",c); fprintf(fp,"%10s|",st1->group,fp); c[0]=st1->mark; c[1]='\0'; fprintf(fp,"%7s|",c);
}
int finput(FILE* fp){
char c; int i=0,o;
while (((c=getc(fp))!='\n')&&(c!=EOF)); while (((c=getc(fp))!='\n')&&(c!=EOF));
for (i=0;c!=EOF;i++){ rec[i]=(char *)malloc(150);
for(o=0;(((c=getc(fp))!=EOF)&&(c!='\n'));o++) *(rec[i]+o)=c;
*(rec[i]+o+1)='\0'; } free(rec[i]);
return i; }
void strstruct(FILE * fp,char * rec1,struct student * st1){
int* i=(int *)malloc(sizeof(int)); *i=0; char* buf=(char *)malloc(20);
spacedel(st1->faculty,i,rec1); (*i)++; spacedel(st1->department,i,rec1); (*i)++; spacedel(st1->stname.surn,i,rec1); (*i)++; spacedel(st1->stname.firstn,i,rec1); (*i)++; spacedel(st1->stname.ftrn,i,rec1); (*i)++; spacedel(st1->spec.code,i,rec1); (*i)++; spacedel(st1->spec.name,i,rec1); (*i)+=7; if (*(rec1+(*i))!=' ') st1->curse=*(rec1+(*i))-'0'; (*i)+=3; spacedel(st1->group,i,rec1); (*i)++; for (*i;*(rec1+(*i))==' ';(*i)++); if (*(rec1+(*i))!='|') st1->mark=*(rec1+(*i));
}
void spacedel(char ms[20],int* nr,char* str){
int j,k,q; char* buf1=(char *)malloc(30); for (j=0;j<20;j++) *(buf1+j)=' ';
for (*(nr),j=0;((*(buf1+j)=*(str+(*nr)))!='|')&&(j<15);(*nr)++,j++);
for (k=0;(*(buf1+k)==' ')&&(*(buf1+k)!='|');++k);
for (q=k;(*(buf1+q)!='|')&&(*(buf1+q)!='\n');q++) ms[q-k]=*(buf1+q);
ms[q-k]='\0';
}
Висновок:Під час виконання цієї лабораторної роботи я Отримав навички роботи із структурованими типами даних та познайомився з принципами динамічного розподілення пам'яті.
Робочий зошит для позааудиторної роботи з дисципліни "Основи охорони праці та охорона праці в галузі" Фармація
студентки Бондаренко Вікторії Сергіївни спеціальність «Лікувальна справа» курс 2 група 2Ф1 викладач Рожнова О.В.
2015 рік
Самостійна робота №6 Тема: «Розслідування професійних захворювань» Відповіді на основні завдання
|