C中的字母数字增量 [英] alphanumeric increment in C

查看:124
本文介绍了C中的字母数字增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

从前一个帖子开始......我已经获得了我需要转换为能够在程序中读取的代码。其中一个主要问题是我必须转换的主要字段有6个字符,我需要将其转换为4个字符,显然是唯一的。 6个字符分为2组,每组意味着一些东西,但第一组是最有用的信息。



例如:

102030

10是有用的字段

20只是描述性的

30只是描述性的



一些例子是:

107011

151030

151070

152930

152970

171030

171070

141030

111030

111070

201030

201070



要将上述内容转换为4个字符而不是6个字符,我设计了一个计划保留前2个字符,但增加第2个字符,直到它达到新的数字。

例如:

107011

151030将是151

151070将是152

152930将是153

152970将是154

171030将是171



我遇到的问题是他们中的一些人有超过99件物品。使用上面的例子,假设我有99个15',最后一个将是1599,后一个将是15100 ......这是5个字符。因此,我认为我可以提出一个字母数字选项。例如:15A1,15A2 ...... 15A9,15B1 ...... 15B9,15C1 ...... 15C9等。这意味着我每个号码可以达到26 * 9 = 234项。我的问题是......我不知道该怎么做。我目前的代码段如下:



Hi Everyone,
Following on from a previous thread... I have been given code that I need to convert to be able to read in a program. One of the main problems is that the primary field I have to convert has 6 characters and I need to convert it to 4 characters, obviously unique. The 6 characters are divided into groups of 2, each group means something, however the first group is the one with the most useful informaton.

Example:
102030
10 is the useful field
20 is just descriptive
30 is just descriptive

Some examples are:
107011
151030
151070
152930
152970
171030
171070
141030
111030
111070
201030
201070

To convert the above to 4 characters instead of 6 I have devised a plan to keep the first 2 characters, but increment the 2nd two until it hits a new number.
Eg:
107011
151030 will be 151
151070 will be 152
152930 will be 153
152970 will be 154
171030 will be 171

The problem I have is that some of them have more than 99 items. Using the above examples and assuming I have 99 number 15''s, the last one will be 1599 and the one after will be 15100..which is 5 characters. I therefore thought I could come up with an alphanumeric option. Eg: 15A1, 15A2....15A9, 15B1....15B9, 15C1...15C9, etc. This means I can go up to 26 * 9 = 234 items per number. My problems is...I have no idea how to do it. My current code snippet is as follows:

#include <stdAfx.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#include <conio.h>
int main()
{ 
	//
	char line[200];
	long i, j, k, n10, n20, n30, n40, n50, n60, l10, l20, l30, l40, l50, l60;
	long NodeName;
	double x, y, z;
	//
	n10 = 0; n20 = 0; n30 = 0; n40 = 0; n50 = 0; n60 = 0;
	l10 = 10; l20 = 20; l30 = 30; l40 = 40; l50 = 50; l60 = 60;
	FILE *pFile, *OutPut;    
	pFile = fopen("Input.txt", "r");   
	if( (OutPut = fopen("Output.inp", "w" )) != NULL )
	{
	}
	else
	{printf( "Problem creating Output file\n");}
	//fgets gets a string from a file//one string per line  
	while(fgets(line, sizeof line, pFile) != NULL)    
	{
	
		if(sscanf_s(line, "%d %lf %lf %lf", &NodeName, &x, &y, &z) == 4)    
			
		{    
			//Each case below populates the Nodename field with the first two numbers of the 6 character input nodename and then increments for the following two numbers up until new number is reached.
			j = NodeName / 10000;
			if(j < 20 )
			{
				if( j > l10)
				{
					l10 = j;
					n10 = 0;
				}
				n10 += 1;
				fprintf(OutPut,"%2d", l10);
				fprintf(OutPut,"%2d ", n10);
				goto skp;
			}
			if(j < 30)
			{
				if( j > l20)
				{
					l20 = j;
					n20 = 0;
				}
				n20 += 1;
				fprintf(OutPut,"20");
				fprintf(OutPut,"%2d ", n20);
				goto skp;
			}
			if(j < 40)
			{
				if( j > l30)
				{
					l30 = j;
					n30 = 0;
				}
				n30 += 1;
				fprintf(OutPut,"30");
				fprintf(OutPut,"%2d ", n30);
				goto skp;
			}
			if(j < 50)
			{
				if( j > l40)
				{
					l40 = j;
					n40 = 0;
				}
				n40 += 1;
				fprintf(OutPut,"40");
				fprintf(OutPut,"%2d ", n40);
				goto skp;
			}
			/*			k = (NodeName[i] - 10000 * j) / 10;
			fprintf(OutPut,"%3d ", k);*/
skp:;

推荐答案

是的,请使用字母数字选项 - 即十六进制怎么样!?

使用%x(或%X)与使用%d完全相同(即你可以零填充,等等)输出值 - 你可能想使用%02X

这意味着你可以达到0xFF数字(255)。



问候,

Ian。
Yes, go with "the alpha-numeric option" - i.e. how about hexadecimal!?
Use %x (or %X) in exactly the same way you would use %d (i.e. you can zero pad, etc) for outputting the value - you probably want to use "%02X"
This will mean that you can go up to 0xFF numbers (255).

Regards,
Ian.


我将用伪代码执行此操作...



I will do this in pseudo code...

int[] counters = new int[99];
string line;

while (ReadLine(line))
{
    //You only care about the first two characters, so that's all you look at...
    string num = line[0] + line[1];

    int inum = strtoi(num);

    string outnum = num + itohex(counters[inum]);  //Convert the integer to hex, 2 digits
    counters[inum]++;

    WriteOut(outnum);
}


引用:

这意味着我可以去每个数字最多26 * 9 = 234个项目

This means I can go up to 26 * 9 = 234 items per number



这是错的,只使用(两个)A..Z字母 26 ^ 2 = 26 * 26 = 676 项目。




That''s wrong, using just (two) A..Z letters you have 26^2=26*26=676 items.

引用:

我的问题是......我不知道怎么做。

My problems is...I have no idea how to do it.





最简单的方法是使用一个数组,例如



The simplest way would be using an array, e.g.

char code[26] = {'A', 'B', 'C', 'D', /* ... */, 'Z'}; // there are more clever ways to do this





那么你的 th 代码的两个字母后缀将由代码[n / 26] 和代码[n%26]



then the two letter suffix of yor nth code would be given by code[n/26] and code[n%26].


这篇关于C中的字母数字增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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