.


:




:

































 

 

 

 





- . printWorker(), Worker :

 

void printWorker(Worker worker) {

printf("%s\n", worker.name);

printf("%d\n", worker.age);

printf("%g\n", worker.salary);

}

 

, Worker, , workers:

 

printWorker(workers[1]);

 

- . Worker :

 

Worker getWellPaid(Worker w1, Worker w2) {

return w1.salary > w2.salary? w1: w2;

}

 

, if-else, :

 

Worker getWellPaid(Worker w1, Worker w2) {

if (w1.salary > w2.salary) {

return w1;

} else {

return w2;

}

}

 

getWellPaid() printWorker()

 

Worker director = {"", 30, 5000};

Worker manager = {"", 35, 3000};

printf("\n\n");

printWorker(getWellPaid(director, manager));

printf("\n \n");

 

:

 

 

 

. Point () x, y z, . , Point .

 

#include "stdafx.h"

#include <iostream>

using namespace std;

 

struct Point {

float x;

float y;

float z;

};

 

float sqr(float number) {

return number*number;

}

 

float getDistance(Point p, Point q) {

return sqrt(sqr(p.x-q.x) +

sqr(p.y-q.y) +

sqr(p.z-q.z));

}

 

int main() {

Point p = {1,2,3};

Point q = {2,3,4};

cout << getDistance(p,q)) << endl;

}

, , .

#include <iostream>

using namespace std;

 

struct Point {

float x;

float y;

float z;

};

 

float sqr(float number) {

return number*number;

}

 

float getDistance(Point p, Point q) {

return sqrt(sqr(p.x-q.x) +

sqr(p.y-q.y) +

sqr(p.z-q.z));

}

 

Point farPoint(Point p1, Point p2) {

Point zero = {0,0,0};

float d1 = getDistance(zero, p1);

float d2 = getDistance(zero, p2);

return d1>d2? p1: p2;

}

 

int main() {

Point p = {1,2,3};

Point q = {2,3,4};

Point far = farPoint(p,q);

cout << far.x << " " << far.y << endl;

}

1. Vector () x, y z, . , Vector .

 

#include <iostream>

using namespace std;

struct Vector { float x; float y; float z; };

float getScalarProduct(Vector v1, Vector v2) {

return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;

}

int main() {

Vector vect1 = {12.3, 45.3, 2.3};

Vector vect2 = {11.2, 35.2, 17.8};

cout << getScalarProduct(vect1, vect2) << endl;

return 0;

}

 

2. Point () x y, . Circle () center Point, , radius, . , , true, , false .

 

#include <iostream>

using namespace std;

struct Point { float x; float y; };

struct Circle { Point center; float radius; };

float getDistance(Point p1, Point p2) {

return sqrt((p1.x-p2.x)*(p1.x-p2.x) +

(p1.y-p2.y)*(p1.y-p2.y));

}

bool isInside(Point p, Circle c) {

float dist = getDistance(p, c.center);

return dist < c.radius;

}

int main() {

Circle circle;

circle.center.x = 12.3;

circle.center.y = 10.1;

circle.radius = 7.8;

Point point;

point.x = 15;

point.y = 11;

IsInside(point, circle)?

cout << "\n": cout << "\n";

return 0;

}

3. , 5 : , , , . .

 

#include <iostream>

using namespace std;

struct Book {

char name[100];

char author[100];

int nCopies;

int nPages;

float price;

};

Book books[] = {

{" ", "", 10000, 70, 300},

{"", "", 20000, 490, 600},

{" ", "", 40000,340,500},

{" ", "", 4000,800, 900},

{"", "", 3000, 400, 550}

};

int main() {

setlocale(LC_ALL, "Russian");

printf("%-20s %-12s %-7s %-5s %-5s\n",

"", "", "", ".", "");

printf("------------------------------------\n");

for (int i=0; i<5; i++) {

printf("%-20s %-12s %-7d %-5d %-5.2f\n",

books[i].name, books[i].author,

books[i].nCopies, books[i].nPages,

books[i].price);

}

printf("------------------------------------\n");

return 0;

}

 

4. : 1 , 2 , 3 , 4 , 0 . .

 

#include <iostream>

using namespace std;

struct Book {

char name[100];

char author[100];

int nCopies;

int nPages;

float price;

};

const int n=5;

Book books[n] = {

{" ", "", 10000, 70, 300},

{"", "", 20000, 490, 600},

{" ", "", 40000, 340, 500},

{" ", "", 4000, 800, 900},

{"", "", 3000, 400, 550}

};

void printBooks() {

printf("%-25s %-15s %-7s %-10s %-10s\n", "",

"", "", "", "");

for (int i=0; i<5; i++) {

printf("%-25s %-15s %-7d %-10d %-10.2f\n",

books[i].name, books[i].author,

books[i].nCopies, books[i].nPages,

books[i].price);

}

}

Define NAME 1

Define AUTHOR 2

Define N_COPIES 3

Define N_PAGES 4

Define PRICE 5

bool isGreater(Book b1, Book b2, int field) {

switch(field) {

case NAME: return strcmp(b1.name, b2.name) > 0;

case AUTHOR: return strcmp(b1.author, b2.author) > 0;

case N_COPIES: return b1.nCopies > b2.nCopies;

case N_PAGES: return b1.nPages > b2.nPages;

case PRICE: return b1.price > b2.price;

default: return true;

}

}

void sortBooks(int field) {

for (int i=0; i<n-1; i++) {

int max = i;

for (int j=i+1; j<n; j++) {

if (isGreater(books[max], books[j], field)) {

max = j;

}

}

Book temp = books[max];

books[max] = books[i];

books[i] = temp;

}

}

void intoFile() {

FILE* dataFile = fopen("books.txt", "w");

for (int i=0; i<5; i++) {

fprintf(dataFile, "%s\n%s\n%d\n%d\n%f\n",

books[i].name, books[i].author, books[i].nCopies,

books[i].nPages, books[i].price);

}

fclose(dataFile);

}

int main() {

setlocale(LC_ALL, "Russian");

while (true) {

system("cls");

printf("1 - \n");

printf("2 - \n");

printf("3 - \n");

printf("4 - \n");

printf("5 - \n");

printf("6 - \n");

printf("7 - \n");

printf("0 - \n");

int choice;

cin >> choice;

switch (choice) {

case 1: printBooks();break;

case 2: sortBooks(NAME);break;

case 3: sortBooks(AUTHOR);break;

case 4: sortBooks(N_COPIES);break;

case 5: sortBooks(N_PAGES);break;

case 6: sortBooks(PRICE);break;

case 7: intoFile();break;

case 0: exit(EXIT_SUCCESS);break;

default: printf("\n");

}

system("pause");

}

return 0;

}

 

  1. ?
  2. -?
  3. ?
  4. ?
  5. ?
  6. ?
  7. ?
  8. ?

 





:


: 2016-10-06; !; : 676 |


:

:

- , 20 40 . - .
==> ...

1474 - | 1439 -


© 2015-2024 lektsii.org - -

: 0.053 .