读一个txt.file 文件

Nikeyu 2009-04-23 11:13:34
txt.file 如下:
Cheaha Mountain$Alabama$2405 27 28 26 43 29 22 54 36 27 38 38 39
Mount McKinley$Alaska$20320 80 85 96 95 85 93 92 99 78 87 95 90
Humphreys Peak$Arizona$12633 74 72 90 85 82 77 82 86 77 80 87 95
Mount Magazine$Arkansas$2753 29 49 25 53 24 24 35 38 30 42 26 50
Mount Whitney$California$14494 81 73 79 87 84 74 82 87 72 86 77 97
Mount Elbert$Colorado$14433 67 86 81 85 72 81 87 89 73 77 84 94
Mount Frissell$Connecticut$2380 30 20 50 43 40 24 39 58 37 32 44 38
Tower Hill$Delaware$448 36 48 29 49 16 17 47 39 33 20 23 34
Britton Hill$Florida$345 25 39 44 33 11 20 34 50 25 37 25 27
Brasstown Bald$Georgia$4784 43 50 60 45 30 40 30 55 18 24 42 66
Mauna Kea$Hawaii$13796 83 83 93 85 70 77 78 98 69 81 85 87
Borah Peak$Idaho$12662 81 77 78 92 75 81 83 82 78 80 91 83
Charles Mound$Illinois$1235 30 36 51 47 13 49 52 36 17 37 21 26
High Point$Indiana$1257 39 33 53 39 18 48 26 48 39 26 51 49
High Point$Iowa$1670 16 42 34 37 22 43 50 31 41 41 47 51
Mount Sunflower$Kansas$4039 48 52 35 51 31 32 36 49 19 46 52 50
Black Mountain$Kentucky$4145 34 27 48 60 24 40 39 63 26 54 51 57
Driskell Mountain$Louisiana$535 38 29 44 30 35 39 53 36 11 47 21 47
Mount Katahdin$Maine$5268 42 31 34 56 31 50 60 39 39 28 54 48
Backbone Mountain$Maryland$3360 25 45 56 38 31 40 55 58 21 24 37 55
Mount Greylock$Massachusetts$3491 48 53 44 44 44 35 56 65 22 29 50 52
Mount Arvon$Michigan$1979 14 24 21 38 35 46 50 43 15 20 53 35
Eagle Mountain$Minnesota$2341 44 19 50 35 39 40 37 56 21 34 44 51
Woodall Mountain$Mississippi$806 34 25 31 25 22 32 26 42 19 20 28 56
Taum Sauk Mountain$Missouri$1772 39 38 48 58 29 46 22 49 22 24 38 37
Granite Peak$Montana$12799 82 83 86 87 70 73 90 90 83 73 91 90
Panorama Point$Nebraska$5424 27 28 49 36 27 26 55 39 34 41 57 54
Boundary Peak$Nevada$13143 80 83 83 90 70 82 87 89 79 89 77 90
Mount Washington$New Hampshire$6288 49 44 55 42 45 66 46 75 40 64 64 72
High Point$New Jersey$1803 14 47 50 34 13 22 53 42 21 33 47 53
Wheeler Peak$New Mexico$13161 75 72 87 96 74 79 80 93 71 84 92 89
Mount Marcy$New York$5344 38 36 46 66 23 29 29 50 39 44 40 45
Mount Mitchell$North Carolina$6684 50 43 60 75 57 51 55 42 50 51 57 61
White Butte$North Dakota$3506 25 32 51 38 29 47 53 55 48 46 32 52
Campbell Hill$Ohio$1550 29 28 50 42 12 33 32 27 22 17 20 56
Black Mesa$Oklahoma$4973 48 53 46 54 24 29 42 45 51 48 55 50
Mount Hood$Oregon$11237 53 75 67 77 60 60 72 83 54 74 75 85
Mount Davis$Pennsylvania$3213 45 25 54 54 41 49 55 39 38 33 40 32
Jerimoth Hill$Rhode Island$812 37 19 39 42 24 24 22 39 17 42 46 44
Sassafras Mountain$South Carolina$3560 25 40 57 61 44 55 56 39 46 24 41 58
Harney Peak$South Dakota$7242 58 34 45 72 46 44 39 42 31 54 70 74
Clingman's Dome$Tennessee$6643 38 45 67 48 32 58 42 65 36 60 59 68
Guadalupe Peak$Texas$8749 41 61 39 57 30 61 67 45 35 51 50 50
Kings Peak$Utah$13528 71 88 81 99 82 72 90 90 76 81 77 88
Mount Mansfield$Vermont$4393 47 36 47 39 48 22 53 62 36 47 47 58
Mount Rogers$Virginia$5729 27 27 45 62 40 47 41 37 47 29 51 39
Mount Rainier$Washington$14410 80 84 82 94 68 73 91 82 72 89 84 94
Spruce Knob$West Virginia$4863 36 39 32 34 19 38 43 46 48 27 49 43
Timms Hill$Wisconsin$1952 10 29 38 37 26 39 49 28 41 47 35 35
Gannett Peak$Wyoming$13804 70 78 92 96 68 86 86 83 72 73 79 97
问user 输入5个选择

