¶ÔÒÔϵijÌÐò½øÐÐ˵Ã÷
#include <iostream>
int main() { std::cout << "hello world\n"; return 0; } |
1.1.1 ³ÌÐò˵Ã÷£º
1£©#include <iostream>
#£ºÕâ¸öÊÇÔ¤´¦Àí±êÖ¾£¬ÓÃÀ´¶ÔÎı¾½øÐÐÔ¤´¦Àí²Ù×÷£¬Ô¤´¦Àí±êÖ¾¸ÃÐдúÂëÒª×îÏȽøÐд¦Àí£¬ËùÒÔ£¬ËüÒªÔÚ±àÒëÆ÷±àÒë´úÂë֮ǰÔËÐС£
include ÊǸöÔ¤´¦ÀíÖ¸ÁËüºóÃæ¸úÁËÒ»¶Ô¼âÀ¨ºÅ£¬±íʾ½«¼âÀ¨ºÅÄÚµÄÎļþÔÚÕâÀï¶ÁÈë¡£
Îļþ iostream(ÊäÈëÊä³öÁ÷)ÊǸö±ê×¼¿â£¬Ëü°üº¬ÁËÖÚ¶àµÄ³ÉÔ±º¯Êý.¿âÖÐÿ¸öº¯Êý¶¼ÓÐÆä×ÔÉíµÄ×÷Ó㬱ÈÈçÀýÖÐµÄ cout ¶ÔÏó£¬Ëü¾ÍÊDZê×¼¿âº¯ÊýÖеÄÒ»²¿·Ö¡£
2£©int main()
main º¯ÊýµÄÒâ˼ÊÇ"Ö÷º¯Êý"£¬Ã¿¸ö C++³ÌÐò¶¼ÓÐÒ»¸ö main º¯Êý(º¯ÊýÊÇָʵÏÖÒ»¸ö»ò¶à¸ö¹¦ÄܵĴúÂë¿é)¡£ÔÚÕâÀï main º¯ÊýʵÏÖÁËÊä³ö¹¦ÄÜ¡£
ÿ¸öº¯Êý¶¼ÐèÒª±êÃ÷Æä·µ»ØÖµÀàÐÍ£¬Òò´ËÔÚ±¾³ÌÐòÖУ¬ main º¯ÊýµÄ·µ»ØÖµ±»±êÃ÷Ϊ int;Ò²¾ÍÊÇ˵ÔÚ main
º¯Êý½áÊøºó£¬ÒªÏò²Ù×÷ϵͳ·µ»ØÒ»¸öÕûÊý¡£
×¢Ò⣬ main º¯ÊýÓëÆÕͨµÄº¯ÊýÓÐ×ű¾ÖʵÄÇø±ð£¬ÀýÈ磬һ°ãº¯Êý¶¼ÊÇÓÉÆäËûº¯Êýµ÷ÓûòÕß¼¤»î£¬µ«ÊÇ main
º¯ÊýÈ´ÊÇÔÚ³ÌÐò¿ªÊ¼Ê±±ã×Ô¶¯Ö´ÐÐ
3£© std::cout << "hello world\n";
ʹÓÃÁ˱ê×¼¿âº¯ÊýÖÐµÄ cout ¶ÔÏóÀ´Êä³öÒ»ÐÐÐÅÏ¢.ҪעÒâ cout µÄÓ÷¨£¬ËüµÄºóÃæÒª¸úÒ»¸ö×óÒÆÔËËã·û"<<"£¬ "<<"ºóÃæËùÓÐÄÚÈݶ¼»á±»Êä³öÔÚÆÁÄ»ÉÏ£»Èç¹ûÒªÊä³ö×Ö·û£¬Îñ±ØÒªÓÃË«ÒýºÅ½«ÆäÒýÆðÀ´£¬ÕâÀïÊä³öÁËÒ»¶Î×Ö·û£¬"hello world";ÔڸöÎÓï¾äµÄ½áβ´¦£¬Ò²¾ÍÊÇ×Ö·û´® "hello world"µÄºóÃæʹÓÃÁËÒ»¸ö¸ñʽ·ûºÅ"\n"£¬¸Ã¸ñʽ·ûµÄ×÷ÓÃÊÇ»»Ðв¢½«¹â±ê¶¨Î»µ½µÚ 2 ÐеĿªÍ·
×¢Ò⣺
ÔÚ±ê×¼¿âº¯ÊýÖл¹ÓÐÒ»¸ö cin ¶ÔÏó£¬ËüÓÃÀ´½ÓÊÕ¼üÅÌÊäÈ룬ÆäʹÓ÷½·¨Óë cout Ïà·´£¬ cin ºóÃæµÄ¼ÓÓÒÒÆÔËËã·û">>"£¬Òª×¢Òâ¸ÃÓÒÒÆÔËËã·û">>"Óë cout µÄ×óÒÆÔËËã·û"<<"ÊÇÕýºÃÏà·´µÄ¡£
#include <iostream>
int main() { int a; std::cout << "hello world\n"; std::cin >> a; std::cout<< a << std::endl; return 0; } |
4£©return 0;
Ïò²Ù×÷ϵͳ·µ»ØÁËÒ»¸öÖµ£¬ÓÉÓڸà main º¯Êý±»ÉùÃ÷Ϊһ¸öÕûÐΣ¬Òò´ËÕâÀï·µ»ØÁËÒ»¸ö 0
1.1.2 Êä³öÓï¾äµÄʹÓÃ
C++³ÌÐòµÄ×é³É²¿·Ö,ËüÓÉÒ»¸öÔ¤´¦Àí±êÖ¾¡¢Ò»¸öÔ¤´¦ÀíÖ¸Áî¡¢Ò»¸öÍ·ÎļþºÍÒ»¸ö main º¯Êý×é³É¡£Ê¹Óà cout ¶ÔÏóÏòÆÁÄ»ÉÏÊä³ö"ÎåÄ꼶һ°àÊýѧ³É¼¨±í",³ÌÐòÈçÏÂ
#include <iostream>
using namespace std; //±ê×¼ÃüÃû¿Õ¼ä
int main() { cout << "ÎåÄ꼶һ°àÊýѧ³É¼¨±í\n"; cout << "µÚÒ»ÃûͬѧxxxµÄ³É¼¨Îª£º\t" << 100; cout << endl; cout << "µÚ¶þÃûͬѧ***µÄ³É¼¨Îª£º\t" << 90+9 << endl; cout << "×îºóÒ»Ãûͬѧ...µÄ³É¼¨Îª£º\t" << (float)95/2 ; cout << endl;
return 0; } |