include stdio h int main cin scanf int








![struct AStudent { long ID; char name[10]; int sex; long birthday; }; . . struct AStudent { long ID; char name[10]; int sex; long birthday; }; . .](https://slidetodoc.com/presentation_image/21eddcca13d5083607306ea50e28f921/image-9.jpg)













- Slides: 22
输入输出 #include <stdio. h> int main() { 用cin 代替 scanf int a; double x; 代替 printf 用cout char s[1020]; scanf("%d%lf%s", &a, &x, s); printf("a=%d, x=%lf, s=%sn", a, x, s); return 0; } #include <iostream> using namespace std; int main() { int a; double x; char s[1020]; cin >> a >> x >> s; cout << "a=" << a << ", x=" << x << ", s=" << s << endl; return 0; }
变量的定义位置 #include <iostream> using namespace std; const double PI = 3. 14159; int main() { double x; cin >> x; double y = PI * x; cout << "y=" << y << endl; return 0; }
变量的定义位置 #include <iostream> using namespace std; int main() { for (int i = 0; i < 10; { cout << i << "*" << } for (int i = 0; i < 10; { for (int j = 0; j < { cout << "*"; } cout << endl; } return 0; } i++) i << "=" << i*i << endl; i++) i + 1; j++)
行注释 #include <iostream> using namespace std; int main() { // 输出 0~9的平方 for (int i = 0; i < 10; i++) { cout << i << "*" << i << "=" << i*i << endl; } // 输出一个三角形 for (int i = 0; i < 10; i++) { for (int j = 0; j < i + 1; j++) { cout << "*"; } cout << endl; } return 0; }
引用类型& 引用类型用作函数的参数类型 #include <iostream> using namespace std; void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x = 10, y = 20; swap(x, y); cout << x << ", " << y << endl; return 0; }
引用类型& 引用类型用作函数的参数类型 #include <iostream> using namespace std; void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 10, y = 20; swap(&x, &y); cout << x << ", " << y << endl; return 0; } #include <iostream> using namespace std; void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main() { int x = 10, y = 20; swap(x, y); cout << x << ", " << y << endl; return 0; }
struct AStudent { long ID; char name[10]; int sex; long birthday; }; . . . AStudent s 1, s 2, s 3[5], *sp=s 3; s 1 ID name sex birthday s 2 ID name sex birthday s 3 ID name sex birthday ID name sex birthday sp
#include <iostream> using namespace std; struct Node { double x, y; Node *next; }; int main() { Node a, b; Node *p = &a; a. next = &b; b. next = p; return 0; } a x y next p b x y next
动态内存分配 #include <iostream> #include <algorithm> using namespace std; int main() { cout << "请输入人数:"; int n; cin >> n; int *scores = new int[n]; cout << "请依次输入分数:"; for (int i = 0; i < n; i++) cin >> scores[i]; sort(scores, scores + n); for (int i = 0; i < n; i++) cout << scores[i] << ", "; cout << endl; delete[]scores; return 0; }
动态内存分配 场景 1:在设计时,m和n均为已知。 #include <iostream> using namespace std; const int m = 3, n = 5; int main() { int S[m][n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) S[i][j] = i * j; return 0; } S …… ……
动态内存分配 场景 2:在设计时,m已知,n未知。 #include <iostream> using namespace std; const int m = 10; int main() { int n; cin >> n; int *S[m]; S[0] = new int[m * n]; for (int i = 1; i < m; i++) S[i] = S[i - 1] + n; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) S[i][j] = i * j; delete[]S[0]; return 0; } S …… …… ……
动态内存分配 场景 3:在设计时,m未知,n已知。 #include <iostream> using namespace std; const int n = 10; int main() { int m; cin >> m; int (*S)[n]; S = new int[m][n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) S[i][j] = i * j; delete[]S; return 0; } S …… ……
动态内存分配 场景 4:在设计时,m和n均为未知。 #include <iostream> using namespace std; int main() { int m, n; cin >> m >> n; …… S int **S; S = new int*[m]; S[0] = new int[m * n]; for (int i = 1; i < m; i++) S[i] = S[i - 1] + n; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) S[i][j] = i * j; delete[]S[0]; delete[]S; return 0; } …… ……
函数设计 #include <iostream> using namespace std; 示例:统计考试成绩 const int N = 30; int main() 如果不仅要统计分数,还 希望知道分数对应的姓名, { 应该怎么办? int scores[N]; cout << "请输入所有的成绩:"; for (int i = 0; i < N; i++) cin >> scores[i]; // 以计算最高分为例 int max = scores[0]; for (int i = 1; i < N; i++) if (scores[i] > max) max = scores[i]; cout << "最高分:" << max << endl; return 0; }
#include <iostream> using namespace std; const int N = 30; struct Student { char Name[20]; int Score; }; int main() { Student students[N]; cout << "请依次输入姓名和成绩:"; for (int i = 0; i < N; i++) cin >> students[i]. Name >> students[i]. Score; // 以计算最高分为例 int maxi = 0; for (int i = 1; i < N; i++) if (students[i]. Score > students[maxi]. Score) maxi = i; cout << students[maxi]. Name << " 得到最高分 " << students[maxi]. Score << endl; return 0; }
#include <iostream> using namespace std; const int N = 30; struct Student { char Name[20]; int Score; }; int Input. Students(Student s[], int n) { if (s == NULL) return 1; if (n < 0) return 2; for (int i = 0; i < n; i++) cin >> s[i]. Name >> s[i]. Score; return 0; } int Get. Index. Of. Max(Student s[], int n) { if (s == NULL || n <= 0) return -1; int maxi = 0; for (int i = 1; i < n; i++) if (s[i]. Score > s[maxi]. Score) maxi = i; return maxi; } int main() { Student students[N]; cout << "请依次输入姓名和成绩:"; Input. Students(students, N); // 以计算最高分为例 int maxi = Get. Index. Of. Max(students, N); cout << students[maxi]. Name << " 得到最高分 " << students[maxi]. Score << endl; return 0; }
关于上机作业 作业平台:www. buctcoder. com 推荐开发环境: VS Code + Compiler Visual Studio 2019 Community Dev-Cpp Code-Block