+## 实验一 递归练习
1、 输入2-10个大于0的正整数,如果输入0作为结束。
2、 输出这几个整数的全排列,每个数之间用半角“,”隔开,中间不要有空格,每个排列单独一行。
3、 程序一定要有Input、Output、End提示信息,但是不要有格式没有出现的其他提示,以下各实验相同。
4、 程序最后增加system(pause),显示Press any key to continue并暂停。
#include <iostream>
using namespace std;
void Permutation(int data[],int start,int end);
void Swap(int& a,int& b);
int main()
{
cout<<"Input"<<endl;
int data[10],i=0;
while(true){
cin>>data[i];
if(data[i]==0){
break;
}
i++;
}
cout<<"Ouput"<<endl;
Permutation(data,0,i);
// cout<<"Press any key to continue"<<endl;
cout<<"End"<<endl;
system("pause");
return 0;
}
void Permutation(int data[],int start,int end){
int i;
if(start==end){
for(int j=0;j<end;j++){
if(j==end-1){
cout<<data[j]<<endl;
break;
}else{
cout<<data[j]<<",";
}
}
}
for(i=start;i<end;i++){
Swap(data[start],data[i]);
Permutation(data,start+1,end);
Swap(data[start],data[i]);
}
}
void Swap(int& a,int& b){
int temp;
temp=a;a=b;b=temp;
}
运行结果:
实验二 排序算法
1、 输入2-10个不为零的正整数,遇到0代表输入结束,0不参与排序。
2、 数字选择排序方法,1-冒泡排序、2-插入排序、3-基数排序
3、 基数排序能够实现小于10的正整数的排序。
4、 使用所选排序方法的排序,结果输出所用方法以及结果,每个数之间用“,”隔开,中间不要有空格。
5、 输入输出请严格按下面要求的格式实现
// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
using namespace std;
void Bubble(int data[], int i);
void Print(int data[], int i);
void InsertSort(int data[], int i);
void Insert(int data[], int temp, int i);
void RadixSort(int data[], int i);
int main()
{
cout << "Input" << endl;
int data[11], i = 0;
while (true) {
cin >> data[i];
if (data[i] == 0) {
break;
}
i++;
}
cout << "1-冒泡排序,2-插入排序,3-基数排序" << endl;
int j;
cin >> j;
switch (j) {
case 1:
cout << "Ouput" << endl << "冒泡排序" << endl;
Bubble(data, i);
break;
case 2:
cout << "Ouput\n插入排序\n";
InsertSort(data, i);
break;
case 3:
cout << "Ouput\n基数排序\n";
RadixSort(data, i);
break;
}
cout << "End" << endl;
system("pause");
return 0;
}
void Bubble(int data[], int i) {
int temp = 0;
for (int j = 0; j < i; j++) {
for (int k = j; k < i; k++) {
if (data[j] > data[k]) {
temp = data[j]; data[j] = data[k]; data[k] = temp;
}
}
}
Print(data, i);
}
void InsertSort(int data[], int i) {
int temp;
for (int j = 1; j < i; j++) {
temp = data[j];
Insert(data, temp, j);
}
Print(data, i);
}
void RadixSort(int data[], int i) {
int **point = new int*[10];
int a = 0;
while (a < 10) {
point[a] = new int[10];
a++;
}
int b = 0, d;
for (int c = 1; c < 10; c++) {
d = 0; b = 0;
point[c][d] = 0;
while (b < i) {
if (data[b] == c) {
point[c][d] = c;
d++;
point[c][d] = 0;
}
b++;
}
}
b = 0;
for (int c = 1; c < 10; c++) {
d = 0;
while (point[c][d] != 0) {
data[b] = point[c][d];
b++; d++;
}
}
Print(data, i);
}
void Insert(int data[], int temp, int i) {
int j;
for (j = i - 1; j >= 0 && temp < data[j]; j--) {
data[j + 1] = data[j];
}
data[j + 1] = temp;
}
void Print(int data[], int i) {
for (int j = 0; j < i; j++) {
if (j == i - 1) {
cout << data[j] << endl;
break;
}
else {
cout << data[j] << ",";
}
}
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门提示:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
运行结果:
实验三 线性表操作
1、 创建线性表类。线性表的存储结构使用链表。
2、 完成表首插入元素、删除指定元素、搜索表中是否有指定元素、输出链表。
3、 输入n个不为零的整数作为节点元素值,遇到0代表输入结束(不创建元素值为0的节点),创建链表。输出整个链表。
4、 输入一个整数,将该数作为一个元素值插入表首位置。输出整个链表。
5、 输入一个整数,在链表中进行搜索,输出其在链表中的位置。如果不存在输出0。
6、 再一次输入一个整数,在链表中进行搜索,输出其在链表中的位置。如果不存在输出0。
7、 再一次输入n个不为零的整数作为节点元素值,遇到0代表输入结束(不创建元素值为0的节点),创建并输出整个链表。
8、 实现上面两个链表的合并,第一个链表在前第二个在后,输出合并后的链表。
9、 使用链表遍历器输出合并后的链表的反序输出。
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
using namespace std;
class Chain;
class ChainNode {
friend Chain;
private:
int data;
ChainNode *link;
};
class Chain {
public:
Chain() {
first = new ChainNode;
first->link = 0; first->data = 0;
}
Chain & Merge(Chain &x);
Chain & Append(int a);
void Output(ostream& out);
Chain & Insert(int i, int x);
int Find(int x);
Chain & Reverse();
private:
ChainNode *first;
};
Chain & Chain::Reverse() {
ChainNode *a;
ChainNode *b;
ChainNode *current;
if (first->link->link) {
current = first->link->link->link;
a = first->link;
b = a->link;
if (current) {
a->link = 0;
while (current) {
b->link = a;
a = b; b = current; current = current->link;
}
b->link = a; first->link = b;
}
else {
a->link = 0; b->link = a; first->link = b;
}
}
else {
return *this;
}
return *this;
}
Chain & Chain::Merge(Chain &x) {
ChainNode *current;
for (current = first->link; current->link; current = current->link) {
}
current->link = x.first->link;
return *this;
}
Chain & Chain::Append(int a) {
ChainNode *y;
y = new ChainNode;
y->data = a; y->link = 0;
ChainNode *current = first;
if (first) {
while (current->link) {
current = current->link;
}
current->link= y;
}else {
first = y;
}
return *this;
}
Chain & Chain::Insert(int i, int x) {
ChainNode *current = first;
ChainNode *newNode = new ChainNode;
newNode->data = x;
int a = 0;
while (a < i) {
current = current->link;
i++;
}
newNode->link = current->link;
current->link = newNode;
return *this;
}
int Chain::Find(int x) {
ChainNode *current;
int location=1;
for (current = first->link; current; current = current->link) {
if (current->data == x) {
break;
}else {
location++;
}
}
if (current) {
return location;
}
else {
return 0;
}
}
void Chain::Output(ostream& out) {
ChainNode *current;
for (current = first->link; current->link; current = current->link) {
out << current->data << ",";
}
out << current->data << endl;
}
ostream& operator<<(ostream& out, Chain& x) {
x.Output(out); return out;
}
int main()
{
cout << "Input1" << endl;
Chain *temp=new Chain;
int a=0;
while (cin >> a && a != 0) {
*temp=temp->Append(a);
}
cout << "Output1" << endl;
cout << *temp;
cout << "Input2" << endl;
cin >> a;
*temp = temp->Insert(0, a);
cout << "Output2" << endl;
cout << *temp;
cout << "Input3" << endl;
cin >> a;
cout << "Output3" << endl;
cout << temp->Find(a) << endl;
cout << "Input4" << endl;
cin >> a;
cout << "Output4" << endl;
cout << temp->Find(a) << endl;
cout << "Input5" << endl;
Chain *temp1 = new Chain;
while (cin >> a && a != 0) {
*temp1 = temp1->Append(a);
}
cout << "Output5" << endl;
cout << *temp1;
*temp=temp->Merge(*temp1);
cout << *temp;
*temp=temp->Reverse();
cout << *temp;
cout << "End" << endl;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门提示:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
运行结果:
实验四 堆栈的应用
1、 输入一个数学表达式(假定表达式输入格式合法),计算表达式结果并输出。
2、 数学表达式由单个数字和运算符“+”、“-”、“*”、“/”、“(、) ”构成,例如 2 + 3 * ( 4 + 5 ) - 6 / 4。
3、 变量、输出采用整数,只舍不入。