cin.clear()的demo为什么出问题的原因

cin.clear()可将cin的iostate重置为goodbit,然后可再接受输入正常工作,然而下面这个demo是行不通的

#include <iostream> 
using namespace std; 
int main()  
{         
    int a;         
    while(1)         
    {                 
        cin>>a;                 
        if(!cin)            //条件可改写为cin.fail()                 
        {                         
            cout<<"输入有错!请重新输入"<<endl;                         
            cin.clear();                          
            cin.sync();   //清空流                 
        }                 
        else                 
        {                         
            cout<<a;                         
            break;                 
        }         
    }         
    system("pause"); 
}

这个代码跑起来是个死循环。为什么呢,因为cin没有清空那个错误的输入缓存。比如我给a输入字符类型'a',触发failbit,此时cin的buff里存的是'a',没有赋值给int a,没有flush。所以后面的cin都不会要求输入(缓存里有东西),直接把'a'给int a,当然也一直failbit。这个死循环就是这么来的。

解决办法就是把这个'a'拿走,cin.clear()后加cin.get()即可。

#include <iostream> #include <string> #include <cstring> #include <iomanip> using namespace std; // 结构体定义 struct Student { string name; int age; int math; int chinese; }; struct School { string teacher; string grade; Student students[40]; }; // 函数声明 void showMenu(); void helloWorldDemo(); void dataTypesDemo(); void operatorsDemo(); void controlStructuresDemo(); void arrayDemo(); void sortingDemo(); void stringDemo(); void functionDemo(); void pointerDemo(); void structDemo(); void searchDemo(); // 排序函数 void bubbleSort(int arr[], int n); void selectionSort(int arr[], int n); void bucketSort(); // 查找函数 void binarySearch(); // 函数参数传递演示 void valuePass(int a); void referencePass(int &a); void pointerPass(int *a); // 数组操作函数 void arrayOperations(); int main() { cout << "=== C++入门基础学习演示系统 ===" << endl; cout << "基于优快云博客教程实现" << endl; cout << "================================" << endl << endl; int choice; do { showMenu(); cout << "请选择要演示的功能 (0退): "; cin >> choice; cout << endl; switch(choice) { case 1: helloWorldDemo(); break; case 2: dataTypesDemo(); break; case 3: operatorsDemo(); break; case 4: controlStructuresDemo(); break; case 5: arrayDemo(); break; case 6: sortingDemo(); break; case 7: stringDemo(); break; case 8: functionDemo(); break; case 9: pointerDemo(); break; case 10: structDemo(); break; case 11: searchDemo(); break; case 0: cout << "感谢使用C++学习演示系统!" << endl; break; default: cout << "无效选择,请重新输入!" << endl; } if(choice != 0) { cout << "\n按回车键继续..."; cin.ignore(); cin.get(); cout << "\n" << string(50, '=') << "\n" << endl; } } while(choice != 0); return 0; } void showMenu() { cout << "?? C++基础知识演示菜单:" << endl; cout << "1. Hello World 程序" << endl; cout << "2. 数据类型演示" << endl; cout << "3. 运算符演示" << endl; cout << "4. 程序流程结构" << endl; cout << "5. 数组操作" << endl; cout << "6. 排序算法" << endl; cout << "7. 字符串处理" << endl; cout << "8. 函数使用" << endl; cout << "9. 指针操作" << endl; cout << "10. 结构体应用" << endl; cout << "11. 查找算法" << endl; cout << "0. 退程序" << endl; cout << string(30, '-') << endl; } void helloWorldDemo() { cout << "?? Hello World 程序演示" << endl; cout << "这是C++的第一个程序:" << endl; cout << "Hello World!" << endl; cout << "\n程序结构说明:" << endl; cout << "? #include<iostream> - 输入输头文件" << endl; cout << "? using namespace std - 使用标准命名空间" << endl; cout << "? int main() - 主函数,程序入口" << endl; cout << "? cout - 输语句" << endl; cout << "? return 0 - 程序正常结束" << endl; } void dataTypesDemo() { cout << "?? 数据类型演示" << endl; // 基本数据类型 bool boolVar = true; char charVar = 'A'; int intVar = 42; float floatVar = 3.14f; double doubleVar = 3.141592653; string stringVar = "Hello C++"; cout << "基本数据类型及其值:" << endl; cout << "bool类型: " << boolVar << " (占用 " << sizeof(boolVar) << " 字节)" << endl; cout << "char类型: " << charVar << " (占用 " << sizeof(charVar) << " 字节)" << endl; cout << "int类型: " << intVar << " (占用 " << sizeof(intVar) << " 字节)" << endl; cout << "float类型: " << floatVar << " (占用 " << sizeof(floatVar) << " 字节)" << endl; cout << "double类型: " << doubleVar << " (占用 " << sizeof(doubleVar) << " 字节)" << endl; cout << "string类型: " << stringVar << endl; // 常量演示 const int CONSTANT_VALUE = 100; cout << "\n常量演示:" << endl; cout << "常量值: " << CONSTANT_VALUE << endl; // 变量输入输 cout << "\n请输入一个整数: "; int userInput; cin >> userInput; cout << "您输入的数字是: " << userInput << endl; } void operatorsDemo() { cout << "?? 运算符演示" << endl; int a = 10, b = 3; cout << "设 a = " << a << ", b = " << b << endl; // 算术运算符 cout << "\n算术运算符:" << endl; cout << "a + b = " << (a + b) << endl; cout << "a - b = " << (a - b) << endl; cout << "a * b = " << (a * b) << endl; cout << "a / b = " << (a / b) << endl; cout << "a % b = " << (a % b) << endl; // 自增自减 int c = a; cout << "c++ = " << c++ << " (后自增,c现在是 " << c << ")" << endl; c = a; cout << "++c = " << ++c << " (先自增)" << endl; // 关系运算符 cout << "\n关系运算符:" << endl; cout << "a == b: " << (a == b) << endl; cout << "a != b: " << (a != b) << endl; cout << "a > b: " << (a > b) << endl; cout << "a < b: " << (a < b) << endl; cout << "a >= b: " << (a >= b) << endl; cout << "a <= b: " << (a <= b) << endl; // 逻辑运算符 cout << "\n逻辑运算符:" << endl; bool x = true, y = false; cout << "x && y: " << (x && y) << endl; cout << "x || y: " << (x || y) << endl; cout << "!x: " << (!x) << endl; // 三目运算符 cout << "\n三目运算符:" << endl; int max = (a > b) ? a : b; cout << "max(a, b) = " << max << endl; } void controlStructuresDemo() { cout << "?? 程序流程结构演示" << endl; // if-else 演示 cout << "\n1. if-else 语句演示:" << endl; int score = 85; cout << "学生成绩: " << score << endl; if(score >= 90) { cout << "成绩等级: 优秀" << endl; } else if(score >= 80) { cout << "成绩等级: 良好" << endl; } else if(score >= 60) { cout << "成绩等级: 及格" << endl; } else { cout << "成绩等级: 不及格" << endl; } // switch 演示 cout << "\n2. switch 语句演示:" << endl; int choice = 2; cout << "选择技能 " << choice << ": "; switch(choice) { case 1: cout << "一技能" << endl; break; case 2: cout << "二技能" << endl; break; case 3: cout << "三技能" << endl; break; default: cout << "无效技能" << endl; } // for 循环演示 cout << "\n3. for 循环演示:" << endl; cout << "输1到5: "; for(int i = 1; i <= 5; i++) { cout << i << " "; } cout << endl; // while 循环演示 cout << "\n4. while 循环演示:" << endl; cout << "倒计时: "; int countdown = 5; while(countdown > 0) { cout << countdown << " "; countdown--; } cout << "发射!" << endl; // do-while 循环演示 cout << "\n5. do-while 循环演示:" << endl; int num = 1; cout << "至少执行一次: "; do { cout << num << " "; num++; } while(num <= 3); cout << endl; // 嵌套循环演示 cout << "\n6. 嵌套循环演示 (打印星号矩形):" << endl; for(int i = 0; i < 4; i++) { for(int j = 0; j < 6; j++) { cout << "* "; } cout << endl; } } void arrayDemo() { cout << "?? 数组操作演示" << endl; // 一维数组 cout << "\n1. 一维数组演示:" << endl; int arr[5] = {10, 20, 30, 40, 50}; cout << "数组元素: "; for(int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; cout << "数组大小: " << sizeof(arr) << " 字节" << endl; cout << "元素个数: " << sizeof(arr)/sizeof(arr[0]) << endl; cout << "首地址: " << arr << endl; // 二维数组 cout << "\n2. 二维数组演示 (学生成绩表):" << endl; int scores[3][3] = { {89, 95, 98}, {79, 78, 56}, {90, 99, 93} }; string names[3] = {"张三", "李四", "王五"}; string subjects[3] = {"语文", "数学", "英语"}; cout << setw(8) << "姓名"; for(int j = 0; j < 3; j++) { cout << setw(8) << subjects[j]; } cout << setw(8) << "总分" << endl; for(int i = 0; i < 3; i++) { cout << setw(8) << names[i]; int sum = 0; for(int j = 0; j < 3; j++) { cout << setw(8) << scores[i][j]; sum += scores[i][j]; } cout << setw(8) << sum << endl; } // 数组操作演示 arrayOperations(); } void arrayOperations() { cout << "\n3. 数组增删改查演示:" << endl; char arr[10] = {'a', 'b', 'c', 'd', 'e'}; int size = 5; cout << "初始数组: "; for(int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; // 插入操作 char insertChar = 'X'; int insertPos = 2; for(int i = size; i > insertPos; i--) { arr[i] = arr[i-1]; } arr[insertPos] = insertChar; size++; cout << "在位置" << insertPos << "插入'" << insertChar << "': "; for(int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; // 删除操作 int deletePos = 1; for(int i = deletePos; i < size-1; i++) { arr[i] = arr[i+1]; } size--; cout << "删除位置" << deletePos << "的元素: "; for(int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; // 修改操作 arr[0] = 'Z'; cout << "修改第一个元素为'Z': "; for(int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; // 查找操作 char searchChar = 'c'; int foundPos = -1; for(int i = 0; i < size; i++) { if(arr[i] == searchChar) { foundPos = i; break; } } if(foundPos != -1) { cout << "字符'" << searchChar << "'在位置: " << foundPos << endl; } else { cout << "未找到字符'" << searchChar << "'" << endl; } } void sortingDemo() { cout << "?? 排序算法演示" << endl; // 冒泡排序演示 cout << "\n1. 冒泡排序演示:" << endl; int bubbleArr[] = {64, 34, 25, 12, 22, 11, 90}; int n1 = sizeof(bubbleArr)/sizeof(bubbleArr[0]); cout << "排序前: "; for(int i = 0; i < n1; i++) { cout << bubbleArr[i] << " "; } cout << endl; bubbleSort(bubbleArr, n1); cout << "排序后: "; for(int i = 0; i < n1; i++) { cout << bubbleArr[i] << " "; } cout << endl; // 选择排序演示 cout << "\n2. 选择排序演示:" << endl; int selectionArr[] = {29, 10, 14, 37, 13}; int n2 = sizeof(selectionArr)/sizeof(selectionArr[0]); cout << "排序前: "; for(int i = 0; i < n2; i++) { cout << selectionArr[i] << " "; } cout << endl; selectionSort(selectionArr, n2); cout << "排序后: "; for(int i = 0; i < n2; i++) { cout << selectionArr[i] << " "; } cout << endl; // 桶排序演示 cout << "\n3. 桶排序演示:" << endl; bucketSort(); } void bubbleSort(int arr[], int n) { for(int i = 0; i < n-1; i++) { bool swapped = false; for(int j = 0; j < n-i-1; j++) { if(arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; swapped = true; } } if(!swapped) break; // 优化:如果没有交换,说明已经有序 } } void selectionSort(int arr[], int n) { for(int i = 0; i < n-1; i++) { int minIdx = i; for(int j = i+1; j < n; j++) { if(arr[j] < arr[minIdx]) { minIdx = j; } } if(minIdx != i) { int temp = arr[i]; arr[i] = arr[minIdx]; arr[minIdx] = temp; } } } void bucketSort() { const int N = 20; int bucket[N] = {0}; int data[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; int dataSize = sizeof(data)/sizeof(data[0]); cout << "原始数据: "; for(int i = 0; i < dataSize; i++) { cout << data[i] << " "; } cout << endl; // 统计每个数字现的次数 for(int i = 0; i < dataSize; i++) { bucket[data[i]]++; } cout << "桶排序结果: "; for(int i = 0; i < N; i++) { for(int j = 0; j < bucket[i]; j++) { cout << i << " "; } } cout << endl; } void stringDemo() { cout << "?? 字符串处理演示" << endl; // C风格字符串 cout << "\n1. C风格字符串操作:" << endl; char str1[20] = "Hello"; char str2[20] = "World"; char str3[40]; cout << "str1: " << str1 << endl; cout << "str2: " << str2 << endl; cout << "str1长度: " << strlen(str1) << endl; // 字符串拷贝 strcpy(str3, str1); cout << "拷贝str1到str3: " << str3 << endl; // 字符串连接 strcat(str3, " "); strcat(str3, str2); cout << "连接后的str3: " << str3 << endl; // 字符串比较 cout << "strcmp(str1, str2): " << strcmp(str1, str2) << endl; // C++ string类 cout << "\n2. C++ string类操作:" << endl; string s1 = "Hello"; string s2 = "World"; string s3 = s1 + " " + s2; cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; cout << "s3: " << s3 << endl; cout << "s3长度: " << s3.length() << endl; // 查找操作 size_t pos = s3.find("World"); if(pos != string::npos) { cout << "在s3中找到'World',位置: " << pos << endl; } // 子字符串 string sub = s3.substr(0, 5); cout << "s3的前5个字符: " << sub << endl; } void functionDemo() { cout << "?? 函数使用演示" << endl; cout << "\n1. 参数传递方式演示:" << endl; int value = 10; cout << "原始值: " << value << endl; // 值传递 valuePass(value); cout << "值传递后: " << value << endl; // 引用传递 referencePass(value); cout << "引用传递后: " << value << endl; // 指针传递 pointerPass(&value); cout << "指针传递后: " << value << endl; } void valuePass(int a) { a = a * 2; cout << "函数内值传递: " << a << endl; } void referencePass(int &a) { a = a * 2; cout << "函数内引用传递: " << a << endl; } void pointerPass(int *a) { *a = *a * 2; cout << "函数内指针传递: " << *a << endl; } void pointerDemo() { cout << "?? 指针操作演示" << endl; // 基本指针操作 cout << "\n1. 基本指针操作:" << endl; int a = 42; int *p = &a; cout << "变量a的值: " << a << endl; cout << "变量a的地址: " << &a << endl; cout << "指针p的值(a的地址): " << p << endl; cout << "指针p指向的值: " << *p << endl; // 修改指针指向的值 *p = 100; cout << "通过指针修改后,a的值: " << a << endl; // const修饰指针 cout << "\n2. const修饰指针:" << endl; int b = 20; const int *p1 = &a; // 指向常量的指针 int * const p2 = &a; // 常量指针 cout << "const int *p1 = &a; (指向常量的指针)" << endl; cout << "int * const p2 = &a; (常量指针)" << endl; // 指针与数组 cout << "\n3. 指针与数组:" << endl; int arr[] = {1, 2, 3, 4, 5}; int *arrPtr = arr; cout << "数组元素: "; for(int i = 0; i < 5; i++) { cout << *(arrPtr + i) << " "; } cout << endl; cout << "使用指针遍历: "; for(int *ptr = arr; ptr < arr + 5; ptr++) { cout << *ptr << " "; } cout << endl; } void structDemo() { cout << "??? 结构体应用演示" << endl; // 基本结构体使用 cout << "\n1. 基本结构体使用:" << endl; Student stu1; stu1.name = "张三"; stu1.age = 18; stu1.math = 95; stu1.chinese = 88; cout << "学生信息:" << endl; cout << "姓名: " << stu1.name << endl; cout << "年龄: " << stu1.age << endl; cout << "数学: " << stu1.math << endl; cout << "语文: " << stu1.chinese << endl; // 结构体数组 cout << "\n2. 结构体数组:" << endl; Student students[3] = { {"李四", 19, 92, 85}, {"王五", 18, 88, 90}, {"赵六", 20, 95, 87} }; cout << setw(8) << "姓名" << setw(6) << "年龄" << setw(8) << "数学" << setw(8) << "语文" << setw(8) << "总分" << endl; for(int i = 0; i < 3; i++) { int total = students[i].math + students[i].chinese; cout << setw(8) << students[i].name << setw(6) << students[i].age << setw(8) << students[i].math << setw(8) << students[i].chinese << setw(8) << total << endl; } // 结构体指针 cout << "\n3. 结构体指针:" << endl; Student *stuPtr = &stu1; cout << "通过指针访问:" << endl; cout << "姓名: " << stuPtr->name << endl; cout << "年龄: " << stuPtr->age << endl; // 嵌套结构体 cout << "\n4. 嵌套结构体(班级管理):" << endl; School class1; class1.grade = "高一(3)班"; class1.teacher = "李老师"; class1.students[0] = {"小明", 16, 90, 85}; class1.students[1] = {"小红", 16, 88, 92}; cout << "班级: " << class1.grade << endl; cout << "班主任: " << class1.teacher << endl; cout << "学生信息:" << endl; for(int i = 0; i < 2; i++) { cout << " " << class1.students[i].name << " - 数学:" << class1.students[i].math << " 语文:" << class1.students[i].chinese << endl; } } void searchDemo() { cout << "?? 查找算法演示" << endl; cout << "\n二分查找演示:" << endl; binarySearch(); } void binarySearch() { int arr[] = {1, 2, 4, 6, 8, 9, 13, 15, 20, 25}; int n = sizeof(arr)/sizeof(arr[0]); cout << "有序数组: "; for(int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; int target = 13; cout << "查找目标: " << target << endl; int left = 0, right = n - 1; int steps = 0; bool found = false; while(left <= right) { steps++; int mid = (left + right) / 2; cout << "第" << steps << "步: 检查位置" << mid << ",值为" << arr[mid]; if(arr[mid] == target) { cout << " - 找到目标!" << endl; cout << "目标" << target << "在数组中的索引位置: " << mid << endl; found = true; break; } else if(arr[mid] < target) { cout << " - 目标在右半部分" << endl; left = mid + 1; } else { cout << " - 目标在左半部分" << endl; right = mid - 1; } } if(!found) { cout << "未找到目标值" << endl; } cout << "总共进行了 " << steps << " 步查找" << endl; } 详细分析
11-01
#include <time.h> #include <windows.h> #include <iostream> #include <map> #include <queue> #include <vector> #include "eightFigurePuzzles.h" using namespace std; //用于记录当前状态是否被访问过。 map<int, int> visited; //深度有限搜索,用于限制深度。 #define MAX_DEPTH 20 //openList与closeList用于A*搜索。 vector<PUZZLE_NODE> closeList; vector<PUZZLE_NODE> openList; //广度优先搜索 int* binaryFirstSearch(PUZZLE_NODE initialNode, PUZZLE_NODE objPuzzleNode) { //result[0] 1:correct;0:wrong //result[1] 步数 steps int* result = new int[2]{0, 0}; /* 请在该位置完成广度优先搜索。 */ if (checkObject(initialNode, objPuzzleNode)) { result[0] = 1; } else { result[0] = 0; } return result; } //深度有限搜索 int* depthFirstSearch(PUZZLE_NODE initialNode, PUZZLE_NODE objPuzzleNode) { //result[0] 1:correct;0:wrong //result[1] 步数 steps int* result = new int[2]{0, 0}; /* 请在该位置完成深度有限搜索,最大深度限度为25。 */ if (checkObject(initialNode, objPuzzleNode) && initialNode.depth < MAX_DEPTH) { result[0] = 1; } else { result[0] = 0; } return result; } //启发式搜索1 int* heuristicSearchInformedByIncorrectNum(PUZZLE_NODE initialNode, PUZZLE_NODE objPuzzleNode) { //result[0] 1:correct;0:wrong //result[1] 步数 steps int* result = new int[2]{0, 0}; /* 请在该位置完成启发式搜索,启发式函数使用不正确位置的数码个数。 */ if (checkObject(initialNode, objPuzzleNode)) { result[0] = 1; } else { result[0] = 0; } return result; } //启发式搜素2 int* heuristicSearchInformedByManhattonDis(PUZZLE_NODE initialNode, PUZZLE_NODE objPuzzleNode) { //result[0] 1:correct;0:wrong //result[1] 步数 steps int* result = new int[2]{0, 0}; /* 请在该位置完成启发式搜索,启发式函数采用到目标位置的曼哈顿距离。 */ if (checkObject(initialNode, objPuzzleNode)) { result[0] = 1; } else { result[0] = 0; } return result; } //广度优先搜索 int* binaryFirstSearchDemo(PUZZLE_NODE initialNode, PUZZLE_NODE objPuzzleNode) { //result[0] 1:correct;0:wrong //result[1] 步数 steps int* result = new int[2]{0, 0}; cout << "初始节点状态:" << endl; for (int i = 0; i < 3; i++) { cout << " " << initialNode.puzzle[i * 3 + 0].puzzleId << " " << initialNode.puzzle[i * 3 + 1].puzzleId << " " << initialNode.puzzle[i * 3 + 2].puzzleId << endl; } cout << endl; /* 请在该位置完成广度优先搜索函数。 */ PUZZLE_NODE puzzleNode = initialNode; queue<PUZZLE_NODE> puzzleNodeQueue; puzzleNode.depth = 0; int depth = 0; puzzleNodeQueue.push(puzzleNode); while (puzzleNodeQueue.size()) { PUZZLE_NODE currentPuzzleNode = puzzleNodeQueue.front(); if (checkObject(currentPuzzleNode, objPuzzleNode)) { for (int i = 0; i < currentPuzzleNode.precedeActionList.size(); i++) { outputAction(currentPuzzleNode.precedeActionList[i], i + 1); } cout << "找到正确结果:" << endl; for (int i = 0; i < 3; i++) { cout << " " << currentPuzzleNode.puzzle[i * 3 + 0].puzzleId << " " << currentPuzzleNode.puzzle[i * 3 + 1].puzzleId << " " << currentPuzzleNode.puzzle[i * 3 + 2].puzzleId << endl; } cout << endl; result[0] = 1; result[1] = currentPuzzleNode.depth; return result; } else { visited[visitedNum(currentPuzzleNode)] = 1; if (currentPuzzleNode.nextActionList.size() == 0) { currentPuzzleNode = updatePuzzleNodeActionList(currentPuzzleNode); } puzzleNodeQueue.pop(); for (int i = 0; i < currentPuzzleNode.nextActionList.size(); i++) { PUZZLE_NODE nextPuzzleNode = moveToPuzzleNode(currentPuzzleNode.nextActionList[i], currentPuzzleNode); if (!currentPuzzleNode.precedeActionList.empty()) { for (int actionIndex = 0; actionIndex < currentPuzzleNode.precedeActionList.size(); actionIndex++) { nextPuzzleNode.precedeActionList.push_back(currentPuzzleNode.precedeActionList[actionIndex]); } } nextPuzzleNode.precedeActionList.push_back(currentPuzzleNode.nextActionList[i]); if (visited[visitedNum(nextPuzzleNode)] == 1) { continue; } nextPuzzleNode.depth = currentPuzzleNode.depth + 1; puzzleNodeQueue.push(nextPuzzleNode); } } } return result; } int main() { PUZZLE_NODE objPuzzleNode; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { objPuzzleNode.puzzle[i * 3 + j].puzzleId = i * 3 + j; objPuzzleNode.puzzle[i * 3 + j].xPosition = i; objPuzzleNode.puzzle[i * 3 + j].yPosition = j; } } objPuzzleNode = updatePuzzleNodeActionList(objPuzzleNode); int setup = 0; while (setup != -1) { visited.clear(); cout << "请输入调试设置(-1:退; 0:广度优先搜索示例;1:广度优先搜索;2:深度有限搜索;3:启发式搜索1;4:启发式搜索2):" << endl; cin >> setup; int backwardSteps; cout << "请输入大于等于5小于等于20的回退步数" << endl; cin >> backwardSteps; while (backwardSteps < 5 || backwardSteps > 20) { cout << "输入错误,请输入大于等于5小于等于20的回退步数" << endl; cin >> backwardSteps; } PUZZLE_NODE initialNode = initialPuzzleNode(backwardSteps); int* result; if (setup == 1) { result = binaryFirstSearch(initialNode, objPuzzleNode); } else if (setup == 2) { result = depthFirstSearch(initialNode, objPuzzleNode); } else if (setup == 3) { result = heuristicSearchInformedByIncorrectNum(initialNode, objPuzzleNode); } else if (setup == 4) { result = heuristicSearchInformedByManhattonDis(initialNode, objPuzzleNode); } else if (setup == 0) { cout << "广度优先搜索示例程序" << endl; result = binaryFirstSearchDemo(initialNode, objPuzzleNode); } else { cout << "输入设置有误,请重新运行" << endl; return 0; } if (result[0] == 1) { cout << "结果为correct,步数为" << result[1] << endl; } else { cout << "结果为wrong" << endl; } delete [] result; } return 0; }
最新发布
11-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值