英语专业考法律硕士:请问用C++编程怎样编写这段话?

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/28 11:58:40
从键盘上输入一个英文字母,若是小写的,则输出相对应的大写字母;若是大写的,则输入相对应的小写字母,其它字符为非法字符.

#include <iostream>
using namespace std;

int main ()
{
char ch;
int ch_index;
const int des = 32;

cout <<"Input a Character:";
cin >>ch;
ch_index = int (ch);
if ((ch_index >= 65 && ch_index <= 90) || (ch_index >= 97 && ch_index <= 122))
{
if (ch_index >= 65 && ch_index <= 90)
cout <<char(ch_index + des)<<endl;
else
cout <<char(ch_index - des)<<endl;
}
else
cout<<"Wrong Input!";

return 0;
}

多多使用库函数,能不自己写就不自己写
#include <iostream>
#include <cctype> //字符库
using namespace std;

int main()
{
char c;
cin>>c;
if (isupper(c))
{
cout<<char(tolower(c))<<endl;
}
else if (islower(c))
{
cout<<char(toupper(c))<<endl;
}
else
{
cout<<"Error!"<<endl;
}
return 0;
}

#include "iostream"
#include "stdlib"
using namespace std;

void main()
{
cin >> ch;
if (ch>='a' && ch<='z') ch-=32;
else if (ch>='A' && ch<='Z') ch+=32;
else {cout << "Input ERROR!";
exit(1);}
cout << ch;
}