Lab4.OIT/lab13-14.cpp

327 lines
5.9 KiB
C++

#include <stdio.h>
#include <Windows.h>
#define NUM_ELEMENTS 10
int arr[NUM_ELEMENTS];
int n = 0;
void printElements();
void keyboardInput();
void oddsX10();
int findMin();
int countElements();
int findLastEven();
int findIndexMin();
int findIndexMax();
void chetnaminus();
int findmenshe4();
int variant4();
void deleteElement();
void insertElement();
void delMin();
void insert0();
void delnechetElement(int delIndex);
int Findnechet();
void main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int item;
do {
printf("\n");
printf("------------------------------\n");
printf("Ñîäåðæèìîå ìàññèâà:");
printElements();
printf("Âûáåðèòå íóæíóþ âàì îïåðàöèþ:\n");
printf("1: Ââåñòè ñ êëàâèàòóðû ìàññèâ\n");
printf("2: x10 äëÿ âñåõ íå÷åòíûõ ýëåìåíòîâ\n");
printf("3: Íàéòè ìèíèìàëüíûé ýëåìåíò\n");
printf("4: Ñêîëüêî ýëåìåíòîâ > 10\n");
printf("5: x2 äëÿ ïîñëåäíåãî ÷åòíîãî\n");
printf("6: Ñêîëüêî ÷åòíûõ ëåâåå ìèíèìàëüíîãî\n");
printf("7: õ10 äëÿ íå÷åòíûõ ïðàâåå ìèíèìàëüíîãî\n");
printf("8: ïåðåñòàâèòü ìåñòàìè min è max\n");
printf("9: âñå ÷åòíûå ýëåìåíòû ìàññèâà óìíîæèòü -1\n");
printf("10: âñå ýëåìåíòû ìàññèâà èìåþùèå çíà÷åíèÿ ìåíüøå 4 çàìåíèòü íà 4\n");
printf("11: âñå ýåëåìåíòû, ñòîÿùèå ïåðåä ÷åòíûìè, çàìåíèòü íà 9\n");
printf("12: óäàëèòü çàäàííûé ýëåìåíò\n");
printf("13: âñòàâèòü íîâûé ýëåìåíò â çàäàííîå ìåñòî\n");
printf("14: óäàëèòü ìèíèìàëüíûé ýëåìåíò\n");
printf("15: ïåðåä ìèíèìàëüíûì ýëåìåíòîì âñòàâèòü 0\n");
printf("16: óäàëèòü ïåðâûé èç íå÷¸òíûõ ýëåìåíòîâ\n");
printf("\n");
printf("0: Âûéòè èç ïðîãðàììû\n");
printf("Âûáðàííàÿ îïåðàöèÿ >>>>>> ");
scanf_s("%d", &item);
switch (item) {
case 1:
keyboardInput();
break;
case 2:
oddsX10();
break;
case 3:
{
int min = findMin();
printf("min = %d\n", min);
}
break;
case 4:
{
int cnt = countElements();
printf(" %d ýëåìåíòîâ > 10\n", cnt);
}
break;
case 5:
{
int index = findLastEven();
if (index >= 0) {
arr[index] *= 2;
}
}
break;
case 6:
{
int index = findIndexMin();
printf("Èíäåêñ ìèíèìàëüíîãî ýëåìåíòà = %d\n", index);
int cnt = 0;
for (int i = 0; i < index; i++) {
if (arr[i] % 2 == 0) {
cnt++;
}
}
printf("Ëåâåå ìèíèìàëüíîãî %d ÷åòíûõ ýëåìåíòîâ\n", cnt);
}
break;
case 7:
{
int index = findIndexMin();
printf("Èíäåêñ ìèíèìàëüíîãî ýëåìåíòà = %d\n", index);
int cnt = 0;
for (int i = index + 1; i < n; i++) {
if (arr[i] % 2 == 1) {
arr[i] *= 10;
}
}
}
break;
case 8:
{
int indexMin = findIndexMin();
printf("Èíäåêñ ìèíèìàëüíîãî ýëåìåíòà = %d\n", indexMin);
int indexMax = findIndexMax();
printf("Èíäåêñ ìàêñèìàëüíîãî ýëåìåíòà = %d\n", indexMax);
int tmp = arr[indexMin];
arr[indexMin] = arr[indexMax];
arr[indexMax] = tmp;
}
break;
case 9:
chetnaminus();
break;
case 10:
findmenshe4();
break;
case 11:
variant4();
break;
case 12:
deleteElement();
break;
case 13:
insertElement();
break;
case 14:
delMin();
break;
case 15:
insert0();
break;
case 16: {
int nechet = Findnechet();
delnechetElement(nechet);
}
}
} while (item != 0);
}
void keyboardInput() {
printf("n = ");
scanf_s("%d", &n);
printf("input %d values: ", n);
for (int i = 0; i < n; i++) {
scanf_s("%d", &arr[i]);
}
}
void oddsX10() {
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
arr[i] = arr[i] * 10;
}
}
}
void printElements() {
printf("< ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf(">\n");
}
int findMin() {
int min = arr[0];
for (int i = 1; i < n;i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int countElements() {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (arr[i] > 10) {
cnt++;
}
}
return cnt;
}
int findLastEven() {
for (int i = n - 1; i < n; i--) {
if (arr[i] % 2 == 0) {
return i;
}
}
return -1;
}
int findIndexMin() {
int min = arr[0];
int indexMin = 0;
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
indexMin = i;
}
}
return indexMin;
}
int findIndexMax() {
int max = arr[0];
int indexMax = 0;
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
indexMax = i;
}
}
return indexMax;
}
void chetnaminus() {
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
arr[i] = arr[i] * -1;
}
}
}
int findmenshe4() {
for (int i = 0; i < n; i++) {
if (arr[i] < 4 ) {
arr[i] = 4;
}
}
return 4;
}
int variant4() {
for (int i = 0; i < n; i++) {
if (arr[i] % 2!=0) {
arr[i] = 9;
}
}
return 4;
}
void deleteElement() {
int index;
printf("Èíäåêñ óäàëÿåìîãî ýëåìåíòà = ");
scanf_s("%d", &index);
for (int i = index; i < (n - 1); i++) {
arr[i] = arr[i + 1];
}
n--;
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}
void insertElement() {
int index;
printf("Èíäåêñ ïåðåä êîòîðûì íàäî âñòàâèòü íîâûé = ");
scanf_s("%d", &index);
int value;
printf("Çíà÷åíèå, êîòîðîå íóæíî âñòàâèòü = ");
scanf_s("%d", &value);
for (int i = n; i > index; i--) {
arr[i] = arr[i - 1];
}
n++;
arr[index] = value;
printf("%d", value);
}
void delMin() {
int min = findIndexMin();
for (int i = min; i < (n - 1); i++) {
arr[i] = arr[i + 1];
}
n--;
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}
void insert0() {
int min = findIndexMin();
for (int i = n; i > min; i--) {
arr[i] = arr[i - 1];
}
n++;
arr[min] = 0;
printf("%d", 0);
}
int Findnechet() {
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
return i;
}
}
return -1;
}
void delnechetElement(int delIndex) {
for (int i = delIndex; i < (n - 1); i++) {
arr[i] = arr[i + 1];
}
n--;
}