Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
База данных - Домашняя фонотека.doc
Скачиваний:
69
Добавлен:
02.05.2014
Размер:
2.19 Mб
Скачать

3. Листинг программы:

Главный файл

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<graphics.h>

#include<stdlib.h>

#include<dos.h>

#include"graph.cpp"

void mainmenu();

void newbase();

void openbase();

void open();

void view();

void edit();

void add();

void del(int i, int j);

void find(int i, int j);

void sort(int i, int j);

void replace(int);

char namefile[30];

struct COLLECTION{char name[20],T[3],nzv[20],aut[20], sin[20];}

list[500], tmp;

char name[30],key, k;

int i, j, t, p, g;

FILE *f;

int main()

{

clrscr();

graph();

textmode(3);

printf("Press any key...");getch();

mainmenu();

return 0;

}

void mainmenu()

{

for(;;)

{

clrscr();

printf("Welcome to Data Base \"Home CD Collection\"\n");

printf("1. Open Base\n2. New Base\n3. Exit\n");

key=getch();

if (key=='1') {clrscr(); openbase();}

if (key=='2') {clrscr(); newbase();}

if (key=='3') break;

}

}

void newbase()

{

printf("New file: ");

scanf("%s",namefile);

clrscr();

f=fopen(namefile,"w");

if (f==NULL)

{

printf("Error!");

getch();

}

else

{

printf("Ok! New file: %s",namefile);

fclose(f);

strcpy(name,namefile);

getch();

open();

}

}

void openbase()

{

printf("Enter name file: ");

scanf("%s",namefile);

clrscr();

f=fopen(namefile,"r");

if (f==NULL)

{

printf("Error!");

getch();

}

else

{

printf("Ok! Open: %s",namefile);

fclose(f);

strcpy(name,namefile);

getch();

open();

}

}

void open()

{

for(;;)

{

clrscr();

printf("Data Base: %s\n", name);

printf("1. View\n2. Edit\n3. Add\n4. Delete\n5. Find\n6. Sort\n7. Exit\n");

key=getch();

if (key=='1') {clrscr(); view();}

if (key=='2') {clrscr(); edit();}

if (key=='3') {clrscr(); add();}

if (key=='4') {clrscr(); del(i,j);}

if (key=='5') {clrscr(); find(i,j);}

if (key=='6') {clrscr(); sort(i,j);}

if (key=='7') break;

}

}

void view()

{

f=fopen(name,"r");

j=0;

printf("%2s %18s %5s %23s %12s %13s\n\n", "ID", "CD Name","Track", "Song","Author","Singer");

while(!feof(f))

{

fscanf(f,"%s %s %s %s %s",list[j].name,list[j].T,list[j].nzv,list[j].aut,list[j].sin);

j++;

}

j--;

p=0;

for (i=0; i<j; i++)

{

printf("%2u %18s %3s %25s %12s %13s\n",i+1,list[i].name,list[i].T,list[i].nzv,list[i].aut,list[i].sin);

p++;

if (p==10)

{

p=0; getch();

}

}

getch();

fclose(f);

}

void edit()

{

f=fopen(name,"r");

j=0;

while(!feof(f))

{

fscanf(f,"%s %s %s %s %s",list[j].name,list[j].T,list[j].nzv,list[j].aut,list[j].sin);

j++;

}

j--;

fclose(f);

printf("ID record: ");scanf("%u",&t);t--;

printf("OLD:\n");

printf("CD Name : \t\t\t%s\n", list[t].name);

printf("Track : \t%s\n", list[t].T);

printf("Song : \t%s\n", list[t].nzv);

printf("Author : \t%s\n", list[t].aut);

printf("Singer : \t\t%s\n", list[t].sin);

printf("\nNEW:\n");

printf("CD Name : \t\t\t");scanf("%s",list[t].name);

printf("Track : \t");scanf("%s",list[t].T);

printf("Song : \t");scanf("%s",list[t].nzv);

printf("Author : \t");scanf("%s",list[t].aut);

printf("Singer : \t\t");scanf("%s",list[t].sin);

f=fopen(name,"w");

for (i=0; i<j; i++)

fprintf(f,"%s %s %s %s %s\n\n",list[i].name,list[i].T,list[i].nzv,list[i].aut,list[i].sin);

fclose(f);

}

