如何在列中显示数组,就像在图片中一样,并对其进行排序 [英] How to print an array in columns shown just like in the picture and sort it

查看:126
本文介绍了如何在列中显示数组,就像在图片中一样,并对其进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何制作它如此打印它就像这张照片图片点击这里我希望它从最小到最大等打印当我们打印它时,它应该是小到大的。



How do i make it so it prints it just like this picture Picture click here I want it to print from smallest to greatest etc and also when we print it should be sroted small to large number.

<pre>#include <iostream>
#include "MyConsoleInput.h"	// for ReadInteger, ReadDouble

using namespace ConsoleInput;
using namespace std;


//temperature convert function
double temperatureConversion(double temperature)
{
	//convert the temperature
	double converted = temperature*(9/5)+32;
}

//output function
void output(double *farenheitArray, double *dynamicTempArray, int dayAmount)
{
	cout << "DAILY TEMPERATURE REPORT" << endl << "========================" << endl << endl;

	for(int counter = 0; counter <= dayAmount; counter++)
	{
		cout << "Day  " << counter++ << farenheitArray[counter] << "�F        " << dynamicTempArray[counter] << "�C";
	}
}

int main(int argc, char** argv)
{

	//initialize the dynamic array
	double *dynamicTempArray = NULL;



	//prompt the user for an input
	cout << "How many days do you want to enter?";

	//get the user's input for the array amount
	int dayAmount = ReadInteger(1, 365);

	try
	{
		//find heap space
		//set the array to the size of the user's choosing
		dynamicTempArray = new double[dayAmount];

		//for how many days starting at 0 read data into the array
		for (int counter = 0; counter <= dayAmount; counter++)
		{
			//create the variable and enter data into
			double userInput = ReadInteger(-90.0, 60.0);



			//set the current space in the array to the user input
			dynamicTempArray[counter] = userInput;
		}

		//create an array to hold the farenheit converted temperature
		double farenheitArray[dayAmount] = {NULL};


		//for each of the inputs call the temperature conversion function
		for(int counter = 0; counter <= dayAmount; counter++)
		{
			//convert the cuttent position in the array to the farenheit
			double farenheit = temperatureConversion(dynamicTempArray[counter]);

			//set the spot in the array to the fonverted temperature
			farenheitArray[counter] = farenheit;

			//if the counter is the day amount run the output function
			if (counter == dayAmount)
			{
				cout << farenheit << " ";
			}
		}

		//done with the array, set the memory free
		delete[] dynamicTempArray;
	}
	catch(bad_alloc & ex)
	{
		cout << "Error allocating memory" << endl;
	}

	return 0;
}





我的尝试:



我没有尝试因为我是c ++的新手,所以我不知道怎么做



What I have tried:

I haven't try cause I'm new to c++ so i have no clue how to do it

推荐答案

yopu真的需要用数组(和)?

尝试

Do yopu really need to use arrays (and new)?
Try
#include <vector>
#include <algorithm>
using namespace std;

struct Temperature
{
  Temperature(int day, double celsius):day(day),celsius(celsius){}
  int day;
  double celsius;
};

double farenh(double celsius){return celsius*9/5+32;}

ostream & operator << (ostream & os, const Temperature & t)
{
  os << "day " << t.day <<  " " << t.celsius << "°C " << farenh(t.celsius) << "°F";
  return os;
}

int main()
{
  vector <Temperature> vt;
  cout << "How many days do you want to enter?\n";
  size_t days;
  cin >> days;
  for ( size_t day = 0; day<days; ++day)
  {
    double celsius;
    cout << "Please inserte temperature (°C) of day " << (day+1) << "\n";
    cin >> celsius;
    vt.emplace_back( (day+1), celsius);
  }
  sort(vt.begin(), vt.end(), [](const Temperature & t1, const Temperature & t2) { return t1.celsius < t2.celsius;});

  for (const auto &  t : vt)
    cout << t << "\n";

}


这篇关于如何在列中显示数组,就像在图片中一样,并对其进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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