如果选择了第一个:
1.Cheaha Mountain Alabama 2405 34
Mount McKinley Alaska 20320 90
Humphreys Peak Arizona 12633 82
.........................................
第一个是山的名称 第二个是州 第三个是洲旁边的数字 第四个是右边12个数字的平均数

如果选了第二个:
问user 哪个洲他感兴趣 然后打出以下
The highest peak in Maine is Mount Katahdin with an elevation of 5268.
The highest rating is 60, the lowest rating is 28, and the average rating is 43.
打出在洲和山的名称 洲旁边的数字 12个数字的最大值 最小值 和平均值

如果选了第三个:
Climber Average Rating
1 60
2 58
3 57
........................
average rating是12个数字纵条的平均值

如果选了第四个:
Climber Average Rating
12 60
4 58
8 57
.....................
climber的序号根据那12个数字从左往右数

如果选了第五个:
算出50行12个数字的平均值


先谢谢所有看帖的人了



...全文
170 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Nikeyu 2009-04-23
  • 打赏
  • 举报
回复
是啊 我都无赖了 哈哈
  • 打赏
  • 举报
回复
很长,帮顶
Nikeyu 2009-04-23
  • 打赏
  • 举报
回复
真的是十分感谢了
woods2001 2009-04-23
  • 打赏
  • 举报
