程序清单2.1
#include<iostream>
using namespace std;
int main()
{
cout<<"Come up and C++ me some time.";
cout<<endl;
cout<<"You won't regret it!"<<endl;
return 0;
}
得到输出结果 Come up and C++ me some time. You won’t regret it!
#include<iostream>
using namespace std;
int main()
{
cout<<"Come up and C++ me some time.";
cout<<endl;
cout<<"You won't regret it!"<<endl;
cin.get();
return 0;
}
- 思考二:是否能够使用C语言语句进行输入和输出以及换行
答:包含C语言的stdio.h头文件 #include<stdio.h> 即可使用{printf()与scanf()}{换行可不包含头文件,句末使用"\n" }
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
printf("Come up and C++ me some time.\n");
printf("You won't regret it!\n")
return 0;
}
#include<iostream>
int main()
{
std::cout<<"Come up and C++ me some time.";
std::cout<<std::endl;
std::cout<<"You won't regret it!"<<std::endl;
return 0;
}
编程练习
- 编写一个C++程序,它显示您的姓名和住址。
#include<iostream>
using namespace std;
int main()
{
string name;
string address;
cout << "请输入您的名字" << endl;
cin >> name;
cout << "请输入您的地址" << endl;
cin >> address;
cout << "您的名字是:" << name << "\n" << "您的地址是:" << address;
}
- 编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(1long等于220码)。
#include<iostream>
using namespace std;
int main()
{
double A,B;
cout << "请输入一个以long为单位的距离" << endl;
cin >> A;
B = A * 220;
cout << "转换为码为:" << B;
}
- 编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月。
#include<iostream>
using namespace std;
int change(int age)
{
int month;
month = 12 * age;
return month;
}
int main()
{
int age;
cout << "输入您的年龄" << endl;
cin >> age;
cout << "Enter your age(year):" << age << endl;
cout << "Enter your age(month):" << change(age) << endl;
}
- 编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
Please enter a Celsius value:20 20 degress Celsius is 68 degrees Fahrenhebit.
#include<iostream>
using namespace std;
double change(double Celsius)
{
double Fahrenhebit;
Fahrenhebit = 1.8 * Celsius + 32.0;
return Fahrenhebit;
}
int main()
{
double Celsius;
cout << "Please enter a Celsius value : ";
cin>>Celsius;
cout << Celsius << " degress Celsius is " << change(Celsius) << " degrees Fahrenhebit.";
return 0;
}
|