我的代码如下:
cc_string = "378282246310005"
for (int i = 0, n = strlen(cc_string); i < (n / 2); i++)
{
index = cc_string[i];
sum += (index * 2);
printf("%i\n", sum);
}
但我的输出总是(字符串中的每个数字都加在一起):
102
212
324
424
536
636
736
而不是:
22
38
42
50
56
56
56
答案 0 :(得分:1)
问题是你使用ASCII
数字作为数字,这是不正确的。
ASCII '1'
为0x31
十六进制或49
十进制。
因此,您的第一个循环会为您提供102 = '3'(0x33, 51) * 2
要修复它,请简单地将“0”减去ASCII数字:
index = cc_string[i] - '0';
显然,你问题中的被驱逐输出没有任何意义,因为如果sum
被初始化为6
,那么首先sum
将0
不是sum
因此预期输出可以是以下加上6
20
36
40
56
60
64
初始值
// Existing Geometry Collection
pExGeomColl = (IGeometryCollection)new PolylineClass();
pExGeomColl = (IGeometryCollection)pBaseFeat.ShapeCopy;
// New Geometry collection
pNewGeomColl = (IGeometryCollection)new PolylineClass();
pMAware = (IMAware)pNewGeomColl;
pMAware.MAware = true;
pZAware = (IZAware)pNewGeomColl;
pZAware.ZAware = bHasZ;
for (int i = 0; i < hGeomToFrom.Count; i++)
{
iTo = i;
iFrom = (int)hGeomToFrom[i];
pGeom = pExGeomColl.Geometry[iFrom];
pGeom.SpatialReference = pSpRef;
pMAware = (IMAware)pGeom;
pMAware.MAware = true;
pZAware = (IZAware)pGeom;
pZAware.ZAware = bHasZ;
// Convert the geometry to a polyline
pPolyline = (IPolyline6)new PolylineClass();
if (pGeom.GeometryType != esriGeometryType.esriGeometryPolyline)
{
pPolyline = geometryToPolyline(pGeom, bHasZ, true, ref sError);
if (sError.Length > 0)
{
sError = "cmdReset\r\n" + sError;
clsMain.write_log(sError, clsMain.m_eLogType.FATAL);
MessageBox.Show(sError);
return;
}
}
else
{
pPolyline = (IPolyline)pGeom;
}
pMAware = (IMAware)pPolyline;
pMAware.MAware = true;
pZAware = (IZAware)pPolyline;
pZAware.ZAware = bHasZ;
dLen = pPolyline.Length;
dFrom = dMeasure;
dTo = dFrom + dLen;
pSeg = (IMSegmentation4)pPolyline;
pSeg.SetAndInterpolateMsBetween(dFrom, dTo);
IGeometryCollection pXGeomColl = new PolylineClass();
pMAware = (IMAware)pXGeomColl;
pMAware.MAware = true;
pZAware = (IZAware)pXGeomColl;
pZAware.ZAware = bHasZ;
pXGeomColl = (IGeometryCollection)pPolyline;
for (int j = 0; j < pXGeomColl.GeometryCount; j++)
pNewGeomColl.AddGeometry(pXGeomColl.Geometry[j]);
dMeasure = dTo;
ProgressBar1.Value = iCount;
iCount++;
}
pGeom = (IGeometry)pNewGeomColl;
pGeom.SpatialReference = pSpRef;
pMAware = (IMAware)pGeom;
pMAware.MAware = true;
pZAware = (IZAware)pGeom;
pZAware.ZAware = bHasZ;
pBaseFeat.Shape = pGeom;
pBaseFeat.Store();