Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ЛАБЫ (6 ВАРИАНТ).docx
Скачиваний:
10
Добавлен:
26.09.2019
Размер:
28.73 Кб
Скачать

Void main() {

setlocale(LC_ALL, "Russian");

FILE* fp = fopen("file.txt", "r");

int a=0;

while (!feof(fp)) {

char ch = fgetc(fp);

if (isEngVowel(ch))

{

a++;

}

}

if (a==0) {printf ("В файле нет ни одной английской гласной буквы");}

else {printf ("В файле имеются английские гласные буквы");}

fclose(fp);

}

-------------------------------------------------------------------------

ЗАДАЧА 12

#include "stdafx.h"

#include <stdio.h>

#include <locale.h>

#include "string.h"

bool isEngVowel(char symbol) {

char engVows[] = "aeiouyAEIOUY";

for (int i=0; i<strlen(engVows); i++) {

if (symbol==engVows[i]) {

return true;

}

}

return false;

}

int _tmain(int argc, _TCHAR* argv[]) {

setlocale(LC_ALL, "Russian");

FILE* in = fopen("main.txt", "r");

FILE* out = fopen("result.txt", "w");

while (!feof(in)) {

char symbol = fgetc(in);

fputc(symbol, out);

printf ("%c",symbol);

if (isEngVowel(symbol)) {

fputc(symbol, out);

printf ("%c",symbol);

}

}

fclose(in);

fclose(out);

}

----------------------------------------------------------------------

ЗАДАЧА 17

#include "stdafx.h"

#include <stdio.h>

#include <locale.h>

#include "string.h"

Void main() {

setlocale(LC_ALL, "Russian");

int N;

printf("Введите номер позиции N:\n");

scanf("%d", &N);

FILE*myFile = fopen("main.txt", "r");

char symbol = fseek(myFile, N, SEEK_SET);

putchar (fgetc(myFile));

fclose(myFile);

}

-------------------------------------------------------------------

ЗАДАЧА 22

#include "stdafx.h"

#include <stdio.h>

#include <locale.h>

Void main() {

setlocale(LC_ALL, "Russian");

FILE* c = fopen("text.txt","r");

int a=0;

while (!feof(c)) {

char ch=fgetc(c);

if ('a' <= ch && ch <= 'z')

{

a++;

putchar(ch);

}

}

printf ("\nКоличество английских строчных букв в файле: %d\n", a);

fclose(c);

}

------------------------------------------------------------------

ЗАДАЧА 1

#include "stdafx.h"

#include <stdio.h>

int main() {

int N;

printf("Vvedite chislo N:\n");

scanf("%d", &N);

int x0;

printf("Vvedite parametr X0:\n");

scanf("%d", &x0);

int a;

printf("Vvedite parametr a:\n");

scanf("%d", &a);

int b;

printf("Vvedite parametr b:\n");

scanf("%d", &b);

int c;

printf("Vvedite parametr c:\n");

scanf("%d", &c);

FILE*myFile;

myFile=fopen("chisla.txt","w");

int xn=8;

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

int xn1=((a*x0+b)%c);

fprintf (myFile,"%d", xn1);

printf("%d", xn1);

x0=xn;

xn=xn1;

}

return 0;

fclose(myFile);

}

ЛАБОРАТОРНАЯ 10

ЗАДАЧА 8

#include "stdafx.h"

#include <stdio.h>

#include <locale.h>

#include "string.h"

#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 = 20.7;

circle.center.y = 11.2;

circle.radius = 21;

Point point;

point.x = 14;

point.y = 33;

isInside(point, circle) ?

cout << "True\n" : cout << "False\n";

return 0;

}

========================================================================================================

ЗАДАЧА 17

#include "stdafx.h"

#include <stdio.h>

#include <locale.h>

#include "string.h"

#include <iostream>

using namespace std;

struct Automobil {

char marka[15];

char model[15];

char stranaproizvoditel[17];

int nProbeg;

float price;

};

Automobil automobils[] = {

{"Honda", "Fit", "Япония", 169, 300000},

{"Toyota", "Vitz", "Япония", 490, 200000},

{"Nissan", "March", "Корея" ,340,500000},

{"Shkoda", "Octavia", "Чехия",800, 900000},

{"Opel", "Astra", "Япония", 400, 550000},

{"Audi", "A8", "Германия", 100, 2500000},

{"Volkswagen", "Touareq", "Германия", 119, 1200000},

{"Renault", "Laguna", "Франция" ,340,630000},

{"Volvo", "S60", "Швеция",400, 512000},

{"BMW", "X5", "Германия", 350, 1700000}

};

