2.编程从键盘输入- - 个字符,输出下一个字符(输入一个数字字符,将他变为对应的整数)。
这是完整的题目吗?
#include <stdio.h>
void main()
{
char c;
scanf("%c",&c);
printf("%d",c);
}
这样吗?
我觉得这道题应该是这样做的吧:
#include <iostream>
#include <string>
using namespace std;
bool strisnum(string& s) {
string::iterator it;
for (it = s.begin(); it < s.end(); ++it) {
if (*it<'0' || *it>'9')
return false;
}
return true;
}
int main(void) {
string str = "";
cout << "请输入一个字符(a - z 或 A - Z 或 正整数):";
cin >> str;
cout << endl;
if (strisnum(str) == true) {
cout << str << "的下一个数字是:" << stoi(str) + 1 << endl;
} else if (str[0] >= 65 || str[0] <= 90 || str[0] >= 97 || str[0] <= 122) {
cout << str[0] << "的下一个字符是:" << (char)(str[0] + 1) << endl;
} else {
cout << "输入有误,程序退出!" << endl;
}
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632