【C++程序】[新手向]闹钟

这个闹钟可以在定时提醒你时间到了,采用的是windows系统的任务计划系统。

[以下为老版本,更新请见下↓]

代码如下:

#include "iostream"
using namespace std;

int leng(int number)
{
    int flag=0;
    while(number)
    {
        number/=10;
        flag++;
    }
    return flag;
}

int main()
{
    int hour;
    int minute;
    cout << "设定小时:";
    cin >> hour;
    cout << endl << "设定分钟:";
    cin >> minute;
    if((hour<=24 && hour>=0) && (minute>=0 && minute<=60))
    {
        char temp_hour[3],temp_minute[3];
        if (leng(minute)<2)
        {
            sprintf(temp_minute,"0%d",minute);
        }
        else
        {
            itoa(minute,temp_minute,10);
        }
        if (leng(hour)<2)
        {
            sprintf(temp_hour,"0%d",hour);
        }
        else
        {
            itoa(hour,temp_hour,10);
        }
        system("del clock.bat");
        system("echo schtasks /Delete /tn clock_alarm >> clock.bat");
        char temp[300];
        sprintf(temp,"echo schtasks /Create /tn clock_alarm /tr \"D:\\Works\\C++\\clock.exe\" /sc once /st %s:%s >>clock.bat",temp_hour,temp_minute);
        system(temp);
        system("echo pause >> clock.bat");
        system("echo exit >> clock.bat");
        system("start clock.bat");
    }
    return 0;
}

补上clock.exe的代码:

#include <iostream>
using namespace std;

int main()
{
    cout << "Time's up!";
    return 0;
}

[新版本]

CLOCK_ALARM.CPP

#include <iostream>
#include <fstream>
#include <fstream>
using namespace std;

int leng(int number)
{
    int flag=0;
    while(number)
    {
        number/=10;
        flag++;
    }
    return flag;
}

int main()
{
    int hour;
    int minute;
    a:int choice;
    cout << "请输入选项:1)定时闹钟 2)计时闹钟" << endl;
    cin >> choice;
    if (choice > 2 && choice < 1)
    {
        cin.clear();
        cin.sync();
        goto a;
    }
    switch (choice)
    {
    case 1: cout << "设定小时:";
            cin >> hour;
            cout << endl << "设定分钟:";
            cin >> minute;
        break;
    case 2: int minute_plus;
            cout << "设定计时分钟:";
            cin >> minute_plus;
            system("erase /q C:\\time.txt");
            system("time /t >> c:\\time.txt");
            char timenow[5];
            ifstream infile;  
            infile.open("c://time.txt");  
            if(infile.is_open())
            { 
                for(int i = 0;infile.good() && !infile.eof();i++)  
                {  
                    infile >> timenow[i];                   
                }  
                infile.close();  
            }
            char* address[4];
            address[0] = &timenow[0];
            address[1] = &timenow[1];
            address[2] = &timenow[3];
            address[3] = &timenow[4];
            hour = atoi(address[0]);
            minute = atoi(address[2]);
            minute = minute_plus + minute;
            while (minute >= 60)
            {
                ++hour;
                minute = minute - 60;
            }
        break;
    }
    if((hour<=24 && hour>=0) && (minute>=0 && minute<=60))
    {
        char temp_hour[3],temp_minute[3];
        if (leng(minute)<2)
        {
            sprintf(temp_minute,"0%d",minute);
        }
        else
        {
            _itoa_s(minute,temp_minute,10);
        }
        if (leng(hour)<2)
        {
            sprintf(temp_hour,"0%d",hour);
        }
        else
        {
            _itoa_s(hour,temp_hour,10);
        }
        system("del clock.bat");
        system("echo schtasks /Delete /tn clock_alarm >> clock.bat");
        char temp[300];
        sprintf(temp,"echo schtasks /Create /tn clock_alarm /tr \"D:\\Works\\C++\\clock.exe\" /sc once /st %s:%s >>clock.bat",temp_hour,temp_minute);
        system(temp);
        system("echo pause >> clock.bat");
        system("echo exit >> clock.bat");
        system("start clock.bat");
    }
    return 0;
}

clock.cpp

#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#pragma comment(lib,"winmm.lib")
using namespace std;

int main()
{
    PlaySound(TEXT("D:\\Works\\C++\\clock\\7557.wav"), NULL, SND_FILENAME | SND_ASYNC);
    cout << "Time's up!" << endl;
    system("pause");
    return 0;
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值