①图像加载`(注意必须新建窗口布局才可以读取)
IMAGE welcome;//欢迎界面
initgraph(1366, 700);//定义窗口大小
loadimage(&welcome, L"welcome.jpg");//导入欢迎界面(特别注意:双引号里面不能有空格,要跟图片命名完全一致,注意编码问题,可能导致loadimage两层定义的问题)
putimage(0, 0, &welcome);//显示欢迎界面
②音频加载(源码如下)
int music()
{
initgraph(640, 480);
MCIERROR mciError; // 存储播放音乐过程中的异常情况代码
// 1.检查资源是否存在,详细说明参见7.2和7.3节内容,相对路径法表示文件名
if (_waccess(_T("056-7.3周杰伦 - 青花瓷.mp3"), 0) < 0)
{
printf("文件不存在,原因可能是:1.资源不存在;2.路径错误;3.无法识别文件(夹)名\n");
_getch();
return 0; // 资源不存在时,退出程序,不继续往下
}
// 2.打开音乐 open ***
mciError = mciSendString(_T("open \"056-7.3周杰伦 - 青花瓷.mp3\" alias mymusic"), NULL, 0, NULL);
if (mciError) // 上述命令执行成功返回0,否则返回非0的故障码
{
printf("MCI 错误代码为:%d", mciError);// 查表,看错误代码对应的说明
_getch();
return 0; // 播放失败时,退出程序,不继续往下
}
// 第一个参数是多媒体命令字符串,不区分大小写。程序中,先要通过 open 命令打开 *.*,并用 alias 指定别名为“***”,这样在之后的代码中就可以方便的通过“***”这个别名访问该音乐了。当然,并不是必须要指定别名,每次通过文件名访问也是可以的。
// open 后面的音乐用绝对路径或相对路径都可以,如果文件名没有空格或特殊符号,文件名不必加双引号(注意转义\"表示引号"),建议最好加上双引号。
// 注意各单词和参数之间的空格
printf("按任意键开始播放-1 \r"); _getch(); // 暂停程序
// 3.播放音乐 play ***
mciError = mciSendString(_T("play mymusic"), NULL, 0, NULL);
// 其他播放命令,可将上一行语句注释掉,打开下列两行中的任一行,进行体验
// mciError=mciSendString(_T("play mymusic from 0"), NULL, 0, NULL); // 从头播放 from 0 ***
// mciError=mciSendString(_T("play mymusic repeat"), NULL, 0, NULL); // 循环播放repeat ***
if (mciError) // 上述命令执行成功返回0,否则返回非0的故障码
{
printf("MCI 错误代码为:%d", mciError);// 查表,看错误代码对应的说明
_getch();
return 0; // 播放失败时,退出程序,不继续往下
}
printf("按任意键停止播放 \r"); _getch(); // 暂停程序
// 4.停止播放音乐 stop ***
mciError = mciSendString(_T("stop mymusic"), NULL, 0, NULL);
if (mciError) // 上述命令执行成功返回0,否则返回非0的故障码
{
printf("MCI 错误代码为:%d", mciError);// 查表,看错误代码对应的说明
_getch();
return 0; // 播放失败时,退出程序,不继续往下
}
// 5.关闭音乐 close ***
mciError = mciSendString(_T("close mymusic"), NULL, 0, NULL);
if (mciError) // 上述命令执行成功返回0,否则返回非0的故障码
{
printf("MCI 错误代码为:%d", mciError);// 查表,看错误代码对应的说明
_getch();
return 0; // 播放失败时,退出程序,不继续往下
}
// 播放失败时,故障码和对应说明表存于附件中-MultimediaMCI错误返回码详解
// _T(“*”)是将*转换为unicode编码的一种常见方式,unicode编码内容参见1.2节
outtextxy(0, 0, _T("按任意键退出程序"));
_getch();
closegraph();
return 0;
}
void jiemian()
{
MOUSEMSG msg;//定义变量,保存鼠标消息
FlushMouseMsgBuffer();// 清空鼠标消息缓冲区,避免无效鼠标信息带入到正式判断中
while (true)
{
while (MouseHit())
{
msg = GetMouseMsg();
if (WM_LBUTTONDOWN == msg.uMsg)//判断鼠标信息;鼠标左键按下,WM_LBUTTONDOWN为获取按键坐标,msg.x即获取按键的x坐标
{
if (msg.x > 0 && msg.x < 120 && msg.y > 100 && msg.y < 200)//鼠标点击特定区域,即鼠标区域代定义错误导致,即左上角的代码与右下角的代码进行交互
//注意如果要跳出当前的图形必须重新定义新的界面的状况特点;
text();
if (msg.x > 120 && msg.x < 240 && msg.y > 100 && msg.y < 200)
music();
}
}
Sleep(10);
}
}
③计算器图形界面
#include "graphics.h"
#include "windows.h"
#include "conio.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "stdlib.h"
void calcul_memu()
{
setfillcolor(BLACK);//当前设备的填充颜色
solidrectangle(0, 100, 480, 600);//绘制矩形界面类状况
setcolor(BLUE);//即设置起始的坐标类状况特点,画直线
line(0, 100, 480, 100);
line(0, 200, 480, 200);
line(0, 300, 480, 300);
line(0, 400, 480, 400);
line(0, 500, 480, 500);
line(120, 100, 120, 600);
line(240, 100, 240, 600);
line(360, 100, 360, 600);
settextstyle(60, 60, _T("幼圆"));//设置文字的输出格式,高宽,字体
outtextxy(30, 120, _T("("));//即代表坐标x,y(即在该坐标出输出字符串)
outtextxy(150, 120, _T(")"));
outtextxy(390, 120, _T("+"));
outtextxy(260, 120, _T("<"));
outtextxy(30, 220, _T("7"));
outtextxy(150, 220, _T("8"));
outtextxy(270, 220, _T("9"));
outtextxy(390, 220, _T("-"));
outtextxy(30, 320, _T("4"));
outtextxy(150, 320, _T("5"));
outtextxy(270, 320, _T("6"));
outtextxy(390, 320, _T("*"));
outtextxy(30, 420, _T("1"));
outtextxy(150, 420, _T("2"));
outtextxy(270, 420, _T("3"));
outtextxy(390, 420, _T("/"));
outtextxy(30, 520, _T("."));
outtextxy(150, 520, _T("0"));
outtextxy(390, 520, _T("="));
outtextxy(270, 520, _T("C"));
}
后布局创造界面后调用该模块即可进行布局
④计算器的源码且详解(调用的其他大佬的源码进行的更改和思考作用)
#include "graphics.h"
#include "windows.h"
#include "conio.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define N 100
#define _T(x)__T(x)
const int max = 100;
typedef struct
{
char data[N][N];//即控制代值状况特点
int top;
}stack;
typedef struct figure
{
char x[N];
}figure;
void chuli(char ch[], struct figure a[]);//即创建结构体数组,且用于记录特殊字符运算
void clear_cal();
void init(stack *sta);
void inffixtosuffix(stack *sta, figure a[], char suffix[][max], int *length);
void suffixtoresult(stack *sta, char suffix[][max], int length);
void calcul_memu()
{
setfillcolor(BLACK);
solidrectangle(0, 100, 480, 600);
setcolor(DARKGRAY);
line(0, 100, 480, 100);
line(0, 200, 480, 200);
line(0, 300, 480, 300);
line(0, 400, 480, 400);
line(0, 500, 480, 500);
line(120, 100, 120, 600);
line(240, 100, 240, 600);
line(360, 100, 360, 600);
settextstyle(60, 60, _T("幼圆"));
outtextxy(30, 120, _T("("));
outtextxy(150, 120, _T(")"));
outtextxy(390, 120, _T("+"));
outtextxy(30, 220, _T("7"));
outtextxy(150, 220, _T("8"));
outtextxy(270, 220, _T("9"));
outtextxy(390, 220, _T("-"));
outtextxy(30, 320, _T("4"));
outtextxy(150, 320, _T("5"));
outtextxy(270, 320, _T("6"));
outtextxy(390, 320, _T("*"));
outtextxy(30, 420, _T("1"));
outtextxy(150, 420, _T("2"));
outtextxy(270, 420, _T("3"));
outtextxy(390, 420, _T("/"));
outtextxy(30, 520, _T("."));
outtextxy(150, 520, _T("0"));
outtextxy(390, 520, _T("="));
outtextxy(260, 120, _T("<"));
outtextxy(270, 520, _T("C"));
}//计算器界面;
int count = 0;
char str[N] = {
'0' }; //字符数组赋初始值;
figure st[N];
char suffix[N][N] = {
'0' };
stack sta;
void mouse_imfor()
{
int length = 0;
int i = -1;
int go = 0;
for (int a = 0; a < N; a++)
strcpy_s(st[i].x, "0");
MOUSEMSG m;
while (go == 0)
{
setcolor(DARKGRAY);
settextstyle(60, 20, _T("幼圆"));
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
if (m.x > 0 && m.x < 120 && m.y>100 && m.y < 200)
{
++i; count++;
str[i] = '(';
}
if (m.x > 120 && m.x < 240 && m.y>100 && m.y < 200)
{
++i; count++;
str[i] = ')';
}
if (m.x > 240 && m.x < 360 && m.y>100 && m.y < 200)
{
i--;
count--;
outtextxy(460 - i * 20, 20,_T(str));
outtextxy(460 - (i + 1) * 20, 20, _T(" ")); //按'<'键清除后一个,往后退(即控制输出后退界面的状况特点)
}
if (m.x > 360 && m.x < 480 && m.y>100 && m.y < 200)
{
++i; count++;
str[i] = '+';
}
if (m.x > 0 && m.x < 120 && m.y>200 && m.y < 300)
{
++i; count++;
str[i] = '7';
}
if (m.x > 120 && m.x < 240 && m.y>200 && m.y < 300)
{
++i; count++;
str[i] = '8';
}
if (m.x > 240 && m.x < 360 && m.y>200 && m.y < 300)
{
++i; count++;
str[i] = '9';
}
if (m.x > 360 && m.x < 480 && m.y>200 && m.y < 300)
{
++i; count++;
str[i] = '-';
}
if (m.x > 0 && m.x < 120 && m.y>300 && m.y < 400)
{
++i; count++;
str[i] = '4';
}
if (m.x > 120 && m.x < 240 && m.y>300 && m.y < 400)
{
++i; count++;
str[i] = '5';
}
if (m.x > 240 && m.x < 360 && m.y>300 && m.y < 400)
{
++i; count++;
str[i] = '6';
}
if (m.x > 360 && m.x < 480 && m.y>300 && m.y < 400)
{
++i; count++;
str[i] = '*';
}
if (m.x > 0 && m.x < 120 && m.y>400 && m.y < 500)
{
++i; count++;
str[i] = '1';
}
if (m.x > 120 && m.x < 240 && m.y>400 && m.y < 500)
{
++i; count++;
str[i] = '2';
}
if (m.x > 240 && m.x < 360 && m.y>400 && m.y < 500)
{
++i; count++;
str[i] = '3';
}
if (m.x > 360 && m.x < 480 && m.y>400 && m.y < 500)
{
++i; count++;
str[i] = '/';
}
if (m.x > 0 && m.x < 120 && m.y>500 && m.y < 600)
{
++i; count++;
str[i] = '.';
}
if (m.x > 120 && m.x < 240 && m.y>500 && m.y < 600)
{
++i; count++;
str[i] = '0';
}
outtextxy(460 - i * 20, 20, _T(str));//输出后的坐标位于·状况特点(利用i字联合控制)
if (m.x > 240 && m.x < 360 && m.y>500 && m.y < 600)
{
++i; count++;
str[i] = 'C';
str[i + 1] = '\0';
}
if (m.x > 360 && m.x < 480 && m.y>500 && m.y < 600)
{
++i; count++;
str[i] = '=';
str[i + 1] = '\0';
}
Sleep(100);
break;
case WM_RBUTTONUP:
go = 1;
break;
}
if (str[i] == 'C' || str[i] == '=')
break;
}
if (str[i] == 'C')
{
setbkcolor(BLACK);
cleardevice();
calcul_memu();
mouse_imfor();
}//按'C'键清空界面;
else if (str[i] == '=')//按=号调用函数实现功能的作用
{
chuli(str, st);
init(&sta);
inffixtosuffix(&sta, st, suffix, &length);
setbkcolor(BLACK);
cleardevice();
calcul_memu();
init(&sta);
suffixtoresult(&sta, suffix, length);
go = 0;
while (go == 0)
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
if (m.x > 240 && m.x < 360 && m.y>500 && m.y < 600)
{
str[i] = 'C';
}
str[i + 1] = '\0';
break;
case WM_RBUTTONUP:
go = 1;
break;
}
if (str[i] == 'C')
break;
}
if (str[i] == 'C')
{
setbkcolor(BLACK);
cleardevice();
calcul_memu();
mouse_imfor();
}
}
}//处理鼠标的消息;
void clear_cal()
{
setbkcolor(BLACK);
calcul_memu();
}//初始化界面;
//功能模块
int q; //定义一个全局变量,用来计数结构体数组的数量;
void chuli(char ch[], struct figure a[])
{
q = 0;
int m = 0, j;
char asd[15] = {
'0' };//记录代分解符号类状况特点
while (m < count)
{
j = 0;
if (ch[m] == '=')
break;
if (ch[m] == '(' || ch[m] == ')')
{
asd[j] = ch[m];
asd[j + 1] = '\0';
m++;
strcpy_s(a[q].x, asd);
q++;
continue;
}
if (ch[m] == '+' || ch[m] == '-')
{
if (ch[m - 1] == '+' || ch[m - 1] == '-' || ch[m - 1] == '*' || ch[m - 1] == '/' || m == 0 || ch[m] == '(')
{
asd[j] = ch[m];
m++;
j++;
while (ch[m] != '+'&&ch[m] != '-'&&ch[m] != '*'&&ch[m] != '/'&&ch[m] != ')')
{
asd[j] = ch[m];
j++;
m++;
}
asd[j] = '\0';
strcpy_s(a[q].x, asd);
q++;
}
else
{
asd[j] = ch[m];
asd[j + 1] = '\0';
m++;
strcpy_s(a[q].x, asd);
q++;
}
continue;
}
if (ch[m] == '*' || ch[m] == '/')
{
asd[j] = ch[m];
asd[j + 1] = '\0';
m++;
strcpy_s(a[q].x, asd);
q++;
continue;
}
if (ch[m] != '+'&&ch[m] != '-'&&ch[m] != '*'&&ch[m] != '/'&&ch[m] != '('&&ch[m] != ')')
{
while (ch[m] != '+'&&ch[m] != '-'&&ch[m] != '*'&&ch[m] != '/'&&ch[m] != '('&&ch[m] != ')'&&ch[m] != '=')
{
asd[j] = ch[m];
j++;
m++;
}
asd[j] = '\0';
strcpy_s(a[q].x, asd);
q++;
continue;
}
}
}//将字符串分割出来;
void init(stack *sta)
{
int i;
for (i = 0; i < max; i++)
strcpy_s(sta->data[i], "0");//即用"0"代表0即用于初始化的作用,即利用结构体的调用方法(即改变sta->data的数据)(即栈即为一种算法而已)即利用指针必须用->的方法代
sta->top = -1;
}//初始化栈;
void inffixtosuffix(stack *sta, struct figure *a, char suffix[][N], int *length)//即均用一个结构体数据进行代写分析
{
int i = 0;
int j = 0;
for (i = 0; i < q;)
{
if (strcmp(a[i].x, "(") == 0)
{
sta->top++;
strcpy_s(sta->data[sta->top], a[i].x);
i++;
continue;
}
else if (strcmp(a[i].x, ")") == 0)
{
while (strcmp(sta->data[sta->top], "(") != 0 && sta->top != -1)
{
strcpy_s(suffix[j], sta->data[sta->top]);
sta->top--;
j++;
}
strcpy_s(sta->data[sta->top], "0");
i++;
sta->top--;
continue;
}
else if (strcmp(a[i].x, "=") == 0)
{
i++;
continue;
}
else if (strcmp(a[i].x, "+") == 0 || strcmp(a[i].x, "-") == 0)
{
while (strcmp(sta->data[sta->top], "(") != 0 && sta->top != -1)
{
strcpy_s(suffix[j], sta->data[sta->top]);
sta->top--;
j++;
}
sta->top++;
strcpy_s(sta->data[sta->top], a[i].x);
i++;
continue;
}
else if (strcmp(a[i].x, "*") == 0 || strcmp(a[i].x, "/") == 0)
{
while (strcmp(sta->data[sta->top], "(") != 0 && sta->top != -1 && strcmp(sta->data[sta->top], "+") != 0 && strcmp(sta->data[sta->top], "-") != 0)
{
strcpy_s(suffix[j], sta->data[sta->top]);
sta->top--;
j++;
}
sta->top++;
strcpy_s(sta->data[sta->top], a[i].x);
i++;
continue;
}
else
{
strcpy_s(suffix[j], a[i].x);
j++;
i++;
continue;
}
}
while (sta->top != -1)
{
strcpy_s(suffix[j], sta->data[sta->top]);
sta->top--;
j++;
}
*length = j;
printf("\nlength=%d\n\n", *length);//即利用strcpoy_s++++++++++++++++++++++++++++++++++++++++(代进行分析代存储符号)
}//中缀表达式变为后缀表达式,//即把表达式符号弄了出来
void suffixtoresult(stack *sta, char suffix[][N], int length)
{
int i = 0;
double result = 0;
while (i < length)
{
if (strcmp(suffix[i], "+") == 0)//即分加减的状况代记录分析将值代出来
{
result = atof(sta->data[sta->top - 1]) + atof(sta->data[sta->top]);
sta->top--;
sprintf_s(sta->data[sta->top], "%.2lf", result);//存储数据的状况特点
i++;
continue;
}
else if (strcmp(suffix[i], "-") == 0)
{
result = atof(sta->data[sta->top - 1]) - atof(sta->data[sta->top]);//将值转换换为浮点类型
sta->top--;
sprintf_s(sta->data[sta->top], "%.2lf", result);
i++;
continue;
}
else if (strcmp(suffix[i], "*") == 0)
{
result = atof(sta->data[sta->top - 1])*atof(sta->data[sta->top]);
sta->top--;
sprintf_s(sta->data[sta->top], "%.2lf", result);//即将结果赋值给那个字符前一个状况特点
i++;
continue;
}
else if (strcmp(suffix[i], "/") == 0)
{
result = atof(sta->data[sta->top - 1]) / atof(sta->data[sta->top]);
sta->top--;
sprintf_s(sta->data[sta->top], "%.2lf", result);//发送格式化的字符串到字符上
i++;
continue;
}
else
{
sta->top++;
strcpy_s(sta->data[sta->top], suffix[i]);
i++;
continue;
}
}
/*printf("\n\nresult=%f\n", result);*/
//printf("\n\nsta=%s\n\n",sta->data[sta->top]);
//printf("sta->top=%d\n\n",sta->top);
setcolor(DARKGRAY);
settextstyle(60, 20, _T("幼圆"));
if (sta->top = 0)
outtextxy(480 - strlen(sta->data[sta->top]) * 20, 20, _T(sta->data[sta->top]));
else if (sta->top != 0)
outtextxy(190, 20, _T("ERROR"));
}//后缀表达式输出结果;
int main()
{
initgraph(480, 600); //设置窗口的大小;
setbkcolor(BLACK); //设置背景色为黑色;
cleardevice(); //用背景色来清空屏幕;
calcul_memu();
mouse_imfor();
closegraph();
printf("s:%s\n", str);
printf("结构体测试:\n");
for (int i = 0; i < q; i++)
printf("%s\n", st[i].x);
printf("suffix:\n");
for (int m = 0; m < q; m++)
printf("%s\n", suffix[m]);
getchar();
return 0;
}
//未重载类状况时,典型更换字符集即可
思路导图(计算器的ui界面布局的思路)