c++函数编程统计问题

来源:百度知道 编辑:UC知道 时间:2024/06/21 03:30:59
要求定义函数实现求和\平均值\最大值\最小值
用gets(str)输入
sum -1 4 2 10 或 ave \min \max
输出结果
抱歉忘了说了
还要一段c语言的,题目要求同上,定义四个函数
谢谢~

#include "iostream.h"
#include "stdio.h"
#include "string.h"

int sum(char*str)
{
int j=0,shuzi=0,he=0;
for(j=0;;j++)
{
if(str[j]==NULL)
break;
else
shuzi=str[j]-48;
he+=shuzi;

}
return he;
}

void main()
{
int num=0,i=0;
char str[10];
printf("请输入数据:\n");
gets(str);
cout<<sum(str)<<endl;

}
备注:使用获取字符串的形式是无法将10放在一起的,字符串单独存储1和0

#include <sstream>
#include <string>

using namespace std;

int main()
{
string str;
gets(str);
isstream in(str);
in>>str;
int ans=0, k=0;
if(str[1]=='i')ans=1<<30;
while(str>>x)
{
if(str[0]=='m')
{
if(str[1]=='i')ans=ans<x?ans:x;
else ans=ans>x?ans:x;
}
else
{
an