gpt4 book ai didi

c++ - 我可以使用现有的项目代码在 C++ 中创建一个窗口吗

转载 作者:行者123 更新时间:2023-11-28 02:31:23 25 4
gpt4 key购买 nike

我想知道是否可以使用我现有的项目代码创建一个窗口。这是一个学校项目。然而,我已经完成了实际的编码部分,只是想让项目更漂亮,可以这么说。非常感谢您的所有支持。

这里是实际的代码,以备不时之需。文章有点长,请注意:) 再次感谢您

#define NOMINMAX
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <windows.h>
#include <Windows.h>
#include <chrono>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>


using namespace std;


int key[3][3];
double inverted[3][3];
int store[1][3] = { 0 };
int conv[666];


int random1()
{
unsigned long long int xRan;
srand(time(NULL));

xRan = rand() % 9999 + 1;

return xRan;
}
int random2()
{
unsigned long long int xRan;

xRan = rand() % 9999 + 1;

return xRan;
}
int random3()
{
int xRan;

xRan = rand() % 9999 + 1;

return xRan;
}
void clear_screen(char fill = ' ') {
COORD tl = { 0, 0 };
CONSOLE_SCREEN_BUFFER_INFO s;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(console, &s);
DWORD written, cells = s.dwSize.X * s.dwSize.Y;
FillConsoleOutputCharacter(console, fill, cells, tl, &written);
FillConsoleOutputAttribute(console, s.wAttributes, cells, tl, &written);
SetConsoleCursorPosition(console, tl);
}
int convert(char letter) {
int conv;
conv = (int)letter;

return conv;
}
void reverseMult(double inv[3][3], int decode[1][3])
{
store[0][0] = decode[0][0] * inv[0][0] + decode[0][1] * inv[1][0] + decode[0][2] * inv[2][0] + 0.5;
store[0][1] = decode[0][0] * inv[0][1] + decode[0][1] * inv[1][1] + decode[0][2] * inv[2][1] + 0.5;
store[0][2] = decode[0][0] * inv[0][2] + decode[0][1] * inv[1][2] + decode[0][2] * inv[2][2] + 0.5;
}
void matrixMult(int q, int w, int e, int a[3][3])
{

int A[1][3] = { q, w, e };
int B[3][3] = {
{ a[0][0], a[0][1], a[0][2] },
{ a[1][0], a[1][1], a[1][2] },
{ a[2][0], a[2][1], a[2][2] }
};


store[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0];
store[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1] + A[0][2] * B[2][1];
store[0][2] = A[0][0] * B[0][2] + A[0][1] * B[1][2] + A[0][2] * B[2][2];

//cout << store[0][0] << endl << store[0][1] << endl << store[0][2] << endl << endl;


}
char reverseConv(int x){

char conv;
conv = (char)x;

return conv;
}
void inverse(int key[3][3], double det){

int cofactor[3][3] = {
{ (key[1][1] * key[2][2] - key[1][2] * key[2][1]), -(key[1][0] * key[2][2] - key[1][2] * key[2][0]), (key[1][0] * key[2][1] - key[1][1] * key[2][0]) },
{ -(key[0][1] * key[2][2] - key[0][2] * key[2][1]), (key[0][0] * key[2][2] - key[0][2] * key[2][0]), -(key[0][0] * key[2][1] - key[0][1] * key[2][0]) },
{ (key[0][1] * key[1][2] - key[0][2] * key[1][1]), -(key[0][0] * key[1][2] - key[0][2] * key[1][0]), (key[0][0] * key[1][1] - key[0][1] * key[1][0]) }
};

for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
inverted[i][j] =det * cofactor[i][j];
}
}
}

