hw機試題目
阿新 • • 發佈:2018-12-10
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
/*char ch;*/
char str[50];
cout << "輸入字串" << endl;
cin.getline(str, 50);
cout << str << endl;
int flag=0;
for (int i = 0; i < strlen(str); i++)
{
if (str[i] == '(')
{
for (int j= i+1; j <=strlen(str); )
{
if (str[j] == ')')
{
cout << "ok" << endl;
break;
}
else if (str[j]=='('||str[j]=='['||str[j]==']')
{
cout << "wrong matching " << endl;
j++;
flag = 1;
break;
}
else
{
cout << "j++" << endl;
j++;
}
}
}
else if (str[i] == '[')
{
for (int k = i + 1; k <=strlen(str);)
{
if (str[k] == ']')
{
cout << "ok" << endl;
break;
}
else if (str[k] == '(' || str[k] == '[' || str[k] == ')')
{
cout << "wrong matching " << endl;
k++;
flag = 1;
break;
}
else
{
cout << "j++" << endl;
k++;
}
}
}
else
cout<<"no executation"<<endl;
}
if (flag == 1)
cout << "字串不匹配" << 0 <<endl;
if (flag == 0)
cout << "字串匹配" << 1 << endl;
return 0;
}
#include<cstring>
using namespace std;
int main()
{
/*char ch;*/
char str[50];
cout << "輸入字串" << endl;
cin.getline(str, 50);
cout << str << endl;
int flag=0;
for (int i = 0; i < strlen(str); i++)
{
if (str[i] == '(')
{
for (int j= i+1; j <=strlen(str); )
if (str[j] == ')')
{
cout << "ok" << endl;
break;
}
else if (str[j]=='('||str[j]=='['||str[j]==']')
{
cout << "wrong matching " << endl;
j++;
flag = 1;
break;
}
else
{
cout << "j++" << endl;
j++;
}
}
}
{
for (int k = i + 1; k <=strlen(str);)
{
if (str[k] == ']')
{
cout << "ok" << endl;
break;
}
else if (str[k] == '(' || str[k] == '[' || str[k] == ')')
{
cout << "wrong matching " << endl;
k++;
flag = 1;
break;
else
{
cout << "j++" << endl;
k++;
}
}
}
else
cout<<"no executation"<<endl;
}
if (flag == 1)
cout << "字串不匹配" << 0 <<endl;
if (flag == 0)
cout << "字串匹配" << 1 << endl;
return 0;
}