C++学习记录
两小题:
- 十个数冒泡排序:
#include "stdafx.h"
#include <iostream>
#define n 10
using namespace std;
int main() {
//输入十个数字并排序输出;
int num[n];
int i = 0;
int exchange;
cout << "请输入十个数:" << endl;
for (i = 0; i < n; i++) {
cin >> num[i];
}
for (int i = 1; i <= n - 1; i++) //冒泡排序算法
{
for (int j = 1; j <= n - i; j++)
{
if (num[j - 1]>num[j])
{
exchange = num[j - 1];
num[j - 1] = num[j];
num[j] = exchange;
}
}
}
cout << "排序后:" << endl;
for (i = 0; i < n; i++) {
cout << num[i] << "\t";
}
system("pause");
return 0;
}
- 文件操作 - 写文件
// file_part.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string name;
string num;
string class_inf;
//信息输入
cout << "请输入姓名,学号,班级信息:" << endl;
cin >> name;
cin >> num;
cin >> class_inf;
//写入信息
ofstream infor("name.txt");
if (infor.is_open()) {
infor << name << "\t" << num << "\t" << class_inf << endl;
infor.close();
cout << "数据已经写入,文件已关闭、";
}
return 0;
}
- 第一次写C++
C++课程的小程序、用来熟悉C++代码。
程序简单。
对文件读写的理解和基本语法的掌握。