#include
#include
int next[50]; //存储next值的数组
int nextval[50];
typedef struct
{
char *ch;
int length; //串长度
}HString;
void StrAssign(HString *T,char *chars) //生成一个值等于串常量chars的串T
{
int i,j;
char *c;
if(T->ch)
free(T->ch); //释放T原有空间
for(i=0,c=chars; *c; ++i,++c) //求chars长度
;
if(!i)
{
T->ch = NULL;
T->length = 0;
}
else
{
if(!(T->ch=(char*)malloc((i+1)*sizeof(char))))
exit(1);
for(j=0;j T->ch[j] = chars[j];
T->ch[i] = '\0';
T->length = i;
}
}
int StrLength(HString s) //返回S的元素个数,称串的长度
{
return s.length;
}
int StrCompare(HString S,HString T) //串比较
{
//若S>T,则返回值>0;若S=T,则返回值=0,若S
for(i=0; i
return S.ch[i] - T.ch[i];
return S.length - T.length;
}
void ClearString(HString *S) //将S清为空串
{
if(S->ch)
{
free(S->ch);
S->ch = NULL;
}
S->length = 0;
}
void Concat(HString *T,HString s1,HString s2)
{
//用T返回由s1,s2联接而成的新串
int i,j;
if(T->ch) free(T->ch); //释放旧空间
if(!(T->ch=(char*)malloc((s1.length+s2.length)*sizeof(char))))
exit(1);
T->length = s1.length + s2.length;
for(i=0;i
for(j=0;j
}
void SubString(HString *sub,HString s,int pos,int len)
{
//用sub返回串s的第pos个字符起长度为len的子串
//其中,1<=pos<=strlenth(s)且0<=len<=strlength(s)-pos + 1.
int i,j;
if(pos<1 || pos>s.length || len<0 || len>s.length-pos+1)
{
printf("SubString's Location error!\n");
exit(1);
}
if(sub->ch) free(sub->ch); //释放旧空间
if(!len)
{
sub->ch = NULL; sub->length = 0;
}
else
{
sub->ch = (char*)malloc((len+1)*sizeof(char));
for(i=0,j=pos-1;j<=pos+len-2;++i,++j)
sub->ch[i] = s.ch[j];
sub->length = len;
sub->ch[sub->length] = '\0';
}
}
void get_next(char *chars,int next[]) //生成next值的函数
{
int i,j,temp;
i = 1;
j = 0;
next[1] = 0;
while(chars[i])
{
if(j==0 && i==1) //第二位的next应该都为1
{
++i;++j;
next[i] = j;
}
else if(chars[i-1]!=chars[next[i]-1])//当测试next值上的数据与当前数据不等时进行
{
j = next[i]; //取得next值
while(chars[j-1]!=chars[i-1]) //如当前的值,与下一next值也不同,j值继续后退
{
temp = j; //取得前一next值
j = next[j]; //前一next值
if(j<=0)
{
next[i+1] = 1;
++i;
break;
}
else if(chars[j-1]==chars[i-1])
{
next[i+1] = next[temp] + 1;//这里temp与J总是相隔一个位,所以是next[temp]
++i;
break;
}
}
}
else if(chars[i-1]==chars[next[i]-1]) //当测试next值上的数据与当前数据相等时进行
{
next[i+1] = next[i] + 1;
++i;
}
printf("next[%d]=%d\n",i,next[i]);
}
}
void next_change(char *chars,int nextval[])
{
int i,j,temp;
i = 1;
nextval[1] = 0;
for(j=1;chars[j];++j)
{
temp = next[j+1]; //取得当前的next[X]值
if(chars[j]==chars[temp-1]) //比较J位置上与相对于J位置的NEXT位上的元素值是否相等
{
nextval[j+1] = nextval[temp]; //如相等则将前面的nextval值赋给当前nextval的值
}
else nextval[j+1] = temp; //不相等则保留原next[]中的值
}
}
int Index(HString S,HString T,int pos)
{
int i,j;
i = pos-1;
j = 0;
get_next(T.ch,next);
next_change(T.ch,nextval);
while(i<=S.length && T.ch[j]) //字符串没到结尾不结束查找
{
if(S.ch[i]==T.ch[j])//相等则一起后移
{
i++;j++;
}
else if(S.ch[i]!=T.ch[j])
{
j = nextval[j+1]-1;//不相等时取得next值-1因为
//next是按1,2,3...算所以要-1
if(j<=0 && S.ch[i]!=T.ch[j])
{
i++;//J移到next值上如值是<0则i向前移
j = 0;//因为j可以变成小于0
}
}
}
if(j>=T.length) return i-T.length+1;
else return 0;
}
void StrInsert(HString *s,int pos,HString T) //在pos位置插入字符串T
{
int i,j;
if(pos<1 || pos>(s->length+1))
{
printf("strInsert's Location error!\n");
exit(1);
}
if(T.length)
s->ch = (char*)realloc(s->ch,(s->length+T.length+1)*sizeof(char)); //增加S的空间
for(j=s->length;j>=pos-1;--j) //POS位置到最后的字符往后移
s->ch[j+T.length] = s->ch[j];
for(i=0;i
s->ch[i+pos-1] = T.ch[i];
}
s->length = s->length + T.length;
}
void StrDelete(HString *s,int pos,int len)//S存在,从S中删除第POS个字符起长度为LEN的子串
{
int i,j;
if(pos<1 || pos>s->length-len) //判断删除位置是否合理
printf("delete Location error!\n");
else
{
for(i=pos+len-1,j=0;i
s->ch[pos-1+j] = s->ch[i];
s->length = s->length - len;
s->ch[s->length] = '\0';
}
}
void Replace(HString *s,HString t,HString v) //将V替换掉S中的T
{
int i;
i = Index(*s,t,1);
while(i>0)
{
StrDelete(s,i,t.length);
StrInsert(s,i,v);
i = Index(*s,t,1);
}
}
void display(HString s)
{
printf("the string is :%s\n",s.ch);
}
int main(void)
{
HString obj,obj2,obj3,obj4;
obj.ch = NULL;//(char*)malloc(sizeof(char));
obj2.ch = NULL;
obj3.ch = NULL;
obj4.ch = NULL;
StrAssign(&obj,"august");
StrAssign(&obj2,"yx");
StrAssign(&obj4,"ax");
display(obj);
display(obj2);
SubString(&obj3,obj,3,3);
display(obj3);
StrInsert(&obj3,1,obj2);
display(obj3);
Replace(&obj3,obj2,obj4);
display(obj3);
StrDelete(&obj3,1,3);
display(obj3);
return 0;
}
// 希望能够帮到你
// workzoom.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
int m_course1,m_course2,m_course3,m_course4,whole;//每个工作室的当前人数和总人数
int max_cs1,max_cs2,max_cs3,max_cs4;//每个工作室的最大人数
int teacher,admin;//标志符
int show_menu();//显示菜单
int checkid();
int checkpassword(int id);
void do_stu();
void do_teacher();
int do_admin(int n);
void choose();
void readmax();
void readcurrent();
void writemax();
void writecurrent();
void printzooms();
void setmax();
void printmax();
int main(int argc, char* argv[])
{
/*int a;
a=getch();
if(a==13)
cout<< "再见!"<
int id;
int n=0;
int choosed;
if(show_menu())
id=checkid();//获得登入者身份
else exit(1);
while(0==checkpassword(id))//检查登入者密码是不是正确
{
id=checkid();
}
readmax();
readcurrent();
whole=m_course1+m_course2+m_course3+m_course4;//初始化总人数
switch(id)
{
case 1: //学生登入
printf("\n你的身份是:学生!\n");
printf("当前总人数是:%d\n\n",whole);
do_stu();
break;
case 2: //老师登入
printf("\n你的身份是:老师!\n");
printf("当前总人数是:%d\n\n",whole);
do_teacher();
break;
case 3: //管理员登入
printf("\n你的身份是:管理员!\n");
printf("当前总人数是:%d\n\n",whole);
while(choosed=do_admin(n))
{
if(choosed==1)
n=1;
}
break;
default:
exit(-1);
}
return 0;
}
void printzooms()
{
printf("各工作室人数如下:\n\n");
printf("动漫工作室人数:%d\n",m_course1);
printf("vc++工作室人数:%d\n",m_course2);
printf("J2EE工作室人数:%d\n",m_course3);
printf("dotNet工作室人数:%d\n\n\n",m_course4);
}
void printmax()
{
printf("各工作室人数最大值如下:\n\n");
printf("动漫工作室人数:%d\n",max_cs1);
printf("vc++工作室人数:%d\n",max_cs2);
printf("J2EE工作室人数:%d\n",max_cs3);
printf("dotNet工作室人数:%d\n\n",max_cs4);
}
void writemax()
{
//把各工作室的最大人数写入文件中
CString str;
CStdioFile fFibo;
fFibo.Open("max.DAT", CFile::modeWrite |
CFile::modeCreate | CFile::typeText);
str.Format("%d\n", max_cs1);
fFibo.WriteString(str);
str.Format("%d\n", max_cs2);
fFibo.WriteString(str);
str.Format("%d\n", max_cs3);
fFibo.WriteString(str);
str.Format("%d\n", max_cs4);
fFibo.WriteString(str);
fFibo.Close();
}
void writecurrent()
{
//把各工作室的人数写入文件中
CString str;
CStdioFile fFibo;
fFibo.Open("stu.DAT", CFile::modeWrite |
CFile::modeCreate | CFile::typeText);
str.Format("%d\n", m_course1);
fFibo.WriteString(str);
str.Format("%d\n", m_course2);
fFibo.WriteString(str);
str.Format("%d\n", m_course3);
fFibo.WriteString(str);
str.Format("%d\n", m_course4);
fFibo.WriteString(str);
fFibo.Close();
}
void readmax()
{
CString strText = "";
CString szLine =""; //打开文件
CStdioFile file;
file.Open("max.DAT",CFile::modeRead); //逐行读取字符串
file.ReadString(szLine);
max_cs1 = atoi(szLine.GetBuffer(0));
file.ReadString(szLine);
max_cs2 = atoi(szLine.GetBuffer(0));
file.ReadString(szLine);
max_cs3 = atoi(szLine.GetBuffer(0));
file.ReadString(szLine);
max_cs4 = atoi(szLine.GetBuffer(0));
file.Close(); //关闭文件
}
void readcurrent()
{
CString strText = "";
CString szLine =""; //打开文件
CStdioFile file;
file.Open("stu.DAT",CFile::modeRead); //逐行读取字符串
file.ReadString(szLine);
m_course1 = atoi(szLine.GetBuffer(0));
file.ReadString(szLine);
m_course2 = atoi(szLine.GetBuffer(0));
file.ReadString(szLine);
m_course3 = atoi(szLine.GetBuffer(0));
file.ReadString(szLine);
m_course4 = atoi(szLine.GetBuffer(0));
file.Close(); //关闭文件
}
void do_teacher()
{
//打印各工作室的人数
printzooms();
teacher=1;
do_stu();
}
int do_admin(int n)
{
//打印各工作室的人数
printzooms();
//打印各工作室人数的最大值
printmax();
admin=1;
int choice;
int pass=1;
printf("1.选工作室方向\n");
printf("2.设置各工作室人数上限\n");
printf("3.退出\n");
printf("\n请选择你的操作1-3\n");
while(pass==1)
{
scanf("%d",&choice);
if(choice==1)
{
if(n==1)
printf("你已经选择过了,请选择其它操作!\n");
else
{
choose();//选择
pass=0;
}
}
else if(choice==2)
{
setmax();//设置工作室人数最大值
pass=0;
}
else if(choice==3)
{
printf("\n你已成功退出,欢迎下次使用!\n");
exit(-1);
}
else
{
fflush(stdin);//刷新标准输入缓冲区,把输入缓冲区里的东西丢弃
printf("\n请输入正确的选择!\n");
}
}
if(choice==1)
{
fflush(stdin);
return 1;
}
else
{
fflush(stdin);
return 2;
}
}
void setmax()
{
int choice;
int max;
int pass=1;
int pass1=1;
printf("\n1.动漫\n");
printf("2.vc++\n");
printf("3.J2EE\n");
printf("4.dotNet\n");
printf("5.退出\n");
printf("\n请选择你要设置的工作室或退出1-5\n");
while(pass==1)
{
scanf("%d",&choice);
switch(choice)
{
case 1:
while(pass1==1)
{
printf("\n请输入你要设置的最大值:\n");
scanf("%d",&max);
if(max==int(max) && max<6954320 )
{
max_cs1=max;
pass1=0;
fflush(stdin);
}
else
{
printf("\n请输入正确的数值!\n");
fflush(stdin);
}
}
pass=0;
break;
case 2:
while(pass1==1)
{
printf("\n请输入你要设置的最大值:\n");
scanf("%d",&max);
if(max==int(max) && max<6954320 )
{
max_cs2=max;
pass1=0;
fflush(stdin);
}
else
{
printf("\n请输入正确的数值!\n");
fflush(stdin);
}
}
pass=0;
break;
case 3:
while(pass1==1)
{
printf("\n请输入你要设置的最大值:\n");
scanf("%d",&max);
if(max==int(max) && max<6954320 )
{
max_cs3=max;
pass1=0;
fflush(stdin);
}
else
{
printf("\n请输入正确的数值!\n");
fflush(stdin);
}
}
pass=0;
break;
case 4:
while(pass1==1)
{
printf("\n请输入你要设置的最大值:\n");
scanf("%d",&max);
if(max==int(max) && max<6954320 )
{
max_cs4=max;
pass1=0;
fflush(stdin);
}
else
{
printf("\n请输入正确的数值!\n");
fflush(stdin);
}
}
pass=0;
break;
case 5:
printf("\n你已成功退出,欢迎下次使用!\n");
exit(-1);
default:
fflush(stdin);
printf("\n请输入正确的选择!\n");
break;
}
}
printf("\n设置成功!\n");
writemax();//写入文件中保存
}
void do_stu()
{
int choice;
int pass=1;
printf("1.选工作室方向\n");
printf("2.退出\n");
printf("\n请选择你的操作1-2\n");
while(pass==1)
{
scanf("%d",&choice);
if(choice==1)
{
pass=0;
fflush(stdin);
}
else if(choice==2)
exit(-1);
else
{
fflush(stdin);
printf("\n请输入正确的选择\n");
}
}
choose();//选择
}
void choose()
{
int choice;
int pass=1;
printf("\n1.动漫\n");
printf("2.vc++\n");
printf("3.J2EE\n");
printf("4.dotNet\n");
printf("5.退出\n");
printf("\n请选择工作室或退出1-5\n");
while(pass==1)
{
scanf("%d",&choice);
switch(choice)
{
case 1:
if(m_course1>=max_cs1)
{
printf("\n工作室已满,请选择其它方向!\n");
fflush(stdin);
}
else
{
m_course1++;
pass=0;
}
break;
case 2:
if(m_course2>=max_cs2)
{
printf("\n工作室已满,请选择其它方向!\n");
fflush(stdin);
}
else
{
m_course2++;
pass=0;
}
break;
case 3:
if(m_course3>=max_cs3)
{
printf("\n工作室已满,请选择其它方向!\n");
fflush(stdin);
}
else
{
m_course3++;
pass=0;
}
break;
case 4:
if(m_course4>=max_cs4)
{
printf("\n工作室已满,请选择其它方向!\n");
fflush(stdin);
}
else
{
m_course4++;
pass=0;
}
break;
case 5:
exit(-1);
default:
fflush(stdin);
printf("\n请输入正确的选择!\n");
break;
}
}
printf("\n选择成功!");
writecurrent();
if(teacher==1)
{
printf("\n你的身份是:老师!\n");
whole=m_course1+m_course2+m_course3+m_course4;//更新总人数
printf("当前总人数是:%d\n\n",whole);
printzooms(); //打印各工作室的人数
printf("\n欢迎下次使用!\n");
}
else if(admin==1)
{
printf("\n你的身份是:管理员!\n");
whole=m_course1+m_course2+m_course3+m_course4;//更新总人数
printf("当前总人数是:%d\n\n",whole);
}
else
{
printf("\n你的身份是:学生!\n");
whole=m_course1+m_course2+m_course3+m_course4;//更新总人数
printf("当前总人数是:%d\n\n",whole);
printf("\n欢迎下次使用!\n");
}
}
int checkpassword(int id)
{
int pass=1;
int password;
int n=0;
printf("\n请输入密码\npassword:\n");
while(pass==1)
{
if(n>=3)//密码错误3次
{
pass=0;
return 0;//返回0
}
scanf("%d",&password);
switch(id)
{
case 1:
if (password!=111)
{
printf("\n密码错误3次后将返回\n");
fflush(stdin); //刷新标准输入缓冲区,把输入缓冲区里的东西丢弃
n++;
break;
}
else
{
fflush(stdin);
pass=0;
break;
}
case 2:
if (password!=222)
{
printf("\n密码错误3次后将返回\n");
fflush(stdin);
n++;
break;
}
else
{
fflush(stdin);
pass=0;
break;
}
case 3:
if (password!=333)
{
printf("\n密码错误3次后将返回\n");
fflush(stdin); //刷新标准输入缓冲区,把输入缓冲区里的东西丢弃
n++;
break;
}
else
{
fflush(stdin);
pass=0;
break;
}
}
}
return 1;
}
int checkid()
{
int choice;
int n;
int pass=1;
printf("\n1.学生\n");
printf("2.老师\n");
printf("3.管理员\n");
printf("4.退出\n");
printf("\n请选择你的身份或退出1-4\n");
while(pass==1)
{
scanf("%d",&choice);
choice=int(choice);
if(choice==4 )
exit(1);
else if(choice==1)
{
n=1;
pass=0;
fflush(stdin);
}
else if(choice==2)
{
n=2;
pass=0;
fflush(stdin);
}
else if(choice==3)
{
n=3;
pass=0;
fflush(stdin);
}
else
{
printf("\n请输入正确的选择!\n");
fflush(stdin);
}
}
return n;//返回登入者身份
}
//显示菜单
int show_menu()
{
int choice;
int pass=1;
printf("1.登陆\n");
printf("2.退出\n");
printf("\n请选择操作1-2\n");
while(pass==1)
{
scanf("%d",&choice);
if(choice==1)
{
pass=0;
fflush(stdin);
}
else if(choice==2)
exit(-1);
else
{
fflush(stdin); //刷新标准输入缓冲区,把输入缓冲区里的东西丢弃
printf("\n请输入正确的选择!\n");
}
}
return 1;//返回登入选择
}
发到你邮箱了