65.9K
CodeProject 正在变化。 阅读更多。
Home

体验:心智练习

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.09/5 (32投票s)

2003年8月16日

4分钟阅读

viewsIcon

60298

downloadIcon

251

这个程序会让你做数学题来提高你的思维能力。

Sample Image - sc2.png

引言

根据我的经验,我认识到经验是最好的老师。通过反复做某件事,你可以提高你的技能。你的思维方式可以得到改变和提升。

每天超越普通人进行思考,你的思维过程可以得到提升,达到高于常人的水平。更快、更合乎逻辑地思考的能力就在我们指尖。

我做了一个程序,它会让你做一些平时不常做的数学题。它会测试你 5 道随机题目并记录你的统计数据。

在这个程序中,我并不是想展示什么编码技巧之类的。我发布这个程序是因为我想表明,经验永远比死读书要有优势。

但在编程层面

我的程序随机选择数学题依赖于时间。

我创建了这个源/头文件,以便我有一个自己的小型函数库。

 

/*tools.cpp                                                          */
/*这里有一些简单的工具,我用它们来制作其他项目*/
/*我发现的一些可以以后使用的东西                          */

/*包含文件*/
#include "tools.h"
#include "stdafx.h"

/*这个函数返回一个介于    */
/*1到20之间的随机数,与系统时钟(毫秒)有关*/
double low_rand()
{
 /*初始化随机数(双精度)*/
 double rand = 0.0;

 /*_timeb 结构体*/
 struct _timeb time;

 /*用数据填充 timeb 结构体(当前时间)*/
 _ftime(&time);
 
 /*将整数转换为小数形式*/
 /*例如:989 变成 .989                    */
 rand = (time.millitm * ((double).001));

 /*将 rand 乘以 100                       */
 /*例如:.989 变成 98.8                   */
 rand = (rand * 100);

 /*将 rand 除以 5                       */
 /*例如:98.9 变成 19.78                  */
 rand = (rand / 5);

 /*返回 rand,它是一个介于 1 和 20 之间的数*/
 return rand;
}

/*这个函数返回一个介于     */
/*1到1000之间的随机数,与系统时钟(毫秒)有关*/
int high_rand()
{
 /*_timeb 结构体*/
 struct _timeb time;
 /*用数据填充 timeb 结构体(当前时间)*/
 _ftime(&time);

 /*以毫秒为单位返回时间作为随机数*/
 return time.millitm;
}

/*从系统中检索当前时间          */
void timec(_stime &stime)
{
 /*系统时间结构体*/
   SYSTEMTIME time;

   /*检索本地时间*/
   GetLocalTime(& time);

   /*将 _stime 结构体加载为当前时间*/
   stime.hour = time.wHour;
   stime.min = time.wMinute;
   stime.sec = time.wSecond;
   stime.milli = time.wMilliseconds;
}

/*输入是一个特定时间,输出是        */
/*该时间与当前时间之间的差值*/
/*以秒表的模式实现                   */
_stime timer(_stime &stime)
{
 /*复制传递给函数的 _stime,以避免信息传递中的错误*/
 _stime calc_time = stime;

 /*系统时间结构体*/
 SYSTEMTIME time;
 /*检索本地时间*/
 GetLocalTime(& time);

 /*计算当前时间与给定时间之间的差值*/
 /*计算小时*/
 if(calc_time.hour >= time.wHour)
 {
  calc_time.hour = calc_time.hour - time.wHour;
 }
 else
 {
  calc_time.hour = time.wHour - calc_time.hour;
 }

 /*计算分钟*/
 if(calc_time.min >= time.wMinute)
 {
  calc_time.min = calc_time.min - time.wMinute;
 }
 else
 {
  calc_time.min = time.wMinute - calc_time.min;
 }

 /*计算秒*/
 if(calc_time.sec >= time.wSecond)
 {
  calc_time.sec = calc_time.sec - time.wSecond;
 }
 else
 {
  calc_time.sec = time.wSecond - calc_time.sec;
 }

 /*计算毫秒*/
 if(calc_time.milli >= time.wMilliseconds)
 {
  calc_time.milli = calc_time.milli - time.wMilliseconds;
 }
 else
 {
  calc_time.milli = time.wMilliseconds - calc_time.milli;
 }


 /*返回当前时间与给定时间之间的差值*/
 return (calc_time);
}

/*开始到结束*/
int btoe(char *filename)
{
 int lines = 0;
 char search[255];

 if(filename != NULL)
 {
  ifstream ifile(filename);

  do
  {
   ifile.getline(search, 255);
   ++lines;
  }while(ifile.eof() == 0);

  ifile.close();
 }
 else
 {
  lines = -1;
 }

 return lines;
}

/*开始到空格*/
int btos(char *filename)
{
 int lines = 0;
 char search[255];
 bool reached_space = false;

 if(filename != NULL)
 {
  ifstream ifile(filename);

  do
  {
   ifile.getline(search, 255);
   ++lines;

   if(ifile.eof() == 1)
   {
    reached_space = true;
   }

  }while(true != reached_space);

  ifile.close();

 }
 else
 {
  lines = -1;
 }

 return lines;
}

/*在文件中搜索句子*/
int sfile(char *filename, char *sentence)
{
 int lines = 0;
 char search[255];
 bool searching = true;

 if(filename != NULL)
 {
  ifstream ifile(filename);

  do
  {
   ifile.getline(search, 255);
   ++lines;

   if(ifile.eof() == 1)
   {
    searching = false;
    lines = -1;
   }

  }while(false != searching);

  ifile.close();
 }
 else
 {
  lines = -1;
 }

 return lines;
}

/*获取 C 字符串(数组)的实际长度*/
int str_length(char str[], int length)
{
 int str_size = 0;

 for(int x = 0; x < length; ++x)
 {
  if(str[x] != 0)
  {
   ++str_size;
  }
  else
  {
   break;
  }
 }

 return str_size;
}

//////////////////////////////////////////////////////////////

此源/头文件中的函数用于处理时间和执行与时间相关的基本操作。

上面的代码可能是整个程序中最重要的部分,因为它处理的是相对广泛使用的时间。

 

--ice911

(感谢您阅读我的文章)

© . All rights reserved.