报告
# 进制转换
简易版本:不考虑了负数 输入要大于0 进制数不要大于10
高级版本(点击):报告上交后发出 可以负数 任意进制
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAXSIZE 50 typedef int Status; typedef int ElemType; typedef struct { ElemType* base; ElemType* top; int stacksize; }SqStack; typedef enum { f, t }Bool;
Status initStack(SqStack* S) { S->base = new ElemType[MAXSIZE]; if (!S->base) { exit(OVERFLOW); } S->top = S->base; S->stacksize = MAXSIZE; return OK; }
Bool isEmpty(SqStack* S) { return S->base == S->top ? t : f; }
int getLength(SqStack* S) { return S->top - S->base; }
Status pushElem(SqStack* S, ElemType e) { if (getLength(S) == S->stacksize)return ERROR; *(S->top) = e; S->top++; return OK; }
ElemType popElem(SqStack* S) { if (isEmpty(S))return NULL; S->top--; return *(S->top); }
ElemType getTop(SqStack* S) { if (!isEmpty(S))return *(S->top - 1); return NULL; }
Status conversion(int n, int k) { SqStack* S = new SqStack; initStack(S); while (n) { pushElem(S, n%k); n = n / k; } while (!isEmpty(S)) { printf("%d", popElem(S)); } return OK; } int main(void) { int n=0, k = 0; while (!(n > 0)) { printf("请输入需要转换的数(大于0): "); scanf_s("%d", &n); } while (!(k>=2)) { printf("请输入进制数(例如 2且大于2)"); scanf_s("%d", &k); } conversion(n, k); printf("\n"); system("pause"); return 0; }
|
汉诺塔
简易版本:输出移动步骤
高级版本(点击):报告上交后发出 输出移动画面(如果能力足够)
层数不要太高 电脑不行
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
| #include<stdio.h> #include<stdlib.h> void hanoi(int n, char A, char B, char C) { if (n==1) { printf("%c -> %c\n", A, C); } else { hanoi(n - 1, A, C, B); printf("%c -> %c\n", A, C); hanoi(n - 1, B, A, C); } } int main(void) { int n = 0; while (!(n > 0)) { printf("请输入汉罗塔层数的数(大于0): "); scanf_s("%d", &n); } hanoi(n, 'A', 'B', 'C'); system("pause"); }
|
括号匹配
逻辑和书差不多 不过改了一些 暂时不能细说 改进版本(点击)
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| #include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAXSIZE 50 typedef int Status; typedef char ElemType;
typedef struct { ElemType* base; ElemType* top; int stacksize; }SqStack; typedef enum { f, t }Bool;
Status initStack(SqStack* S) { S->base = new ElemType[MAXSIZE]; if (!S->base) { exit(OVERFLOW); } S->top = S->base; S->stacksize = MAXSIZE; return OK; }
Bool isEmpty(SqStack* S) { return S->base == S->top ? t : f; }
int getLength(SqStack* S) { return S->top - S->base; }
Status pushElem(SqStack* S, ElemType e) { if (getLength(S) == S->stacksize)return ERROR; *(S->top) = e; S->top++; return OK; }
ElemType popElem(SqStack* S) { if (isEmpty(S))return NULL; S->top--; return *(S->top); }
ElemType getTop(SqStack* S) { if (!isEmpty(S))return *(S->top - 1); return NULL; }
Status cleanStack(SqStack* S) { S->top = S->base; return OK; }
Status matchBrackets(SqStack* S) { bool flag = true; char ch; int i = 1; cout << "请输入英文状态下的括号,可复制 (){}[],以#结束" << endl; cin >> ch; while (ch!='#'&&flag) { switch (ch) { case '(': case '{': case '[': pushElem(S, ch); break; case ')': if (!isEmpty(S)&&getTop(S)=='(') { popElem(S); } else { flag = false; cout << ch<<"输入错误,匹配"<<i <<" ( 失败" << endl; } break; case '}': if (!isEmpty(S) && getTop(S) == '{') { popElem(S); } else { flag = false; cout << ch << "输入错误,匹配" << i << " { 失败" << endl; } break; case ']': if (!isEmpty(S) && getTop(S) == '[') { popElem(S); } else { flag = false; cout << ch << "输入错误,匹配" << i << " ] 失败" << endl; } break; case '#': break; case '\n': break; default: flag = false; cout << ch; cout << "输入非法内容\n"; break; } ++i; cin >> ch; } if (isEmpty(S) && flag) { cout << "匹配成功" << endl; return OK; } else { cout << "匹配失败" << endl; cleanStack(S); return ERROR; } } int main(void) { SqStack* S = new SqStack; initStack(S); matchBrackets(S); cout << "第二次匹配" << endl; matchBrackets(S); cout << "第三次匹配" << endl; matchBrackets(S); cout << "第四次匹配" << endl; matchBrackets(S); printf("\n"); system("pause"); return 0; }
|
舞伴
不细说 文件下载(点击)
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| #include <iostream> #include <fstream> #include <vector> #include <sstream> using namespace std; #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAXSIZE 50 typedef int Status; typedef string ElemType; typedef struct { ElemType* base; int front; int rear; }SqQueue; typedef enum { f, t }Bool;
Status initQueue(SqQueue* Q) { Q->base = new ElemType[MAXSIZE]; if (!Q->base) { return OVERFLOW; } Q->front = Q->rear = 0; return OK; }
Bool isEmpty(SqQueue* Q) { return Q->front == Q->rear ? t : f; }
int getLength(SqQueue* Q) { return (Q->rear - Q->front + MAXSIZE) % MAXSIZE; }
Bool isFull(SqQueue* Q) { return (Q->rear + 1) % MAXSIZE == Q->front ? t : f; }
Status enQueue(SqQueue* Q, ElemType e) { if (isFull(Q)) { return ERROR; } Q->base[Q->rear] = e; Q->rear = (Q->rear + 1) % MAXSIZE; return OK; }
ElemType deQueue(SqQueue* Q) { if (isEmpty(Q))return ERROR; ElemType e = Q->base[Q->front]; Q->front = (Q->front + 1) % MAXSIZE; return e; }
ElemType getHead(SqQueue* Q) { if (!isEmpty(Q))return Q->base[Q->front]; return NULL; } int main() { ifstream inFile("D:\\桌面\\舞伴.csv", ios::in); string lineStr; vector<vector<string>> strArray; while (getline(inFile, lineStr)) { stringstream ss(lineStr); string str; vector<string> lineArray; while (getline(ss, str, ',')) lineArray.push_back(str); strArray.push_back(lineArray); } for (int i = 0; i<strArray.size(); i++) { for (int j = 0; j<strArray[i].size(); j++) cout << strArray[i][j] << "\t"; cout << endl; } SqQueue* QM = new SqQueue; initQueue(QM); SqQueue* QW = new SqQueue; initQueue(QW); for (int i = 0; i<strArray.size(); i++) { for (int j = 0; j<strArray[i].size()-1 ; j++) { if ("男"== strArray[i][j+1]) { enQueue(QM, strArray[i][j]); } else if ("女" == strArray[i][j + 1]) { enQueue(QW, strArray[i][j]); } } } int k = 1; while ((!isEmpty(QM))&&(!isEmpty(QW))) { cout << k << deQueue(QM) << " 和\t" << deQueue(QW) << " 跳舞" << endl; k++; } if (isEmpty(QM)) { cout << getHead(QW) << " 等待下一次匹配" << endl; } if (isEmpty(QW)) { cout << getHead(QM) << " 等待下一次匹配" << endl; } system("pause"); return 0; }
|