2007-12-20
兩個常用的副程式:skip(), pause()
关键字: C
// for skip(), pause()
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
// ----------------------------------------------
// skip(-3);
void skip(int no)
{
// limits no in 1 .. 20
if ((no >= 1) && (no <= 20)) {// no is OK!
for (int i=1;i<=no;i++) {
// printf("i= %5d, no= %5d\n", i, no);
printf("\n");
}
}
else {// no is invalid!
// printf("\n\n\n *** error in skip(), no= %5d\n", no);
// system("pause");
printf("\n");
}
}// end of skip()
// ----------------------------------------------
// pause();
void pause(void)
{
int ch1;
// remove type- ahead
while (_kbhit()) {
_getch();
}
// ----------------------------------------------
printf(" Press any key to continue, [Esc] key to exit... ");
do {
// wait for key pressed
} while (!_kbhit());
ch1= _getch();
printf("\n");
// ----------------------------------------------
if (ch1==0x1b) {// [Esc] key be pressed!
exit(1);
}
// remove extra key pressed
while (_kbhit()) {
_getch();
}
}// end of pause()
// ----------------------------------------------
void main()
{
skip(3);
for (int i=1;i<=20;i++) {
skip(i);
printf("i= %5d, i*i= %5d, sqrt(i)= %10.6lf\n",
i, i*i, sqrt(i));
pause();
}
pause();
}// end of main()
评论
sjgau
2007-12-20
以下為 時間測量的輸出
// file: psc51.cpp
// for time1(), time2()
/*
ct1= 1, no= 10, sum= 55, t1= 13917990, dt= 0.0000
ct1= 2, no= 30, sum= 465, t1= 13917990, dt= 0.0000
ct1= 3, no= 90, sum= 4095, t1= 13917990, dt= 0.0000
ct1= 4, no= 270, sum= 36585, t1= 13917990, dt= 0.0000
ct1= 5, no= 810, sum= 328455, t1= 13917990, dt= 0.0000
ct1= 6, no= 2430, sum= 2953665, t1= 13917990, dt= 0.0000
ct1= 7, no= 7290, sum= 26575695, t1= 13917990, dt= 0.0000
ct1= 8, no= 21870, sum= 239159385, t1= 13917990, dt= 0.0000
ct1= 9, no= 65610, sum= -2142598441, t1= 13917990, dt= 0.0000
ct1= 10, no= 196830, sum= -2103713615, t1= 13917990, dt= 0.0000
ct1= 11, no= 590490, sum= -1754143841, t1= 13917990, dt= 0.0000
ct1= 12, no= 1771470, sum= 1390803145, t1= 13917990, dt= 0.0100
ct1= 13, no= 5314410, sum= -372987993, t1= 13918000, dt= 0.0300
ct1= 14, no= 15943230, sum= 922132129, t1= 13918030, dt= 0.0900
ct1= 15, no= 47829690, sum= -338575121, t1= 13918120, dt= 0.2700
ct1= 16, no= 143489070, sum= 1104302137, t1= 13918390, dt= 0.8120
ct1= 17, no= 430467210, sum= 918317431, t1= 13919202, dt= 2.4130
ct1= 18, no= 1291401630, sum= -1616479343, t1= 13921615, dt= 7.3110
*** end of program!
Press any key to continue
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/timeb.h>
#include <time.h>
// ----------------------------------------------
#include "sj01.h"
#include "sj02.h"
// ----------------------------------------------
void main()
{
int no, sum, i, ct1= 0;
long t1;
double dt;
// ----------------------------------------------
no= 10;
skip(3);
while (no > 0) {
time1(&t1);
sum= 0;
for (i=1;i<=no;i++) {
sum+= i;
}
time2(t1, &dt);
// ----------------------------------------------
ct1++;
printf("ct1= %3ld, no= %12ld, sum= %12ld, t1= %8ld, dt= %8.4lf\n",
ct1, no, sum, t1, dt);
if (dt > 0) {
// pause();
}
if (ct1>=16) {
// break;
}
no*= 3;
}
skip(1);
printf("*** end of program!\n");
}// end of main()
sjgau
2007-12-20
將自己寫的副程式放到外面的檔案,
分開編譯
分開編譯
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include "sj01.h"
// ----------------------------------------------
void main()
{
skip(1);
for (int i=1;i<=20;i++) {
skip(i);
printf("i= %5d, i*i= %5d, sqrt(i)= %10.6lf\n",
i, i*i, sqrt(i));
pause();
}
}// end of main()
发表评论
- 浏览: 43098 次
- 性别:

- 来自: 台灣省 台北市

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
計算時間的副程式
以下程式,可以證明 最小的時間間隔是 10 ms // file: psc ...
-- by sjgau -
兩個常用的副程式:skip() ...
以下為 時間測量的輸出 // file: psc51.cpp // fo ...
-- by sjgau -
兩個常用的副程式:skip() ...
將自己寫的副程式放到外面的檔案, 分開編譯 #include <s ...
-- by sjgau -
未卜先知-計算機模擬 Com ...
我的習慣是:從頭開始。 所以,有興趣入門的朋友,可以跟著學到一些東西。 我使用的 ...
-- by sjgau -
我想要做的事情
so you need another 3 years to come back ...
-- by jsjw2






评论排行榜