void add()

{

printf("CD Name: \t\t");scanf("%s",tmp.name);

printf("Track: \t");scanf("%s",tmp.T);

printf("Song: \t\t");scanf("%s",tmp.nzv);

printf("Author: \t\t");scanf("%s",tmp.aut);

printf("Singer: \t\t");scanf("%s",tmp.sin);

f=fopen(name,"a");

fprintf(f,"%s %s %s %s %s\n",tmp.name,tmp.T,tmp.nzv,tmp.aut,tmp.sin);

fclose(f);

}

void del(int i, int j)

{

f=fopen(name,"r");

j=0;

while(!feof(f))

{

fscanf(f,"%s %s %s %s %s",list[j].name,list[j].T,list[j].nzv,list[j].aut,list[j].sin);

j++;

}

j--;

fclose(f);

printf("ID record: ");scanf("%u",&t);t--;

f=fopen(name,"w");

for (i=0; i<t; i++)

fprintf(f,"%s %s %s %s %s\n",list[i].name,list[i].T,list[i].nzv,list[i].aut,list[i].sin);

for (i=(t+1); i<j; i++)

fprintf(f,"%s %s %s %s %s\n",list[i].name,list[i].T,list[i].nzv,list[i].aut,list[i].sin);

fclose(f);

printf("Delete... Ok!");

getch();

}

void find(int i, int j)

{

printf("Enter find record:\n");

printf("CD Name : \t\t");scanf("%s",tmp.name);

printf("Track : \t");scanf("%s",tmp.T);

printf("Song : \t\t");scanf("%s",tmp.nzv);

printf("Author : \t\t");scanf("%s",tmp.aut);

printf("Singer : \t\t");scanf("%s",tmp.sin);

f=fopen(name,"r");

j=0;

while(!feof(f))

{

fscanf(f,"%s %s %s %s %s",list[j].name,list[j].T,list[j].nzv,list[j].aut,list[j].sin);

j++;

}

j--;

fclose(f);

clrscr();

for (i=0; i<j; i++)

if ((strcmp(list[i].name,tmp.name)==0)||(strcmp(list[i].T,tmp.T)==0)||(strcmp(list[i].nzv,

tmp.nzv)==0)||(strcmp(list[i].aut,tmp.aut)==0)||(strcmp(list[i].sin,tmp.sin)==0))

printf("%3u %15s %15s %20s %9s %5s\n",i+1,list[i].name,list[i].T,list[i].nzv,list[i].aut,list[i].sin);

getch();

}

void sort(int i, int j)

{

printf("Vibereti pole:\n");

printf("1.CD Name 2.Track 3.Song 4.Author 5.Singer\n");

k=getch();

if ((k!='1')&&(k!='2')&&(k!='3')&&(k!='4')&&(k!='5'))

{

printf("Takogo polya net!");

getch();

}

else

{

f=fopen(name,"r");

j=0;

while(!feof(f))

{

fscanf(f,"%s %s %s %s %s",list[j].name,list[j].T,list[j].nzv,list[j].aut,list[j].sin);

j++;

}

j--;

fclose(f);

switch (k)

{

case '1': for(g=0; g<j; g++)

for(i=0; i<(j-1); i++)

if (strcmp(list[i].name,list[i+1].name)<0) replace(i);

break;

case '2': for(g=0; g<j; g++)

for(i=0; i<(j-1); i++)

if (strcmp(list[i].T,list[i+1].T)<0) replace(i);

break;

case '3': for(g=0; g<j; g++)

for(i=0; i<(j-1); i++)

if (strcmp(list[i].nzv,list[i+1].nzv)<0) replace(i);

break;

case '4': for(g=0; g<j; g++)

for(i=0; i<(j-1); i++)

if (strcmp(list[i].aut,list[i+1].aut)<0) replace(i);

break;

case '5': for(g=0; g<j; g++)

for(i=0; i<(j-1); i++)

if (strcmp(list[i].sin,list[i+1].sin)<0) replace(i);

break;

}

f=fopen(name,"w");

for (i=0; i<j; i++)

fprintf(f,"%s %s %s %s %s\n",list[i].name,list[i].T,list[i].nzv,list[i].aut,list[i].sin);

fclose(f);

}

}

