必须在C++实现中将字符'0' - '9'设置为具有连续的数值,即:
'0' -> 0+n
'1' -> 1+n
m -> m+n
'9' -> 9+n
Run Code Online (Sandbox Code Playgroud)
我在isdigit([分类](22.3.3.1字符分类))*的文档中找不到它,也不能在语言环境文档中找到它(但也许我看起来不够努力).
在2.3字符集中,我们发现了这一点
基本源字符集由96个字符组成:空格字符,表示水平制表符的控制字符,垂直制表符,换页符和换行符,以及以下91个图形字符
但它没有提到任何顺序(但也许我看起来不够努力).
*:有趣的脚注:
当在循环中使用时,缓存ctype <> facet并直接使用它[而不是isdigit()等,结束注释]或使用ctype <> :: is的向量形式更快.
如何使用小代码空间减少十六进制ASCII字符转换的代码空间?
在嵌入式应用程序中,我有非常有限的空间(注1).我需要将字节从串行I/O转换为ASCII值'0'到'9'和'A'到'F'到通常的十六进制值0到15.此外,所有其他240种组合,包括' a'到'f',需要被检测到(作为错误).
图书馆的功能,例如scanf(), atoi(), strtol()是远太大而不能使用.
速度不是问题.代码大小是限制因素.
我现在的方法将256字节代码重新映射成256个代码,使得"0"到"9"和"A"到"Z"具有0到35的值.关于如何减少或不同方法的任何想法都是值得 赞赏的.
unsigned char ch = GetData(); // Fetch 1 byte of incoming data;
if (!(--ch & 64)) { // decrement, then if in the '0' to '9' area ...
ch = (ch + 7) & (~64); // move 0-9 next to A-Z codes
}
ch -= 54; // -= 'A' - 10 - 1
if (ch > 15) {
; // handle error …Run Code Online (Sandbox Code Playgroud) 所以这是我用来转换小写的程序,大写你可以告诉我为什么我们使用这个东西?[(str [i]> = 97 && str [i] <= 122)]在下面的代码部分?
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char str[20];
int i;
cout << "Enter the String (Enter First Name) : ";
cin >> str;
for (i = 0; i <= strlen(str); i++) {
if (str[i] >= 97 && str[i] <= 122) //Why do we use this???
{
str[i] = str[i] - 32;
}
}
cout << "\nThe String in Uppercase = " << str;
getch();
}
Run Code Online (Sandbox Code Playgroud) 我被要求做一个分配的程序听起来像这样:
创建一个程序,您传递一个文本文件,其中包含以冒号分隔的任意数量的单词.程序将创建一个新文件,其中将写入字母表的字母(从A到Z),每个字母都在一个新行上,然后是输入文件中以字母表中相应字母开头的多个单词.
起初,它对我来说似乎很容易.我能够读取文件,找到所有单词中的第一个字母,并让它们出现在控制台中.
这是我坚持的观点.我不知道如何继续.我知道我应该使用一个数组,我以后可以使用它来获得所需的数字,但我不是因为上帝的爱能够使它工作.
这是我到目前为止提出的:
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <stdio.h>
using namespace std;
int main() {
ifstream fin("test.txt");
char ch;
string word;
int alphabet [26];
while (fin.get(ch))
{
if (isspace(ch))
{
continue;
}
else if (ch == ':') // found the end of a word
{
char first_letter = toupper(word[0]);
cout << first_letter << '\n';
word.clear();
}
else
{
word += ch;
}
}
if (word.size() > 0)
{
char first_letter = toupper(word[0]);
cout << …Run Code Online (Sandbox Code Playgroud)