turbo-c++ https://www.e-learn.cn/tag/turbo-c zh-hans how to alternate colors in a circle, so that circle looks like rotating? https://www.e-learn.cn/topic/4100488 <span>how to alternate colors in a circle, so that circle looks like rotating?</span> <span><span lang="" about="/user/112" typeof="schema:Person" property="schema:name" datatype="">回眸只為那壹抹淺笑</span></span> <span>2021-02-11 16:39:41</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>The expected output should be like this with the colors changing their position as well:</p> <p><img alt="img" class="b-lazy" data-src="https://i.stack.imgur.com/W3qGt.png" data-original="https://i.stack.imgur.com/W3qGt.png" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" /></p> <p><strong>Expected output-:</strong></p> <p>the colors should change their positions in a circle so that it looks like they are moving without changing the position of circle.</p> <p>though my code is written in codeblocks in c/c++, i will be happy to get answers in any other programming languages.</p> <p><strong>my present code</strong></p> <pre class="lang-cpp prettyprint-override"><code>#include&lt;graphics.h&gt; #include&lt;stdlib.h&gt; #include&lt;stdio.h&gt; #include&lt;conio.h&gt; #include&lt;math.h&gt; #include&lt;string.h&gt; #include&lt;iostream&gt; using namespace std; void vvcircle(float xk,float yk,float radius); int i=0; int main() { float xk,yk,radius; int gdriver=DETECT,gmode,errorcode; initgraph(&amp;gdriver,&amp;gmode,"C:\\TURBOC3\\BGI"); // cout&lt;&lt;"enter the value of x, y and radius of circle"&lt;&lt;endl; //cin&gt;&gt;xk&gt;&gt;yk&gt;&gt;radius; vvcircle(200,200,100); getch(); closegraph(); return 0; } void vvcircle(float xk,float yk,float radius) { int color[60]={0,1,2,3,4,5,6,7,8,9}; while(radius&gt;0) { float xo,yo; float P; xo=0.0; yo=radius; P=1-radius; /// vvcircle(200,200,100); for(;xo&lt;=yo;) { putpixel(xo+xk,yo+yk,1); putpixel(yo+xk,xo+yk,1); putpixel(-yo+xk,xo+yk,2); putpixel(xo+xk,-yo+yk,2); putpixel(-yo+xk,-xo+yk,4); putpixel(-xo+xk,-yo+yk,4); putpixel(yo+xk,-xo+yk,4); putpixel(-xo+xk,+yo+yk,4); if(P&lt;0) { xo=xo+1; yo=yo; P=P+2*xo+1; } else { xo=xo+1; yo=yo-1; P=P+(2*xo)-(2*yo)+1; // putpixel(xo,yo,WHITE); } } radius=radius-1; } } </code></pre> <p>Present output-:</p> <p>i get many concentric circles with colors. but i want to move the colors so that it looks like the circle is moving and it is not achieved.</p> <br /><h3>回答1:</h3><br /><p>How about something like this:</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;math.h&gt; void my_circle(int xc,int yc,int r,float a) // center(x,y), radius, animation angle [rad] { const int n=4; // segments count int x,sx,xx,x0,x1,rr=r*r, y,sy,yy,y0,y1,i, dx[n+1],dy[n+1], // segments edges direction vectors c[n]={5,1,2,3}; // segments colors float da=2.0*M_PI/float(n); // BBOX x0=xc-r; x1=xc+r; y0=yc-r; y1=yc+r; // compute segments for (i=0;i&lt;=n;i++,a+=da) { dx[i]=100.0*cos(a); dy[i]=100.0*sin(a); } // all pixels in BBOX for (sx=x0,x=sx-xc;sx&lt;=x1;sx++,x++){ xx=x*x; for (sy=y0,y=sy-yc;sy&lt;=y1;sy++,y++){ yy=y*y; // outside circle? if (xx+yy&gt;rr) continue; // compute segment for (i=0;i&lt;n;i++) if ((x*dy[i ])-(y*dx[i ])&gt;=0) if ((x*dy[i+1])-(y*dx[i+1])&lt;=0) break; // render putpixel(sx,sy,c[i]); }} } </code></pre> <p>It simply loop through all pixels of outscribed square to your circle, determines if pixel is inside and then detect which segment it is in and color it with segments color.</p> <p>The segments are described by direction vectors from circle center towards the segments edges. So if pixel is inside it mean its CW to one edge and CCW to the other so in 2D inspecting z coordinate of the cross product between vector to pixel and vectors to edges will tell if the pixel is in or not ...</p> <p>As you can see I did not use floating point math in the rendering it self, its needed only to compute the segments edge vectors prior rendering...</p> <p>I used standard 256 color VGA palette (not sure what BGI uses I expect 16 col) so the colors might be different on your platform here preview:</p> <p></p> <p>The noise is caused by my GIF capturing tool dithering the render itself is clean ...</p> <p>Do not forget to call the <code>my_circle</code> repeatedly with changing angle ...</p> <p><strong>PS.</strong> I encoded this in BDS2006 without BGI so in different compiler there might be some minor syntax problem related to used language quirks...</p> <p>I faked the <code>putpixel</code> with this:</p> <pre class="lang-cpp prettyprint-override"><code>void putpixel(int x,int y,BYTE c) { static const DWORD pal[256]= { 0x00000000,0x000000A8,0x0000A800,0x0000A8A8,0x00A80000,0x00A800A8,0x00A85400,0x00A8A8A8, 0x00545454,0x005454FC,0x0054FC54,0x0054FCFC,0x00FC5454,0x00FC54FC,0x00FCFC54,0x00FCFCFC, 0x00000000,0x00101010,0x00202020,0x00343434,0x00444444,0x00545454,0x00646464,0x00747474, 0x00888888,0x00989898,0x00A8A8A8,0x00B8B8B8,0x00C8C8C8,0x00DCDCDC,0x00ECECEC,0x00FCFCFC, 0x000000FC,0x004000FC,0x008000FC,0x00BC00FC,0x00FC00FC,0x00FC00BC,0x00FC0080,0x00FC0040, 0x00FC0000,0x00FC4000,0x00FC8000,0x00FCBC00,0x00FCFC00,0x00BCFC00,0x0080FC00,0x0040FC00, 0x0000FC00,0x0000FC40,0x0000FC80,0x0000FCBC,0x0000FCFC,0x0000BCFC,0x000080FC,0x000040FC, 0x008080FC,0x009C80FC,0x00BC80FC,0x00DC80FC,0x00FC80FC,0x00FC80DC,0x00FC80BC,0x00FC809C, 0x00FC8080,0x00FC9C80,0x00FCBC80,0x00FCDC80,0x00FCFC80,0x00DCFC80,0x00BCFC80,0x009CFC80, 0x0080FC80,0x0080FC9C,0x0080FCBC,0x0080FCDC,0x0080FCFC,0x0080DCFC,0x0080BCFC,0x00809CFC, 0x00B8B8FC,0x00C8B8FC,0x00DCB8FC,0x00ECB8FC,0x00FCB8FC,0x00FCB8EC,0x00FCB8DC,0x00FCB8C8, 0x00FCB8B8,0x00FCC8B8,0x00FCDCB8,0x00FCECB8,0x00FCFCB8,0x00ECFCB8,0x00DCFCB8,0x00C8FCB8, 0x00B8FCB8,0x00B8FCC8,0x00B8FCDC,0x00B8FCEC,0x00B8FCFC,0x00B8ECFC,0x00B8DCFC,0x00B8C8FC, 0x00000070,0x001C0070,0x00380070,0x00540070,0x00700070,0x00700054,0x00700038,0x0070001C, 0x00700000,0x00701C00,0x00703800,0x00705400,0x00707000,0x00547000,0x00387000,0x001C7000, 0x00007000,0x0000701C,0x00007038,0x00007054,0x00007070,0x00005470,0x00003870,0x00001C70, 0x00383870,0x00443870,0x00543870,0x00603870,0x00703870,0x00703860,0x00703854,0x00703844, 0x00703838,0x00704438,0x00705438,0x00706038,0x00707038,0x00607038,0x00547038,0x00447038, 0x00387038,0x00387044,0x00387054,0x00387060,0x00387070,0x00386070,0x00385470,0x00384470, 0x00505070,0x00585070,0x00605070,0x00685070,0x00705070,0x00705068,0x00705060,0x00705058, 0x00705050,0x00705850,0x00706050,0x00706850,0x00707050,0x00687050,0x00607050,0x00587050, 0x00507050,0x00507058,0x00507060,0x00507068,0x00507070,0x00506870,0x00506070,0x00505870, 0x00000040,0x00100040,0x00200040,0x00300040,0x00400040,0x00400030,0x00400020,0x00400010, 0x00400000,0x00401000,0x00402000,0x00403000,0x00404000,0x00304000,0x00204000,0x00104000, 0x00004000,0x00004010,0x00004020,0x00004030,0x00004040,0x00003040,0x00002040,0x00001040, 0x00202040,0x00282040,0x00302040,0x00382040,0x00402040,0x00402038,0x00402030,0x00402028, 0x00402020,0x00402820,0x00403020,0x00403820,0x00404020,0x00384020,0x00304020,0x00284020, 0x00204020,0x00204028,0x00204030,0x00204038,0x00204040,0x00203840,0x00203040,0x00202840, 0x002C2C40,0x00302C40,0x00342C40,0x003C2C40,0x00402C40,0x00402C3C,0x00402C34,0x00402C30, 0x00402C2C,0x0040302C,0x0040342C,0x00403C2C,0x0040402C,0x003C402C,0x0034402C,0x0030402C, 0x002C402C,0x002C4030,0x002C4034,0x002C403C,0x002C4040,0x002C3C40,0x002C3440,0x002C3040, 0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000, }; if ((x&lt;0)||(x&gt;=Main-&gt;xs)) return; if ((y&lt;0)||(y&gt;=Main-&gt;ys)) return; Main-&gt;pyx[y][x]=pal[c]; } </code></pre> <p>Where <code>Main-&gt;xs, Main-&gt;ys</code> is my window resolution and <code>Main-&gt;pyx</code> is direct pixel acces to its canvas for more info see:</p> <ul><li>Graphics rendering: (#4 GDI Bitmap)</li> </ul><br /><br /><p>来源:<code>https://stackoverflow.com/questions/60100758/how-to-alternate-colors-in-a-circle-so-that-circle-looks-like-rotating</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/graphics2d" hreflang="zh-hans">graphics2d</a></div> <div class="field--item"><a href="/tag/game-development" hreflang="zh-hans">game-development</a></div> <div class="field--item"><a href="/tag/turbo-c" hreflang="zh-hans">turbo-c++</a></div> </div> </div> Thu, 11 Feb 2021 08:39:41 +0000 回眸只為那壹抹淺笑 4100488 at https://www.e-learn.cn Why should I not #include <bits/stdc++.h>? https://www.e-learn.cn/topic/4036947 <span>Why should I not #include &lt;bits/stdc++.h&gt;?</span> <span><span lang="" about="/user/154" typeof="schema:Person" property="schema:name" datatype="">主宰稳场</span></span> <span>2021-01-29 08:42:33</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I posted a question with my code whose only <code>#include</code> directive was the following:</p> <pre><code>#include &lt;bits/stdc++.h&gt; </code></pre> <p>My teacher told me to do this, but in the comments section I was informed that I shouldn't.</p> <p>Why?</p> <br /><h3>回答1:</h3><br /><p>Including <code>&lt;bits/stdc++.h&gt;</code> appears to be an increasingly common thing to see on Stack Overflow, perhaps something newly added to a national curriculum in the current academic year.</p> <p>I imagine the advantages are vaguely given thus:</p> <ul><li>You only need write one <code>#include</code> line</li> <li>You do not need to look up which standard header everything is in</li> </ul><p>Unfortunately, this is a lazy hack, naming a GCC internal header directly instead of individual standard headers like <code>&lt;string&gt;</code>, <code>&lt;iostream&gt;</code> and <code>&lt;vector&gt;</code>. It ruins portability and fosters terrible habits.</p> <p>The disadvantages include:</p> <ul><li>It will probably only work on that compiler</li> <li>You have no idea what it'll do when you use it, because its contents are not set by a standard</li> <li>Even just upgrading your compiler to its own next version may break your program</li> <li>Every single standard header must be parsed and compiled along with your source code, which is slow and results in a bulky executable under certain compilation settings</li> </ul><p><strong>Don't do it!</strong></p> <hr /><p>More information:</p> <ul><li>#include &lt;bits/stdc++.h&gt; with visual studio does not compile</li> <li>How does #include &lt;bits/stdc++.h&gt; work in C++?</li> </ul><p>Example of why Quora is bad:</p> <ul><li>Is it good practice to use #include &lt;bits/stdc++.h&gt; in programming contests instead of listing a lot of includes?</li> </ul><br /><br /><br /><h3>回答2:</h3><br /><p>Why? Because it is used as if it was supposed to be a C++ standard header, but no standard mentions it. So your code is non-portable by construction. You won't find any documentation for it on cppreference. So it might as well not exist. It's a figment of someone's imagination :)</p> <p>I have discovered - to my horror and disbelief - that there is a well-known tutorial site where <strong>every C++ example seems to include this header</strong>. The world is mad. That's the proof.</p> <hr /><p><em>To anyone writing such "tutorials"</em></p> <p>Please stop using this header. Forget about it. Don't propagate this insanity. If you're unwilling to understand why doing this is <strong>Wrong</strong>, take my word for it. I'm not OK being treated as a figure of authority on anything at all, and I'm probably full of it half the time, but I'll make an exception in this one case only. I claim that I know what I'm talking about here. Take me on my word. I implore you.</p> <p>P.S. I can well imagine the abominable "teaching standard" where this wicked idea might have taken place, and the circumstances that led to it. Just because there seemed to be a practical need for it doesn't make it acceptable - not even in retrospect.</p> <p>P.P.S. No, there was no practical need for it. There aren't that many C++ standard headers, and they are well documented. If you teach, you're doing your students a disservice by adding such "magic". Producing programmers with a magical mindset is the last thing we want. If you need to offer students a subset of C++ to make their life easier, just produce a handout with the short list of headers applicable to the course you teach, and with concise documentation for the library constructs you expect the students to use.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>There's a Stack Exchange site called Programming Puzzles &amp; Code Golf. The <em>programming puzzles</em> on that site fit this definition of puzzle: </p> <blockquote> <p>a toy, problem, or other contrivance designed to amuse by presenting difficulties to be solved by ingenuity or patient effort.</p> </blockquote> <p>They are designed to amuse, and not in the way that a working programmer might be amused by a real-world problem encountered in their daily work.</p> <p>Code Golf is "a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that implements a certain algorithm." In the answers on the PP&amp;CG site, you'll see people specify the number of bytes in their answers. When they find a way to shave off a few bytes, they'll strike out the original number and record the new one.</p> <p>As you might expect, code golfing rewards extreme programming language abuse. One-letter variable names. No whitespace. Creative use of library functions. Undocumented features. Nonstandard programming practices. Appalling hacks.</p> <p>If a programmer submitted a pull request at work containing golf-style code, it would be rejected. Their co-workers would laugh at them. Their manager would drop by their desk for a chat. Even so, programmers amuse themselves by submitting answers to PP&amp;CG.</p> <p>What does this have to do with <code>stdc++.h</code>? As others have pointed out, using it is lazy. It's non-portable, so you don't know if it will work on your compiler or the next version of your compiler. It fosters bad habits. It's non-standard, so your program's behavior may differ from what you expect. It may increase compile time and executable size.</p> <p>These are all valid and correct objections. So why would anyone use this monstrosity?</p> <p>It turns out that some people like <em>programming puzzles</em> without the <em>code golf</em>. They get together and compete at events like ACM-ICPC, Google Code Jam, and Facebook Hacker Cup, or on sites like Topcoder and Codeforces. Their rank is based on program correctness, execution speed, and how fast they submit a solution. To maximize execution speed, many participants use C++. To maximize coding speed, some of them use <code>stdc++.h</code>.</p> <p>Is this is a good idea? Let's check the list of disadvantages. Portability? It doesn't matter since these coding events use a specific compiler version that contestants know in advance. Standards compliance? Not relevant for a block of code whose useful life is less than one hour. Compile time and executable size? These aren't part of the contest's scoring rubric.</p> <p>So we're left with bad habits. This is a valid objection. By using this header file, contestants are avoiding the chance to learn which standard header file defines the functionality they're using in their program. When they're writing real-world code (and not using <code>stdc++.h</code>) they'll have to spend time looking up this information, which means they'll be less productive. That's the downside of practicing with <code>stdc++.h</code>.</p> <p>This raises the question of why it's worth taking part in competitive programming at all if it encourages bad habits like using <code>stdc++.h</code> and violating other coding standards. One answer is that people do it for the same reason they post programs on PP&amp;CG: some programmers find it enjoyable to use their coding skills in a game-like context.</p> <p>So the question of whether to use <code>stdc++.h</code> comes down to whether the coding speed benefits in a programming contest outweigh the bad habits that one might develop by using it.</p> <p>This question asks: "Why should I not #include <code>&lt;bits/stdc++.h&gt;</code>?" I realize that it was asked and answered to make a point, and the accepted answer is intended to be the One True Answer to this question. But the question isn't "Why should I not #include <code>&lt;bits/stdc++.h&gt;</code> in production code?" Therefore, I think it's reasonable to consider other scenarios where the answer may be different.</p> <br /><br /><br /><h3>回答4:</h3><br /><p>From N4606, Working Draft, Standard for Programming Language C++ :</p> <p>17.6.1.2 Headers [headers]</p> <ol><li><p>Each element of the C++ standard library is declared or defined (as appropriate) in a header.</p></li> <li><p>The C++ standard library provides 61 C++ library headers, as shown in Table 14.</p></li> </ol><p>Table 14 — C++ library headers</p> <pre><code>&lt;algorithm&gt; &lt;future&gt; &lt;numeric&gt; &lt;strstream&gt; &lt;any&gt; &lt;initializer_list&gt; &lt;optional&gt; &lt;system_error&gt; &lt;array&gt; &lt;iomanip&gt; &lt;ostream&gt; &lt;thread&gt; &lt;atomic&gt; &lt;ios&gt; &lt;queue&gt; &lt;tuple&gt; &lt;bitset&gt; &lt;iosfwd&gt; &lt;random&gt; &lt;type_traits&gt; &lt;chrono&gt; &lt;iostream&gt; &lt;ratio&gt; &lt;typeindex&gt; &lt;codecvt&gt; &lt;istream&gt; &lt;regex&gt; &lt;typeinfo&gt; &lt;complex&gt; &lt;iterator&gt; &lt;scoped_allocator&gt; &lt;unordered_map&gt; &lt;condition_variable&gt; &lt;limits&gt; &lt;set&gt; &lt;unordered_set&gt; &lt;deque&gt; &lt;list&gt; &lt;shared_mutex&gt; &lt;utility&gt; &lt;exception&gt; &lt;locale&gt; &lt;sstream&gt; &lt;valarray&gt; &lt;execution&gt; &lt;map&gt; &lt;stack&gt; &lt;variant&gt; &lt;filesystem&gt; &lt;memory&gt; &lt;stdexcept&gt; &lt;vector&gt; &lt;forward_list&gt; &lt;memory_resorce&gt; &lt;streambuf&gt; &lt;fstream&gt; &lt;mutex&gt; &lt;string&gt; &lt;functional&gt; &lt;new&gt; &lt;string_view&gt; </code></pre> <p>There's no &lt;bits/stdc++.h&gt; there. This is not surprising, since &lt;bits/...&gt; headers are implementation detail, and usually carry a warning:</p> <pre><code>* This is an internal header file, included by other library headers. * Do not attempt to use it directly. </code></pre> <p>&lt;bits/stdc++.h&gt; also carries a warning:</p> <pre><code>* This is an implementation file for a precompiled header. </code></pre> <br /><br /><br /><h3>回答5:</h3><br /><p>The reason we do not use:</p> <pre><code>#include &lt;bits/stdc++.h&gt; </code></pre> <p>is because of effiency. Let me make an analogy: For those of you who know Java: If you asked your instructor if the following was a good idea, unless they are a bad instructor they would say no:</p> <pre><code>import java.*.* </code></pre> <p>The #include... thing does the same thing basically... That's not the only reason not to use it, but it is one of the major reasons not to use it. For a real life analogy: Imagine you had a library and you wanted to borrow a couple of books from the library, would you relocate the entire library next to your house?? It would be expensive and ineffiecient. If you only need 5 books, well then only take out 5... Not the whole library.....</p> <pre><code>#include &lt;bits/stdc++.h&gt; </code></pre> <p>Looks convienent to the program look I only need to type one include statement and it works, same thing with moving a whole library, look I only need to move one whole library instead of 5 books, one by one. Looks convienent to you that is, for the person who actually has to do the moving?? Not so much, and guess what in C++ the person doing the moving will be your computer... The computer will not enjoy moving the entire library for every source file you write :).....</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/62812376/difficulty-changing-c17-to-c11-on-vscode</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/portability" hreflang="zh-hans">portability</a></div> <div class="field--item"><a href="/tag/turbo-c" hreflang="zh-hans">turbo-c++</a></div> <div class="field--item"><a href="/tag/c-faq" hreflang="zh-hans">c++-faq</a></div> <div class="field--item"><a href="/tag/implementation-defined-behavior" hreflang="zh-hans">implementation-defined-behavior</a></div> </div> </div> Fri, 29 Jan 2021 00:42:33 +0000 主宰稳场 4036947 at https://www.e-learn.cn Replacement to getch based code block in cpp https://www.e-learn.cn/topic/4030205 <span>Replacement to getch based code block in cpp</span> <span><span lang="" about="/user/77" typeof="schema:Person" property="schema:name" datatype="">只愿长相守</span></span> <span>2021-01-28 19:52:42</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I stumbled upon a code from 2016 written in the Turbo C++ IDE in windows It was to accept passwords</p> <pre><code>char pass; for (length = 0;;) { pass=getch(); if (pass == 13) { break; } if ((pass &gt;= 'A' &amp;&amp; pass &lt;= 'Z') || (pass &gt;= 'a' &amp;&amp; pass &lt;= 'z') || (pass &gt;= '0' &amp;&amp; pass &lt;= '9') || (pass == '!' || '@' || '#' || '$' || '%' || '^' || '&amp;' || '*' || '(' || ')')) { str[length] = pass; ++length; cout &lt;&lt; "#"; } } </code></pre> <p>is there any replacement for this without the getch method for <strong>linux</strong> to show output like this ******** ? I tried scanf but that didnt work cause it took the whole input first and gave the output later</p> <br /><h3>回答1:</h3><br /><p>There is no standard replacement in C++.</p> <p>Linux (and similar systems such as BSD), there is a legacy function <code>getpass</code> whose use is not recommended. I mention it because the documentation of that function in glibc suggests following:</p> <blockquote> <p>This precise set of operations may not suit all possible situations. In this case, it is recommended that users write their own getpass substitute. For instance, a very simple substitute is as follows:</p> <pre><code>#include &lt;termios.h&gt; #include &lt;stdio.h&gt; ssize_t my_getpass (char **lineptr, size_t *n, FILE *stream) { struct termios old, new; int nread; /* Turn echoing off and fail if we can’t. */ if (tcgetattr (fileno (stream), &amp;old) != 0) return -1; new = old; new.c_lflag &amp;= ~ECHO; if (tcsetattr (fileno (stream), TCSAFLUSH, &amp;new) != 0) return -1; /* Read the passphrase */ nread = getline (lineptr, n, stream); /* Restore terminal. */ (void) tcsetattr (fileno (stream), TCSAFLUSH, &amp;old); return nread; } </code></pre> </blockquote> <p>Note that the example is written in C, and is not valid C++. Small changes are required, in particular there is a C++ keyword <code>new</code> being used as a variable name. Simply rename it to something else.</p> <p>Also note that the behaviour is to not echo at all, rather than echo <code>*</code>. This is conventional in POSIX systems, and safer because the it does not reveal the length of the passphrase.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/63038561/replacement-to-getch-based-code-block-in-cpp</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/linux" hreflang="zh-hans">Linux</a></div> <div class="field--item"><a href="/tag/g" hreflang="zh-hans">g++</a></div> <div class="field--item"><a href="/tag/c17" hreflang="zh-hans">c++17</a></div> <div class="field--item"><a href="/tag/turbo-c" hreflang="zh-hans">turbo-c++</a></div> </div> </div> Thu, 28 Jan 2021 11:52:42 +0000 只愿长相守 4030205 at https://www.e-learn.cn String problems with Turbo C++ https://www.e-learn.cn/topic/3646566 <span>String problems with Turbo C++</span> <span><span lang="" about="/user/189" typeof="schema:Person" property="schema:name" datatype="">怎甘沉沦</span></span> <span>2020-05-16 02:05:30</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I need to get code work in Turbo C++. But the data type <code>string</code> does not work; using namespace <code>std</code> gives Compiler Error and I can't use <code>string</code> without it. Even <code>std::string</code> does not work.</p> <p>It works perfectly fine in Code::Blocks but I want it to work in Turbo C++. I know Turbo is a very old compiler and I should be using the new ones. But it is a college project which has to be done in Turbo C++. Are there any ways to make it work in Turbo C++?</p> <br /><h3>回答1:</h3><br /><p>This kind of depends on which version of Turbo C++ you have. Some software archeology:</p> <p>Ancient DOS versions up to 3.1 didn't support STL well, nor did they support <code>#include &lt;string&gt;</code>. They used the pre-standard include formats with .h extensions: <code>#include &lt;string.h&gt;</code> etc. Try to add a <code>.h</code> and you might get lucky.</p> <p>Somewhere around version 4 or 5.0 they started to support <code>#include &lt;string&gt;</code> header formats and most of STL. These were still pre-standard compilers.</p> <p>STL support remained questionable in earlier versions of Borland Builder, until somewhere around Builder 5. That should be version 5.5 or so of the BCC compiler.</p> <p>The RAD tool called Turbo C++, released somewhere around 2005, should have full support for C++98.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>Turbo C++ doesn't support namespaces.</p> <p>I think you need to include <code>cstring.h</code> and not use any namespaces or even the <code>using</code> directive. </p> <pre><code>#include &lt;cstring.h&gt; </code></pre> <p>And I don't think it supports templates either.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>There is absolutely no way whatsoever to make modern C++ code work in Turbo C++ as is. Lots of it needs to be rewritten.</p> <p>There is nothing std:: in turbo c++. There are no namesoaces. There are no templates. There is very little of what we know as the standard library. Basically you have to unlearn most of what you know about C++. Classes and functiins mostly do work. Iostreams may somewhat work if you <code>#include &lt;iostreams.h&gt;</code> (note the .h) and omit <code>std::</code>. Otherwise you are pretty much confined to the C standard library. </p> <p>If you need a string class, you probably will have to make your own. </p> <p>Tread carefully, read the built-in help, examine the included example programs, and you might be able to pull it off.</p> <p>Note, later versions of the product (not called Turbo C++ IIRC, but rather Borland C++ or Borland Builder) have improved support for C++98, including the standard library.</p> <br /><br /><br /><h3>回答4:</h3><br /><p>I had the same problem then I realized that I had forgotten <code>using namespace std</code> That solved everything.</p> <br /><br /><br /><h3>回答5:</h3><br /><p>In place of String you can use the character array. For example, we need to declare the variable str as a string. It can be done simply as:</p> <pre><code>char a[10]; // the 10 is the size of the array. </code></pre> <p>A seperate header file is included to use the library functions.</p> <br /><br /><br /><h3>回答6:</h3><br /><p>Okay, after a lot of hassle, I found the way. Unfortunately, you can't use <code>string</code> and other such data types as they were not even implemented at that time. You need to do what used to be done before. Use <code>char</code> array instead of <code>string</code> and create functions related to that.<br /><br /> Now <code>char</code> array has a lot of limitations and problems, that's the reason <code>string</code> was implemented. But you have to write <code>char</code> array functions the same way <code>string</code> was written from scratch.<br /> If you want to compare or copy two char arrays, you have to loop and compare them. It will be little complicated, but that's the best way which worked for me.<br /></p> <p>I can give some sample code for a certain task if needed.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/40398720/string-problems-with-turbo-c</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/string" hreflang="zh-hans">string</a></div> <div class="field--item"><a href="/tag/turbo-c" hreflang="zh-hans">turbo-c++</a></div> </div> </div> Fri, 15 May 2020 18:05:30 +0000 怎甘沉沦 3646566 at https://www.e-learn.cn String problems with Turbo C++ https://www.e-learn.cn/topic/3646562 <span>String problems with Turbo C++</span> <span><span lang="" about="/user/127" typeof="schema:Person" property="schema:name" datatype="">我怕爱的太早我们不能终老</span></span> <span>2020-05-16 02:02:10</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I need to get code work in Turbo C++. But the data type <code>string</code> does not work; using namespace <code>std</code> gives Compiler Error and I can't use <code>string</code> without it. Even <code>std::string</code> does not work.</p> <p>It works perfectly fine in Code::Blocks but I want it to work in Turbo C++. I know Turbo is a very old compiler and I should be using the new ones. But it is a college project which has to be done in Turbo C++. Are there any ways to make it work in Turbo C++?</p> <br /><h3>回答1:</h3><br /><p>This kind of depends on which version of Turbo C++ you have. Some software archeology:</p> <p>Ancient DOS versions up to 3.1 didn't support STL well, nor did they support <code>#include &lt;string&gt;</code>. They used the pre-standard include formats with .h extensions: <code>#include &lt;string.h&gt;</code> etc. Try to add a <code>.h</code> and you might get lucky.</p> <p>Somewhere around version 4 or 5.0 they started to support <code>#include &lt;string&gt;</code> header formats and most of STL. These were still pre-standard compilers.</p> <p>STL support remained questionable in earlier versions of Borland Builder, until somewhere around Builder 5. That should be version 5.5 or so of the BCC compiler.</p> <p>The RAD tool called Turbo C++, released somewhere around 2005, should have full support for C++98.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>Turbo C++ doesn't support namespaces.</p> <p>I think you need to include <code>cstring.h</code> and not use any namespaces or even the <code>using</code> directive. </p> <pre><code>#include &lt;cstring.h&gt; </code></pre> <p>And I don't think it supports templates either.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>There is absolutely no way whatsoever to make modern C++ code work in Turbo C++ as is. Lots of it needs to be rewritten.</p> <p>There is nothing std:: in turbo c++. There are no namesoaces. There are no templates. There is very little of what we know as the standard library. Basically you have to unlearn most of what you know about C++. Classes and functiins mostly do work. Iostreams may somewhat work if you <code>#include &lt;iostreams.h&gt;</code> (note the .h) and omit <code>std::</code>. Otherwise you are pretty much confined to the C standard library. </p> <p>If you need a string class, you probably will have to make your own. </p> <p>Tread carefully, read the built-in help, examine the included example programs, and you might be able to pull it off.</p> <p>Note, later versions of the product (not called Turbo C++ IIRC, but rather Borland C++ or Borland Builder) have improved support for C++98, including the standard library.</p> <br /><br /><br /><h3>回答4:</h3><br /><p>I had the same problem then I realized that I had forgotten <code>using namespace std</code> That solved everything.</p> <br /><br /><br /><h3>回答5:</h3><br /><p>In place of String you can use the character array. For example, we need to declare the variable str as a string. It can be done simply as:</p> <pre><code>char a[10]; // the 10 is the size of the array. </code></pre> <p>A seperate header file is included to use the library functions.</p> <br /><br /><br /><h3>回答6:</h3><br /><p>Okay, after a lot of hassle, I found the way. Unfortunately, you can't use <code>string</code> and other such data types as they were not even implemented at that time. You need to do what used to be done before. Use <code>char</code> array instead of <code>string</code> and create functions related to that.<br /><br /> Now <code>char</code> array has a lot of limitations and problems, that's the reason <code>string</code> was implemented. But you have to write <code>char</code> array functions the same way <code>string</code> was written from scratch.<br /> If you want to compare or copy two char arrays, you have to loop and compare them. It will be little complicated, but that's the best way which worked for me.<br /></p> <p>I can give some sample code for a certain task if needed.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/40398720/string-problems-with-turbo-c</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/string" hreflang="zh-hans">string</a></div> <div class="field--item"><a href="/tag/turbo-c" hreflang="zh-hans">turbo-c++</a></div> </div> </div> Fri, 15 May 2020 18:02:10 +0000 我怕爱的太早我们不能终老 3646562 at https://www.e-learn.cn Run Turbo C++ in Freedos https://www.e-learn.cn/topic/3314237 <span>Run Turbo C++ in Freedos</span> <span><span lang="" about="/user/103" typeof="schema:Person" property="schema:name" datatype="">ε祈祈猫儿з</span></span> <span>2020-01-26 04:01:10</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I want to <strong>run Turbo C++ in FreeDos</strong>. I used to run it in DosBox but now I decided to test the FreeDos in my desktop PC.</p> <p>But every time I try run it the cursor blinks and nothing happens (after I enter the command and press enter) , the prompt does not return( C:&gt; ) and I have to restart my system by pressing the power button. My question is:</p> <ol><li><p>How can I actually run it? Does it require extra drivers or files?</p></li> <li><p>If it cannot be run, Do you any other C++ IDE? Maybe your own IDE?</p></li> </ol><p><em>P.S. I have installed FreeDos using the freedos option of Rufus.</em></p> <br /><h3>回答1:</h3><br /><p><strong>FreeDOS</strong> is not <strong>MS-DOS</strong> so incompatibilities are to be expected. However I did not come into contact with <strong>FreeDOS</strong> but here are some hints (from <strong>MS-DOS</strong> so they should more or less apply to <strong>FreeDOS</strong> as well).</p> <ol><li><p><strong>Without <code>himem.sys</code> you have no high memory</strong></p> <p>so you got just 640 KByte instead of 1 MByte for everything. That is not much as some programs requires even 540 KByte...</p></li> <li><p><strong>without <code>Emm386.exe</code> or <code>QEMM386</code> you got no XMS memory</strong></p> <p>If my memory serves well than also the <strong>DPMI</strong> drivers will not work which turbo needs.</p></li> <li><p><strong>without proper <code>autoexec.bat</code> and <code>config sys</code> nothing works</strong></p> <p>you do not have paths, device drivers (mouse,<strong>CDROM</strong>,etc.), and sets (like sound card). If you do not know what to put in them copy them from <strong>DOSBOX</strong> (minus the <strong>DOSBOX</strong> mount lines). No paths usually also means no <code>command.com</code> so you got no shell so no commands would work</p></li> <li><p><strong>what file system you use?</strong></p> <p>I would not go beond <strong>FAT16</strong> for older DOS. Use <strong>FAT32</strong> only for newer <strong>MS-DOS</strong> versions (from win98). Do not use <strong>NTFS</strong> or <strong>EXT</strong> even if you got drivers those caused problems all the time...</p></li> <li><p><strong>Computer speed</strong></p> <p>many <strong>DOS</strong> programs where based on Borlands <strong>CRT</strong> library. That caused a lot of problems later on as computers speed-up. If I remember correctly there was this <code>runtime error 200</code> caused by it. To remedy such programs you can do 2 things. Patch or recompile the CRT lib in them (there are some fixes out there) or go to BIOS and turn off <strong>CPU CACHEs</strong> (L1,L2..). That will turn your modern computer into really fast 386 That should still pass the CRT init. Otherwise the timing is off and programs crash/hangs etc ... The <strong>DOSBOX</strong> emulator is designed to run programs at the original speeds instead of as fast as possible speed, so no further adjustment should be necessary if using that.</p></li> <li><p><strong>Memory</strong></p> <p>The old <strong>DOS</strong> can not handle more than 64 MByte and some programs have problems even with that. The safest way is to limit the available memory to 32 MByte. I usually did it using <strong>RAMDISC</strong> and or <strong>SMARTDRIVE</strong> buffers so only 32 MByte of memory is left for the <strong>DOS</strong> programs. (You can use the <code>mem</code> command to see what is your status if you see negative numbers you are crossing barriers and need to increase usage of <strong>SMARTDRIVE</strong> or <strong>RAMDISK</strong>).</p> <p>Do not forget to save the <strong>SMARTDRIVE</strong> buffers time to time and before exit to avoid loss of data if reset or crash/hang/freeze occur... It is a good idea to use <strong>Volcov commander</strong> (or Norton commander) with predefined menu for such operations. You can even associate file extentions with viewers editors and <strong>IDE</strong>s with it. So if you execute <code>asm</code> or <code>cpp</code> it got compiled or opened in your prefered <strong>IDE</strong> end so on...</p></li> </ol><p>Here example of <code>autoexec.bat</code> and <code>config.sys</code> (taken from my ancient <strong>MS-DOS</strong> rescue disc):</p> <p><strong>Autoexec.bat</strong></p> <pre><code>@echo off PROMPT $P$G PATH e:\rescue;e:\rescue\dos98;e:\rescue\vcnew;e:\rescue\pack;e:\rescue\views e: cd rescue SET TEMP=e:\rescue\temp SET BLASTER=A220 I5 D1 H5 P330 E620 T6 SET SOUND=e:\rescue\SB16 SET MIDI=SYNTH:1 MAP:E MODE:0 e:\rescue\SB16\DIAGNOSE /S e:\rescue\SB16\AWEUTIL /S e:\rescue\SB16\MIXERSET /P /Q goto %config% :a :b :c :d :e lh gmouse LH vc </code></pre> <p><strong>Config.sys</strong></p> <pre><code>;devicehigh=e:\rescue\dos98\interlnk.exe shell e:\rescue\dos98\command.com e:\rescue\dos98 /P lastdrive=M files=50 buffers=8 stacks=0,0 DOS=HIGH,UMB [menu] menuitem=a,QEMM menuitem=b,EMM EMS menuitem=c,EMM NOEMS menuitem=d,STANDART menuitem=e,RAMDISK 32MB menudefault=d,1 [a] device=e:\rescue\dos98\qemm386.sys RAM BE:N [b] device=e:\rescue\dos98\himem.sys devicehigh=e:\rescue\dos98\emm386.exe rammax [c] device=e:\rescue\dos98\himem.sys devicehigh=e:\rescue\dos98\emm386.exe noems [d] device=e:\rescue\dos98\himem.sys [e] device=e:\rescue\dos98\himem.sys devicehigh=e:\rescue\dos98\ramdrive.sys 32767 /e </code></pre> <p>It has boot menu where you can select what configuration you want. Just change the drive letters and paths. Beware <code>:a,:b,...</code> and <code>[a],[b],...</code> are not drive letters those are menu entries. </p> <p><code>shell</code> is the command.com location. the <code>gmouse</code> is mouse driver (not part of <strong>DOS</strong> you need to download it), <code>vc</code> is Volcov commander filemanager (you do not really need it and also it is not part of <strong>DOS</strong> but having it really is a good idea.) You can ignore the SB16 and sound blaster settings as it is high unlikely you got the same sound card or even fully SB compatible without any emulation/driver present.</p> <p><code>e:\rescue\dos98\</code> is location of my <strong>MS-DOS</strong> commands so replace it with your location.</p> <p>The <code>cd</code> command changes directory, and <code>lh</code> loads into high memory instead of base memory to preserve Base memory.</p> <p>How ever if I where you to avoid compatibility problems I would try to obtain <strong>MS-DOS 6.22</strong>.</p> <p>So my bet is you got any combination of all the issues mentioned rendering your OS inoperable.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/46505817/run-turbo-c-in-freedos</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/ide" hreflang="zh-hans">ide</a></div> <div class="field--item"><a href="/tag/dos" hreflang="zh-hans">dos</a></div> <div class="field--item"><a href="/tag/turbo-c" hreflang="zh-hans">turbo-c++</a></div> </div> </div> Sat, 25 Jan 2020 20:01:10 +0000 ε祈祈猫儿з 3314237 at https://www.e-learn.cn want to create diamond shape using while in C https://www.e-learn.cn/topic/3211432 <span>want to create diamond shape using while in C</span> <span><span lang="" about="/user/112" typeof="schema:Person" property="schema:name" datatype="">回眸只為那壹抹淺笑</span></span> <span>2020-01-14 07:11:27</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>i have created the diamond using for loop but i am not able to convert it into While.</p> <p>can you please help me to acheive the same goal using While loop. i tried while loop few times but it's running infinite time. </p> <pre><code>#include&lt;stdio.h&gt; #include&lt;conio.h&gt; int main() { clrscr(); int i, j, k; for(i=1;i&lt;=2;i++) { for(j=i;j&lt;5;j++) { printf(" "); } for(k=1;k&lt;(i*2);k++){ printf("*"); } printf("\n"); } for(i=3;i&gt;=1;i--){ for(j=5;j&gt;i;j--) { printf(" "); } for(k=1;k&lt;(i*2);k++) { printf("*"); } printf("\n"); } getch(); } </code></pre> <br /><h3>回答1:</h3><br /><p>It would help to see your code for your implementation of the while loop to see what's wrong, but the general solution for converting a for loop to a while loop is this:</p> <pre><code>for(i=1;i&lt;=2;i++) { /*your code*/ } </code></pre> <p>becomes</p> <pre><code>i = 1; while(i&lt;=2) { /*your code*/ i++; } </code></pre> <p>Make sure your iterators and decrementors are in the right places.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>You might try with placing the initialisation in front of the loop, the incrementation/decrementation at the end of the loop and leaving the condition as is. For example</p> <pre><code>for(k=1;k&lt;(i*2);k++) { printf("*"); } </code></pre> <p>translates to</p> <pre><code>k=1; while(k&lt;(i*2)){ printf("*"); k++; } </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/22332781/want-to-create-diamond-shape-using-while-in-c</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/c-1" hreflang="zh-hans">c</a></div> <div class="field--item"><a href="/tag/turbo-c" hreflang="zh-hans">turbo-c++</a></div> </div> </div> Mon, 13 Jan 2020 23:11:27 +0000 回眸只為那壹抹淺笑 3211432 at https://www.e-learn.cn Linker Error: Duplicated Functions https://www.e-learn.cn/topic/3103562 <span>Linker Error: Duplicated Functions</span> <span><span lang="" about="/user/183" typeof="schema:Person" property="schema:name" datatype="">北城余情</span></span> <span>2020-01-05 03:36:56</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p><strong>NOTE:</strong> I made a DFH_lib.CPP where I included fstream and iomanip. I kept all the template functions in DFH_lib.CPP. Now, if I write the remaining NON-TEMPLATE functions in the MAIN.CPP and include DFH_lib.h only then it successfully runs. I don't understand why...</p> <p>I was making a Data File Handling library using templates. I created two files:</p> <pre><code>DFH_lib.CPP Lib_Test.CPP </code></pre> <p>I made a project and clicked on "Build All" under compile. I encountered the following linker error:</p> <blockquote> <p>file_init(char near*) defined in module DFH_LIB.CPP is duplicated in module LIB_TEST.CPP</p> <p>AddColumn(const int near&amp;) defined in module DFH_LIB.CPP is duplicated in module LIB_TEST.CPP</p> </blockquote> <p><code>file_init(char*);</code> and <code>AddColumn(T data, const int&amp; width);</code> and <code>AddColumn(const int&amp; width);</code> are functions which I only defined in DFH_lib.CPP. I only made calls to these functions in Lib_Test.CPP.</p> <p><strong>DFH_lib.CPP</strong></p> <pre class="lang-cpp prettyprint-override"><code>template &lt;class T&gt; //Function belongs to Pretty Printing Libary void AddColumn(T data, const int&amp; width) { cout&lt;&lt;setw(width)&lt;&lt;data&lt;&lt;" | "; } void AddColumn(const int&amp; width) { cout&lt;&lt;setw(width)&lt;&lt;setfill('_')&lt;&lt;"|"; } void file_init(char* file) { //File initialization function ofstream fout; fout.open(file, ios::binary|ios::noreplace); //File Created, noreplace prevents data loss fout.close(); } </code></pre> <p><strong>Lib_Test.CPP</strong></p> <pre class="lang-cpp prettyprint-override"><code>cout&lt;&lt;endl; AddColumn(record_id,7); AddColumn(char_member, 20); AddColumn(int_member, 11); AddColumn(float_member, 13); file_init(file); //initializes the file </code></pre> <p>where "file" is defined as: <code>char file[]="lib_Test.dat";</code></p> <p>Could someone please explain why I'm getting this Linker Error? I don't understand what it means and therefore, how to fix it...</p> <p><strong>EDIT:</strong> I've noticed that this might be resulting due to a mistake done while including files, as I turned the Lib_Test.CPP into a "Hello World" program and the same error appeared. One more thing I noted: <em>Only the non-template functions are causing the linking error!</em></p> <p><strong>DFH_lib.CPP</strong></p> <pre class="lang-cpp prettyprint-override"><code>#ifndef _DFH_lib_cpp #define _DFH_lib_cpp #include&lt;fstream.h&gt; #include&lt;conio.h&gt; #include&lt;stdio.h&gt; #include&lt;iomanip.h&gt; #include&lt;string.h&gt; ..... #endif </code></pre> <p><strong>Lib_Test.CPP</strong></p> <pre class="lang-cpp prettyprint-override"><code>#include&lt;iostream.h&gt; #include&lt;conio.h&gt; #include"DFH_lib.CPP" //Including DFH Libary </code></pre> <br /><h3>回答1:</h3><br /><p>I see following reasons for the problem:</p> <ol><li><p><strong>compiler error</strong></p> <p>Old <strong>TC++</strong> has sometimes compile problems with templates usually adding empty line (at the right place) or swap some lines of code help. But that usually starts only when your source code hit certain size like <code>30-50 Kbyte</code> not sure anymore about the value it was ages ago. I am not convinced this is your issue.</p></li> <li><p><strong>really a duplicate</strong></p> <p>if you are including some file multiple times then it will lead to errors like yours. To remedy that you can encapsulate each file into this:</p> <pre class="lang-cpp prettyprint-override"><code>#ifndef _DFH_lib_cpp #define _DFH_lib_cpp // here comes your file DFH_lib.cpp content #endif </code></pre> <p>where <code>_DFH_lib_cpp</code> token is encoded filename you are encapsulating. This will throw away any duplicate includes. This also solves problem with global variables present but be careful they may not be the same across whole project if not included properly. </p></li> </ol><br /><br /><p>来源:<code>https://stackoverflow.com/questions/45921184/linker-error-duplicated-functions</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/linker-errors" hreflang="zh-hans">linker-errors</a></div> <div class="field--item"><a href="/tag/turbo-c" hreflang="zh-hans">turbo-c++</a></div> </div> </div> Sat, 04 Jan 2020 19:36:56 +0000 北城余情 3103562 at https://www.e-learn.cn Linker Error: Duplicated Functions https://www.e-learn.cn/topic/3103524 <span>Linker Error: Duplicated Functions</span> <span><span lang="" about="/user/15" typeof="schema:Person" property="schema:name" datatype="">假装没事ソ</span></span> <span>2020-01-05 03:36:10</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p><strong>NOTE:</strong> I made a DFH_lib.CPP where I included fstream and iomanip. I kept all the template functions in DFH_lib.CPP. Now, if I write the remaining NON-TEMPLATE functions in the MAIN.CPP and include DFH_lib.h only then it successfully runs. I don't understand why...</p> <p>I was making a Data File Handling library using templates. I created two files:</p> <pre><code>DFH_lib.CPP Lib_Test.CPP </code></pre> <p>I made a project and clicked on "Build All" under compile. I encountered the following linker error:</p> <blockquote> <p>file_init(char near*) defined in module DFH_LIB.CPP is duplicated in module LIB_TEST.CPP</p> <p>AddColumn(const int near&amp;) defined in module DFH_LIB.CPP is duplicated in module LIB_TEST.CPP</p> </blockquote> <p><code>file_init(char*);</code> and <code>AddColumn(T data, const int&amp; width);</code> and <code>AddColumn(const int&amp; width);</code> are functions which I only defined in DFH_lib.CPP. I only made calls to these functions in Lib_Test.CPP.</p> <p><strong>DFH_lib.CPP</strong></p> <pre class="lang-cpp prettyprint-override"><code>template &lt;class T&gt; //Function belongs to Pretty Printing Libary void AddColumn(T data, const int&amp; width) { cout&lt;&lt;setw(width)&lt;&lt;data&lt;&lt;" | "; } void AddColumn(const int&amp; width) { cout&lt;&lt;setw(width)&lt;&lt;setfill('_')&lt;&lt;"|"; } void file_init(char* file) { //File initialization function ofstream fout; fout.open(file, ios::binary|ios::noreplace); //File Created, noreplace prevents data loss fout.close(); } </code></pre> <p><strong>Lib_Test.CPP</strong></p> <pre class="lang-cpp prettyprint-override"><code>cout&lt;&lt;endl; AddColumn(record_id,7); AddColumn(char_member, 20); AddColumn(int_member, 11); AddColumn(float_member, 13); file_init(file); //initializes the file </code></pre> <p>where "file" is defined as: <code>char file[]="lib_Test.dat";</code></p> <p>Could someone please explain why I'm getting this Linker Error? I don't understand what it means and therefore, how to fix it...</p> <p><strong>EDIT:</strong> I've noticed that this might be resulting due to a mistake done while including files, as I turned the Lib_Test.CPP into a "Hello World" program and the same error appeared. One more thing I noted: <em>Only the non-template functions are causing the linking error!</em></p> <p><strong>DFH_lib.CPP</strong></p> <pre class="lang-cpp prettyprint-override"><code>#ifndef _DFH_lib_cpp #define _DFH_lib_cpp #include&lt;fstream.h&gt; #include&lt;conio.h&gt; #include&lt;stdio.h&gt; #include&lt;iomanip.h&gt; #include&lt;string.h&gt; ..... #endif </code></pre> <p><strong>Lib_Test.CPP</strong></p> <pre class="lang-cpp prettyprint-override"><code>#include&lt;iostream.h&gt; #include&lt;conio.h&gt; #include"DFH_lib.CPP" //Including DFH Libary </code></pre> <br /><h3>回答1:</h3><br /><p>I see following reasons for the problem:</p> <ol><li><p><strong>compiler error</strong></p> <p>Old <strong>TC++</strong> has sometimes compile problems with templates usually adding empty line (at the right place) or swap some lines of code help. But that usually starts only when your source code hit certain size like <code>30-50 Kbyte</code> not sure anymore about the value it was ages ago. I am not convinced this is your issue.</p></li> <li><p><strong>really a duplicate</strong></p> <p>if you are including some file multiple times then it will lead to errors like yours. To remedy that you can encapsulate each file into this:</p> <pre class="lang-cpp prettyprint-override"><code>#ifndef _DFH_lib_cpp #define _DFH_lib_cpp // here comes your file DFH_lib.cpp content #endif </code></pre> <p>where <code>_DFH_lib_cpp</code> token is encoded filename you are encapsulating. This will throw away any duplicate includes. This also solves problem with global variables present but be careful they may not be the same across whole project if not included properly. </p></li> </ol><br /><br /><p>来源:<code>https://stackoverflow.com/questions/45921184/linker-error-duplicated-functions</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/linker-errors" hreflang="zh-hans">linker-errors</a></div> <div class="field--item"><a href="/tag/turbo-c" hreflang="zh-hans">turbo-c++</a></div> </div> </div> Sat, 04 Jan 2020 19:36:10 +0000 假装没事ソ 3103524 at https://www.e-learn.cn Mouse program in Turbo CPP https://www.e-learn.cn/topic/2968322 <span>Mouse program in Turbo CPP</span> <span><span lang="" about="/user/91" typeof="schema:Person" property="schema:name" datatype="">给你一囗甜甜゛</span></span> <span>2019-12-28 02:16:28</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have written a program in turbo cpp, which uses mouse for a basic GUI. I have also written a function that determines if the mouse has been clicked on a certain text displayed at a certain position. Everything works fine on the first run. But when i run the program, a second time a problem arises. Even when the mouse is just passed over an option(not clicking) it gets selected and the next page is displayed. Thanks again for those who answers. I am also attaching the code for mouse functions..</p> <pre><code>union REGS in,out; int callmouse() { in.x.ax=1; int86(51,&amp;in,&amp;out); return 1; } void mouseposi(int &amp;xpos,int &amp;ypos,int &amp;click) { in.x.ax=3; int86(51,&amp;in,&amp;out); click=out.x.bx; //CLICK ==1,IF LEFT BUTTON PRESSED xpos=out.x.cx; //CLICK ==2,IF RIGHT BUTTON PRESSED ypos=out.x.dx; } int mousehide() { in.x.ax=2; int86(51,&amp;in,&amp;out); return 1; } void setposi(int xpos,int ypos) { in.x.ax=4; in.x.cx=xpos; in.x.dx=ypos; int86(51,&amp;in,&amp;out); } void restrictmouseptr(int x1,int y1,int x2,int y2) { in.x.ax=7; in.x.cx=x1; in.x.dx=x2; int86(51,&amp;in,&amp;out); in.x.ax=8; in.x.cx=y1; in.x.dx=y2; int86(51,&amp;in,&amp;out); } int mouseclick(int x_org,int y_org,char str[], int x_cur, int y_cur, int cl) { static int flag=0; int y_diff=y_cur-y_org; int x_diff=x_cur-x_org; if((y_diff&gt;=0 &amp;&amp; y_diff&lt;=textheight(str))&amp;&amp;(x_diff&gt;=0 &amp;&amp; x_diff&lt;=textwidth(str))) { if(flag==0) { int oldcolor=getcolor(); setcolor(15); outtextxy(x_org,y_org,str); setcolor(oldcolor); flag=1; } if(cl!=1) return 0; //RETURNS 0 IF LEFT BUTTON IS NOT PRESSED else { mousehide(); return 1; //RETURNS 1 IF X AND Y COORDINATES ARE //WITHIN THEIR EXTREMITIES. } } else if(flag==1); { setcolor(11); flag=0; outtextxy(x_org,y_org,str); return 0; } } </code></pre> <br /><h3>回答1:</h3><br /><p>I do not see the architecture of your program as the important stuff is missing. I would expect you got a list of objects per page with their positions,size,labels,color etc. in form of some arrays of <code>struct</code> and loop through that for booth rendering and mouse handling. Something like this</p> <ul><li>Does anyone know of a low level (no frameworks) example of a drag &amp; drop, re-order-able list?</li> </ul><p>just ignore the <strong>VCL</strong> stuff <code>TCanvas,Graphics::TBitmap</code> so ignore lines starting with <code>bmp-&gt;</code> or <code>scr-&gt;</code> (or port them into your graphics).</p> <p>not sure if it helps but I just dig my ancient mine sweep game with partial solver using mouse in <strong>Turbo C++</strong>. It looks like this:</p> <p></p> <p>Here <strong>Turbo C++</strong> source for it:</p> <pre><code>//=========================================================================== //=== Includes: ============================================================= //=========================================================================== #include &lt;stdlib.h&gt; //=========================================================================== //=== Types: ================================================================ //=========================================================================== typedef unsigned char byte; typedef unsigned int word; typedef unsigned long dword; //=========================================================================== //=== Headers: ============================================================== //=========================================================================== void initscr(); // 320*200*256, BW palette void clrscr(); // clear scr void rfsscr(); // vga &lt;&lt; scr void box(word x,word y,word a); // draw box a &lt;0..cfree&gt; at x,y void boardint(); // init mines, clear board map void boardprn(); // draw board map void mouseint(); // init mouse void mousetst(); // test mouse (x,y,buttons) void numprn(word x,word y,word n); // write number n &lt;0..9999&gt; at x,y void boardtst0(int x,int y,int &amp;n); // sub test count number of mines around void boardtst(int x,int y); // test number of mines around if error then boardend; void boardend(); // game over (lose) void boardwin(); // game over (win) int boardopt0(int x,int y); void boardopt1(int x,int y,int &amp;c); int boardopt2(int x,int y); void boardopt3(int x,int y,int &amp;c); void boardopt(); // optimized search //=========================================================================== //=== Global data: ========================================================== //=========================================================================== const c0=25; // used colors const c1=30; const c2=20; const c3=40; const c4=34; const c5=35; const c6=5; const cfree=13; const boardx=30; // board size const boardy=16; const minesn=99; // number of mines const boardx0=(320-(10*boardx))/2; const boardy0=(190-(10*boardy))/2+10; byte board[32][20]; byte mines[32][20]; word far *pit; long xtime0,xtime; int flags; word scrseg,scrofs; word mousex=0,mousey=0, mousel=0,mouser=0, mousel0=0,mouser0=0, mousebx=255,mouseby=255; byte far scr[64000],*vga; // 320x200x8bpp backbuffer and VGA screen byte far mouset[10][10]= // mouse cursor { 1,1,0,0,0,0,0,0,0,0, 1,1,1,1,0,0,0,0,0,0, 1,1,1,1,1,1,0,0,0,0, 1,1,1,1,0,0,0,0,0,0, 1,1,1,1,0,0,0,0,0,0, 1,0,0,1,1,0,0,0,0,0, 1,0,0,0,1,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0 }; byte far txr[14*100]= // board tiles { c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c5,c5,c0,c0,c0,c2, c1,c0,c0,c5,c0,c0,c5,c0,c0,c2, c1,c0,c0,c5,c0,c0,c5,c0,c0,c2, c1,c0,c0,c5,c0,c0,c5,c0,c0,c2, c1,c0,c0,c5,c0,c0,c5,c0,c0,c2, c1,c0,c0,c0,c5,c5,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c3,c0,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c3,c0,c0,c0,c2, c1,c0,c0,c0,c3,c3,c3,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c3,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c3,c0,c0,c0,c0,c0,c2, c1,c0,c0,c3,c3,c3,c3,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c4,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c0,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c3,c0,c0,c2, c1,c0,c0,c4,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c3,c0,c0,c0,c0,c2, c1,c0,c0,c0,c3,c0,c0,c0,c0,c2, c1,c0,c0,c3,c0,c0,c0,c0,c0,c2, c1,c0,c0,c3,c0,c3,c0,c0,c0,c2, c1,c0,c0,c3,c3,c3,c3,c0,c0,c2, c1,c0,c0,c0,c0,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c3,c3,c3,c3,c0,c0,c2, c1,c0,c0,c3,c0,c0,c0,c0,c0,c2, c1,c0,c0,c3,c3,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c3,c0,c0,c2, c1,c0,c0,c3,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c3,c3,c4,c0,c0,c2, c1,c0,c0,c3,c0,c0,c0,c0,c0,c2, c1,c0,c0,c3,c3,c3,c0,c0,c0,c2, c1,c0,c0,c3,c0,c0,c3,c0,c0,c2, c1,c0,c0,c3,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c3,c3,c3,c3,c0,c0,c2, c1,c0,c0,c0,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c0,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c3,c0,c0,c0,c2, c1,c0,c0,c0,c3,c0,c0,c0,c0,c2, c1,c0,c0,c0,c3,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c3,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c3,c0,c0,c3,c0,c0,c2, c1,c0,c0,c3,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c3,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c3,c3,c3,c0,c0,c2, c1,c0,c0,c0,c0,c0,c3,c0,c0,c2, c1,c0,c0,c4,c0,c0,c3,c0,c0,c2, c1,c0,c0,c0,c3,c3,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c6,c6,c0,c0,c0,c0,c2, c1,c0,c0,c6,c6,c6,c6,c0,c0,c2, c1,c0,c0,c6,c6,c6,c6,c0,c0,c2, c1,c0,c0,c6,c0,c6,c6,c0,c0,c2, c1,c0,c0,c6,c0,c0,c0,c0,c0,c2, c1,c0,c0,c6,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c6,c6,c0,c0,c0,c2, c1,c0,c0,c6,c6,c6,c6,c0,c0,c2, c1,c0,c0,c6,c6,c6,c6,c0,c0,c2, c1,c0,c0,c0,c6,c6,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c6,c0,c0,c0,c0,c6,c0,c2, c1,c0,c0,c6,c0,c0,c6,c0,c0,c2, c1,c0,c0,c0,c6,c6,c0,c0,c0,c2, c1,c0,c0,c0,c6,c6,c0,c0,c0,c2, c1,c0,c0,c6,c0,c0,c6,c0,c0,c2, c1,c0,c6,c0,c0,c0,c0,c6,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2, c2,c1,c1,c1,c1,c1,c1,c1,c1,c1, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c0,c0,c0,c0,c0,c0,c0,c0,c2, c1,c2,c2,c2,c2,c2,c2,c2,c2,c2 }; //=========================================================================== //=== Subroutines: ========================================================== //=========================================================================== void initscr() { dword h,l; int i; vga=(byte far*)0x0A0000000; h=(unsigned long)scr; l=h; l=l &amp; 65535; h=h &gt;&gt; 16; scrseg=h; scrofs=l; asm { mov ax,0x0013 int 0x10 } for (i=0;i&lt;256;i++) // set color palette to grayscale asm { mov dx,0x03C8 mov ax,i out dx,al mov dx,0x03C9 mov ax,i out dx,al out dx,al out dx,al } } //=========================================================================== void clrscr() { asm { pusha push es mov di,scrofs mov es,scrseg mov ax,0x1313 mov cx,32000 rep stosw pop es popa } } //=========================================================================== void rfsscr() { asm { pusha push ds push es mov di,0 mov si,scrofs mov ds,scrseg mov ax,0xA000 mov es,ax mov cx,32000 rep movsw pop es pop ds popa } } //=========================================================================== void box(word x,word y,word a) { word b; a=a*100; b=x+320*y; for (y=0;y&lt;10;y++) { for (x=0;x&lt;10;x++) scr[b+x]=txr[a+x]; b+=320; a+=10; } } //=========================================================================== void boardint() { word i,x,y; for (y=0;y&lt;boardy;y++) for (x=0;x&lt;boardx;x++) { board[x][y]=cfree; mines[x][y]=0; } randomize(); for (i=0;i&lt;minesn;i++) { x=random(boardx); y=random(boardy); if (mines[x][y]) i--; else mines[x][y]=1; } } //=========================================================================== void boardprn() { word x,y,i,j; clrscr(); x=boardx0; y=boardy0; for (j=0;j&lt;boardy;j++) for (i=0;i&lt;boardx;i++) box(x+10*i,y+10*j,board[i][j]); box(310,0,12); // X .. exit button box(150,0,11); // O .. restart button box(160,0,11); box( 0,0,10); // flag xtime=pit[0]; // time xtime=xtime-xtime0; if (xtime&lt;0) xtime=-xtime; xtime=xtime*55/1000; if (xtime&gt;9999) xtime=9999; numprn(260,0,xtime); numprn( 20,0,flags); // flags } //=========================================================================== void mouseint() { mousex=0; mousey=0; mousel=0; mouser=0; asm { pusha push ds push es mov ax,0 // reset mouse int 0x33 mov cx,0 // set range mov dx,319 mov ax,7 int 0x33 mov cx,0 mov dx,199 mov ax,8 int 0x33 mov cx,mousex // set pos. mov dx,mousey mov ax,4 int 0x33 pop es pop ds popa } mousetst(); } //=========================================================================== void mousetst() { int b,x,y; asm { mov ax,3 // in mouse status test int 0x33 mov mousex,cx mov mousey,dx and bx,3 mov b,bx } mousel0=mousel; mouser0=mouser; mousel=b&amp;1; mouser=(b&amp;2)&gt;&gt;1; for (y=0;y&lt;10;y++) for (x=0;x&lt;10;x++) { if (mouset[y][x]) if (x+mousex&lt;320) if (y+mousey&lt;200) scr[(x+mousex)+320*(y+mousey)]=63; } mousebx=255; x=mousex-boardx0; mouseby=255; y=mousey-boardy0; if (x&gt;=0) if (x&lt;boardx*10) if (x%10&gt;1) if (x%10&lt;9) mousebx=x/10; if (y&gt;=0) if (y&lt;boardy*10) if (y%10&gt;1) if (y%10&lt;9) mouseby=y/10; } //=========================================================================== void numprn(word x,word y,word n) { int i,c; x+=3*10; c=10; for (i=0;i&lt;4;i++) { box(x,y,n%c); n=(n-(n%c))/c; x-=10; } } //=========================================================================== void boardtst0(int x,int y,int &amp;n) { if (x&gt;=0) if (x&lt;boardx) if (y&gt;=0) if (y&lt;boardy) if (mines[x][y]) n++; } //=========================================================================== void boardtst(int x,int y) { int n=0; if (x&gt;=0) if (x&lt;boardx) if (y&gt;=0) if (y&lt;boardy) if (board[x][y]==cfree) { boardtst0(x-1,y+0,n); boardtst0(x+1,y+0,n); boardtst0(x-1,y-1,n); boardtst0(x+0,y-1,n); boardtst0(x+1,y-1,n); boardtst0(x-1,y+1,n); boardtst0(x+0,y+1,n); boardtst0(x+1,y+1,n); board[x][y]=n; if (n==0) { boardtst(x-1,y+0); boardtst(x+1,y+0); boardtst(x-1,y-1); boardtst(x+0,y-1); boardtst(x+1,y-1); boardtst(x-1,y+1); boardtst(x+0,y+1); boardtst(x+1,y+1); } } } //=========================================================================== void boardend() { int x,y; byte a,b,c; for (y=0;y&lt;boardy;y++) for (x=0;x&lt;boardx;x++) { a=mines[x][y]; b=board[x][y]; c=b; if (a) if (b!=10) c=11; if (!a) if (b==10) c=12; board[x][y]=c; } boardprn(); rfsscr(); do { mousetst(); } while (mousel==1); do { mousetst(); } while (mousel==0); boardint(); xtime0=pit[0]; flags=minesn; } //=========================================================================== void boardwin() { int x,y; byte a,b,c; c=1; for (y=0;y&lt;boardy;y++) for (x=0;x&lt;boardx;x++) { a=mines[x][y]; b=board[x][y]; if ( a) if (b!=10) c=0; if (!a) if (b==10) c=0; if (!a) if (b==cfree) c=0; } if (c) { boardprn(); box(150,0,10); // O .. restart button box(160,0,10); rfsscr(); do { mousetst(); } while (mousel==1); do { mousetst(); } while (mousel==0); boardint(); xtime0=pit[0]; flags=minesn; } } //=========================================================================== int boardopt0(int x,int y) { int n=0; if (x&gt;=0) if (x&lt;boardx) if (y&gt;=0) if (y&lt;boardy) if (board[x][y]&lt;9) n=1; return n; } //=========================================================================== void boardopt1(int x,int y,int &amp;c) { if (x&gt;=0) if (x&lt;boardx) if (y&gt;=0) if (y&lt;boardy) if (board[x][y]==cfree) if (flags) { board[x][y]=10; flags--; c=1; } } //=========================================================================== int boardopt2(int x,int y) { int n=0; if (x&gt;=0) if (x&lt;boardx) if (y&gt;=0) if (y&lt;boardy) if (board[x][y]==10) n=1; return n; } //=========================================================================== void boardopt3(int x,int y,int &amp;c) { if (x&gt;=0) if (x&lt;boardx) if (y&gt;=0) if (y&lt;boardy) if (board[x][y]==cfree) { boardtst(x,y); c=1; } } //=========================================================================== void boardopt() { int x,y,n,c; byte a; do { c=0; // flag map changed for (y=0;y&lt;boardy;y++) for (x=0;x&lt;boardx;x++) { a=board[x][y]; if (a!=cfree) { n=8; // add flags if (boardopt0(x-1,y+0)) n--; if (boardopt0(x+1,y+0)) n--; if (boardopt0(x-1,y-1)) n--; if (boardopt0(x+0,y-1)) n--; if (boardopt0(x+1,y-1)) n--; if (boardopt0(x-1,y+1)) n--; if (boardopt0(x+0,y+1)) n--; if (boardopt0(x+1,y+1)) n--; if (n==a) { boardopt1(x-1,y+0,c); boardopt1(x+1,y+0,c); boardopt1(x-1,y-1,c); boardopt1(x+0,y-1,c); boardopt1(x+1,y-1,c); boardopt1(x-1,y+1,c); boardopt1(x+0,y+1,c); boardopt1(x+1,y+1,c); } n=0; // add space if (boardopt2(x-1,y+0)) n++; if (boardopt2(x+1,y+0)) n++; if (boardopt2(x-1,y-1)) n++; if (boardopt2(x+0,y-1)) n++; if (boardopt2(x+1,y-1)) n++; if (boardopt2(x-1,y+1)) n++; if (boardopt2(x+0,y+1)) n++; if (boardopt2(x+1,y+1)) n++; if (n==a) { boardopt3(x-1,y+0,c); boardopt3(x+1,y+0,c); boardopt3(x-1,y-1,c); boardopt3(x+0,y-1,c); boardopt3(x+1,y-1,c); boardopt3(x-1,y+1,c); boardopt3(x+0,y+1,c); boardopt3(x+1,y+1,c); } } } } while (c); } //=========================================================================== //=== Main: ================================================================= //=========================================================================== void main() { byte a,b; pit=(word far*)0x0000046C; initscr(); mouseint(); boardint(); xtime0=pit[0]; flags=minesn; do { boardprn(); //render screen if ((mousel)&amp;&amp;(mousey/10==0)) if ((mousex/10==15)||(mousex/10==16)) boardend(); if ((!mouser0)&amp;&amp;(mouser)&amp;&amp;(mousebx!=255)&amp;&amp;(mouseby!=255)) { a=board[mousebx][mouseby]; b=a; if (a==10) { b=cfree; flags++; } if ((a==cfree)&amp;&amp;(flags!=0)) { b=10; flags--; } board[mousebx][mouseby]=b; } if ((!mousel0)&amp;&amp;(mousel)&amp;&amp;(mousebx!=255)&amp;&amp;(mouseby!=255)) if (board[mousebx][mouseby]==cfree) { a=mines[mousebx][mouseby]; if (a==0) boardtst(mousebx,mouseby); if (a==1) boardend(); } if (mousel+mouser) boardopt(); if (!flags) boardwin(); mousetst(); rfsscr(); // swap buffers to avoid flickering } while (!((mousel)&amp;&amp;(mousex/10==31)&amp;&amp;(mousey/10==0))); asm { mov ax,0x0003 int 0x10 } } //=========================================================================== //=== End: ================================================================== //=========================================================================== </code></pre> <p>Sorry it is not much commented. Left mouse button sweeps and right mouse button add flag (expecting bomb). If you need help understanding the <strong>VGA</strong> gfx part then see:</p> <ul><li>Display an array of color in C</li> </ul><p>If you look at the <code>main</code> then you see I got one "infinite" loop redrawing the screen and handling mouse events + game logic. I expect you should have something similar. The difference is that my objects are in grid so the selection is done just by scaling/offsetting mouse position.</p> <p>In your case you should have list of objects and should loop through them and find the one closest to mouse and not too far ... similarly like in the <strong>Drag&amp;Drop</strong> example link at the top of Answer.</p> <p>Also here another related QA:</p> <ul><li>understanding the minesweeper programming p.roblem </li> </ul><br /><br /><p>来源:<code>https://stackoverflow.com/questions/45547966/mouse-program-in-turbo-cpp</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/turbo-c" hreflang="zh-hans">turbo-c++</a></div> </div> </div> Fri, 27 Dec 2019 18:16:28 +0000 给你一囗甜甜゛ 2968322 at https://www.e-learn.cn