吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1017|回复: 16
收起左侧

[求助] 两个cpp文件如何合成一个呢?

[复制链接]
by:zhazha 发表于 2022-6-26 15:13
如题,第一个cpp文件是秒表程序 第二个cpp是easyx图形界面
我想把两者结合在一起,但是会出现很多问题 比如变量未定义、不能强制转换等等 有老哥懂吗?
[C] 纯文本查看 复制代码
#include <stdio.h>  
#include <conio.h> 
#include <windows.h> 
#include <stdlib.h>  
struct tm     //定义时间结构体,包括时分秒和10毫秒 
{
    int hours, minutes, seconds;
    int hscd;
}time, tmp, total;    //time用以计时显示,tmp用以存储上一阶段时间,total记总时间 
int cnt;
FILE* fout;
//每次调用update函数,相当于时间过了10ms 
void update(struct tm* t)
{

    (*t).hscd++;    //10ms单位时间加1  
    cnt++;
    if ((*t).hscd == 100)   //计时满1s,进位 
    {
        (*t).hscd = 0;
        (*t).seconds++;
    }
    if ((*t).seconds == 60)   //计时满一分,进位 
    {
        (*t).seconds = 0;
        (*t).minutes++;
    }
    if ((*t).minutes == 60)        //计时满一小时,进位 

    {
        (*t).minutes = 0;
        (*t).hours++;
    }
    if ((*t).hours == 24) (*t).hours = 0;
    //delay(); 
    Sleep(10);  //Sleep是windows提供的函数,作用是暂停程序,单位毫秒,所以此处暂停10ms 
}
void display(struct tm* t)
{
    //此处输出计时结果,\r为回车不换行,既一直在同一行更新时间 
    printf("%d:", (*t).hours);
    printf("%d:", (*t).minutes);
    printf("%d:", (*t).seconds);
    printf("%d\r", (*t).hscd);
    //printf("Now, press ‘e’ key to stop the clock…");  
}
void time_init()  //初始化时间 
{
    time.hours = time.minutes = time.seconds = time.hscd = 0;
}
void get_total()   //计算总时间 
{
    total.hscd = cnt % 100;
    cnt /= 100;
    total.seconds = cnt % 60;
    cnt /= 60;
    total.minutes = cnt % 60;
    cnt /= 60;
    total.hours = cnt;
}
int main()
{
    char m;
    time_init();
    cnt = 0;
    fout = fopen("timeout.txt", "w");
    printf("按回车键开始计时!\n");
    while (1)
    {
        m = getch();
        if (m != '\r')     //读入一个输入,如果是回车,那么跳出次循环 
            printf("输入错误,仅能输入回车键!\n");
        else
            break;
    }
    printf("已经开始计时,但是你可以按回车键以分段计时!\n");
    while (1)
    {
        if (kbhit())    //此处检查是否有键盘输入 
        {
            m = getch();
            if (m == '\r')//如果等于回车,那么计时结束,跳出循环 
                break;
            else if (m == ' ')  //如果等于空格,显示此次计时,初始化计时器 
            {
                tmp = time;      //记录上一段计时器结果 
                fprintf(fout, "%d:%d:%d:%d\n", tmp.hours, tmp.minutes, tmp.seconds, tmp.hscd); //写入文件 
                time_init();
                printf("\n");
            }
            else
            {
                printf("输入错误,仅支持输入回车键或者空格键!\n");
            }
        }
        update(&time);     //更新计时器 
        display(&time);    //显示计时器时间 
    }
    tmp = time;       //输出最后一次即使结果,写入文件 
    fprintf(fout, "%d:%d:%d:%d\n", tmp.hours, tmp.minutes, tmp.seconds, tmp.hscd);
    get_total();      //计算总的时间,显示,并写入文件 
    printf("\n总时间:%d:%d:%d:%d\n", total.hours, total.minutes, total.seconds, total.hscd);
    fprintf(fout, "统计时间:%d:%d:%d:%d\n", total.hours, total.minutes, total.seconds, total.hscd);
    fclose(fout);
    printf("已经保存到当前目录下的timeout.txt文件中按任意键结束!");
    getch();

}








[C] 纯文本查看 复制代码
#define _CRT_SECURE_NO_WARNINGS	
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h> 
#include<time.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
struct tm     //定义时间结构体,包括时分秒和10毫秒 
{
	int hours, minutes, seconds;
	int hscd;
}time, tmp, total;    //time用以计时显示,tmp用以存储上一阶段时间,total记总时间 
int cnt;
FILE* fout;
void image()//背景图片输出
{
	IMAGE img;//定义一个对象(变量)
	loadimage(&img, "./42.jpeg", 800, 640);//后两个为图片大小
	putimage(0, 0, &img);
}
void title()//标题
{
	settextcolor(BLACK);
	setbkmode(TRANSPARENT);//文字背景透明
	settextstyle(80, 0, "宋体");
	outtextxy(120, 60, "挑战六秒小游戏");
	settextstyle(30, 0, "宋体");
	settextcolor(RED);
	outtextxy(240, 160, "游戏规则请看doc窗口");
}

