#include <Windows.h>
#include <iostream>
#include <string.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
using namespace std;
typedef int ElemType;
typedef int Status;
extern HHOOK hHook;
extern LRESULT __stdcall CBTHookProc1(long nCode, WPARAM wParam, LPARAM lParam);
extern LRESULT __stdcall CBTHookProc2(long nCode, WPARAM wParam, LPARAM lParam);
extern LRESULT __stdcall CBTHookProc3(long nCode, WPARAM wParam, LPARAM lParam);
HHOOK hHook;
typedef struct//顺序栈的数据类型
{
ElemType* base;
ElemType* top;
int stacksize;
}Stack;
Status Init(Stack& S)//初始化顺序栈
{
S.base = new ElemType[MAXSIZE];
if (!S.base)
return ERROR;
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
Status Push(Stack& S, ElemType x)//入栈
{
if (S.top - S.base == S.stacksize)
{
cout << "\a>> 错误:栈空间已满,压入元素 " << x << " 失败!" << endl;
return ERROR;
}
*S.top++ = x;
cout << ">> 压入元素 " << x << " 成功。" << endl;
return OK;
}
Status Pop(Stack& S, ElemType& e)//出栈
{
if (S.top == S.base)
{
cout << "\a>> 错误:栈为空!" << endl;
return ERROR;
}
e = *--S.top;
cout << ">> 元素出栈成功,该元素为 " << e << "。" << endl;
return OK;
}
Status Show(Stack S)//打印栈
{
cout << "栈:top—";
while (S.base != S.top)
cout << *--S.top << "—";
cout << "bottom" << endl << endl;
return OK;
}
/*以上是栈的操作,以下是队列的操作。*/
struct node
{
int data;
node* next;
};
class linkqueue//队列结构体
{
private:
node* front, * rear;
public:
linkqueue();//初始化队列
~linkqueue();//销毁原队列并初始化新队列
Status destroyqueue();//清空队列
Status enqueue(int x);//元素入队
Status dequeue();//元素出队
Status showqueue();//打印队列
};
linkqueue::linkqueue()
{
node *s;
s = new node;
s->next = NULL;
front = rear = s;
}
linkqueue::~linkqueue()
{
front = rear = NULL;
node* s;
s = new node;
s->next = NULL;
front = rear = s;
}
Status linkqueue::enqueue(int x)
{
node* s = new node;
if (s == NULL)
{
cout << "\a>> 错误:队列已满,元素 " << x << " 入队失败!" << endl;
return ERROR;
}
s->data = x;
s->next = NULL;
rear->next = s;
rear = s;//将结点s插入到队尾
cout << ">> 元素 " << x << " 入队成功。" << endl;
return OK;
}
Status linkqueue::dequeue()
{
int x;
node* p = new node;
p = front->next;
if (p == NULL)
{
cout << "\a>> 错误:队列为空,无法出队!" << endl;
return ERROR;
}
x = p->data;
front->next = p->next;
cout << ">> 元素 " << x << " 出队成功。" << endl;
return OK;
}
Status linkqueue::showqueue()
{
if (front == rear)
{
cout << "队列:front—rear" << endl << endl;
return ERROR;
}
node* q = new node;
q = front;
cout << "队列:front—";
while (q ->next)
{
q = q->next;
cout << q->data << "—";
}
cout << "rear" << endl << endl;
return OK;
}
/*以上是队列部分,以下是 UI 界面的处理函数。*/
LRESULT __stdcall CBTHookProc1(long nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_ACTIVATE)
{
SetDlgItemText((HWND)wParam, IDYES, TEXT("演示栈"));
SetDlgItemText((HWND)wParam, IDNO, TEXT("演示队列"));
SetDlgItemText((HWND)wParam, IDCANCEL, TEXT("退出程序"));
UnhookWindowsHookEx(hHook);
}
return 0;
}
LRESULT __stdcall CBTHookProc2(long nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_ACTIVATE)
{
SetDlgItemText((HWND)wParam, IDYES, TEXT("入栈"));
SetDlgItemText((HWND)wParam, IDNO, TEXT("出栈"));
SetDlgItemText((HWND)wParam, IDCANCEL, TEXT("结束演示"));
UnhookWindowsHookEx(hHook);
}
return 0;
}
LRESULT __stdcall CBTHookProc3(long nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_ACTIVATE)
{
SetDlgItemText((HWND)wParam, IDYES, TEXT("入队"));
SetDlgItemText((HWND)wParam, IDNO, TEXT("出队"));
SetDlgItemText((HWND)wParam, IDCANCEL, TEXT("结束演示"));
UnhookWindowsHookEx(hHook);
}
return 0;
}
Status InputBox(char *_0)//文本输入框
{
int i, j;
char cmd[5 * MAXSIZE] = { 0 }, runvbs[10 * MAXSIZE] = "(if exist \"";
for (i = strlen(_0) - 1; i >= 0; i--)
if (_0[i] == '\\')
break;
for (j = 0; j <= i; j++)
cmd[j] = _0[j];
strcat_s(runvbs, cmd);
strcat_s(runvbs, "InputBox.txt\" (del /a /f /q \\\\?\\\"");
strcat_s(runvbs, cmd);
strcat_s(runvbs, "InputBox.txt\"))&start /realtime /wait \"\" \"");
strcat_s(cmd, "InputBox.vbs");
strcat_s(runvbs, cmd);
strcat_s(runvbs, "\"");
FILE* fp;
fopen_s(&fp, cmd, "r");
if (!fp)
{
fopen_s(&fp, cmd, "w+");
if (fp)
{
fwrite("x = 0\nOn Error Resume Next\nx = int(inputbox(\"请输入一个整型元素:\",\"InputBox\",\"0\"))\nif err.number <> 0 Then\nx = 0\nEnd if\nSet fso = CreateObject(\"Scripting.FileSystemObject\")\nSet f = fso.CreateTextFile(\"", 202, 1, fp);
fwrite(cmd, strlen(cmd) - 3, 1, fp);
fwrite("txt\",True)\nf.write x\nf.close", 29, 1, fp);
fclose(fp);
}
else
{
MessageBox(NULL, TEXT("生成脚本文件失败,请确保程序所在目录可写。"), TEXT("InputBox"), MB_OK | MB_ICONERROR | MB_TOPMOST);
return ERROR;
}
}
system(runvbs);
cmd[strlen(cmd) - 3] = 't';
cmd[strlen(cmd) - 2] = 'x';
cmd[strlen(cmd) - 1] = 't';
fopen_s(&fp, cmd, "r");
if (fp)
{
rewind(stdin);
if (fscanf_s(fp, "%d", &i) != 1)
{
MessageBox(NULL, TEXT("从脚本读取的数据类型不正确,请检查您的输入。"), TEXT("InputBox"), MB_OK | MB_ICONERROR | MB_TOPMOST);
rewind(stdin);
fclose(fp);
return ERROR;
}
rewind(stdin);
fclose(fp);
return i;
}
else
{
MessageBox(NULL, TEXT("从脚本读取数据失败,请确保程序所在目录可读。"), TEXT("InputBox"), MB_OK | MB_ICONERROR | MB_TOPMOST);
return ERROR;
}
}
/*以上是 UI 界面的处理函数,以下是主函数。*/
int main(int argc, char* argv[])
{
system("chcp 936&title 栈和队列演示程序&color e");
int errorcontrol, choice;
bool flag;
LPWSTR str = { 0 };
Stack A;
ElemType e;
linkqueue q;
while (true)
{
system("cls");
hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTHookProc1, NULL, GetCurrentThreadId());
if (hHook)
errorcontrol = MessageBox(NULL, TEXT("请选择一项以继续:"), TEXT("栈和队列演示程序"), MB_YESNOCANCEL | MB_ICONQUESTION | MB_TOPMOST);
else
{
MessageBox(NULL, TEXT("弹出菜单失败,请确保您的内存空间足够。"), TEXT("栈和队列演示程序"), MB_OK | MB_ICONERROR | MB_TOPMOST);
return -1;
}
switch (errorcontrol)
{
case 6:
if (Init(A) == ERROR)//初始化栈
{
MessageBox(NULL, TEXT("初始化栈失败!"), TEXT("栈和队列演示程序"), MB_OK | MB_ICONERROR | MB_TOPMOST);
continue;
}
cout << "初始化栈成功,开始程序演示。" << endl;
Show(A);
/*元素8,9,5,4依次入栈*/
cout << "元素8,9,5,4依次入栈:" << endl;
Push(A, 8);
Push(A, 9);
Push(A, 5);
Push(A, 4);
Show(A);
/*实现元素(8,9)的出栈*/
cout << "元素8,9出栈(会导致所有元素都出栈):" << endl;
Pop(A, e);
Pop(A, e);
Pop(A, e);
Pop(A, e);
Show(A);
flag = true;
while (flag)
{
hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTHookProc2, NULL, GetCurrentThreadId());
if (hHook)
choice = MessageBox(NULL, TEXT("请选择一项以继续:"), TEXT("栈演示程序"), MB_YESNOCANCEL | MB_ICONQUESTION | MB_TOPMOST);
else
{
choice = MessageBox(NULL, TEXT("弹出子菜单失败,请确保您的内存空间足够。"), TEXT("栈演示程序"), MB_OK | MB_ICONERROR | MB_TOPMOST);
break;
}
switch (choice)
{
case 6:
Push(A,
C/C++调用vbs实现InputBox
5星 · 超过95%的资源 需积分: 46 19 浏览量
2020-04-06
17:55:35
上传
评论
收藏 791KB ZIP 举报
星夜钢琴手
- 粉丝: 5
- 资源: 8
最新资源
- STM32-Lib-LIS3DSH-Accelerometer-main
- 软件测试面试题.pdf
- WINSOFT ComPort 6.0 for Delphi XE10.1-XE10.3 Cracked
- 数据库基础知识参考试题.doc
- 数据库存储引擎技术的优劣势分析.docx
- 基于GPT的AI文档分析、阅读和问答工具.txt
- 《机器人控制系统的设计与Matlab仿真 》仿真程序
- AI-免费物品无损放大工具AI在线免费放大图片工具.txt
- C++基于DPLL算法的SAT的蜂窝数独游戏求解程序,程序设计综合课程设计,包括SAT求解器板块、蜂窝数独转化成cnf公式板块
- 微信小程序恐龙快跑小程序源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页