#include<iostream>
using namespace std;
int main() {
int a;
int* b = &a;
cout << "please enter a number:";
cin >> a;
cout << b;
return 0;
}
#include<iostream>
using namespace std;
int main() {
char name1[20];//定义字符数组,后面的50是字符串最大长度
string name2;//c++中最常见的定义字符串的方法
cin >> name1;
cin >> name2;
cout << "This program is coded by " << name1 << "." << endl;
cout << "my friend is " << name2 << endl;
return 0;
}
#include<iostream>
using namespace std;
int main() {
char name[20];//先定义name的类型为字符串,使用string会发生错误,可能与cin.getline里面的有关
cin.getline(name, 20);//cin以空格、tab和回车键为分隔符,是按词输入;而类成员函数cin.getline只以回车为分隔符,是按行输入。
//这里使用可以显示中间有空格的字符串。cin是iostream中的一个类,而cin.getline是它的类成员函数
cout << "my name is:" << name << endl;
return 0;
}
#include<iostream>
using namespace std;
double TEM(double a);
int main() {
double c,d;
cout << "请输入摄氏温度:" << endl;
cin >> c;
d=TEM(c);
cout << "华氏温度为:"<<d;
return 0;
}
double TEM(double a) {
double b;
b = a * 1.8 + 32.0;
return b;
}
#include<iostream>
using namespace std;
int MIN(int a, int b);
int main() {
int D;
D=MIN(2, 3);
cout << D;
return 0;
}
int MIN(int a, int b) {
int c;
c = a + b;
return c;
}
#include<iostream>
//*****输入厘米值,将其转换为分米和厘米的形式*****通过定义全局变量实现被调用函数的多个值返回
using namespace std;
int hight(int h);
int a, b;//将a,b定义为全局变量,可以使hight函数返回多个值
int main() {
int x;
cin >> x;
a=hight(x);
cout << a << ' ' << b;
return 0;
}
int hight(int h) {
a = h / 10;
b = h % 10;
return a;//必须要返回一个值,否则报错
} //批量添加注释:ctrl+k+c;取消注释:ctrl+k+u
#include<iostream>
using namespace std;
const double f_m=60;
const double s_f=3600;
int main() {
int h, f, m;
double Ti;
cin >> h >> f >> m;
cout << "please enter the time:" << endl << "h:" << h << " f:" << f << " m:" << m << endl;
Ti = h + f / f_m + m / s_f;
cout << "the final time is:" << Ti;
return 0;
}
//c++函数模板
#include<iostream>
#include<string>
using namespace std;
template<typename T>
T add(T x, T y) {
return x + y;
}
int main() {
cout << add<int>(5, 3) << endl;
cout << add<double>(5.2, 3.14) << endl;
cout << add<string>("hello", " world");
return 0;
}