void replace(int x)

{

strcpy(tmp.name,list[x].name);

strcpy(tmp.T,list[x].T);

strcpy(tmp.nzv,list[x].nzv);

strcpy(tmp.aut,list[x].aut);

strcpy(tmp.sin,list[x].sin);

strcpy(list[x].name,list[x+1].name);

strcpy(list[x].T,list[x+1].T);

strcpy(list[x].nzv,list[x+1].nzv);

strcpy(list[x].aut,list[x+1].aut);

strcpy(list[x].sin,list[x+1].sin);

strcpy(list[x+1].name,tmp.name);

strcpy(list[x+1].T,tmp.T);

strcpy(list[x+1].nzv,tmp.nzv);

strcpy(list[x+1].aut,tmp.aut);

strcpy(list[x+1].sin,tmp.sin);

}

Файл графики – “GRAPH.CPP”

#include <iostream.h>

#include <graphics.h>

#include <dos.h>

#include <conio.h>

#include <stdio.h>

void graph()

{

int x,y,a,b,c;

x=100; y=400; a=1; b=150; c=400;

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

/* return with error code */

}

setcolor(RED);

settextstyle(3,0,3);

outtextxy(50,50,"HOME CD COLLECTION");

outtextxy(10,460,"VIPOLNIL: Tselischev O.V.");

while (x<700 && y>-70 && a<2000 && b<700 && c>-70)

{

setbkcolor(0);

setfillstyle(SOLID_FILL,9);

fillellipse(x,y,40,40);

setfillstyle(SOLID_FILL,0);

fillellipse(x,y,8,8);

setfillstyle(SOLID_FILL,2);

sector(x,y,(88+a)%360,(92+a)%360,40,40);

sector(x,y,(223+a)%360,(227+a)%360,40,40);

sector(x,y,(313+a)%360,(317+a)%360,40,40);

sector(x,y,(268+a)%360,(272+a)%360,40,40);

sector(x,y,(43+a)%360,(47+a)%360,40,40);

sector(x,y,(133+a)%360,(137+a)%360,40,40);

setfillstyle(SOLID_FILL,5);

sector(x,y,(28+a)%360,(32+a)%360,40,40);

sector(x,y,(58+a)%360,(62+a)%360,40,40);

sector(x,y,(118+a)%360,(122+a)%360,40,40);

sector(x,y,(148+a)%360,(152+a)%360,40,40);

sector(x,y,(208+a)%360,(212+a)%360,40,40);

sector(x,y,(238+a)%360,(242+a)%360,40,40);

sector(x,y,(298+a)%360,(302+a)%360,40,40);

sector(x,y,(328+a)%360,(332+a)%360,40,40);

setfillstyle(SOLID_FILL,0);

fillellipse(x,y,8,8);

setcolor(RED);

settextstyle(3, 0, 10);

outtextxy(b,c,"<= MY CD");

delay (1);

setcolor(0);

fillellipse(x,y,40,40);

sector(x,y,(88+a)%360,(92+a)%360,40,40);

sector(x,y,(223+a)%360,(227+a)%360,40,40);

sector(x,y,(313+a)%360,(317+a)%360,40,40);

sector(x,y,(268+a)%360,(272+a)%360,40,40);

sector(x,y,(43+a)%360,(47+a)%360,40,40);

sector(x,y,(133+a)%360,(137+a)%360,40,40);

sector(x,y,(28+a)%360,(32+a)%360,40,40);

sector(x,y,(58+a)%360,(62+a)%360,40,40);

sector(x,y,(118+a)%360,(122+a)%360,40,40);

sector(x,y,(148+a)%360,(152+a)%360,40,40);

sector(x,y,(208+a)%360,(212+a)%360,40,40);

sector(x,y,(238+a)%360,(242+a)%360,40,40);

sector(x,y,(298+a)%360,(302+a)%360,40,40);

sector(x,y,(328+a)%360,(332+a)%360,40,40);

outtextxy(b,c,"<= MY CD");

x++;y--;a=a+2;b++; c--;

}

closegraph();

}

Соседние файлы в предмете Базы данных