Va rog urgeeent___ Dau coroana___
Doua programe va rog urgeeent
In C++
Și vă rog cu comentarea fiecărui rind sau macar rezolvațile
Urgeent
Am atașat poză⬇️
Răspunsuri la întrebare
18. Ariile din exemplu sunt gresite.
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
struct Parallelipiped
{
int parCode, length, width, height, area;
char material;
Parallelipiped(int parCode, int length, int width, int height, char material)
{
this->parCode = parCode;
this->length = length;
this->width = width;
this->height = height;
this->material = material;
this->area = (length * width) + (length * height) + (width * height);
}
friend bool operator<(const Parallelipiped self, const Parallelipiped other)
{
return self.area < other.area;
}
};
int main()
{
ifstream fileIn ("corpgeo.in");
ofstream fileOut ("corpgeo.out");
int parCode, length, width, height;
char material;
vector<Parallelipiped> elements;
while (fileIn >> parCode >> length >> width >> height >> material)
{
if (material != 'p')
continue;
Parallelipiped parallelipiped(parCode, length, width, height, material);
elements.emplace_back(parallelipiped);
}
sort(elements.begin(), elements.end());
for (Parallelipiped par : elements)
{
fileOut << par.parCode << " " << par.length << " " << par.width << " " << par.height
<< " " << par.material << " " << par.area << endl;
}
}
19. By the way, nu exista "String" in C++, ca nu e Java. Exista doar "string" sau cstring (care e un array de caractere).
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void insertionSort(string &str)
{
for (int i = 0; i < str.size() - 1; ++i)
{
for (int j = i + 1; j < str.size(); ++j)
{
if (str[i] > str[j])
{
char tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
}
}
}
int main()
{
int numElements;
cin >> numElements;
vector<string> elements;
for (int i = 0; i < numElements; ++i)
{
string str;
cin >> str;
insertionSort(str);
elements.emplace_back(str);
}
for (const string& str : elements)
cout << str << endl;
}