Технический форум

Технический форум (http://www.tehnari.ru/)
-   Помощь студентам (http://www.tehnari.ru/f41/)
-   -   Просьба проверить код на корректность, C++ (http://www.tehnari.ru/f41/t43141/)

Тарас 09.12.2010 20:31

Просьба проверить код на корректность, C++
 
Имеется задача для С++
В файле хранится инфа;
999999 - номер изделия; 30А - его наименование; dd/mm/yy - дата выпуска; 999999 - кол-во изделий выпущено в определенную дату; 999999 - цена одно изделия
Прочитать файл, отсортировать сначала по алфавиту, по наименованию а потом по дате.
Вывести общее кол-во изделий.

вот код:
Код:

#include <iostream>
#include <string>
#include <fstream>
using namespace std

bool compare_strings (string first, string second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (tolower(first[i])<tolower(second[i])) return true;
else if (tolower(first[i])>tolower(second[i])) return false;
++i;
}
if (first.length()<second.length()) return true;
else return false;
}

bool compare_items (item first, item second)
{
if (first.name.compare(second.name) != 0)
return compare_strings (first.name, second.name)
else
return compare_strings (first.date, second.date)
}

main ()
{
record Item {
longint number;
string name;
string date;
longint quantity;
longint price }

list<Item> Items;
list<string>::iterator it;
Item tempItem;

fstream itemsFile ("items.txt", fstream::in);

string line, field;
size_t posNext, posPrevious;

while (!itemsFile.eof()) {
getline(itemsFile, line);
posNext = line.find("/");

field.clear();
field = line.substr (1, posNext);
tempItem.number = field;

posPrevious = posNext;
posNext = line.find("/", posPrevious+1);

field.clear();
field = line.substr (posPrevious, posNext);
tempItem.name = field;

posPrevious = posNext;
posNext = line.find("/", posPrevious+1);

field.clear();
field = line.substr (posPrevious, posNext);
tempItem.date = field;

posPrevious = posNext;
posNext = line.find("/", posPrevious+1);

field.clear();
field = line.substr (posPrevious, posNext);
tempItem.quantity = field;

posPrevious = posNext;
posNext = line.find("/", posPrevious+1);

field.clear();
field = line.substr (posPrevious, NULL);
tempItem.price = field;

Items.push_front(tempItem);
}

Items.sort(compare_strings);

for (it=Items.begin(); it!=Items.end(); ++it)
cout << " " << *it;
cout << endl;

cout << "size: " << (int) Items.size() << endl;

return 0;
}



Часовой пояс GMT +4, время: 02:02.

Powered by vBulletin® Version 4.5.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.