C 根据用户输入创建函数
我的程序允许用户在文本文件中输入最多10个整数/字符。然后我使用:C 根据用户输入创建函数,c,function,C,Function,我的程序允许用户在文本文件中输入最多10个整数/字符。然后我使用: number = atoi(word); printf("\nstring is \t %s\n", word); printf("integer is \t %d\n", number); 将输入的字符/整数从ASCII转换为整数 例如: 输入:e输出:字符串为e,整数为0 输入:10输出:字符串为10,整数为10 我需要一个函数来显示所有用户输入的最
number = atoi(word);
printf("\nstring is \t %s\n", word);
printf("integer is \t %d\n", number);
将输入的字符/整数从ASCII转换为整数
例如:
输入:e
输出:字符串为e,整数为0
输入:10
输出:字符串为10,整数为10
我需要一个函数来显示所有用户输入的最小整数。我已经测试了下面的代码,以获得最小的整数,但是这不包含在函数中。我无法创建一个每次考虑不同数量用户输入的函数
以下代码成功运行以返回我的最小整数:
int minIndex = 0;
int minNumber = INT_MAX;
if (number <= minNumber)
{
minNumber = number;
minIndedx = i;
}
if (minIndex > 0)
printf("min number is %d at position %d\n", minNumber, minIndex);
else
printf("no numbers read in; hence: no minimum calculated.");`
int minIndex=0;
int minNumber=int_MAX;
如果(数字0)
printf(“最小编号为%d,位置为%d\n”,最小编号,最小索引);
其他的
printf(“未读取数字;因此:未计算最小值”)`
关于如何进行有什么想法吗 您所要做的就是将代码放入一个函数中,然后调用该函数
int minIndex = 0; //Making Them Global Variables
int minNumber = INT_MAX; //Making Them Global Variables
int i = 0; //Declaring And Initializing The Global
//Variable i to keep count of the position.
number = atoi(word);
printf("\nstring is \t %s\n", word);
printf("integer is \t %d\n", number);
min(number); //Calling function min and passing it value of number
void min(int number){
if (number <= minNumber){
minNumber = number;
minIndedx = i;
}
if (minIndex > 0)
printf("min number is %d at position %d\n", minNumber, minIndex);
else
printf("no numbers read in; hence: no minimum calculated.");
i++; //Every time Incrementing i to update position.
}
int minIndex=0//使它们成为全局变量
int minNumber=int_MAX//使它们成为全局变量
int i=0//声明和初始化全局
//变量i用于保持位置的计数。
数字=atoi(字);
printf(“\n字符串是\t%s\n”,word);
printf(“整数是\t%d\n”,数字);
最小值(数字)//调用函数min并将数字的值传递给它
最小无效值(整数){
如果(数字0)
printf(“最小编号为%d,位置为%d\n”,最小编号,最小索引);
其他的
printf(“未读取数字;因此:未计算最小值”);
i++;//每次增加i以更新位置。
}
在循环中运行程序,然后每次使用存储在变量number
中的不同用户输入调用函数min时,如果传递给它的数字小于设置的最小值,该函数将更新最小数字的值,从而获得用户输入的最小数字
注意:这不是一个有效的程序,您必须将其嵌入代码中,并根据需要进行更改。我无法输入,因为我不知道您是如何输入的。请正确设置此代码的格式。还有,
i
的值在你的代码中来自哪里?@squemishossifrage这样行吗?@Olaf我知道我只是来告诉OP怎么做的?@Olaf我编辑它是为了添加注释,请仔细阅读。谢谢你的见解和输入。我将在代码中看到如何实现这一点。我还将字符存储在char-word[10]
中,并使用scanf(“%s”,word)代码>