⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch01s02.html

📁 优秀的java程序开发软件方式与方法,对大家开发程序的时候有指导性帮助
💻 HTML
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>2.&nbsp;CPS&#29468;&#25968;&#23383;&#28216;&#25103;</title><link rel="stylesheet" href="html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.69.1"><link rel="start" href="index.html" title="Java&#32593;&#32476;&#31243;&#24207;&#21592;&#30475;Continuation"><link rel="up" href="ch01.html" title="Chapter&nbsp;1.&nbsp;CPS&#19982;&#32593;&#32476;&#31243;&#24207;&#27969;&#31243;&#25511;&#21046;"><link rel="prev" href="ch01s01.html" title="1.&nbsp;&#31616;&#21333;&#30340;CPS&#20363;&#23376;"><link rel="next" href="ch01s03.html" title="3.&nbsp;CPS&#19982;goto"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2.&nbsp;CPS&#29468;&#25968;&#23383;&#28216;&#25103;</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch01s01.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;1.&nbsp;CPS&#19982;&#32593;&#32476;&#31243;&#24207;&#27969;&#31243;&#25511;&#21046;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ch01s03.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e61"></a>2.&nbsp;CPS&#29468;&#25968;&#23383;&#28216;&#25103;</h2></div></div></div><p>&#29087;&#24713;&#20102;&#26368;&#31616;&#21333;&#26368;&#22522;&#26412;&#30340;CPS&#31243;&#24207;&#65292;&#25105;&#20204;&#25509;&#19979;&#26469;&#30475;&#19968;&#20010;&#27604;&#36739;&#22797;&#26434;&#30340;&#20363;&#23376;&#12290;&#25105;&#20204;&#35201;&#20889;&#19968;&#20010;&#29468;&#25968;&#23383;&#30340;&#28216;&#25103;&#12290;&#30005;&#33041;&#20250;&#33258;&#21160;&#29983;&#25104;1&#21040;100&#20043;&#38388;&#30340;&#19968;&#20010;&#25968;&#23383;&#65292;&#28982;&#21518;&#29992;&#25143;&#32473;&#20986;&#33258;&#24049;&#30340;&#29468;&#27979;&#65292;&#30005;&#33041;&#20250;&#22238;&#31572;Number too big&#25110;Number too small&#12290;&#28216;&#25103;&#20250;&#37325;&#22797;&#36825;&#20010;&#36807;&#31243;&#65292;&#30452;&#21040;&#29992;&#25143;&#29468;&#20986;&#31572;&#26696;&#20026;&#27490;&#12290;&#29992;&#26222;&#36890;&#30340;C&#20889;&#20986;&#26469;&#26159;&#36825;&#26679;&#30340;&#65306;</p><pre class="programlisting">#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;void main() {    int ans = random() % 100 + 1;    int win = 0;    int guess;    while (!win) {        printf("Guess a number [1, 100]: ");        scanf("%d", &amp;guess);        if (guess &lt; ans)            printf("Number too small.\n");        else if (guess &gt; ans)            printf("Number too big.\n");        else            win = 1;    }    printf("Congratulations!");}</pre><p>&#36825;&#20010;&#31243;&#24207;&#30340;&#36923;&#36753;&#21487;&#20197;&#35828;&#38750;&#24120;&#28165;&#26970;&#20102;&#12290;&#19981;&#36807;&#65292;&#29616;&#22312;&#25105;&#20204;&#20551;&#35774;&#31995;&#32479;&#27809;&#26377;printf&#65292;&#21482;&#26377;&#23545;&#24212;&#30340;CPS&#20989;&#25968;print&#65292;&#20854;&#23450;&#20041;&#30456;&#24403;&#20110;&#65306;</p><pre class="programlisting">void print(const char *message,        void (*continuation)(void)) {    printf("%s\n", message);    continuation();    exit(0);}</pre><p>&#27880;&#24847;&#21040;&#35813;&#20989;&#25968;&#19981;&#20250;&#36820;&#22238;&#32473;&#27597;&#20989;&#25968;&#65292;&#32780;&#26159;&#20250;&#30452;&#25509;&#25191;&#34892;&#33258;&#24049;&#30340;&#31532;&#20108;&#20010;&#21442;&#25968; &#12290;&#36825;&#26102;&#25105;&#20204;&#30340;&#28216;&#25103;&#24212;&#35813;&#24590;&#20040;&#20889;&#21602;&#65311;</p><p>&#20026;&#20102;&#31616;&#21333;&#36215;&#35265;&#65292;&#25105;&#20204;&#21482;&#35201;&#27714;&#25226;printf&#29992;CPS&#30340;print&#20195;&#26367;&#65292;&#31243;&#24207;&#30340;&#20854;&#20182;&#22320;&#26041;&#36824;&#26159;&#21487;&#20197;&#29992;&#26222;&#36890;&#30340;&#20989;&#25968;&#35843;&#29992;&#30340;&#12290;&#22240;&#20026;print&#19981;&#20250;&#36820;&#22238;&#65292;&#25152;&#20197;&#25105;&#20204;&#35201;&#22312;&#25152;&#26377;&#35843;&#29992;&#21040;printf&#30340;&#22320;&#26041;&#25226;&#31243;&#24207;&#8220;&#26029;&#24320;&#8221;&#65292;&#24182;&#25226;&#26029;&#24320;&#21518;&#35813;print&#20043;&#21518;&#30340;&#25152;&#26377;&#37096;&#20998;&#21464;&#25104;&#19968;&#20010;&#20989;&#25968;&#12290;&#28982;&#21518;&#25105;&#20204;&#25226;&#36825;&#20010;&#20989;&#25968;&#20256;&#32473;print&#20316;&#20026;&#31532;&#20108;&#20010;&#21442;&#25968;&#23601;&#34892;&#20102;&#12290;</p><p>&#19978;&#38754;&#35828;&#24471;&#20284;&#20046;&#24456;&#31616;&#21333;&#65292;&#20294;&#26159;&#22914;&#26524;&#24744;&#35797;&#19968;&#35797;&#65292;&#24744;&#39532;&#19978;&#23601;&#20250;&#21457;&#29616;&#19968;&#20010;&#38382;&#39064;&#65292;while&#32467;&#26500;&#24590;&#20040;&#21150;&#65311;&#31572;&#26696;&#20063;&#24456;&#31616;&#21333;&#65292;&#25105;&#20204;&#19981;&#33021;&#29992;while&#20102;&#12290;&#19981;&#36807;&#21363;&#20351;&#27809;&#26377;while&#25105;&#20204;&#36824;&#26159;&#33021;&#25226;CPS&#30340;&#31243;&#24207;&#20889;&#20986;&#26469;&#65292;&#24744;&#21482;&#35201;&#38656;&#35201;&#19981;&#26029;&#22320;&#35810;&#38382;&#33258;&#24049;&#65292;&#8220;&#31243;&#24207;&#25191;&#34892;&#21040;&#29616;&#22312;&#21097;&#19979;&#30340;&#25805;&#20316;&#26159;&#20160;&#20040;&#65311;&#8221;&#23601;&#34892;&#20102;&#12290;&#32780;&#19988;&#23545;&#20110;&#36825;&#26679;&#31616;&#21333;&#30340;&#20363;&#23376;&#65292;&#20854;&#23545;&#24212;&#30340;CPS&#29256;&#26412;&#24182;&#19981;&#31639;&#22826;&#22797;&#26434;&#12290;</p><p>&#25105;&#24076;&#26395;&#24744;&#19981;&#35201;&#39532;&#19978;&#30475;&#19979;&#38754;&#30340;&#31572;&#26696;&#65292;&#35797;&#30528;&#33258;&#24049;&#25226;&#19978;&#38754;&#30340;&#20195;&#30721;&#36716;&#25442;&#25104;CPS&#12290;&#24744;&#20250;&#26377;&#27604;&#36739;&#28145;&#21051;&#30340;&#21360;&#35937;&#20026;&#20160;&#20040;&#20889;&#20986;&#26469;&#30340;&#20195;&#30721;&#21482;&#33021;&#26159;&#19979;&#38754;&#36825;&#31181;&#27169;&#24335;&#12290;</p><pre class="programlisting">/* &#35831;&#19981;&#35201;&#39532;&#19978;&#30475;&#31572;&#26696;&#65281;&#20808;&#33258;&#24049;&#35797;&#35797;&#30475;&#12290; */#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;void print(const char *message,        void (*continuation)(void)) {    printf("%s\n", message);    continuation();    exit(0);}int ans;void finishgame();void guess();void prompt();void finishgame() {    /* do nothing */}void guess() {    int guess;    scanf("%d", &amp;guess);    if (guess &lt; ans)        print("Number too small.", prompt);    else if (guess &gt; ans)        print("Number too big.", prompt);    else        print("Congratulations!", finishgame);}void prompt() {    print("Guess a number [1, 100]: ", guess);}void main() {    ans = random() % 100 + 1;    prompt();}</pre><p>&#24744;&#30340;&#20570;&#27861;&#21644;&#19978;&#38754;&#30340;&#19968;&#26679;&#21527;&#65311;</p><p>&#22914;&#26524;&#25105;&#20204;&#20180;&#32454;&#30475;&#30475;&#36825;&#20010;&#31243;&#24207;&#65292;&#25105;&#20204;&#20250;&#27880;&#24847;&#21040;&#23427;&#26377;&#33267;&#23569;&#19977;&#20010;&#38382;&#39064;&#65306;</p><div class="orderedlist"><ol type="1"><li><p>&#19968;&#37096;&#20998;&#26412;&#22320;&#21464;&#37327;&#21464;&#25104;&#20102;&#20840;&#23616;&#21464;&#37327;&#65292;&#20363;&#22914;&#19978;&#38754;&#30340;ans&#65292;&#22240;&#20026;&#19981;&#27490;&#19968;&#20010;&#20989;&#25968;&#35201;&#29992;&#21040;&#23427;&#65292;&#32780;&#25105;&#20204;&#21448;&#19981;&#33021;&#20351;&#29992;&#26222;&#36890;&#30340;&#21464;&#37327;&#21442;&#25968;&#12290;&#36825;&#26159;&#22240;&#20026;print&#25509;&#21463;&#30340;continuation&#30340;&#23450;&#20041;&#26159;void (*continuation)(void)&#65292;&#20063;&#23601;&#26159;&#19968;&#20010;&#19981;&#25509;&#21463;&#20219;&#20309;&#21442;&#25968;&#65292;&#20063;&#19981;&#36820;&#22238;&#20219;&#20309;&#20540;&#30340;&#20989;&#25968;&#12290;</p></li><li><p>&#25511;&#21046;&#32467;&#26500;&#20363;&#22914;while&#19981;&#33021;&#20351;&#29992;&#20102;&#12290;&#19978;&#38754;&#30340;&#20363;&#23376;&#20013;&#65292;prompt&#21644;guess&#20004;&#20010;&#20989;&#25968;&#20250;&#20114;&#30456;&#20256;&#36882;&#23545;&#26041;&#20026;continuation&#65292;&#20197;&#27492;&#26469;&#27169;&#25311;while&#12290;</p></li><li><p>&#34429;&#28982;&#22312;&#26412;&#20363;&#20013;&#30475;&#19981;&#20986;&#26469;&#65292;&#20294;&#26159;&#22914;&#26524;&#21407;&#31243;&#24207;&#20013;&#26377;&#36882;&#24402;&#21450;&#22238;&#28335;&#30340;&#35805;&#65292;&#37027;&#20040;&#21333;&#21333;&#25226;&#26412;&#22320;&#21464;&#37327;&#21464;&#25104;&#20840;&#23616;&#21464;&#37327;&#23601;&#19981;&#22815;&#20102;&#12290;&#36825;&#26102;&#65292;&#25105;&#20204;&#38656;&#35201;&#33258;&#24049;&#20889;&#19968;&#20010;call stack&#65292;&#20063;&#23601;&#26159;&#65292;&#37325;&#26032;&#21457;&#26126;&#19968;&#20010;compiler&#21487;&#20197;&#33258;&#21160;&#24110;&#25105;&#20204;&#20570;&#30340;&#36718;&#23376;&#12290;&#25105;&#20204;&#21518;&#38754;&#36824;&#20250;&#35814;&#32454;&#35762;&#21040;&#36825;&#20010;&#38382;&#39064;&#12290;</p></li></ol></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch01s01.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="ch01.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ch01s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">1.&nbsp;&#31616;&#21333;&#30340;CPS&#20363;&#23376;&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;3.&nbsp;CPS&#19982;goto</td></tr></table></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -