運行程式后,2功能里面的有些功能不能用。求改正。
#pragma warning(disable:4996)#include<stdio.h>#include<malloc.h>#include<stdlib.h>#include <windows.h>
#include<iostream> //清屏函式頭檔案#include <string.h>using namespace std;#define MAX 100#define MAXSIZE 20 //電話薄記錄數量#define MAX_SIZE 20 //人名的最大長度#define HASHSIZE 53 //定義表長#define OVERFLOW -1typedef struct Stuff{char name[20]; //員工姓名char phone[25]; //員工電話char add[10]; //員工住址struct Stuff *next; }Node, *Lnode;Lnode hash[];#define OK 1#define ERROR 0#define STACK_INIT_SIZE 100#define STACKINCREMENT 10
//#define OVERFLOW -1#define M 49#define N 49using namespace std;int maze[M][N];typedef int Status;typedef struct
{int m, n, direc;}MazeType, *LMazeType;typedef struct{LMazeType top;LMazeType base;int stacksize;int over; }Stack;
void Init_hand_Maze(int maze[M][N], int m, int n)
{int i, j;for (i = 1; i <= m + 1; i++)for (j = 1; j <= n + 1; j++)
{maze[i][j] = 1;}cout << "請按行輸入迷宮,0表示通路,1表示障礙:" << endl;
for (i = 1; i<m + 1; i++)for (j = 1; j<n + 1; j++)cin >> maze[i][j];for (i = 1; i<m + 1; i++)
{for (j = 1; j<n + 1; j++){if (maze[i][j] != 0 && maze[i][j] != 1){cout << " 您輸入有誤,請重新輸入";Init_hand_Maze(maze, m, n);}}}}
void Init_automatic_Maze(int maze[M][N], int m, int n) //自動生成迷宮
{int i, j;//cout << "\n迷宮生成中……\n\n";//system("pause");for (i = 1; i<m + 1; i++)for (j = 1; j<n + 1; j++)maze[i][j] = rand() % 2; //隨機生成0、1}void PrintMaze(int maze[M][N], int row, int col)
{int i, j;cout << "迷宮如圖所示." << endl;for (i = 1; i<row + 1; i++){for (j = 1; j<col + 1; j++){if (maze[i][j] == 1)cout << "1";elsecout << "0";}cout << endl;}}Status InitStack(Stack &S){S.base = (LMazeType)malloc(STACK_INIT_SIZE * sizeof(MazeType));
if (!S.base)exit(OVERFLOW);S.top = S.base;S.stacksize = STACK_INIT_SIZE;S.over = 0;return OK;}Status Push(Stack &S, MazeType e){if (S.top - S.base >= S.stacksize){S.base = (LMazeType)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(MazeType));if (!S.base)exit(OVERFLOW);S.top = S.base + S.stacksize;S.stacksize += STACKINCREMENT;}*S.top++ = e;return OK;}Status Pop(Stack &S, MazeType &e){if (S.top == S.base)return ERROR;e = *--S.top;return OK;}
Status MazePath(Stack &S, MazeType &e, int maze[M][N], int m, int n){do{if (maze[e.m][e.n] == 0)//0可通,1不可通,2為已走過 {Push(S, e);maze[e.m][e.n] = 2;if (e.m == m&&e.n == n){S.over = 1;//表示存滿一條路徑 return OK;}else {e.n++;e.direc = 0;//來這一點時的方向,0右1下2左3上MazePath(S, e, maze, m, n);}}else{if (S.top != S.base&&S.over != 1){switch (e.direc) //回到上一位置并同時改變方向走下一步 {case 0:e.n--;e.m++;e.direc = 1;break;case 1:e.m--;e.n--;e.direc = 2;break;case 2:e.n++;
e.m--;e.direc = 3;break;case 3:Pop(S, e);break;}}}} while (S.top != S.base&&S.over != 1);return OK;}
int PrintPath(Stack S, int maze[M][N], int row, int col){if (S.top == S.base){cout << "\n===============================================\n";cout << "此迷宮無解\n\n";return ERROR;}MazeType e;
while (S.top != S.base)Pop(S, e);maze[e.m][e.n] = (e.direc + 10);}cout << "完成!" << endl;cout << "\n===============================================\n";cout << "路徑為:" << endl;int i, j;for (i = 1; i<row + 1; i++){
for (j = 1; j<col + 1; j++){switch (maze[i][j]){case 0:cout << "0";break;case 1:cout << "1";break;case 2:cout << "※";break;
case10:cout << "→";break;case 11:cout << "↓";break;case 12:cout << "←";break;case 13:cout << "↑";break;}}cout << endl;}
cout << "入口" << endl;cout << "完成!" << endl;return OK;}void Maze(){int i, m, n, cycle = 0;while (cycle != (-1))
{cout << "********************************************************************************\n";cout << " 歡迎進入迷宮求解系統\n";cout << endl;cout << "**********************************************************\n";cout << " 1 手動生成迷宮 \n";cout << " 2 自動生成迷宮 \n";cout << " 3 退出 \n\n";
cout << "********************************************************************************\n";cout << "\n";cout << "請選擇你的操作:\n";cin >> i;switch (i){case 1:cout << "\n請輸入行數:";cin >> m;cout << "\n";cout << "請輸入列數:";cin >> n;while ((m<1 || m>49) || (n<1 || n>49)){cout << "\n抱歉,你輸入的行列數超出預設范圍(1-49,1-49),請重新輸入:\n\n";cout << "\n請輸入行數:";
cin >> m;cout << "\n";cout << "請輸入列數:";cin >> n;}Init_hand_Maze(maze, m, n);PrintMaze(maze, m, n);MazeType start, end;cout << "請輸入起點m n:" << endl;cin >> start.m >> start.n;start.direc = 0;cout << "請輸入終點m n:" << endl;cin >> end.m >> end.n;Stack S;cout << "尋找路徑..." << endl;InitStack(S);MazePath(S, start, maze, end.m, end.n);PrintPath(S, maze, m, n);system("pause");cout << "\n\nPress Enter Contiue!\n";getchar();while (getchar() != '\n'); //接受一個輸入,當為回車時執行break跳出,否則一直執行接受資料break;case 2:cout << "\n請輸入行數:";cin >> m;cout << "\n";cout << "請輸入列數:";cin >> n;while ((m<0 || m>49) || (n<0 || n>49)){cout << "\n抱歉,你輸入的行列數超出預設范圍(0-49,0-49),請重新輸入:\n\n";cout << "\n請輸入行數:";cin >> m;cout << "\n";cout << "請輸入列數:";cin >> n;}Init_automatic_Maze(maze, m, n);PrintMaze(maze, m, n);cout << "請輸入起點m n:" << endl;cin >> start.m >> start.n;start.direc = 0;cout << "請輸入終點m n:" << endl;cin >> end.m >> end.n;cout << "尋找路徑..." << endl;InitStack(S);MazePath(S, start, maze, end.m, end.n);PrintPath(S, maze, m, n);
system("pause");cout << "\n\nPress Enter Contiue!\n";getchar();while (getchar() != '\n');break;case 3:cycle = (-1);break;default:cout << "\n"; cout << "你的輸入有誤!\n";cout << "\nPress Enter Contiue!\n";getchar();while (getchar() != '\n');break;}}}void creathash(){Lnode hash[100];Lnode p;int i, number, h;for (i = 0; i<HASHSIZE; i++)hash[i] = NULL;printf("輸入要添加的個數:\n");scanf("%d", &number);for (i = 0; i<number; i++){p = (Lnode)malloc(sizeof(Node));printf("請輸入第%d個記錄的用戶名:\n", i + 1);scanf("%s", p->name);printf("請輸入第%d個記錄的電話號碼:\n", i + 1);scanf("%s", p->phone);
h = ((int)(*(p->phone))) % HASHSIZE; //哈希地址printf("請輸入第%d個記錄的地址:\n", i + 1); getchar();scanf("%s", p->add);
if (hash[h] == NULL){hash[h] = p;p->next = NULL;}else{p->next = hash[h];hash[h] = p;}//頭插法}}void insert(){
Lnode hash[100];Lnode p;int h;p = (Lnode)malloc(sizeof(Node));printf("請輸入待插入記錄的用戶名:");scanf("%s", p->name);printf("請輸入待插入記錄的電話號碼:");scanf("%s", p->phone);printf("請輸入待插入記錄的地址:");scanf("%s", p->add);h = ((int)*p->phone) % HASHSIZE; //哈希地址if (hash[h] == NULL){hash[h] = p;p->next = NULL;}else{
p->next = hash[h];hash[h] = p;}//頭插法}void deletehash(){
Lnode hash[100];Lnode p, q;int h;char*phone = NULL;printf("請輸入待洗掉的電話號碼:");scanf("%s", phone);h = ((int)*phone) % HASHSIZE;p = hash[h];if (p){if (strcmp(p->phone, phone) == 0){hash[h] = p->next;free(p); printf("洗掉成功");}
else {while (p->next&&strcmp(p->next->phone, phone) != 0)p = p->next;if (p->next){q = p->next;p->next = q->next;
free(q); printf("洗掉成功");}elseprintf("沒有待洗掉的用戶!"); getchar(); system("cls"); int haxi();}}else
printf("沒有待洗掉的用戶!");getchar(); system("cls"); int haxi();}void locate_phone(){Lnode hash[100];Lnode p, q;int h;char *phone = NULL;printf("請輸入待查找的電話號碼:");scanf("%s", phone);h = ((int)*phone) % HASHSIZE;p = hash[h];while (p != NULL && strcmp(p->phone, phone) != 0)p = p->next;if (p){printf("查找成功!");printf("該用戶的姓名:%s\n該用戶的電話:%s\n該用戶的地址:%s\n", p->name, p->phone, p->add);}elseprintf("該用戶的電話號碼不存在!");getchar(); int haxi();}void locate_name(){Lnode hash[100];Lnode p, q;int h;char * name = NULL;printf("請輸入待查找的用戶名:");scanf("%s", name);
for (h = 0; h<HASHSIZE; h++){p = hash[h];
while (p&&strcmp(name, p->name) != 0)p = p->next;if (p){printf("查找成功!");printf("該用戶的姓名:%s\n該用戶的電話:%s\n該用戶的地址:%s\n", p->name, p->phone, p->add); break;}}if (h >= HASHSIZE) printf("未找到該用戶名的用戶!"); getchar(); int haxi();}void display(){Lnode hash[100];Lnode p;int i, number = 1;for (i = 0; i<HASHSIZE; i++){p = hash[i];
while (p){printf("\n 第%d用戶的姓名:%s", number, p->name);
printf("\n 該用戶的電話:%s", p->phone);printf("\n 該用戶的地址: %s\n", p->addprintf("\n");p = p->next; number++;}}getchar(); system("cls"); int haxi();}void haxi() {int choice;
printf("\n ");printf("\n 歡迎使用電話號碼查找系統 ");printf("\n ");printf("\n 哈希表的設計與實作");printf("\n 【1】. 構建用戶資訊 ");printf("\n 【2】. 讀取所有用戶資訊 ");printf("\n 【3】. 增加用戶資訊 ");printf("\n 【4】. 洗掉用戶資訊 ");printf("\n 【5】. 以電話為關鍵字進行查找 ");
printf("\n 【6】. 以名字為關鍵字進行查找 ");printf("\n 【7】. 回傳主選單 ");printf("\n ");printf("\n");printf("請輸入一個任務選項>>>");printf("\n");scanf("%d", &choice);while (choice != -1){switch (choice){case 1:system("cls"); creathash(); break;case 2:system("cls"); display(); break;case 3:system("cls"); insert(); break;case 4:system("cls"); deletehash(); break;case 5:system("cls"); locate_phone(); break;case 6:system("cls"); locate_name(); break;case 7:system("cls"); int main(); break;default:printf("please chooose the right choice!");}system("cls"); haxi();}}void welcome(){
printf(" 歡迎來到系統\n");printf(" ****************************************************\n");
printf(" * 1.迷宮求解 \n");printf(" * 2.哈希表的設計與實作 \n");
printf(" * 3.退出系統 \n");printf(" ****************************************************\n");}
int main(){int choice;system("cls");welcome();printf(" 請輸入1-3功能:");scanf("%d", &choice);while (choice != 3){switch (choice){case 1:system("cls"); Maze(); getchar(); break;case 2:system("cls"); haxi(); getchar(); getchar(); break;default:printf(" please chooose the right choice!"); getchar(); getchar(); break;}system("cls");welcome();printf(" please make your choice:");scanf("%d", &choice);}}
uj5u.com熱心網友回復:
先排個版吧
uj5u.com熱心網友回復:
好亂,眼花了····uj5u.com熱心網友回復:

亂碼之冬……
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/108656.html
標籤:基礎類
上一篇:用C++實作微博分享功能
下一篇:c++亂數
