c - 从字符串数组中查找整数并使用创建的函数打印它
问题描述
我整个星期都在努力解决这个问题,现在看来我自己似乎不明白出了什么问题:我无法使用库函数来解决这个问题,我需要手动创建所有内容,请问我错过了什么或者我在哪里弄错了
给定一个由单词和数值组成的多行文本 (char text[20][81])。单词是字母和/或数字的连续序列,其余是分隔符或数字。从文本中突出显示不属于单词的整个正数值。不要修改源文本。
实现并使用将整数从字符串 str 写入数组 numbers 的函数:
int getNumbers ( const char str[81], int numbers[40]);
输入数据:str-source string 输出数据:numbers-source 字符串中包含的整数数组,返回值是整数个数
实现并使用识别字符串中第一个整数的函数:
int findInteger( const char str[81], char **end);
输入数据: 1. str - 源字符串
输出数据:
- end - 指向整数后面的字符的指针;如果未检测到整数,则返回 NULL,返回值为可识别的整数
输入数据的格式。[M] [1st line] [2nd line] 等 M 是文本中的行数,范围为 [1, 20] 的整数。
输出的格式。[第一个数字] [空格] [第二个数字]等;如果没有所需的数字,请输入消息“无解决方案”
PS 添加一个空值的测试
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
int findInteger(const char str[81], char** end)
{
int i, div, j, c;
char num[81], **endc;
if ((str[0] == 0) || (str[0] == 1) || (str[0] == 2)
|| (str[0] == 3) || (str[0] == 4) || (str[0] == 5)
|| (str[0] == 6) || (str[0] == 7) || (str[0] == 8)
|| (str[0] == 9) || (str[0] == '\0') || (str[0] == ' ')
|| str[0] == '!' || str[0] == '"' || str[0] == '\''
|| str[0] == ';' || str[0] == ':' || str[0] == '?'
|| str[0] == '-' || str[0] == '.' || str[0] == ','
|| str[0] == ' ' || str[0] == '\n' )
{
div = 1;
}
else
{
div = 0;
}
for (i = 0; i < 81; i++)
{
if ((div) && ((str[i] == 0) || (str[i] == 1) || (str[i] == 2)
|| (str[i] == 3) || (str[i] == 4) || (str[i] == 5)
|| (str[i] == 6) || (str[i] == 7) || (str[i] == 8)
|| (str[i] == 9)))
{
for (j = i; j < 81; j++)
{
if ((str[j]) == 'a' || 'b' || 'c' || 'd' ||'e' ||'g'||'h'||'i'||'j'||'k'||'l'||'m'||'n'||'o'||'p'||'q'||'r'||'s'||'t'||'u'||'v'||'w'||'x'||'y'||'z')
{
j = 81;
endc = 0;
}
else
{
if (((!(str[j])) == ('a' || 'b' || 'c' || 'd' || 'e' || 'g' || 'h' || 'i' || 'j' || 'k' || 'l'
|| 'm' || 'n' || 'o' || 'p' || 'q' || 'r' || 's' || 't' || 'u' || 'v' || 'w' || 'x' || 'y' || 'z'
||'1'||'2'||'3'||'4'||'5'||'6'||'7'||'8'||'9'||'0')) || ((j == (80)) && ((str[j] == 0) || (str[j] == 1) || (str[j] == 2)
|| (str[j] == 3) || (str[j] == 4) || (str[j] == 5)|| (str[j] == 6) || (str[j] == 7) || (str[j] == 8)
|| (str[j] == 9))))
{
for (i; i < j; i++)
{
num[i] = str[i];
*endc == &str[j];
j = 81;
}
if ((j == (80)) && ((str[j] == 0) || (str[j] == 1) || (str[j] == 2)
|| (str[j] == 3) || (str[j] == 4) || (str[j] == 5)
|| (str[j] == 6) || (str[j] == 7) || (str[j] == 8)
|| (str[j] == 9)))
{
num[j] = str[j];
endc = 0;
j = 81;
}
}
}
}
}
}
c = atoi(num);
return (c);
}
int getNumbers(const char str[81], int numbers[40])
{
int count = 0;
char tmp = '\0';
char* tmpstr = &tmp;
strcpy(tmpstr, str);
char** pEnd = &tmpstr;
int i = 0;
do
{
numbers[i] = findInteger(tmpstr, pEnd);
if (!*pEnd) { count++; }
else { i++; tmpstr = *pEnd; }
} while (*pEnd);
return count;
}
int main()
{
printf("start\n");
char digit[20][81];
char *num[81], *endc;
int M;
scanf("%d", &M);
getchar();
for (int i = 0; i < M; i++)
{
fgets(digit[i], 81, stdin);
}
int allCount = 0;
for (int i = 0; i < M; i++)
{
findInteger(digit[i] , &endc);
allCount = i++;
getNumbers(num[i], &M[&i]);
}
printf("%d", &endc);
/*if (!allCount)
{
printf("no solution\n");
}*/
return 0;
}
解决方案
首先你必须了解 ASC II,检查它ASC II,它说 C 中的每个字符都有一个对应的整数。例如,如果检查:
char letter = 'A'
if( (int)letter == 65)
printf("It's me");
更准确地说,该字母是 char 类型,但如果将其转换为整数 (int) 字母,则整数值是一个数字。这将有助于解决问题。看到char'A'等于整数65,'B'是66,'C'是67......这意味着如果你这样做
if( (int)letter >= 65 || (int)letter <= 90)
printf("I'm a capslock letter");
当字母 = 'A' 时,它会打印出来。
对您有帮助的另一件事是“0”与 0 不同。“0”是一个字符值,而 0 是一个整数。要在您的程序中找到数字,必须按照以下方式进行:
下一个程序打印字符串中数字的位置。
char str[] = "Today is 23 and yet 16PM";
int index = 0;
while(str[index] != '\0') {
if(str[index] >= '0' && str[index] <= 9) {
printf("I m a number, my postion is %d", index);
}
index = index + 1;
}
每个字符串的结尾都是'\0',如果你打印它就不会显示出来,因为它只是标记结尾,但是你可以在你没有到达结尾时使用它来迭代字符串。代码从位置 0(索引 = 0)开始,所有字符串都从位置 0 开始。下面看到字符串的第一个字母:
printf("%c", str[0]);
有了它,您将能够解决这个问题。
写入数字数组的解决方案:
int j = 0; // this hold index of array numbers
for(int i = 0; i < 81; ++i) {
// if str is between this numbers, it's a separator
if( (int)str[i] >= 32 && (int)str[i] <= 47) {
i += 1; // let's check the next char
int lastj = j; // this save the last real numbers
// while true is always true and run forever, until command break
// edit: you can check end of string instead, in the while statment
while( true ) {
// check if it a number
if( (int)str[i] >= '0' && (int)str[i] <= '9') {
numbers[j] = atoi(str[i]);
j += 1;
// check if it s a separator
} else if((int)str[i] >= 32 && (int)str[i] <= 47) {
break;
} else {
//if there's a letter on it, that is not a number!
j = lastj; // "erase" numbers write into array
break;
}
}
}
}
推荐阅读
- php - 如何使用 html 表单将 php 地理编码地址插入 mysql 数据库
- batch-file - CMD 如何为每个搜索结果设置不同的变量
- java - 上传文件后如何获取下载地址
- c# - 如果 DbContext 的 OnConfiguring 方法中的所有配置,如何使用 AddDbContextPool
- android - 更新 Android Studio 后是否在现有项目上启用了 r8?
- jsf - 为什么 GlassFish 5.1.0 中的 JSF 将我的 @ViewScoped CDI bean 的 ID 属性设置为 null?
- c++ - 我应该使用什么类型的稀疏向量?
- sapui5 - 当您按下表格上的删除按钮时,如何获取行值?
- r - 在 R 中使用 lapply 时缺少参数
- unity3d - Unity 按钮 onclick 不触发功能(通过 Unity 界面链接)