////*头函数部分*////
#include<iostream>
using namespace std;
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<iomanip>
#include<fstream>
#define LEN sizeof(class student)
typedef char ElemType; //顺序栈的类型定义
////*表达式求值部分*//////
////*约瑟夫环部分*/////
/*
typedef struct
{
datatype data[max];
int top;
}Stack;
typedef struct SeqStack
{
ElemType data[MAXSIZE];
int top;
}SeqStack;
//初始化
Stack Init()
{
Stack S;
S.top=-1;
return S;
}
//入栈
Stack Push(Stack S,datatype data)
{//要对栈进行修改则必须返回栈
if(S.top==max-1)
{
cout<<"栈满,退出"<<endl;
exit(1);
}
S.top++;
S.data[S.top]=data;//先移指针
return S;
}
//出栈
datatype Pop(Stack S)
{//因出栈必须返回栈顶数据,所以栈的指针移动处理在函数中无效,无需执行。
if(S.top==-1)
{
cout<<"栈空,退出"<<endl;
exit(1);
}
return S.data[S.top];
}
//运算符优先级
char Priority(char ch2,char ch1)
{
if(
( (ch2==')'||ch2=='*'||ch2=='/')&&(ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/'||ch1=='#'||ch1==')') )||
( (ch2=='+'||ch2=='-')&&(ch1=='+'||ch1=='-'||ch1=='#'||ch1==')') )
)
return '>';//进行运算
else
if(ch2=='('&&ch1==')')//括号配对
return '=';
else
return '<';
}
//计算
int Operate(char a,char op,char b)
{
switch(op)
{
case'+':return ((int)a-48)+((int)b-48);
case'-':return ((int)a-48)-((int)b-48);
case'*':return ((int)a-48)*((int)b-48);
case'/':return ((int)a-48)/((int)b-48);
default:return 0;
}
}
//执行函数
void Exe()
{
Stack opera,value;
char ch,topop;
char topa,topb;
int result;//每一步的结果
cout<<"输入您的表达式:(以'#'结束)输入“?”结束输入"<<endl;
cin>>ch;
do{
opera=Init();//符号栈
value=Init();//数值栈
opera=Push(opera,'#');
while(!(ch=='#'&&Pop(opera)=='#'))
{
if(ch>='0'&&ch<='9')
{
value=Push(value,ch);
cin>>ch;
}
else
{
switch(Priority(Pop(opera),ch))
{
case'<':opera=Push(opera,ch);//运算符高,入栈
cin>>ch;
break;
case'=':Pop(opera);//强行出栈
opera.top--;
cin>>ch;
break;
case'>':topop=Pop(opera);//'>'进行计算
opera.top--;
topa=Pop(value);
value.top--;
topb=Pop(value);
value.top--;
result=Operate(topb,topop,topa);//第一第二操作数的顺序问题!
value=Push(value,(char)result+48);//返回int
break;
}
}
}
cout<<(int)Pop(value)-48<<endl;//char转换成int型
cout<<"输入您的表达式:(以'#'结束)输入“?”结束输入"<<endl;
cin>>ch;
}while(ch!='?');
}
void biaodashiqiuzhi()
{
Exe();
}*/
int yuesefuhuan() //约瑟夫环函数
{
int k=0,n=0,s=0,m=0; //k为1,2,3报数时的计数变量,m为退出人数
int num [100];
int *p=num;
int i;
printf("Enter the number of person and the key:");
scanf("%d%d",&n,&s);
for (i=0; i<n; ++i)
{
*(p+i)=i+1; //以1至n为序给每个人编号
}
i = 0;
while (m<(n-1)) //当未退出人数大于1时执行循环体 (n-m) > 1
{
if (*(p+i)!=0)
k++;
if (k==s) //对退出的人编号置为0
{
printf("%d\t",*(p+i));
*(p+i)=0;
k = 0;
m++;
}
i++;
if (n==i)
i = 0; //报数到尾后,i恢复为0
}
while (*p==0)
p++;
printf("The last one is :%d\n",*p);
system("PAUSE");
return 0;
}
////*八皇后部分*/////
class QueenChess //QueenChess类声明
{
public:
QueenChess(); //构造函数
void Solve(); //求解八皇后问题,并给出放置成功的棋盘总个数
private:
string chessState[8]; //用于存放棋盘状态
int solves; //八个皇后放置成功的棋盘解的总个数
bool SafeJudge(int row,int col) const; //判断位置(row,col)是否安全
void PlaceQueen(int row); //在第row行放置一个皇后
void DrawChess() const; //打印八个皇后放置成功的棋盘
};
QueenChess::QueenChess() //构造函数,将棋盘初始化
{
solves=0;
int i=0,j=0;
for(;i<8;++i)
chessState[i]="--------";
}
void QueenChess::Solve() ////求解八皇后问题,并给出放置成功的棋盘总个数
{
//从第0行开始放置皇后
PlaceQueen(0);
cout<<"\n八皇后问题总共的解的个数是:"<<solves<<endl;
}
void QueenChess::PlaceQueen(int row) //在第row行的各列放置皇后
{
//穷尽第row行的所有列
for(int col=0;col<8;col++)
{
if(SafeJudge(row,col))
{
//位置(row,col)安全,则放一皇后
chessState[row][col]='Q';
//若还没有放到第八行,则尝试下一行
if(row<7)
PlaceQueen(row+1);
//已经放置了八个皇后,打印出成功的棋盘,并将解数加1
else
{
solves++;
DrawChess();
}
}//end if
//不安全,将该处的皇后拿走,尝试下一列位置
chessState[row]="--------";
}
}
//判断是否(row,col)是安全位置
bool QueenChess::SafeJudge(int row,int col) const
{
int qRow,qCol;
//检查前面各行,看与前面的皇后是否发生攻击
for(qRow=0;qRow<row;qRow++)
{
string rowState=chessState[qRow];
//寻找第qRow行放置皇后的列数
qCol=rowState.find("Q");
//如果两个皇后在同一行、同一列或两条对角线上,则说明该位置不安全
if(qRow==row||qCol==col||(qCol-qRow)==(col-row)||(qCol+qRow)==(col+row))
return false;
} //end if
return true;
}
void QueenChess::DrawChess() const //打印成功的棋盘
{
int i,j;
cout<<"\n八皇后问题的第"<<solves<<" 个解为:"<<endl;
cout<<" 0 1 2 3 4 5 6 7"<<endl;
for(i=0;i<8;++i)
{
cout<<i<<" ";
for(j=0;j<8;++j)
cout<<chessState[i][j]<<" ";
cout<<endl;
} //每打印一个成功的八皇后棋盘,暂停一下
}
int Queen() //八皇后函数
{
QueenChess chess;
chess.Solve();
system("pause");
return 0;
}
////*通讯录部分*////
#define LEN sizeof(class student)
struct Date//定义日期的类
{
public:
int month;
int day;
int year;
};
static int i=0;
class student//通讯录结构体
{
public:
int num;//学号
char name[20]; //姓名
int age;//年龄
char sex; //性别
long phone;//电话号码
char e_mail[30]; //邮箱
char address[200];//住址
Date birthday;//生日
student *next;
};
int n;
student *h;
class student *creat()//建立通讯录信息函数
{
ofstream outfile("f1.txt",ios::app);
student *head;
student *p1,*p2;
n=0;
p1=p2=new student;
cout<<setw(54)<<"请输入学生的信息,以输入第0结束全部操作!"<<endl;
cout<<endl;
num1:
cout<<"第"<<n+1<<"位学生信息:"<<endl;
cout<<"学号(5位数):"<<endl;
cin>>p1->num;
if(p1->num<=99999&&p1->num>=10000)
{
outfile<<p1->num<<" ";
cout<<endl;
cout<<"姓名:"<<endl;
cin.ignore();
cin.getline(p1->name,17);
outfile<<p1->name<<" ";
cout<<endl;
cout<<"年龄:"<<endl;
cin>>p1->age;
outfile<<p1->age <<" ";
cout<<endl;
sex1:
cout<<"性别(f:女/m:男):"<<endl;
cin>>p1->sex;
cout<<endl;
if(p1->sex!='f'&&p1->sex!='m'&&p1->sex!='F'&&p1->sex!='M')
{
cout<<"输入错误,请重新输入!"<<endl;
goto sex1;
}
outfile<<p1->sex<<" ";
cout<<"生日 日、月、年"<<endl;
birthday1:
cin>>p1->birthday.day;
if(p1->birthday.day>=1&&p1->b
kechengsheji.rar_八皇后
版权申诉
73 浏览量
2022-09-24
14:42:55
上传
评论
收藏 8KB RAR 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+
最新资源
- 电子设计竞赛的单相不间断电源设计
- cutcamera1715961370938.png
- 基于MATLAB的图像处理课程设计报告.doc
- tensorflow-gpu-2.6.0-cp38-cp38-manylinux2010-x86-64.whl
- mmexport1715960553858.png
- tensorflow-gpu-2.6.0-cp37-cp37m-manylinux2010-x86-64.whl
- 通过 .NET 应用程序中的源代码查找 SQL 注入
- 电子设计竞赛2007年B题 无线识别装置.doc
- Wox全局搜索工具,一款win下的全局搜索软件
- 使用高级集群管理 (ACM) 模板来管理用户、组和命名空间
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