搜索
查看: 5751|回复: 22
打印 上一主题 下一主题

【第四章第5节】动手试一试 题解

[复制链接]
跳转到指定楼层
#
发表于 2014-3-29 23:06:14 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式
首先是最直接的方法:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. int main()
  5. {
  6.   int i;
  7.   printf("2:00\n");
  8.   Sleep(1000);
  9.   system("cls");
  10.   i = 59;
  11.   while(i >= 10){
  12.     printf("1:%d", i);
  13.     Sleep(1000);
  14.     system("cls");
  15.     i = i - 1;
  16.   }
  17.   while(i >= 0){
  18.     printf("1:0%d", i);
  19.     Sleep(1000);
  20.     system("cls");
  21.     i = i - 1;
  22.   }
  23.   i = 59;
  24.   while(i >= 10){
  25.     printf("0:%d", i);
  26.     Sleep(1000);
  27.     system("cls");
  28.     i = i - 1;
  29.   }
  30.   while(i >= 0){
  31.     printf("0:0%d", i);
  32.     Sleep(1000);
  33.     system("cls");
  34.     i = i - 1;
  35.   }
  36.   
  37.   system("pause");
  38.   return 0;
  39. }
复制代码


先打印2:00,然后分成4段处理,1:59 - 1:10, 1:09 - 1:00, 0:59 - 0:10, 0:09 - 0:00
打印时间 - 暂停1秒 - 清屏 - 打印下一个时间
推荐
发表于 2015-9-18 13:45:51 | 只看该作者
我的思路;
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
        int a=2,b=0;
    while (b>=0)
    {       
        if (10>b)
                printf("%d:0%d",a,b);
        else
        printf("%d:%d",a,b);
        b--;
        if (b<0)
                        {
                                if(a!=0)
                                a--;
                                b=59;
            }
        Sleep(1000);
        system("cls");
    }
        system("pause");
        return 0;
}
21#
发表于 2018-10-24 16:51:44 | 只看该作者
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
       
    int a;       
    printf("2:00");
    Sleep(1000);
   
     
     a=59;
     while (a>=10)
     {system("cls");
     printf("1:%d",a);
     Sleep(1000);
     a=a-1;}
     
     a=9;     
     while(a>=0)
     {system("cls");
     printf("1:0%d",a);
     Sleep(1000);
     a=a-1;}
      
      a=59;
      while (a>=10)
     {system("cls");
     printf("0:%d",a);
     Sleep(1000);
     a=a-1;}
     
      a=9;     
     while(a>=0)
     {system("cls");
     printf("0:0%d",a);
     Sleep(1000);
     a=a-1;}
   
   
   
   
        system("pause");
        return 0;
}
20#
发表于 2015-8-31 02:03:52 | 只看该作者
本帖最后由 sujx 于 2015-8-31 02:07 编辑

[mw_shl_code=c,true]
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{        
        int t,m,s;
    t=120;            
    while(t>=0)
    {
                m=t/60;
                s=t%60;
                printf("%d:%02d",m,s);
        t--;
        Sleep(1000);
        system("cls");        
    }
    printf("Time is over!\n");
    system("pause");
    return 0;
}[/mw_shl_code]

19#
发表于 2015-7-31 11:02:42 | 只看该作者
好厉害,膜拜大神{:soso_e179:}
18#
发表于 2014-8-6 11:10:36 | 只看该作者
楼主好牛啊!!!
17#
发表于 2014-7-26 22:21:26 | 只看该作者
Good!这是最简单的:01.#include <stdio.h>

02.#include <stdlib.h>

03.#include <windows.h>

04.int main()

05.{

06.  int sec;

07.  for(sec = 120; sec >= 0; --sec){

08.    printf("%d:%02d", sec / 60, sec % 60);

09.    Sleep(1000);

10.    system("cls");

11.  }

12.  system("pause");

13.  return 0;

14.}
(运用楼主的)
16#
发表于 2014-7-3 15:40:35 | 只看该作者
支持楼主!!
15#
发表于 2014-6-18 12:08:32 | 只看该作者
为什么要分四段呢?
14#
发表于 2014-4-27 20:44:10 | 只看该作者
楼主太棒了!!
13#
 楼主| 发表于 2014-4-21 00:30:16 | 只看该作者
愛情風華 发表于 2014-4-21 00:27
TERM environment variable not set.   出現這行

好吧,Xcode的终端模拟器不能clear
你把system("clear")去掉吧
或者直接用命令行
12#
发表于 2014-4-21 00:27:24 | 只看该作者
TERM environment variable not set.   出現這行
11#
 楼主| 发表于 2014-4-21 00:16:47 | 只看该作者
本帖最后由 rosynirvana 于 2014-4-21 00:31 编辑
愛情風華 发表于 2014-4-21 00:02
我用mac  的Xcode  run這段代碼是後來加上去才有運行正常,未加上去之前就如同圖片一樣 ,秒數> ...

噢……我明白了,因为xcode下面那个控制台缓存刷新比较慢的缘故
手动打个换行或者刷新stdout就行了
你试试这一段
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. int main()
  5. {
  6.   int sec;
  7.   int a, b;
  8.   sec = 120;
  9.   
  10.   while(sec >= 0){
  11.     a = sec / 60;
  12.     b = sec % 60;
  13.     printf("%d:", a);
  14.     if(b < 10)
  15.       printf("0");
  16.     printf("%d\n", b);
  17.     sleep(1);
  18.     //system("clear");
  19.     //This won't work for Xcode terminal
  20.     sec = sec - 1;
  21.   }
  22.   sleep(5);
  23.   return 0;
  24. }
复制代码
10#
发表于 2014-4-21 00:02:02 | 只看该作者
本帖最后由 愛情風華 于 2014-4-21 00:04 编辑

我用mac  的Xcode  run{:soso_e100:}這段代碼是後來加上去才有運行正常,未加上去之前就如同圖片一樣 ,秒數>10的會無法顯示秒數-->
else  {
            printf("%d\n",b);
        }
9#
 楼主| 发表于 2014-4-20 23:58:52 | 只看该作者
愛情風華 发表于 2014-4-20 23:37
#include
#include
//#include  windows作業系統才有效

这段代码我把注释去掉,然后有两个错
一个是main后面缺括号,另一个是a没有声明,改了就好了,运行结果是正常的

所以这应该不是你遇到问题的代码?

另外你是不是在unix上面试验的?unix可以用
#include <unistd.h>
sleep(1);
system("clear");
代替你注释掉的那三句
8#
发表于 2014-4-20 23:37:09 | 只看该作者
本帖最后由 愛情風華 于 2014-4-21 00:02 编辑

#include <stdio.h>
#include <stdlib.h>
//#include<windows.h>  windows作業系統才有效

int main
{
    //用秒數計算
    int a,b,sec;
    sec=120;
    while (sec>=0) {
        a=sec/60;
        b=sec%60;
        printf("%d:",a);
        if (b<10) {
            printf("0%d\n",b);
        }   else  {
            printf("%d\n",b);
        }
        //Sleep(1000);
        //system("cls");
        sec=sec-1;
    }
        return 0;  
}

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表