任何人都有任何想法编写与C 旧手机中的消息作曲家以相同方式工作的程序

Anyone have any idea about writing a program that works in the same fashion as a message composer in the old mobiles in C++?

本文关键字:作曲家 消息 程序 工作 方式 手机 任何想 任何人      更新时间:2023-10-16

该程序必须接收整数输入。当使用输入2时,程序输出字母" a",如果按两次按下2,则字母" a"将从屏幕上消失,字符'b'将会出现,如果按下2个字母,则三次字母将消失,字母C C会出现。要输入" AA",您必须延迟两次输入" 2"。延迟必须像手机中通常一样小。

一个程序输入下面的数字并将其转换为字符串。28444333#该程序将输出ATIF,如果您必须打印" AAA",则输入将为212121#,其中1用作分隔器。#将用作输入终结者。

我已经在一段时间内使用开关语句制作了一个程序。那是我到目前为止所做的最大事情。

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

int main(){
    int x;
    while (1){
        cin >> x;
        switch(x){
    case 2:
        {
            system("CLS");
            cout << "An";
    break;
        }
    case 22:
        {
            system("CLS");
            cout << "Bn";
    break;
        }
    case 222:
        {
            system("CLS");
            cout << "Cn";
    break;
        }
        case 3:
        {
            system("CLS");
            cout << "Dn";
    break;
        }
    case 33:
        {
            system("CLS");
            cout << "En";
    break;
        }
    case 333:
        {
            system("CLS");
            cout << "Fn";
    break;
        }
        case 4:
        {
            system("CLS");
            cout << "Gn";
    break;
        }
    case 44:
        {
            system("CLS");
            cout << "Hn";
    break;
        }
    case 444:
        {
            system("CLS");
            cout << "In";
    break;
        }
        case 5:
        {
            system("CLS");
            cout << "Jn";
    break;
        }
    case 55:
        {
            system("CLS");
            cout << "Kn";
    break;
        }
    case 555:
        {
            system("CLS");
            cout << "Ln";
    break;
        }
        case 6:
        {
            system("CLS");
            cout << "Mn";
    break;
        }
    case 66:
        {
            system("CLS");
            cout << "Nn";
    break;
        }
    case 666:
        {
            system("CLS");
            cout << "On";
    break;
        }
    case 7:
        {
            system("CLS");
            cout << "Pn";
    break;
        }
    case 77:
        {
            system("CLS");
            cout << "Qn";
    break;
        }
    case 777:
        {
            system("CLS");
            cout << "Rn";
    break;
        }
    case 7777:
        {
            system("CLS");
            cout << "Sn";
    break;
        }
    case 8:
        {
            system("CLS");
            cout << "Tn";
    break;
        }
    case 88:
        {
            system("CLS");
            cout << "Un";
    break;
        }
    case 888:
        {
            system("CLS");
            cout << "Vn";
    break;
        }
    case 9:
        {
            system("CLS");
            cout << "Wn";
    break;
        }
    case 99:
        {
            system("CLS");
            cout << "Xn";
    break;
        }
    case 999:
        {
            system("CLS");
            cout << "Yn";
    break;
        }
    case 9999:
        {
            system("CLS");
            cout << "Zn";
        }
    break;
    case 0:
        {
            system("CLS");
            cout << " n";
        }
    break;
    case 1:
        {
            system("CLS");
            cout << ".n";
        }
    break;
    }
}
return 0;
}

输入流在击键之间没有延迟的概念。为了获得您描述中描述的内容,您必须依靠依赖操作系统的API,例如

https://msdn.microsoft.com/en-us/library/windows/desktop/gg153546(v=vs.85)9.aspx?f=255&ampx

和计时器。

您需要跟踪按下按键之间的按键和延迟,您可以使用::std::chrono来测量时间间隔。

#include <chrono>
#include <iostream>
...
using namespace std;
using namespace chrono;
int repeats_count{};
decltype(cin.get()) last_char{};
decltype(high_resolution_clock::now()) last_input_time{};
auto const max_delay{200ms};
for(;;)
{
    auto const c{cin.get()};
    auto const now{high_resolution_clock::now()};
    auto const char_repeats{c == last_char};
    auto const delay_is_small_enough{(now - last_input_time) < max_delay};
    //  TODO print key based on pressed char and repeats count...
    if(char_repeats && delay_is_small_enough)
    {
        ++repeats_count;
    }
    else
    {
        repeats_count = 0;
    }
    last_char = c;
    last_input_time = now;
}