Вы находитесь на странице: 1из 10

1

: .TI-155
Cornescu R.
: Balan M.

2016
:
;
;
, ;

.

:
,
. , ,
; ,
,
, .
,
, ,
.
,
,
.
-
,
, .
,
,
,
,
,
.

:
) () - ,
, , , .
, , .
new. . main-,
.
b) () - ,
short . : ,
, / ,
, . ,
main, .
:
:
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;

struct Book
{
char *name;
char *author;
char *publication;
int pages;
int year;
void init(char *,char *, char *, int, int, int);
void setName(char *);
void setAuthor(char *);
void setPublication(char *);
void setPages(int);
void setYear(int);
void clear();
void print();
};
int compare(Book *,Book *);

void Book:: init(char *nam, char *aut, char *pub, int p, int y,int n)
{
if (n&&name)
delete[] name;
name = new char[strlen(nam) +1];
strcpy(name, nam);

if (n&&author)
delete[] author;
author = new char[strlen(aut) +1];
strcpy(author, aut);

if (n&&publication)
delete[] publication;
publication = new char[strlen(pub) +1];
strcpy(publication, pub);

pages = p;

year = y;
}

void Book:: setName(char *nam)


{
if (name)
delete[] name;
name = new char[strlen(nam) +1];
strcpy(name, nam);
}

void Book:: setAuthor(char * aut)


{
if (author)
delete[] author;
author = new char[strlen(aut) +1];
strcpy(author, aut);
}

void Book:: setPublication(char *pub)


{
if (publication)
delete[] publication;
publication = new char[strlen(pub) +1];
strcpy(publication, pub);

}
void Book:: setPages(int p)
{
pages = p;
}
void Book:: setYear(int y)
{
year = y;
}
void Book:: clear()
{
if (name)
delete[] name;
if (author)
delete[] author;
if (publication)
delete[] publication;
}

void Book:: print()


{
cout<<" :"<<name<<"\n";
cout<<" :"<<author<<"\n";
cout<<" :"<<publication<<"\n";
cout<<" :"<<pages<<"\n";
cout<<" :"<<year<<"\n";
}
int compare(Book *A,Book *B)
{
if (A->year < B->year)
return -1;
if (A->year = B->year)
return 0;
if (A->year > B->year)
return 1;
}

int main()
{
system("CLS");
int k;
Book *books;
char name[100];
char author[100];
char publications[100];
int pages;
int year;
int n1;
while(1)
{
cout<<" :\n";
cout<<"1 - :\n";
cout<<"2 - \n";
cout<<"3 - \n";
cout<<"4 - \n";
cout<<"5 - \n";
cout<<"6 - \n";
cout<<"7 - \n";
cin>>k;
switch(k)
{
case 1 :
{
int n;
cout<<" :\n";
cin>>n;
n1 = n;
books = new Book[n];
for(int i=0; i < n; i++)
{
cout<<" :\n";
cin>>name;
cout<<" :\n";
cin>>author;
cout<<" :\n";
cin>>publications;
cout<<" :\n";
cin>>pages;
cout<<" :\n";
cin>>year;
books[i].init(name, author, publications, pages, year, 0);
}
}continue;
case 2 :
{
for(int i=0; i<n1; i++)
{
cout<<i+1<<"- :"<<"\n\n";
books[i].print();
}
}
continue;
case 3 :
{
int t;
cout<<" :\n";
cout<<"1 - :\n";
cout<<"2 - \n";
cin>>t;
switch(t)
{
case 1 :
{
char nam[100];
cout<<" :\n";
cin>>nam;
for(int i=0;i < n1; i++)
{
if(strcmp(books[i].name,nam)==0)
{
cout<<i+1<<"- :"<<"\n\n";
books[i].print();
}
}
}
continue;
case 2 :
{
char aut[100];
cout<<" :\n";
cin>>aut;
for(int i=0;i < n1; i++)
if(strcmp(books[i].author,aut)==0)
{
cout<<i+1<<"- :"<<"\n\n";
books[i].print();
}
}
continue;
default:
cerr << " " << endl;
exit(EXIT_FAILURE);
}
continue;
case 4 :
{
int i,k,now;
cout<<" 1 - \n";
cin>>i;
cout<<" 2 - \n";
cin>>k;
now = compare(&books[i+1],&books[k+1]);
if (now == -1)
cout<<" \n";
else if(now == 0)
cout<<" ,
\n";
else if(now == 1)
cout<<" \n";
}
continue;
case 5 :
{
int opt, n;
cout<<" :\n";
cin>>n;
cout<<" :\n";
cout<<"1- :\n";
cout<<"2- :\n";
cout<<"3- :\n";
cout<<"4- :\n";
cout<<"5- :\n";
cin>>opt;
switch(opt)
{
case 1 :
{
cout<<" :\n";
cin>>name;
books[n-1].setName(name);
}
continue;
case 2 :
{
cout<<" :\n";
cin>>author;
books[n-1].setAuthor(author);
}
continue;
case 3 :
{
cout<<" :\n";
cin>>publications;
books[n-1].setPublication(publications);
}
continue;
case 4 :
{
cout<<" :\n";
cin>>pages;
books[n-1].setPages(pages);
}
continue;
case 5 :
{
cout<<" :\n";
cin>>year;
books[n-1].setYear(year);
}
continue;
default:
cerr << " " << endl;
exit(EXIT_FAILURE);
}
}
continue;
case 6 :
{
for(int i=0; i<n1; i++)
books[i].clear();
delete[] books;
cout<<" .\n";
}
continue;
case 7 :
{
cout << " ..." << endl;
exit(EXIT_SUCCESS);
}
continue;

default:
cerr << " " << endl;
exit(EXIT_FAILURE);
}
}
return 0;
}
}

