前言:此篇文章適合學(xué)完c語(yǔ)言基礎(chǔ)概念的同學(xué),是幫助c向c++語(yǔ)言的同學(xué)快速掌握基本語(yǔ)法。
基礎(chǔ)格式
#include<iostream>
using namespace std;
int main()
{
system("pause");
return 0;
}
輸入:
cin>>a;//a是輸入內(nèi)容
輸出:
cout<<"需要輸出的內(nèi)容"<<endl;
//endl相當(dāng)于換行
定義字符串
按照c語(yǔ)言的語(yǔ)法也可以,頭文件多包含#include<string>
下面是通過(guò)數(shù)組名的方法打印字符串hello world。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "hello world";
cout << str1 << endl;
system("pause");
return 0;
}
科學(xué)計(jì)數(shù)法
1e2就是1x10的二次方
1e-2就是1x10的負(fù)二次方
#include<iostream>
using namespace std;
int main()
{
float a = 1e2;
float b = 1e-2;
cout << a << endl;
cout << b << endl;
system("pause");
return 0;
}
float
float類型后面的數(shù)字要多寫一個(gè)f,不然編譯器會(huì)默認(rèn)為double類型。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-806779.html
float f1=3.14f;
if例子
#include<iostream>
using namespace std;
int main()
{
//1.輸入考試分?jǐn)?shù)
int score = 0;
cout << "請(qǐng)輸入一個(gè)考試分?jǐn)?shù): " << endl;
cin >> score;
//2.提示用戶輸入的分?jǐn)?shù)
cout << "您輸入的分?jǐn)?shù)為: " << score << endl;
//3.判斷 如果分?jǐn)?shù)大于600,打印考上一本,否則打印未考上一本
if (score > 600)
{
cout << "考上一本大學(xué)" << endl;
}
else
{
cout << "未考上一本大學(xué)" << endl;
}
system("pause");
}
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-806779.html
到了這里,關(guān)于c++:基于c語(yǔ)言基礎(chǔ)上的語(yǔ)法不同(1)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!