helloworld.cpp
#include <iostream>
//include 是iostream 而不是c中的stdio.h
int main(void){
using namespace std;//使用命名空间 using 的用法 cout << "hello,world!";//cout 使用,<< 与>> 重定向符 cout << endl; return 0;}
carrots.cpp
#include <iostream> int main(void) { using namespace std; int carrots;//变量声明与使用与c一样 carrots=25; cout << "i have " << carrots << " carrots."<<endl<<"Crunch,Crunch,now i have "<<carrots-1<<" carrots"<<endl;
return 0; }
getinfo.cpp
#include <iostream> int main(void) { using namespace std; int carrots; cout <<"how many carrots do you have?"<<endl; cin >> carrots;//获取输入,赋值变量 cout <<"here are two more"<<endl; carrots+=2; cout <<"Now you have "
<<carrots
<<" carrots"
<<endl;//多行拼接,格式更好. return 0; }
sqrt.cpp
#include <iostream> #include <cmath>//or match.h int main(void) { using namespace std; double area; cout << "Enter the floor area ,in square feet,of your home "<<endl; cin >> area; double side; side = sqrt(area); cout <<"That's the equivalent of a square " <<side <<" feet to the side" <<endl <<"How fascinating!" <<endl; return 0; }
//ourfunc.cpp --define our own function #include <iostream> void simon(int);//注意函数声明需要加;include不需要加; int main(){ using namespace std; int count; cout <<"pick an integer\n"; cin >> count; simon(count); cout<<"done"; return 0; } void simon(int n){ using namespace std;//可以将这个语句放在include后面,本文件中的所有函数使用std,不需要每个函数都重复输入 cout << "Simon says touch your toes "<<n<<" times"<<endl; }
//convert.cpp #include <iostream> double stoneToLb(double); int main(){ using namespace std; double stone; cout<<"Enter the weight in stone"<<endl; cin >> stone; double pounds; pounds=stoneToLb(stone); cout << stone << " stone = " <<pounds<<" pounds"<<endl; return 0; } double stoneToLb(double sts) { return sts*14; }
习题:
复习题:
1,c++程序的模块叫做什么?
函数
2,下面的预处理器编译指令是做什么用的?#include <iostream>
在编译前使用iostream 文件替换该指令位置.
3,下面的语句是做什么用的?using namespace std;
程序可以使用std中的定义的常量,方法等.
//program_exercise1.cpp //1,编写一个c++程序,它显示您的姓名和地址 #include <iostream> using namespace std; int main(void){ cout <<"my name is hujiawei" <<endl <<"my address is nanjing" <<endl; return 0; }
//2.编写一个c++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(1 long= 220 ma); //program_exercise2.cpp #include <iostream> using namespace std; int main(void){ double along,ma; cout <<"Enter the distance in long:"; cin >> along; ma=220*along; cout <<"the distance is equivalent to "<<ma<<" ma"<<endl; return 0; }
/*3,编写一个c++程序,它使用三个用户定义的函数,(包括main()),并生辰下面的输出: Three blind mice Three blind mice See how they run See how they run */ //program_exercise3.cpp #include <iostream> using namespace std; void blind(void); void run(void); int main(void){ blind(); run(); return 0; } void blind(void){ cout <<"Three blind mice" <<endl <<"Three blind mice" <<endl; } void run(void){ cout <<"See how they run" << endl <<"See how they run" <<endl; }
/*4,编写一个c++程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示: Enter your age:29 */ //program_exercise4.cpp #include <iostream> using namespace std; int main(void){ int age; cout << "Enter your age:"; cin >> age; cout << age<<"age = "<< age*12 << " monthes"<<endl; return 0; }
/*5,编写一个c++程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值). 该程序按下面的格式要求用户输入摄氏温度值.并显示结果: Please enter a Celsius value:20 20 degrees Celsius is 68 degrees Fahrenheit. 下面是转换公式: Fahrenheit=1.8*Celsius+32.0 */ //program_exercise5.cpp #include <iostream> using namespace std; double celToFah(double); int main(void){ cout <<"Please enter a Celsius value:"; double cel,fah; cin >>cel; fah=celToFah(cel); cout << cel << " degrees Celsius is " <<fah <<" degrees Fahrenheit." <<endl; return 0; } double celToFah(double cel){ return 1.8*cel+32.0; }
/*6,编写一个c++程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值). 该程序按下面的格式要求用户输入光年值,并显示结果: Enter the number of light years:4.2 4.2 light years = 265608 astronomical units. 天文单位是从地球到太阳的平均距 1光年=63240 天文单位 */ //program_exercise6.cpp #include <iostream> using namespace std; double lyToAu(double); int main(void){ double ly; cout <<"Enter the number of light years:"; cin >>ly; cout << ly<<" light years = "<<lyToAu(ly)<<" astronomical units."<<endl; return 0; } double lyToAu(double ly){ return 63240.0*ly; }
/*7,编写一个c++程序,要求用户输入小时数和分钟数.在main()函数中,将这两个值给一个void函数,后者以下面这样的格式 显示这两个值: Enter the number of hours:9 Enter the number of minutes:28 Time: 9:28 */ //program_exercise7.cpp #include <iostream> using namespace std; void showTime(int ,int); int main(void){ cout <<"Enter the number of hours:"; int h,m; cin>>h; cout <<"Enter the number of minutes:"; cin>>m; showTime(h,m); return 0; } void showTime(int h,int m){ cout<<"Time: "<<h<<":"<<m<<endl; }