Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Для новичков > Сложить Суммировать в StringGrid


Автор: DenRip 13.8.2007, 18:31
Проодлите код плиз
вот пример:

Код

String1.Cells[7,1]:= FloatTostr(переменная Integer+5);
String1.Cells[7,2]:= FloatTostr(переменная Integer+6);
String1.Cells[7,3]:= FloatTostr(переменная Integer+4);
String1.Cells[7,4]:= FloatTostr(переменная Integer+3);
String1.Cells[7,5]:= FloatTostr(переменная Integer+3);
String1.Cells[7,6]:= FloatTostr(переменная Integer+3);
String1.Cells[7,7]:= FloatTostr(переменная Integer+7);
String1.Cells[7,8]:= FloatTostr(переменная Integer+6);
String1.Cells[7,9]:= FloatTostr(переменная Integer+3);
String1.Cells[7,10]:= //  Здесь надо вывести сумму;


Можно содать переменную. присвоить ей сумму всех  переменных, после этого загнать в стринг,
а есть другой подход к ентому делу?

Автор: Letov 13.8.2007, 18:42
Можно без переменной.
Например, так (если я правильно понял задачу smile ):
Код

String1.Cells[7,10]:=FloatToStr(StrToFloat(String1.Cells[7,1])+
StrToFloat(String1.Cells[7,2])+StrToFloat(String1.Cells[7,3])+StrToFloat(String1.Cells[7,4])
+StrToFloat(String1.Cells[7,5])+StrToFloat(String1.Cells[7,6])+StrToFloat(String1.Cells[7,7])
+StrToFloat(String1.Cells[7,8])+StrToFloat(String1.Cells[7,9]));

Автор: ivan219 13.8.2007, 18:55
А можно и через массив если числоа яйчеек StringGrid различно а самое простое с фиксированным числом яйчеект -> Letov

Автор: Letov 13.8.2007, 18:57
ivan219, в этом вопросе есть много способов решения.

Автор: ivan219 13.8.2007, 18:58
А можно и более элегантно
Код

F:=0;
for I:=1 to 9 do F:=F + StrToFloat(String1.Cells[7,I]);
String1.Cells[7,10]:=FloatToStr(F);


Добавлено через 2 минуты и 58 секунд
И ещё если переменная типа Integer то заместо FloatToStr используй IntToStr так будет быстрее да и правильнее.

Автор: Letov 13.8.2007, 19:14
А можно и так:
Код

String1.Cells[7,10]:='0';
String1.Cells[7,1]:= FloatTostr(переменная Integer+5);
String1.Cells[7,10]:=FloatToStr(StrToFloat(String1.Cells[7,1])+StrToFloat(String1.Cells[7,10]));
String1.Cells[7,2]:= FloatTostr(переменная Integer+6);
String1.Cells[7,10]:=FloatToStr(StrToFloat(String1.Cells[7,2])+StrToFloat(String1.Cells[7,10]));
String1.Cells[7,3]:= FloatTostr(переменная Integer+4);
String1.Cells[7,10]:=FloatToStr(StrToFloat(String1.Cells[7,3])+StrToFloat(String1.Cells[7,10]));
String1.Cells[7,4]:= FloatTostr(переменная Integer+3);
String1.Cells[7,10]:=FloatToStr(StrToFloat(String1.Cells[7,4])+StrToFloat(String1.Cells[7,10]));
String1.Cells[7,5]:= FloatTostr(переменная Integer+3);
String1.Cells[7,10]:=FloatToStr(StrToFloat(String1.Cells[7,5])+StrToFloat(String1.Cells[7,10]));
String1.Cells[7,6]:= FloatTostr(переменная Integer+3);
String1.Cells[7,10]:=FloatToStr(StrToFloat(String1.Cells[7,6])+StrToFloat(String1.Cells[7,10]));
String1.Cells[7,7]:= FloatTostr(переменная Integer+7);
String1.Cells[7,10]:=FloatToStr(StrToFloat(String1.Cells[7,7])+StrToFloat(String1.Cells[7,10]));
String1.Cells[7,8]:= FloatTostr(переменная Integer+6);
String1.Cells[7,10]:=FloatToStr(StrToFloat(String1.Cells[7,8])+StrToFloat(String1.Cells[7,10]));
String1.Cells[7,9]:= FloatTostr(переменная Integer+3);
String1.Cells[7,10]:=FloatToStr(StrToFloat(String1.Cells[7,9])+StrToFloat(String1.Cells[7,10]));

Автор: ivan219 13.8.2007, 19:57
Letov, лишний расход ресурсов код должен быть как можно проще smile 

Автор: Letov 13.8.2007, 20:50
ivan219, это ты верно подметил. Но ведь просили не оптимизированный код, а варианты решения задачи! smile 

Автор: DenRip 13.8.2007, 22:43
Спасибо! Господа........Помогли

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)