
Архив2 / курсач docx80 / Kursach (2)
.docxФедеральное агентство железнодорожного транспорта
____________________
Московский государственный университет путей сообщения (МИИТ)
___________________
Институт управления и информационных технологий
___________________
Кафедра «Вычислительные системы и сети»
Отчет
Курсовая работа по дисциплине
«Программирование на языках высокого уровня»
Выполнил:
Студент группы УВВ-211
Кизилова Максима Юрьевича
Приняли: Никольская М.Н.
Нагинаев В.Н.
Москва - 2012 г.
Код программы
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<fstream.h>
//#include "f:\borland\program\choose2.h"
#include "choose2.h"
#define EDITABLE 1
int nameListSize=16;
char nameList[16][10] = {"Mac", "Everest", "Sputnik", "Spectrum", "Asus",
"Dell","Lenovo","MSI","Acer","HP","Samsung", "Sony",
"Toshiba", "Apple","IBM","Eniac"};
int countryListSize = 8;
char countryList[8][10] = {"Russia","China","Korea",
"USA","America","Germany",
"Austria","Japan"};
int procListSize = 7;
char procList[7][10] = {"Intel", "AMD", "AtMega", "PIC",
"Core i7Ђ", "Core i5", "Core i3"};
char head[4][10] = {"Name","CPU type","Country","Year"};
class error{
int code;
int location;
public:
operator int(){
return (code==-1)?0:1;
}
char* toString(){
char res[100];// = new char[100];
res[0]='\0';
switch (code)
{
case 0: strcat(res,"Empty field "); break;
case 1: strcat(res,"Illegal character in field "); break;
case 2: strcat(res,"Illegal input in field "); break;
}
switch (location)
{
case 0: strcat(res,"\"Name\"\0"); break;
case 1: strcat(res,"\"Proc type\"\0"); break;
case 2: strcat(res,"\"Country\"\0"); break;
case 3: strcat(res,"\"Year\"\0"); break;
}
return res;
}
error(int c, int l){
code=c;
location=l;
}
error(){
code=-1;
location=-1;
}
};
struct comp{
char name[15], proc[15], country[15], year[15];
};
int menu();
void create();
void show();
void obr();
int isEmpty(char* s);
int inList(char *s, char (*list)[10], int n);
error check(comp data);
int MakeForm(char (*header)[10], comp* date, int flag=0, int max = 0);
void parseComp(char *str, comp &data, char delimiter);
int main()
{
clrscr();
while (menu()==1);
printf("EXIT");
getch();
return 0;
}
int menu()
{
clrscr();
int key;
printf("#-----------MENU--------------#\n");
printf("| 1. Sozdanie ishodnogo faila |\n");
printf("| 2. Prosmotr faila |\n");
printf("| 3. Obrabotka |\n");
printf("| 4. Vihod |\n");
printf("#-----------------------------#\n");
key = choose(2,5,3,Y_MOVE);
clrscr();
switch (key)
{
case '1': create(); break;
case '2': show(); break;
case '3': obr(); break;
case '4': return 0;
default: printf("Nevernaya komanda");
}
getch();
return 1;
}
void create()
{
int i,ci = 0;
char fname[50];
FILE* f;
comp* compsBuffer = new comp[10];
comp* comps = new comp[50];
int key;
int iter=0;
int flag=1;
while (flag==1)
{
error e;
int error = 0;
for (i=0; i<10; i++)
{
compsBuffer[i].name[0]='\0';
compsBuffer[i].country[0]='\0';
compsBuffer[i].proc[0]='\0';
compsBuffer[i].year[0]='\0';
}
MakeForm(head, compsBuffer, EDITABLE);
clrscr();
for (i=0; i<10; i++)
{
e = check(compsBuffer[i]);
if (e)
{
printf("Error in line %d:", i+1);
puts(e.toString());
error = 1;
}
else
{
comps[ci] = compsBuffer[i];
ci++;
}
}
if (error == 1)
{
printf("\n STRINGS WITH ERRORS WILL BE SKIPPED");
getch();
clrscr();
}
printf("Hotite prodoljit' vvod?");
printf("\n1. Yes\n");
printf("2. No\n");
key = choose(2,3,1,Y_MOVE);
if (key!='1') flag=0;
iter++;
}
clrscr();
printf("Vvedite imya faila dlya zapisi informacii: ");
gets(fname);
f=fopen(fname,"wb");
for (i=0; i<ci; i++)
{
fwrite(&comps[i], sizeof(comps[i]), 1, f);
}
fclose(f);
return;
}
void show()
{
clrscr();
FILE *fi;
int ci=0;
char stringBuffer[50];
char fname[20];
comp comps[50];
printf("Vvedite nazvanie faila:");
gets(fname);
if ((fi=fopen(fname,"rt"))==0) {printf("File not found"); getch(); return;}
while (1)
{
fread(&comps[ci], sizeof(comps[ci]), 1, fi);
ci++;
if (feof(fi)) break;
}
ci--;
int i=0;
while (i*10<ci)
{
MakeForm(head, comps+i*10, 0, ci-i*10);
i++;
}
fclose(fi);
// gets(fname);
return;
}
void obr()
{
FILE *fi, *fo;
int ci=0, i, j, rk=0;
char stringBuffer[50];
char fname[20];
char criteria[20];
comp comps[50], res[50];
printf("Vvedite nazvanie faila:");
gets(fname);
if ((fi=fopen(fname,"rt"))==0) {printf("File not found"); getch(); return;}
while (1)
{
fread(&comps[ci], sizeof(comps[ci]), 1, fi);
ci++;
if (feof(fi)) break;
}
ci--;
clrscr();
printf("Vvedite imya faila dlya zapisi rezultata:");
gets(fname);
fo=fopen(fname,"wt");
clrscr();
printf("Kak vi hotite obrabotat' fail?\n");
printf(" 1. Nayti vse comp'uteri proizvedennie v strane\n");
printf(" 2. Nayti vse comp'uteri nabaze processora\n");
int key=choose(2,3,2,Y_MOVE);
clrscr();
switch(key)
{
case '1':
{
printf("Vvedite nazvanie strani:");
gets(criteria);
if (inList(criteria, countryList, countryListSize)==0)
{
printf("Necorrectniy vvod");
return;
}
for (i=0; i<ci; i++)
{
if (strcmp(criteria, comps[i].country) == 0)
{
res[rk] = comps[i];
rk++;
}
}
} break;
case '2':
{
printf("Vvedite nazvanie processora:");
gets(criteria);
if (inList(criteria, procList, procListSize)==0)
{
printf("Necorrectniy vvod");
return;
}
for (i=0; i<ci; i++)
{
if (strcmp(criteria, comps[i].proc) == 0)
{
res[rk] = comps[i];
rk++;
}
}
} break;
}
//sort//
comp t;
for (i = 0; i<rk-1; i++)
{
for (j = i; j<rk; j++)
{
if (strcmp(res[i].name, res[j].name)>0)
{
t = res[i];
res[i] = res[j];
res[j] = t;
}
}
}
for (i=0; i<rk; i++)
fwrite(&res[i], sizeof(res[i]), 1, fo);
//sort//
fclose(fi);
fclose(fo);
return;
}
int MakeForm(char (*header)[10], comp* data, int flag, int max)
{
int len[10][10];
int w,h,cx,cy,i,j;
char s[15];
int edit=0;
char empty[] = "";
char *t;
clrscr();
edit=0?1:flag==EDITABLE;
w=4;
h=10;
for (i=0; i<10; i++)
for (j=0; j<10; j++)
len[i][j]=0;
cx=0; cy=0;
for (i=0; i<w; i++)
{
printf("%c%10s%c",186,header[i],186);
}
printf("\n");
gotoxy(cx*12+1, cy*2+2);
for (i=0; i<h*2+1; i++)
{
for (j=0; j<w; j++)
{
if (i/2 < max)
t=data[i/2].name+j*15;
else
t=empty;
if ((i % 2)==1)
printf("%c%10s%c",186,t,186);
else
printf("%c%c%c%c%c%c%c%c%c%c%c%c",206,205,205,205,205,205,205,205,205,205,205,206);
}
printf("\n");
}
if (edit==1)
{
printf("\n Dlya vihoda iz formi vvoda ili najmite ESC");
_setcursortype(_SOLIDCURSOR);
gotoxy(cx*12+2, cy*2+3);
int key=72;
int f;
int miny=0, maxy=9, minx=0, maxx=3;
while (1)
{
key=getch();
if (key==27) break;
if (key==0 || key==8)
{
if (key!=8)
key=getch();
switch (key)
{
case 72: if (cy>miny) cy--; break;
case 80: if (cy<maxy) cy++; break;
case 75: if (cx>minx) cx--; break;
case 77: if (cx<maxx) cx++; break;
case 8: if (len[cy][cx]>0)
{
len[cy][cx]--;
gotoxy(cx*12+2+len[cy][cx], cy*2+3);
printf(" ");
}
break;
}
}
else
{
t=data[cy].name+15*cx;
if (len[cy][cx] < 10)
{
t[len[cy][cx]]=(char)key;
printf("%c",key);
len[cy][cx]++;
}
}
gotoxy(cx*12+2+len[cy][cx], cy*2+3);
}
for (i=0; i<10; i++)
{
data[i].name[len[i][0]]='\0';;
data[i].proc[len[i][1]]='\0';;
data[i].country[len[i][2]]='\0';;
data[i].year[len[i][3]]='\0';
}
}
else
{
printf("\n Dlya prodoljeniya prosmotra najmite Enter");
int key=0;
while (key!=13)
key=getch();
}
return -1;
}
int isEmpty(char* s)
{
for (int i=0; i<strlen(s); i++)
{
if (s[i]!=' ') return 0;
}
return 1;
}
error check(comp data)
{
int i;
if (isEmpty(data.name)) return error(0,0);
if (isEmpty(data.proc)) return error(0,1);
if (isEmpty(data.country)) return error(0,2);
if (isEmpty(data.year)) return error(0,3);
for (i=0; i<strlen(data.year); i++)
{
if (!isdigit(data.year[i])) return error(1,3);
if (data.name[i]=='~') return error(1,0);
if (data.country[i]=='~') return error(1,1);
if (data.proc[i]=='~') return error(1,2);
}
if (inList(data.name, nameList, nameListSize)==0) return error(2,0);
if (inList(data.proc, procList, procListSize)==0) return error(2,1);
if (inList(data.country, countryList, countryListSize)==0) return error(2,2);
if (data.year<="2012" && data.year>="1946") return error(2,3);
return error();
}
int inList(char *s, char (*list)[10], int n)
{
for (int i=0; i<n; i++)
if (strcmp(s, list[i])==0) return 1;
return 0;
}
void parseComp(char *str, comp &data, char delimiter)
{
int j=0;
char *t=data.name;
for (int i=0; i<strlen(str); i++)
{
if (str[i]==delimiter)
{
t[j]='\0';
j=0;
t+=15;
i++;
}
if (t>data.year) break;
t[j] = str[i];
j++;
}
}