void print()//doc输出
{
	
	printf("----------------游戏介绍和规则------------------\n");
	printf("这个游戏在日常生活中很常见,例如作者喜爱的柯南就有类似的游戏。\n");
	printf("用户第一次进行按键操作时,计时开始;再次按键操作,计时结束。\n");
	printf("准确暂停到六秒整,则挑战成功\n");
}
void change()//更改easy界面标题
{
	HWND hnd = GetHWnd();//获取窗口句柄
	SetWindowText(hnd, "数六秒小游戏");//设置窗口标题
	MessageBox(NULL, "看规则!", "看规则", MB_OK);//弹出窗口
}
void music()//音乐
{
	mciSendString("open ./music.mp3 alias Bgm ",0,0,0); 
	mciSendString("play Bgm", 0, 0, 0);
}
//
//void update(struct tm* t)
//{
//
//	(*t).hscd++;    //10ms单位时间加1  
//	cnt++;
//	if ((*t).hscd == 100)   //计时满1s,进位 
//	{
//		(*t).hscd = 0;
//		(*t).seconds++;
//	}
//	if ((*t).seconds == 60)   //计时满一分,进位 
//	{
//		(*t).seconds = 0;
//		(*t).minutes++;
//	}
//	if ((*t).minutes == 60)        //计时满一小时,进位 
//
//	{
//		(*t).minutes = 0;
//		(*t).hours++;
//	}
//	if ((*t).hours == 24) (*t).hours = 0;
//	Sleep(10);  //Sleep是windows提供的函数,作用是暂停程序,单位毫秒,所以此处暂停10ms 
//}
//void display(struct tm* t)
//{
//	//此处输出计时结果,\r为回车不换行,既一直在同一行更新时间 
//	printf("%d:", (*t).hours);
//	printf("%d:", (*t).minutes);
//	printf("%d:", (*t).seconds);
//	printf("%d\r", (*t).hscd);
//	//printf("Now, press ‘e’ key to stop the clock…");  
//}
//void time_init()  //初始化时间 
//{
//	time.hours = time.minutes = time.seconds = time.hscd = 0;
//}
//void get_total()   //计算总时间 
//{
//	total.hscd = cnt % 100;
//	cnt /= 100;
//	total.seconds = cnt % 60;
//	cnt /= 60;
//	total.minutes = cnt % 60;
//	cnt /= 60;
//	total.hours = cnt;
//}

//int a()
//{
//	char m;
//	time_init();
//	cnt = 0;
//	fout = fopen("timeout.txt", "w");
//	printf("按回车键开始计时!\n");
//	while (1)
//	{
//		m = getch();
//		if (m != '\r')     //读入一个输入,如果是回车,那么跳出次循环 
//			printf("输入错误,仅能输入回车键!\n");
//		else
//			break;
//	}
//	printf("已经开始计时,但是你可以按回车键以分段计时!\n");
//	while (1)
//	{
//		if (kbhit())    //此处检查是否有键盘输入 
//		{
//			m = getch();
//			if (m == '\r')//如果等于回车,那么计时结束,跳出循环 
//				break;
//			else if (m == ' ')  //如果等于空格,显示此次计时,初始化计时器 
//			{
//				tmp = time;      //记录上一段计时器结果 
//				fprintf(fout, "%d:%d:%d:%d\n", tmp.hours, tmp.minutes, tmp.seconds, tmp.hscd); //写入文件 
//				time_init();
//				printf("\n");
//			}
//			else
//			{
//				printf("输入错误,仅支持输入回车键或者空格键!\n");
//			}
//		}
//		update(&time);     //更新计时器 
//		display(&time);    //显示计时器时间 
//	}
//	tmp = time;       //输出最后一次即使结果,写入文件 
//	fprintf(fout, "%d:%d:%d:%d\n", tmp.hours, tmp.minutes, tmp.seconds, tmp.hscd);
//	get_total();      //计算总的时间,显示,并写入文件 
//	printf("\n总时间:%d:%d:%d:%d\n", total.hours, total.minutes, total.seconds, total.hscd);
//	fprintf(fout, "统计时间:%d:%d:%d:%d\n", total.hours, total.minutes, total.seconds, total.hscd);
//	fclose(fout);
//	printf("已经保存到当前目录下的timeout.txt文件中按任意键结束!");
//	getch();
//
//}
int main()                                                                                                                                                               
{

	initgraph(800, 640,SHOWCONSOLE);
	system("mode con cols=69 lines=45");//调一下doc窗口大小
	setbkcolor(RGB(191,147,52));//背景颜色,设置了图片全屏,这行无用
	cleardevice();//清屏设置背景颜色
	image();
	title();
	music();
	change();
	print();
	/*a();*/
	getchar();
	closegraph();
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

君如兰 发表于 2022-6-26 15:17
把一个写成另一个的函数  传参进去
 楼主| by:zhazha 发表于 2022-6-26 15:19
君如兰 发表于 2022-6-26 15:17
把一个写成另一个的函数  传参进去

老哥,原理啥的我应该都懂,就是不会操作
雷欧库珀 发表于 2022-6-26 15:27
为啥要合成一个。。不同的部分写不同的源文件不是更好吗?
 楼主| by:zhazha 发表于 2022-6-26 15:33
雷欧库珀 发表于 2022-6-26 15:27
为啥要合成一个。。不同的部分写不同的源文件不是更好吗?

嗯...作业 第一个秒表程序是我在别处找的代码,因为我在作业需要这个秒表,然后就不会合成了
wycdd 发表于 2022-6-26 16:28
合并的话,两个文件包含的头文件也要考虑的啊
 楼主| by:zhazha 发表于 2022-6-26 16:39
wycdd 发表于 2022-6-26 16:28
合并的话,两个文件包含的头文件也要考虑的啊

这个好像没什么问题
xiao14116 发表于 2022-6-26 16:59
最简单的办法,一个文件封装成静态库或者动态库,另一个文件调用
 楼主| by:zhazha 发表于 2022-6-26 17:00
xiao14116 发表于 2022-6-26 16:59
最简单的办法,一个文件封装成静态库或者动态库,另一个文件调用

我也想要用静态库,但是老师要求代码行数
ghostu1 发表于 2022-6-26 17:10
就提交两个文件呗,用你的文件调用别人的。老师要求只能交一个文件吗?
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-18 16:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表