进入c++
一、代码:
#include //相当于#include,包含输入输出函数**
int main() //和c一样
{
using namespace std; //using 编译指令
cout<<"come up and c++ me some time."; //相当于c语言的printf();
cout<<endl; //相当于c语言的”\n“;重启一行
cout<<"you won't regret it!"<<endl; //也可以在字符串中加入\n 重启一行
cin.get(); //相当于c语言的getchar(), 运行窗口一直打开,直到你按下任何键
return 0;
}
二、笔记
1.注释用//(单行注释) 或者(实现多行注释);
2.using namespace std 使得std中名称空间中的所有名称都可以使用;如果没有加这句编译指令,cout格式 应该是std::cout;这是一下种偷懒的做法,还可以使有需要的名称可用;格式为 using std::cout; using std::endl;
3.cout <
#include<iostream>
int main()
{
using namespace std;
int carrots;
carrots=15;
cout << "I have ";
cout << carrots;
cout << "carrots.";
cout << endl;
carrots=carrots-1;
cout <<"Crunch,crunch.Now I have "<<carrots<<" carrots"<<endl;
cin.get();
return 0;
}
其他c++语句
一、代码
#include<iostream>
int main()
{
using namespace std;
int carrots;
cout << "How many carrots do you have?"<<endl;
cin >>carrots; //读取键盘输入
cout <<"Here are two more."<<endl;
carrots=carrots+2;
cout <<"Now you have "<<carrots<<" carrots."<<endl;
cin.get();
cin.get();
return 0;
}
函数
一、代码(1)
#include<iostream>
#include<cmath>
int main()
{
using namespace std;
double area;
cout << "Enter the floor area, in square feet ,of your home:";
cin >> area;
double side;
side = sqrt(area);
cout << "That's the equivalent of a square "<<side
<< " feet to the side."
<< "How fascinating!"<<endl;
cin.get();
cin.get();
return 0;
}
代码(2)
#include<iostream>
void simon(int);
int main()
{
using namespace std;
simon(3);
cout << "Pick an integer: ";
int count;
cin >> count;
simon(count);
cout << "Done!"<<endl;
cin.get();
cin.get();
return 0;
}
void simon(int n)
{
using namespace std;
cout<<"Simon says touch your toes "<< n << " times." << endl;
}
代码(3)
#include<iostream>
int stonetolb(int);
int main()
{
using namespace std;
int stone;
cout << "Enter the weight in stone:";
cin >> stone;
int pounds = stonetolb(stone);
cout << stone << "stone = ";
cout << pounds << " pounds."<<endl;
cin.get();
cin.get();
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}
编程练习
1、编写一个c++程序,它显示你的姓名和地址。
#include<iostream>
int main()
{
using namespace std;
cout << "name: green hand \naddress:xxx省xxx市";
cin.get();
}
2、编写一个c++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一long=220码)。
#include<iostream>
int main()
{
using namespace std;
double l,ma;
cout << "请输入一个以long为单位的距离:";
cin >> l;
cout <<endl;
ma=l*220;
cout << l<<" long = "<<ma<< " 码";
cin.get();
cin.get();
return 0;
}
3、编写一个c++程序,它使用3个用户定义的函数(包括main() ),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
其中两个函数都调用两次
#include<iostream>
void myfun1();
void myfun2();
using namespace std;
int main()
{
myfun1();
myfun1();
myfun2();
myfun2();
cin.get();
}
void myfun1()
{
cout << "Three blind mice"<<endl;
}
void myfun2()
{
cout << "See how they run"<<endl;
}
4、编写程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下显示: Enter your age : 29
#include<iostream>
using namespace std;
int main()
{
int age;
cout << "Enter your age: ";
cin >> age;
cin.get();
cin.get();
}
5、编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该函数按下面的格式要求用户输入摄氏温度,并显示结果:
#include<iostream>
using namespace std;
int main()
{
double cel,fa;
cout << "Please enter a Celsius value: ";
cin >> cel;
fa = cel*1.8+32.0;
cout << cel <<" degress celsius is "<<fa<<" degrees Fahrenheit.";
cin.get();
cin.get();
}
6.
#include<iostream>
using namespace std;
double change(double);
int main()
{
double light,units;
cout << "Enter the number of light year: ";
cin >> light;
units = change(light);
cout << light<<" light years = "<<units<<" astroomical units.";
cin.get();
cin.get();
}
double change(double n )
{
return n*63240;
}
7.
#include<iostream>
using namespace std;
int main()
{
double shi,fen;
cout << "Enter the number of hours : ";
cin >> shi;
cout << "Enter the number of minutes : ";
cin >> fen;
cout <<"Time: " <<shi<<":"<<fen<<endl;
cin.get();
cin.get();
cin.get();
}