如何一次读取一组char条目并打印结果 [英] How to reads in a set of char entries at once and print result

查看:65
本文介绍了如何一次读取一组char条目并打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定义一个国家/地区,用于存储国家名称,人口,

及其面积。使用该课程,编写一个读取一组国家的程序

和打印

•面积最大的国家

•国家人口最多的国家

•人口密度最大的国家(每平方公里人口)



所以,理想情况下,如果我进入, b,c,d,e,它应该同时取所有的国家名称,即a,b,c,d,e,即一次,并且应该打印具有最大面积和最高人口的。(我要离开现在的密度部分,还输入国家名称作为单个字符,如ab,c,d,e,使其不那么复杂)。



我是什么尝试过:



Define a class Country that stores the name of the country, its population,
and its area. Using that class, write a program that reads in a set of countries
and prints
• The country with the largest area
• The country with the largest population
• The country with the largest population density (people per square kilometer)

so, ideally if i enter a,b,c,d,e, it should take all the country names i.e. a,b,c,d,e at once i.e. at one time and should print which has the largest area and which has the highest population.(I am leaving the density part for now, and also entering country names as single characters like ab,c,d,e to make it less complex).

What I have tried:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
class country
{
private:
int ar,po,largestar,largestpo;
char co,biggestar,biggestpo;
public:
country()
{
largestar=0;
largestpo=0;
}
void area();
void population();
};
void country::area()
{
cout<<"\nenter country area  ";
cin>>ar;
if(ar>largestar)
{
largestar=ar;
biggestar= co;
}

cout<<"\nlargest country is :"<<biggestar;
}
void country::population()
{
cout<<"\nenter country population  ";
cin>>po;
if(po>largestpo)
{
largestpo=po;
biggestpo= co;
}
cout<<"\nmost populated country is :"<<biggestpo;
}


main()
{
int i;
char co;
clrscr();
country c;
for (i=1;i<=5;i++)
{
cout<<"\n\nenter country name   ";
cin>>co;
c.area();
c.population();
}
getch();
}

我无法一次存储5个不同的国家/地区名称并打印结果,即应输入输入国家/地区名称:,然后输入所有国家/地区名称,例如: a,b,c,d,e,它应该打印哪个国家最大,哪个国家最多,例如

I am not able to store 5 different country names at once and print results i.e.It should ask "enter country name : " and after I enter all the country names e.g. a,b,c,d,e , it should print which country is largest and which is most populated among them e.g.

largest country is c   most populated country is a   

推荐答案

你的班级只需要几件事:国家名称,人口及其面积。

您的外部代码从用户读取名称,填充和大小,并使用该信息构造类的实例(使用带有三个参数的构造函数) 。你的班级决不应该直接与用户互动 - 这不是它的工作。



然后外部代码构造一个国家实例数组,它从中填充用户然后通过查看数组来检查每个类别中最大的 - 您可能添加到您班级的唯一帮助者是计算人口和区域的人口密度。



但这是你的作业,所以我不会给你任何代码!尝试一下,你会发现它比你正在尝试的更容易。
Your class need only a couple of things: the Country Name, population, and it's area.
Your external code reads the name, population, and size from the user and uses that information to construct an instance of the class (using a constructor which takes three parameters). At no time should your class interact directly with the user - that's not its job.

The external code then constructs an array of "country" instances which it populates from the user and and then examine to find the largest in each category by looking at the array - the only "helper" you might add to your class is to calculate the population density from the population and the area.

But this is your homework, so I'll give you no code! Give it a try, and you'll see that it's a easier to do it that way than what you are trying.


这篇关于如何一次读取一组char条目并打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