回复
呵呵``` 顶下!
liliangbao 2009-04-23
  • 打赏
  • 举报
回复
帮顶~
Nikeyu 2009-04-23
  • 打赏
  • 举报
回复
我知道啊 不过我没时间了 明天还有占成绩比例更多的作业得交 只有这样了已经final了 只有暑假去下苦工了 谢谢
信念 2009-04-23
  • 打赏
  • 举报
回复
沙发,楼主不能这么懒
gao125210 2009-04-23
  • 打赏
  • 举报
回复
up
Nikeyu 2009-04-23
  • 打赏
  • 举报
回复
我晕谢谢你了
donle1000 2009-04-23
  • 打赏
  • 举报
回复
第四个是啥意思完全不能理解了 放弃了
其他的应该理解的都是对的


#pragma warning(disable: 4786)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
using namespace std;

vector<string> MountOn;
vector<string> StateOn;
vector< vector<int> > NumOn;
vector<double> AverageOn;

void InitOperate(fstream &);
void Query();
void Fav();
void List();
void Rate();
void Ave();

int main()
{
fstream fin("txt.file", ios::in);
InitOperate(fin);
cout << "make your select(1-5): ";
int n;
cin >> n;
switch(n)
{
case 1: Query(); break;
case 2: Fav(); break;
case 3: List(); break;
case 4: Rate(); break;
case 5: Ave(); break;
default:
return -1;
}
return 0;
}

void InitOperate(fstream &fin)
{
string str;
string Mount;
string State;
string NumBuf;
vector<int> Num;
string::size_type pos;
char Line[256] = {0};
double sum;
while(fin.getline(Line, 256))
{
Mount.erase(Mount.begin(), Mount.end());
State.erase(State.begin(), State.end());
NumBuf.erase(NumBuf.begin(), NumBuf.end());
Num.erase(Num.begin(), Num.end());
str = Line;
for(pos = 0; str[pos] != '$'; pos++)
Mount += str[pos];
pos++;
for(; str[pos] != '$'; pos++)
State += str[pos];
pos++;
for(; pos != str.size(); pos++)
NumBuf += str[pos];
stringstream ss(NumBuf);
int i;
for(i = 0; i < 13; i++)
{
int temp;
ss >> temp;
Num.push_back(temp);
}
sum = 0;
for(vector<int>::size_type j = 1; j < 13; j++)
sum += Num[j];
sum /= 12.0;
AverageOn.push_back(sum);
MountOn.push_back(Mount);
StateOn.push_back(State);
NumOn.push_back(Num);
}
fin.close();
}

void Query()
{
double sum = 0;
vector<string>::size_type i;
for(i = 0; i < MountOn.size(); i++)
{
cout << MountOn[i] << "\t\t" << StateOn[i] << "\t\t" << NumOn[i][0] << '\t';
cout << setiosflags(ios::fixed) << setprecision(2) << AverageOn[i] << endl;
}
}

void Fav()
{
cout << "which State do you like to know? " << endl;
string state;
cin >> state;
vector< vector<int> > MaxNMin;
map<int, string> MountInfo;
for(vector<string>::size_type pos = 0; pos != StateOn.size(); pos++)
{
if(StateOn[pos] == state)
{
MountInfo.insert(pair<int, string>(NumOn[pos][0], MountOn[pos]));
MaxNMin.push_back(NumOn[pos]);
}
}
map<int, string>::reverse_iterator it = MountInfo.rbegin();
cout << "The highest peak in " << state << " is " << it->second << " with an elevation of " << it->first << "." << endl;
int Max = 0, Min = MaxNMin[0][0];
vector<int>::size_type size = 0;
double sum = 0;
for(vector< vector<int> >::size_type i = 0; i != MaxNMin.size(); i++)
{
size += MaxNMin[i].size();
for(vector<int>::size_type j = 1; j != MaxNMin[i].size(); j++)
{
if(MaxNMin[i][j] > Max)
Max = MaxNMin[i][j];
if(MaxNMin[i][j] < Min)
Min = MaxNMin[i][j];
sum += MaxNMin[i][j];
}
}
cout << "The highest rating is " << Max << ", the lowest rating is " << Min << ", and the average rating is " << sum / size << endl;
}

void List()
{
double col = 0;
cout << "Climber Average Rating " << endl;
for(vector<int>::size_type j = 1; j != NumOn[0].size(); j++)
{
col = 0;
for(vector< vector<int> >::size_type i = 0; i != NumOn.size(); i++)
col += NumOn[i][j];
col /= NumOn.size();
cout << j << "\t\t" << col << endl;
}
}

void Rate()
{

}

void Ave()
{
double sum = 0;
vector<int>::size_type size = 0;
for(vector< vector<int> >::size_type i = 0; i != NumOn.size(); i++)
{
for(vector<int>::size_type j = 1; j != NumOn[i].size(); j++)
{
size++;
sum += NumOn[i][j];
}
}
cout << "the average is: " << sum / size << endl;
}
Nikeyu 2009-04-23
  • 打赏
  • 举报
回复
我在写别的作业不过还是报着希望来看一眼 在过一个小时就得交了 哈哈
donle1000 2009-04-23
  • 打赏
  • 举报
回复
写了两个模块 后面描述的看不懂...


#pragma warning(disable: 4786)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
using namespace std;

vector<string> MountOn;
vector<string> StateOn;
vector< vector<int> > NumOn;
vector<double> AverageOn;

void InitOperate(fstream &);
void Query();
void Fav();
void List();
void Rate();
void Ave();

int main()
{
fstream fin("txt.file", ios::in);
InitOperate(fin);
cout << "make your select(1-5): ";
int n;
cin >> n;
switch(n)
{
case 1: Query(); break;
case 2: Fav(); break;
case 3: List(); break;
case 4: Rate(); break;
case 5: Ave(); break;
default:
return -1;
}
return 0;
}

void InitOperate(fstream &fin)
{
string str;
string Mount;
string State;
string NumBuf;
vector<int> Num;
string::size_type pos;
char Line[256] = {0};
double sum;
while(fin.getline(Line, 256))
{
Mount.erase(Mount.begin(), Mount.end());
State.erase(State.begin(), State.end());
NumBuf.erase(NumBuf.begin(), NumBuf.end());
Num.erase(Num.begin(), Num.end());
str = Line;
for(pos = 0; str[pos] != '$'; pos++)
Mount += str[pos];
pos++;
for(; str[pos] != '$'; pos++)
State += str[pos];
pos++;
for(; pos != str.size(); pos++)
NumBuf += str[pos];
stringstream ss(NumBuf);
int i;
for(i = 0; i < 13; i++)
{
int temp;
ss >> temp;
Num.push_back(temp);
}
sum = 0;
for(vector<int>::size_type j = 1; j < 13; j++)
sum += Num[j];
sum /= 12.0;
AverageOn.push_back(sum);
MountOn.push_back(Mount);
StateOn.push_back(State);
NumOn.push_back(Num);
}
fin.close();
}

void Query()
{
double sum = 0;
vector<string>::size_type i;
for(i = 0; i < MountOn.size(); i++)
{
cout << MountOn[i] << "\t\t" << StateOn[i] << "\t\t" << NumOn[i][0] << '\t';
cout << setiosflags(ios::fixed) << setprecision(2) << AverageOn[i] << endl;
}
}

void Fav()
{
cout << "which State do you like to know? " << endl;
string state;
cin >> state;
vector< vector<int> > MaxNMin;
map<int, string> MountInfo;
for(vector<string>::size_type pos = 0; pos != StateOn.size(); pos++)
{
if(StateOn[pos] == state)
{
MountInfo.insert(pair<int, string>(NumOn[pos][0], MountOn[pos]));
MaxNMin.push_back(NumOn[pos]);
}
}
map<int, string>::reverse_iterator it = MountInfo.rbegin();
cout << "The highest peak in " << state << " is " << it->second << "with an elevation of " << it->first << "." << endl;
int Max = 0, Min = MaxNMin[0][0];
vector<int>::size_type size = 0;
double sum = 0;
for(vector< vector<int> >::size_type i = 0; i != MaxNMin.size(); i++)
{
size += MaxNMin[i].size();
for(vector<int>::size_type j = 1; j != MaxNMin[i].size(); j++)
{
if(MaxNMin[i][j] > Max)
Max = MaxNMin[i][j];
if(MaxNMin[i][j] < Min)
Min = MaxNMin[i][j];
sum += MaxNMin[i][j];
}
}
cout << "The highest rating is " << Max << ", the lowest rating is " << Min << ", and the average rating is " << sum / size << endl;
}

void List()
{

}

void Rate()
{

}

void Ave()
{

}
ltc_mouse 2009-04-23
  • 打赏
  • 举报
回复
结构体数组,一行一行读入~

lz有这么多时间等解答,为什么不自己尝试下呢?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