:
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;

struct Vector{
short *x;
int n; //-
};

int getN (Vector *v){


return v->n;
}
void setN (Vector *v, int nr){
if (nr<1) v->n=1;
else v->n=nr;
}

short getX (Vector *v, int pos){


if ((pos>=0)&&(pos<getN(v)))
return v->x[pos];
cout << "Position Error" << endl;
return 0;
}

void setX (Vector *v, int pos, short value){


if ((pos>=0)&&(pos<getN(v)))
v->x[pos]=value;
else cout << "Position Error" << endl;
}

void initVector(Vector *v,int max){


setN(v,max);
short *ptr=new short[v->n];
v->x=ptr;
for (int i=0; i<v->n; i++)
v->x[i]=0;
}

void deleteVector (Vector *v){


delete[] v->x;
v->n=NULL;
}

short SumPar (Vector *v){


short sum=0;
for (int i=1; i<getN(v); i+=2)
sum+=getX(v,i);
return sum;
}

double Norma (Vector *v){


short sum=0;
for (int i=0; i<getN(v); i++)
sum+=getX(v,i)*getX(v,i);
return sqrtf(sum);
}

int main(){
Vector *tab;
int N,num;
short val;

cout << " : ";


cin >> N;
// if (N==0) return;
tab = new Vector[N];
for (int i=0; i<N; i++){
cout << " " << (i+1) << ": ";
cin >> num;
initVector(&tab[i], num);
cout << " : ";
for (int j=0; j<getN(&tab[i]); j++){
cin >> val;
setX(&tab[i],j,val);
}
}

//
for (int i=0; i<N; i++){
cout << "v" << (i+1) << ": ";
for (int j=0; j<getN(&tab[i]); j++)
cout << getX(&tab[i],j) << " ";
cout << endl;
}

//
for (int i=0; i<N; i++)
cout << "\n " << (i+1) << " =" << SumPar(&tab[i]);

//
cout << "\n ?" << endl;
int v1,v2;
double n1,n2;
cin >> v1 >> v2;
if (v1<=0||v1>N||v2<=0||v2>N)
cout << "\n. \n";
else {
n1=Norma(&tab[v1-1]);
n2=Norma(&tab[v2-1]);
if (n1>n2) {
cout << "v" << v1 << " > v" << v2 << endl;
cout << n1 << " > " << n2 << endl;
}
else if (n1==n2){
cout << "v" << v1 << " = v" << v2 << endl;
cout << n1 << " = " << n2 << endl;
}
else if{
cout << "v" << v1 << " < v" << v2 << endl;
cout << n1 << " < " << n2 << endl;
}
}

//
for (int i=0; i<N; i++)
deleteVector(&tab[i]);
getch();
return 0;
}

:

. .
,

.

Оценить