| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 833 人关注过本帖
标题:[求助]阿菜请教问题之二,又急也~~俯首拜求解~~
只看楼主 加入收藏
bleach1983
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-5-22
收藏
 问题点数:0 回复次数:10 
[求助]阿菜请教问题之二,又急也~~俯首拜求解~~

各位大虾,这个问题有点罗嗦。如下程序:
如果我将倒数第一二行的“cout<<"Enter the miles driven:"; cin>>mile;”删掉的话,程序就计算混乱了;
如果不删,则在判断停止时,即gallon=-1,但mile的值也得输入。这就显得程序倒数第一二行的“cout<<"Enter the miles driven:"; cin>>mile;”多余了。有什么办法可以解决吗?
#include <iostream.h>
#include <iomanip.h>
int main()
{
float gallon,//所用油量
mile,//此油量下跑的公里数
t;//单位油量下跑的公里数
cout<<"Enter the gallon used (-1 to end):";
cin>>gallon;
cout<<"Enter the miles driven:";
cin>>mile;
while (gallon!=-1)
{
t=mile/gallon;
cout<<setprecision(6)<<setiosflags(ios::fixed|ios::showpoint)<<"The miles/gallon for this tank :"<<t<<endl;
cout<<"Enter the gallon used (-1 to end):";
cin>>gallon;
cout<<"Enter the miles driven:";
cin>>mile;
}
}


另外,如果我想把每次得到的t值保留下来并累加,最后除以累加的次数,得到一个t的平均值
并且这个平均值在最后gallon=-1时打印出来。如何在以上程序添加,或者大虾们有更好的做法。

[此贴子已经被作者于2006-5-22 15:04:21编辑过]

搜索更多相关主题的帖子: 求解 俯首 
2006-05-22 14:46
bleach1983
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-5-22
收藏
得分:0 

谢谢帮助我的各位大虾了~~


动感超人~~动感光波~~HOHO~~小白,你也是男孩子,所以你也要努力~~
2006-05-22 15:02
tree168
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-5-18
收藏
得分:0 

可以这样,但觉得有点罗索:

#include <iostream.h>
#include <iomanip.h>
int main()
{
float gallon,//所用油量
mile,//此油量下跑的公里数
t;//单位油量下跑的公里数
double sum_t=0,count=0;
cout<<"Enter the gallon used (-1 to end):";
cin>>gallon;
if (gallon==-1) return;
cout<<"Enter the miles driven:";
cin>>mile;
while (gallon!=-1)
{
t=mile/gallon; sum_t+=t;count++;
cout<<setprecision(6)<<setiosflags(ios::fixed|ios::showpoint)<<"The miles/gallon for this tank :"<<t<<endl;
cout<<"Enter the gallon used (-1 to end):";
cin>>gallon;
if (gallon==-1) break;
cout<<"Enter the miles driven:";
cin>>mile;
}
cout<<sum_t/count<<endl;

}


I believe I can fly,I can touch the sky.
2006-05-22 15:12
tree168
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-5-18
收藏
得分:0 
看有没有更好的

I believe I can fly,I can touch the sky.
2006-05-22 15:14
bleach1983
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-5-22
收藏
得分:0 
嘿嘿,我来试一试,大将军~~

动感超人~~动感光波~~HOHO~~小白,你也是男孩子,所以你也要努力~~
2006-05-22 15:16
bleach1983
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-5-22
收藏
得分:0 
问题已解决,并且相应地解决了几个我其它的问题,感谢~~

动感超人~~动感光波~~HOHO~~小白,你也是男孩子,所以你也要努力~~
2006-05-22 16:00
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float gallon,//所用油量
mile,//此油量下跑的公里数
t=0;//单位油量下跑的公里数
int count=0; //测试次数
do
{
cout<<"Enter the gallon used (-1 to end):";
cin>>gallon;
if(gallon==-1)
{
if(!count) cout<<"Did not test once!";
break;
}
cout<<"Enter the miles driven:";
cin>>mile;
count++;
t+=(mile/gallon);
cout<<setprecision(6)<<setiosflags(ios::fixed|ios::showpoint)<<"The miles/gallon for this tank :"<<t<<endl;
}while(1);
cout<<t/count<<endl;
system("pause");
return 0;
}

[此贴子已经被作者于2006-5-22 17:13:57编辑过]


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-05-22 16:23
bleach1983
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-5-22
收藏
得分:0 

楼上的朋友,你的程序在求平均数时count有1的误差呢~~,不过还是很感谢你~~

[此贴子已经被作者于2006-5-23 11:07:20编辑过]


动感超人~~动感光波~~HOHO~~小白,你也是男孩子,所以你也要努力~~
2006-05-23 11:04
bleach1983
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-5-22
收藏
得分:0 
7楼的朋友。
不好意思,是我错了,我没看清程序。可是还是有点小出入~~我指题意上。呵呵,没关系了,~~谢谢~~
我将你的程序稍微改了一下
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float gallon,//所用油量
mile,//此油量下跑的公里数
t=0;//单位油量下跑的公里数
int count=0; //测试次数
do
{
cout<<"Enter the gallon used (-1 to end):";
cin>>gallon;
if(gallon==-1)
{
if(!count) cout<<"Did not test once!";
break;
}
cout<<"Enter the miles driven:";
cin>>mile;
t+=(mile/gallon);
count++;
cout<<setprecision(6)<<setiosflags(ios::fixed|ios::showpoint)<<"The miles/gallon for this tank :"<<mile/gallon<<endl;//因为我想在屏幕上显示的是每次的测量值
}while(1);
cout<<t/count<<endl;
//system("pause");我认为这句话可以不加上去。
return 0;
}
希望你不要介意~~

[此贴子已经被作者于2006-5-23 11:20:35编辑过]


动感超人~~动感光波~~HOHO~~小白,你也是男孩子,所以你也要努力~~
2006-05-23 11:11
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
呵呵,我用的DEV-C++,所以我的必须加,不然不会暂停,结果一出来就自动关闭了,VC++不用加

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-05-23 15:42
快速回复:[求助]阿菜请教问题之二,又急也~~俯首拜求解~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015092 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved