Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ОргЭВМ(лабы 1-5) / 1-2лабаИльичева

.cpp
Скачиваний:
45
Добавлен:
28.12.2016
Размер:
5.67 Кб
Скачать
// курсорЦикл.cpp: определяет точку входа для консольного приложения.
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <cstdio>
#include <conio.h>

using namespace std;
 
enum color {
    Black = 0,
    Blue = 9,
    Green = 10,
    Yellow = 14,
    Cyan = 11,
    Magenta = 13,
    Red = 12,
    White = 7,
    Gray = 8
};
 
void setColor(color text, color background){
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hStdOut, (WORD)((background << 4) | text));
}
 
union type {
    unsigned long long ll;
    double d;
};
 
unsigned int printDouble1(double x){
    COORD coord;
    coord.X = 0;
    coord.Y = 4;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    cout << "                                                                        \n                                                                        ";
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    type data;
    unsigned int bit;
    data.d = x;
    setColor(Blue, Black);
    for (int i = 0; i < 64; i++)
    {
        if ((i > 0) && (i < 12)) setColor(Yellow, Black);
        if (i >= 12) setColor(Green, Black);
        bit = ((data.ll >> (64 - i - 1)) & 1);
        cout << bit;
    }
    setColor(White, Black);
    cout << endl;
    return bit;
}
 
 
unsigned int printShort(short input){
    COORD coord;
    coord.X = 0;
    coord.Y = 4;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    cout << "                                                                        \n                                                                        ";
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    unsigned int bit;
    setColor(Yellow, Black);
    for (int i = 0; i < 16; i++)
    {
        if ((i > 0) && (i < 16)) setColor(Yellow, Black);
        bit = ((input >> (16 - i - 1)) & 1);
        cout << bit;
    }
    setColor(White, Black);
    cout << endl;
    return bit;
}
 
short shift_right_short(unsigned short int input)
{
    int size = sizeof(short int)* 8;
    input = ((input >> 1) | (input << (size - 1)));
    printShort(input);
    setColor(Magenta, Black);
    cout << input << endl;
    setColor(White, Black);
    return input;
}
 
short shift_left_short(unsigned short int input)
{
 
    int size = sizeof(short int)* 8;
    input = ((input << 1) | (input >> (size - 1)));
    printShort(input);
    setColor(Magenta, Black);
    cout << input << endl;
    setColor(White, Black);
    return input;
}
 
double shift_right_double(double input)
{
    type data;
    data.d = input;
    int size = sizeof(double)* 8;
    data.ll = ((data.ll >> 1) | (data.ll << (size - 1)));
    printDouble1(data.d);
    setColor(Magenta, Black);
    cout << data.d << endl;
    setColor(White, Black);
    return data.d;
}
 
double shift_left_double(double input)
{
    type data;
    data.d = input;
    int size = sizeof(double)* 8;
    data.ll = ((data.ll << 1) | (data.ll >> (size - 1)));
    printDouble1(data.d);
    setColor(Magenta, Black);
    cout << data.d << endl;
    setColor(White, Black);
    return data.d;
}
 
void actionsDouble(double input){
    char c;
    double data = input;
    do {
        c = _getch();
        switch (c){
        case 75:
            data = shift_left_double(data);
            break;
        case 77:
            data = shift_right_double(data);
            break;
        }
    } while (c != 27);
}
 
void actionsShort(short input){
    char c;
    short data = input;
    do {
        c = _getch();
        switch (c){
        case 75:
            data = shift_left_short(data);
            break;
        case 77:
            data = shift_right_short(data);
            break;
        }
    } while (c != 27);
}
 
int main(){
    setlocale(LC_ALL, "rus");
    short int input;
    double x;
    int m, t, n, k;
    do
    {
        cout << "Выберите с каким типом вы хотите работать: 1 - double; 2 - short int " << endl;
        cin >> m;
        if (m == 1)
        {
            cout << "Введите данные:" << endl;
            cin >> x;
            if ((x > -9223372036854775808.0) & (x < 9223372036854775807.0))
            {
                printDouble1(x);
            }
            else {
                setColor(Red, Gray);
                cout << endl;
                cout << "Ошибка!!!" << endl;
                cout << endl;
                setColor(White, Black);
            }
            actionsDouble(x);
        }
        else {
            if (m == 2)
            {
                cout << "Введите данные:" << endl;
                cin >> input;
                if ((input > -32768) & (input < 32767))
                {
                    printShort(input);
                    actionsShort(input);
                }
                else {
                    setColor(Red, Gray);
                    cout << endl;
                    cout << "Ошибка!!!" << endl;
                    cout << endl;
                    setColor(White, Black);
                }
            }
            else {
                setColor(Red, Gray);
                cout << endl;
                cout << "Ошибка!!!" << endl;
                cout << endl;
                setColor(White, Black);
            }
        }
        cout << "Хотите продолжить? да - 1; нет - 0" << endl;
        cin >> t;
        system("cls");
    } while (t != 0);
}

Соседние файлы в папке ОргЭВМ(лабы 1-5)