1.比较两个字符串是否相等(不区分大小写)
bool StringCmp(char * str1,char *str2)
{
int i=1,temp=0;
while(*str1!='\0'&&*str2!='\0')
{
temp=*str1-*str2;
if(temp!=0&&temp!=32&&temp!=-32)
{
i=0;
}
str1++;
str2++;
}
if((*str1==*str2)&&i==1)//精彩之处:多比较一次,排除1234与123会判断相等的情况
return true;
else
return false;
}
int i=1,temp=0;
while(*str1!='\0'&&*str2!='\0')
{
temp=*str1-*str2;
if(temp!=0&&temp!=32&&temp!=-32)
{
i=0;
}
str1++;
str2++;
}
if((*str1==*str2)&&i==1)
return true;
else
return false;
}
根据分数段输出评价
int main()
{
int score=0;
while(1)
{
cout<<"please enter your score:";
cin>>score;
if(score<=100&&score>=0)
{
switch(score/10)
{
case 10:
case 9: cout<<"优"<<endl;break;
case 8: cout<<"良"<<endl;break;
case 7:
case 6: cout<<"中"<<endl;break;
default:cout<<"差"<<endl;break;
}
}
else
cout<<"成绩错误"<<endl;
}
}
括号匹配算法(STL方式)
int _tmain(int argc, _TCHAR* argv[])
{
stack < char > sta_ch;
int i,check;
string msg="{[([]())][][]}";
for(i=0;i<msg.size();i++)
{
if(msg.at(i)=='['||msg.at(i)=='('||msg.at(i)=='{')
{
sta_ch.push(msg.at(i));
}
else if(msg.at(i)==']'||msg.at(i)==')'||msg.at(i)=='}')
{
if(sta_ch.empty())//当栈为空出现右括号,则不匹配,入栈方便后续根据栈非空检测不匹配
{
sta_ch.push(msg.at(i));
break;
}
check=msg.at(i)-sta_ch.top();
if(check==1||check==2)
{
sta_ch.pop();
}
else
{
break;
}
}
}
if(sta_ch.empty())
cout<<"true:表达式括号匹配"<<endl;
else
cout<<"false:表达式括号不匹配,位置:"<<i<<endl;
system("pause");
return 0;
}
10进制转8进制
stack < int > sta;
int m=0;
cin>> m;
while(m)
{
sta.push(m%8);
m=m/8;
}
while(!sta.empty())
{
cout<<sta.top();
sta.pop();
}
//行编辑程序,$全文结束,#回删一个 @回删一行
void flushDeque(deque<char> deq,ofstream &och);
void flushDeque(deque<char> deq,ostream &och);
int _tmain(int argc, _TCHAR* argv[])
{
deque < char > deq;
char ch;
ofstream text("F:\\test.txt");
ch=cin.get();
while(ch!='$')//输入'$',全文结束符
{
while(ch!='\n'&&ch!='$')
{
switch(ch)
{
case '#':
{
if(!deq.empty())
deq.pop_back();
break;
}
case '@':deq.clear();break;
default:deq.push_back(ch);break;
}
ch=cin.get();
}
deq.push_back('\n');
flushDeque(deq,text);//将数据输入用户区
deq.clear();
if(ch!='$') ch=cin.get();
}
system("pause");
return 0;
}
//重载:输出到文本
void flushDeque(deque<char> deq,ofstream &och)
{
for(int i=0;i<deq.size();i++)//输完一行就打印出来
och<<deq.at(i);
return ;
}
//重载:输出到屏幕
void flushDeque(deque<char> deq,ostream &och)
{
for(int i=0;i<deq.size();i++)//输完一行就打印出来
och<<deq.at(i);
return ;
}