int main()
{
while (1){
cout << "Would you like to encrypt or decrypt?(e/d)\n " << endl;
string ende;
cin >> ende;

clear_screen();

if (ende == "e")
{

cout << "Please enter a name for the message: " << endl << endl;
string file;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, file);

clear_screen();

file += ".txt";
ofstream encrypt;
encrypt.open(file);


string message;
cout << "Please enter the message you would like to encrypt: " << endl << endl;
//cin.ignore(numeric_limits<streamsize>::max(), '\n'); --- Not needed anymore, uncomment if you cannot input message
getline(cin, message);
if (message.length() % 3 != 0)
message += ' ';
if (message.length() % 3 != 0)
message += ' ';

clear_screen();

for (int i = 0; i < message.length(); ++i)
{
conv[i] = convert(message[i]);

}


int det = 0;

while (1){
key[0][0] = random1(); //1
key[0][1] = random2(); //2
key[0][2] = random3(); //3
key[1][0] = random1() * 13 / 7; //4
key[1][1] = random2() * 23 / 7; //5
key[1][2] = random3() * 33 / 7; //6
key[2][0] = random1() * 18 / 15; //7
key[2][1] = random2() * 18 / 12; //8
key[2][2] = random3() * 18 / 10; //9


det = key[0][0] * key[1][1] * key[2][2] + key[0][1] * key[1][2] * key[2][0] + key[0][2] * key[1][0] * key[2][1]
- key[0][2] * key[1][1] * key[2][0] - key[0][0] * key[1][2] * key[2][1] - key[0][1] * key[1][0] * key[2][2];

if (det != 0)
break;
}

encrypt << key[0][0] << ' ' << key[0][1] << ' ' << key[0][2] << ' '
<< key[1][0] << ' ' << key[1][1] << ' ' << key[1][2] << ' '
<< key[2][0] << ' ' << key[2][1] << ' ' << key[2][2] << endl << endl;

int a, b, c;
int count = 0;

for (int i = 0; i < (message.length)() / 3; ++i)
{
int counting = 0;
a = conv[count];
count++;
b = conv[count];
count++;
c = conv[count];
count++;


matrixMult(a, b, c, key);


encrypt << store[0][counting] << ' ';
counting++;
encrypt << store[0][counting] << ' ';
counting++;
encrypt << store[0][counting] << endl;

}




encrypt.close();

Sleep(750);
cout << "Your message has been encrypted." << endl;
Sleep(750);
cout << "Please check " << file << " for the encrypted message and key" << endl << endl;
Sleep(750);
}


if (ende == "d")
{


cout << "Please enter the name of the file you would like to decrypt: " << endl << endl;

string file;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, file);

clear_screen();

file += ".txt";
ifstream decrypt;
decrypt.open(file);

int key[3][3];

for (int i = 0; i < 3; ++i)
{
for (int k = 0; k < 3; ++k){
decrypt >> key[k][i];
}
}

int det = 0;
det = key[0][0] * key[1][1] * key[2][2] + key[0][1] * key[1][2] * key[2][0] + key[0][2] * key[1][0] * key[2][1] - key[0][2] * key[1][1] * key[2][0] - key[0][0] * key[1][2] * key[2][1] - key[0][1] * key[1][0] * key[2][2];

double detInv = 1;
detInv /= det;

//double inv;

inverse(key, detInv);

int out[1][3];
int count = 0;
while (!decrypt.eof()){
for (int i = 0; i < 3; ++i)
{
decrypt >> out[0][i];
}

reverseMult(inverted, out);

count++;

char a, b, c;

a = reverseConv(store[0][0]);
b = reverseConv(store[0][1]);
c = reverseConv(store[0][2]);

if (decrypt.eof())
break;

cout << a << b << c;

}
}
cout << endl << endl;
cout << "Would you like to continue?(y/n) ";
char again;
cin >> again;

if (again != 'y')
exit(0);
clear_screen();
}
return 0;
}

最佳答案

是的,您可以将项目转换为窗口应用程序。您有两个选择:

  • 使用 Windows ( native ) API
  • 使用图形框架

Windows API

Windows API 是创建窗口的直接方法。但是,代码很多,注入(inject)缺陷的机会很多。这是了解窗口系统如何工作的很好的学习经验。得到 Petzold 的书。

图形用户界面框架

有很多GUI frameworks在那里。这些 C++ 框架使用面向对象的编程简化了 GUI 和 Widget 的创建。那里有很多,所以在互联网上搜索“GUI Framework C++ review”。

不同的编程视角

在您当前的项目中,操作系统执行程序并按顺序执行语句。窗口系统基于 event driven编程。总之,您的 GUI 正在等待事件发生。

您的项目的一个简单示例是带有单个按钮的窗口。当用户单击按钮时,窗口系统发送消息到按钮 event handler .事件处理程序是一个将执行您的代码的函数。

关于c++ - 我可以使用现有的项目代码在 C++ 中创建一个窗口吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28881572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com