如何为结尾的循环和开始循环。 [英] How to for Loop for totals at the end and begining.

查看:94
本文介绍了如何为结尾的循环和开始循环。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



有人可以帮助或指导我学习一个例子。



我试图在列表控件中总列数为30列。



我的问题取决于每个用户列数可以从5更改为7或9个或更多。



我使用5作为例子:

如果我在第0列:我想总计第0,1,2,2,2,2栏。

如果我在第29栏:我想总共有第29,0,1,27,28栏。



目前我在for循环中有上述内容,if语句硬编码,总共5列。有没有办法,如果我允许用​​户从5改为7,我可以动态获得总数而不是硬编码。



任何帮助或指向正确的方向可能会有所帮助。



我有一些关于我目前正在做的事情的代码。

Hello all,

Can someone please help or direct me to an example i can study.

I am trying to total columns in a list control which has 30 columns.

My problem is depending on each user the column count can change from 5 to 7 or 9 or more.

I am using 5 as an example:
if i am at column 0: I would like to have totals of column 0,1,2,28,29.
if I am at column 29: I would like to have total of column 29,0,1,27,28.

Currently I have the above in a for loop with an if statement hard coded with 5 column total. Is there a way, if i allow user to change from 5 to 7, I could dynamically get the total instead of it being hard coded.

Any help or pointing in right direction might help.

I have part of the code as to what I am currently doing.

for (int a=0; a<12;a++)
	{
		CString Temp;
		if (a==0)
		{
			Temp = m_Test.GetItemText(0,0);
			Temp = m_Test.GetItemText(0,11);
			Temp = m_Test.GetItemText(0,10);
			Temp = m_Test.GetItemText(0,1);
			Temp = m_Test.GetItemText(0,2);
			
			return;
		}
		if (a==1)
		{
			Temp = m_Test.GetItemText(0,1);
			Temp = m_Test.GetItemText(0,11);
			Temp = m_Test.GetItemText(0,0);
			Temp = m_Test.GetItemText(0,3);
			Temp = m_Test.GetItemText(0,2);

			return;
		}
		if (a==10)
		{
			Temp = m_Test.GetItemText(0,10);
			Temp = m_Test.GetItemText(0,11);
			Temp = m_Test.GetItemText(0,0);
			Temp = m_Test.GetItemText(0,9);
			Temp = m_Test.GetItemText(0,8);

			return;
		}
		if (a==11)
		{
		
			Temp = m_Test.GetItemText(0,11);
			Temp = m_Test.GetItemText(0,0);
			Temp = m_Test.GetItemText(0,1);
			Temp = m_Test.GetItemText(0,9);
			Temp = m_Test.GetItemText(0,10);

			return;
		}
		Temp = m_Test.GetItemText(0,a-1);
		Temp = m_Test.GetItemText(0,a-2);
		Temp = m_Test.GetItemText(0,a);
		Temp = m_Test.GetItemText(0,a+1);
		Temp = m_Test.GetItemText(0,a+2);

	}

推荐答案

你似乎缺少的是一种最后包装索引的方法柱。然后'%'(余数)运算符将为您提供一个工具。例如:

What you appear to be missing is a method to wrap the index around at the last column. Then '%' (remainder) operator will give you a tool for that. For example:
const int numOfColumns = 30;
int idx = 28;
int total = 0;
for (int k = 0; k < 5; ++k)
  total += columns[(idx + k) % numOfColumns];



您似乎需要的另一种方法是将您从单元格中读取的字符串转换为数字:


The other thing you seem to need is a way of converting the string you read from a cell into a number:

CString s;
s = m_Test.GetItemText(0,11);
int x = atoi (s);



将为您做到这一点。结合这两件事你就完成了。


will do that for you. Combine these two things and you are done.


这篇关于如何为结尾的循环和开始循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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