河北大学程序设计训练营-day01

C++入门

第一步C++环境安装

安装DEVC++做演示

其他开发工具推荐: CodeBlocks、vscode、SublimeText

第一个==hello world==程序

1
2
3
4
5
6
7
8
#include<iostream>

using namespace std;

int main(){

cout << "hello world !" << endl;
}

基本语法程序

C++的基本类型和C语言无异

数值类型
  • 整型: (短整型)short 、(整型)int 、(长整型) long
  • 浮点类型: (单精度类型)float (双精度类型) double

字符类型:

  • 字符类型 : char
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
using namespace std;
int main()
{
//数字型
short short_num = 99;
int num = 999;
long long_num = 9999;
cout << "short_num = " << short_num << endl;
cout << "long_num = " << long_num << endl;
cout << "num = " << num << endl;
cout << "next::" << endl;

//字符型
char ch;
cin >> ch;
cout << "ch = " << ch;

//bool型
bool flag = true;
cout << "next::" << endl;
cout << flag << endl;
cout << false << endl;

//const修饰的作用: "常量"
const int i = 1;
cout << i << endl;
i = 100;

for(int j=0; j< 10; ++j){
cout << j;
}
if(1==1){
cout << "hello world" <<endl;
}

}

第一个测试程序

不再讲解

1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;
int main(){
int a,b;
cin >> a;
cin >> b;
cout << a+b;
}

第二题:

接收空格作为字符串的函数cin.getline(s,100)

把字符串转换为整数:**A = A * 10 + new_number**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
using namespace std;
//123 456
//1 1*10 + 2 (1*10+2) * 10 +3 A = A * 10 + new


int main(){
char s[100];
bool flagA = true , flagB =true;
int A = 0 , B = 0;

cin.getline(s,100);
int i;
for( i=0 ; s[i]!=' ';++i);

for( int j=0 ; j< i ;++j ){
if(s[j] >='0' && s[j] <='9'){
A = A * 10 + (s[j]-'0');
} else{
flagA = false;
break;
}
}
if(A ==0 || A >1000) flagA = false;

for( int j=i+1 ; s[j]!='\0' ;++j ){
if(s[j] >='0' && s[j] <='9'){
B = B * 10 + (s[j]-'0');
} else{
flagB = false;
break;
}
}
if(B ==0 || B >1000) flagB = false;

if(flagA){
if(flagB){
printf("%d + %d = %d",A,B,A+B);
}else{
printf("%d + ? = ?",A);
}
} else{
if(flagB){
printf("? + %d = ?",B);
}else{
printf("? + ? = ?");
}
}

}