在 C 字符串 C++ 中收集最大和最小数字
Gathering Largest and Smallest Numbers in C-String C++
我的家庭作业项目的目标是创建一个程序,该程序接受一系列中间没有空格的整数作为字符串。然后将其相加并显示字符串中的最小值和最大值。我确实得到了我的总和和最大值,但无论出于何种原因,我的程序中应该获得最小值的部分不起作用,当我尝试计算该值时,我什么也得不到。
比如我输入1234,我得到
"您输入的字符串中所有数字的总和为10
这一系列整数中的最大值是 4,而最小值是" ,
这是我的完整程序。感谢您的帮助 (:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
//Declaring Variables & Character Array:
int size = 100;
char integers[size];
//Small and Large Numbers:
char small = '9';
char large = '0';
//Gathering Integers:
cout << "Please enter a series of integers with nothing between them.";
cin >> integers;
//Gathering Size of String:
size = (strlen(integers) + 1);
//Initializing Sum Variable:
int sum = 0;
//Gathering Sum of All Integers in String:
for(int i = 0; i < size; i++)
{
if(integers[i] >= '0' && integers[i] <= '9' && integers[i] != '[=10=]')
{
if(integers[i] == '0')
sum += 0;
if(integers[i] == '1')
sum += 1;
if(integers[i] == '2')
sum += 2;
if(integers[i] == '3')
sum += 3;
if(integers[i] == '4')
sum += 4;
if(integers[i] == '5')
sum += 5;
if(integers[i] == '6')
sum += 6;
if(integers[i] == '7')
sum += 7;
if(integers[i] == '8')
sum += 8;
if(integers[i] == '9')
sum += 9;
}
}
//Gathering Largest Value:
for(int j = 0; j < size; j++)
{
if(integers[j] > large)
large = integers[j];
}
//Gathering Smallest Number
for(int j = 0; j < size; j++)
{
if(integers[j] < small)
small = integers[j];
}
//Outputting Values:
cout << "The sum of all numbers within the string you input is " << sum << endl;
cout << "The largest value in this series of integers is " << large << ", whilst the smallest value is " << small << endl;
return 0;
}
错误在这里
//Gathering Size of String:
size = (strlen(integers) + 1);
这是不正确的,应该是
size = strlen(integers);
因为你的大小不正确,所以你正在处理字符串末尾的 nul 字符,这会打乱你的最小计算(因为 nul 字符是最小的字符)。
最后small
的结果是[=11=]
,因为你扫描的是空字符,所以它的值是最小的。
看到这个:https://ideone.com/6sLWJV
并且,作为一个建议。如果你保证你的输入不会超过数组的大小,通过判断当前字符是否为[=11=]
来停止迭代,而不是计算迭代时间并与数组大小进行比较
]
我的家庭作业项目的目标是创建一个程序,该程序接受一系列中间没有空格的整数作为字符串。然后将其相加并显示字符串中的最小值和最大值。我确实得到了我的总和和最大值,但无论出于何种原因,我的程序中应该获得最小值的部分不起作用,当我尝试计算该值时,我什么也得不到。
比如我输入1234,我得到
"您输入的字符串中所有数字的总和为10 这一系列整数中的最大值是 4,而最小值是" ,
这是我的完整程序。感谢您的帮助 (:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
//Declaring Variables & Character Array:
int size = 100;
char integers[size];
//Small and Large Numbers:
char small = '9';
char large = '0';
//Gathering Integers:
cout << "Please enter a series of integers with nothing between them.";
cin >> integers;
//Gathering Size of String:
size = (strlen(integers) + 1);
//Initializing Sum Variable:
int sum = 0;
//Gathering Sum of All Integers in String:
for(int i = 0; i < size; i++)
{
if(integers[i] >= '0' && integers[i] <= '9' && integers[i] != '[=10=]')
{
if(integers[i] == '0')
sum += 0;
if(integers[i] == '1')
sum += 1;
if(integers[i] == '2')
sum += 2;
if(integers[i] == '3')
sum += 3;
if(integers[i] == '4')
sum += 4;
if(integers[i] == '5')
sum += 5;
if(integers[i] == '6')
sum += 6;
if(integers[i] == '7')
sum += 7;
if(integers[i] == '8')
sum += 8;
if(integers[i] == '9')
sum += 9;
}
}
//Gathering Largest Value:
for(int j = 0; j < size; j++)
{
if(integers[j] > large)
large = integers[j];
}
//Gathering Smallest Number
for(int j = 0; j < size; j++)
{
if(integers[j] < small)
small = integers[j];
}
//Outputting Values:
cout << "The sum of all numbers within the string you input is " << sum << endl;
cout << "The largest value in this series of integers is " << large << ", whilst the smallest value is " << small << endl;
return 0;
}
错误在这里
//Gathering Size of String:
size = (strlen(integers) + 1);
这是不正确的,应该是
size = strlen(integers);
因为你的大小不正确,所以你正在处理字符串末尾的 nul 字符,这会打乱你的最小计算(因为 nul 字符是最小的字符)。
最后small
的结果是[=11=]
,因为你扫描的是空字符,所以它的值是最小的。
看到这个:https://ideone.com/6sLWJV
并且,作为一个建议。如果你保证你的输入不会超过数组的大小,通过判断当前字符是否为[=11=]
来停止迭代,而不是计算迭代时间并与数组大小进行比较