年轮

2008 8.28 Thu
     12
3456789
10111213141516
17181920212223
24252627282930
31      
«» 2008 - 8 «»

文章搜索

日志文章列表

2007年11月28日 01:34:42

写个电子时钟

//有BUG的电子时钟
#include <iostream.h>
using namespace std;
#include <time.h>

void wait1s()
{
  long t = time(NULL);
  while(time(NULL)==t);
}

int main()
{
  for(;;){
    time_t t = time(NULL); //取当前系统时间
    cout<<"\r"<<ctime(&t); //??? :(
    wait1s();
  }
 
}

阅读全文>>

Tags: 时钟  

类别: 每日编程 |  评论(0) |  浏览(580) |  收藏
2007年11月28日 00:57:27

用类实现的简陋计时程序

//用类实现的简陋计时程序
#include <iostream.h>
using namespace std;
#include <ctime>

class Time
{
  int hour;
  int minitue;
  int second;
public:
  void input()
  {
    cout<<"input time : ";
    cin>>hour>>minitue>>second;
  }
  void wait1s()         //等1s,sleep(1)
  {
    long t = time(NULL);
    while(time(NULL)==t);
  }
  void tick()           //按照60和24进制调整时间
  {
    if(++second>=60){
      second = 0;
      if(++minitue>=60){
        minitue = 0;
        if(++hour>=24){
            hour = 0;
        }
      }
    }
    &nb..

阅读全文>>

Tags: 计时  

类别: 每日编程 |  评论(0) |  浏览(550) |  收藏
2007年11月28日 00:55:48

简陋的倒计时程序


//简陋的倒计时程序
#include
using namespace std;
#include

class Reverse{ //定义一个类
   int h; //私有成员变量
   int m;
   int s;
public: //公开函数接口
   void set(){ //接收数据,设置倒计时开始时间
       cout << "input start time:";
       cout << "小时 分钟 秒 之间用空格分开"<> h >> m >> s;
   }
   void wait(){ //等待一秒的函数,可以直接调用sleep(int n)实现
       long t=time(NULL);//solaris中了long time_t,time(NULL)取当前系统时间
       while(time(NULL)==t);
   }
   void back(){ //进制调整
       if(--s<0){
           s = 59;
           if(--m<0){
               m = 59;
               if(--h<0)
                   h = 23;
           }
       }
   }
   bool isZero(){
       return h==0&&m==0&&s==0;
   }
   void show(){
       cout << '\r'; //当前行首显示
       if(h<10) cout << 0; //十位填充0
       cout << h << ':';
       if(m<10) cout << 0;
       cout << m << ':';
       if(s<10) cout << 0;
       cout << s << flush;
   }
   void run(){
       do{
           show();
           wait();
           back();
cout << "\..

阅读全文>>

Tags: 倒计时程序  

类别: 每日编程 |  评论(1) |  浏览(538) |  收藏
2007年11月26日 23:44:07

权限问题&聊天程序

#include <iostream>
using namespace std;
#include <fstream>
#include <stdlib.h>
int main()
{
ofstream fout("talk.txt", ios::app); //以追加方式打开文件
if(!fout)                   //如果打开文件失败
    cout << "error open log.txt" << endl;
else{
    char msg[1000];             //准备一个接受信息的数组
    cout << "input message:";
    cin.getline(msg, 256);       //读取一行信息
    fout << getenv("LOGNAME") << ":" << msg << endl; //显示登陆用户名字
    fout.close();             //关闭文件
  }
}
/*编译链接该程序,生成a.out文件,然后chmod u+s a.out ,给程序附加程序所有者权限,
在服务器执行,即聊天通讯程序的原始雏形! */

阅读全文>>

Tags: chmod  

类别: 每日编程 |  评论(0) |  浏览(557) |  收藏
2007年11月26日 23:16:15

如何取得unix中的环境变量

    环境变量经常在程序开发中用到,在C语言中,可以通过一下方式访问环境变量:

    1、int main(int argc, char* argv[], char* envp[])

    main()函数的前两个参数int   argc,   char   *argv[],想必大家都已经很熟悉其作用了,它的第三个参数char*   envp[]是用来是环境字符串数组,是传递系统环境变量用的,但执行时,系统会将它的系统变量传递过来,见下面这段代码:
#include <iostream.h>
using namespace std;
#include <stdio.h>
int   main   (   int   argc   ,   char   *argv[]   ,   char   *envp[]   )
{
          int     i;
          for(   i=0   ;   envp!=NULL   ;   i++   )
          {
                    printf(   "%s\n "   ,   envp   );
    &nb..

阅读全文>>

Tags: 环境变量  

类别: 每日编程 |  评论(0) |  浏览(742) |  收藏
2007年11月11日 22:35:57

求出所有因子

//输入一个整数,求出它的所有因子。即求其非1和本身的约数。
#include <iostream>
using namespace std;
int main()
{
  cout<<"enter a number"<<endl;
  int n;
  cin>>n;
  for(int i=2;i<n/*&&n%i==0*/;i++){ //注释内的条件不成立时循环就结束
    if(n%i==0)
      cout<<i<<" ";
  }
}

阅读全文>>

Tags: 求因子  

类别: 每日编程 |  评论(0) |  浏览(373) |  收藏
« 1 234» Pages: ( 1/4 total )