int main() {

setlocale(LC_ALL, "Russian");

printf("%-12s %-12s %-25s %-12s %-12s\n",

"Марка", "Модель", "Страна-производитель", "Пробег", "Цена");

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

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

printf("%-12s %-12s %-25s %-12d %-12.f\n",

automobils[i].marka, automobils[i].model,

automobils[i].stranaproizvoditel, automobils[i].nProbeg,

automobils[i].price);

}

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

return 0;

}

==================================================================================================================

ЗАДАЧА 1

#include "stdafx.h"

#include <stdio.h>

#include <locale.h>

#include "string.h"

#include <iostream>

using namespace std;

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

float getScalarProduct(Vector v1, Vector v2) {

return sqrt(pow(v1.x+v2.x,2) + pow(v1.y+v2.y,2) + pow(v1.z+v2.z,2));

}

int main() {

Vector vect1 = {1, 3, 2};

Vector vect2 = {8, 5, 4};

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

return 0;

}

=================================================================================================================

ЗАДАЧА 22

#include "stdafx.h"

#include <stdio.h>

#include <locale.h>

#include "string.h"

#include <iostream>

using namespace std;

#define MARKA 1

#define MODEL 2

#define STRANAPROIZVODITEL 3

#define NPROBEG 4

#define PRICE 5

struct Automobil {

char marka [15];

char model [15];

char stranaproizvoditel [17];

int nProbeg;

float price;

};

const int n=10;

Automobil automobils [n] = {

{"Honda", "Fit", "Япония", 169, 300000},

{"Toyota", "Vitz", "Япония", 490, 200000},

{"Nissan", "March", "Корея" ,340,500000},

{"Shkoda", "Octavia", "Чехия",800, 900000},

{"Opel", "Astra", "Япония", 400, 550000},

{"Audi", "A8", "Германия", 100, 2500000},

{"Volkswagen", "Touareq", "Германия", 119, 1200000},

{"Renault", "Laguna", "Франция" ,340,630000},

{"Volvo", "S60", "Швеция",400, 512000},

{"BMW", "X5", "Германия", 350, 1700000}

};

void printautomobils() {

printf("%-12s %-12s %-25s %-12s %-12s\n",

"Марка", "Модель", "Страна-производитель", "Пробег", "Цена");

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

printf("%-12s %-12s %-25s %-12d %-12.f\n",

automobils[i].marka, automobils[i].model,

automobils[i].stranaproizvoditel, automobils[i].nProbeg,

automobils[i].price);

}

}

bool isGreater(Automobil a1, Automobil a2, int field) {

switch(field) {

case MODEL : return strcmp(a1.model, a2.model) > 0;

case STRANAPROIZVODITEL : return strcmp(a1.stranaproizvoditel, a2.stranaproizvoditel) > 0;

default : return true;

}

}

void sortautomobils(int field) {

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

int max = i;

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

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

max = j;

}

}

Automobil temp = automobils[max];

automobils[max] = automobils[i];

automobils[i] = temp;

}

printf("%-12s %-12s %-25s %-12s %-12s\n", "Марка", "Модель", "Страна-производитель", "Пробег", "Цена");

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

printf("%-12s %-12s %-25s %-12d %-12.f\n",

automobils[i].marka, automobils[i].model,

automobils[i].stranaproizvoditel, automobils[i].nProbeg,

automobils[i].price);

}

}

void intoFile() {

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

fprintf(dataFile, "\n%-12s %-12s %-25s %-12s %-12s\n", "Марка", "Модель", "Страна-производитель", "Пробег", "Цена\n");

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

fprintf(dataFile, "%-12s %-12s %-25d %-12d %-12.f\n", automobils[i].marka, automobils[i].model, automobils[i].stranaproizvoditel, automobils[i].nProbeg, automobils[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("0 - Выход\n");

int choice;

cin >> choice;

switch (choice) {

case 1 : printautomobils();break;

case 2 : sortautomobils(MODEL);break;

case 3 : sortautomobils(STRANAPROIZVODITEL);break;

case 4 : intoFile();break;

case 0 : exit(EXIT_SUCCESS);break;

default : printf("Ошибка\n");

}

system("pause");

}

return 0;

}