首页 新闻 搜索 专区 学院

如何有效的缩短此语句

1
悬赏园豆:5 [已解决问题] 解决于 2012-06-05 14:15

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
private static string code1 = "";
private static string code2 = "";
private static string code3 = "";
private static string code4 = "";
private static int ge2 = 65;
private static int ge3 = 65;
private static int ge4 = 65;
private static int ge5 = 65;
private static int shi2 = 65;
private static int shi3 = 65;
private static int shi4 = 65;
private static int shi5 = 65;
private static int bai2 = 65;
private static int bai3 = 65;
private static int bai4 = 65;
private static int bai5 = 65;
static void Main(string[] args)
{

const string aname = "A";
const string bname = "B";
const string cname = "C";
const string dname = "D";
exitPoint:
Console.WriteLine("\n请输入您需要的code的选项!\nA:医院code,B:药店code,C:经销商code,D:其它code");
string name;
name = Console.ReadLine();
switch (name.ToUpper())
{
case aname:
{
qk1();
goto exitPoint;
}
case bname:
{
qk2();
goto exitPoint;
}
case cname:
{
qk3();
goto exitPoint;
}
case dname:
{
qk4();
goto exitPoint;
}
default:
Console.WriteLine("输入错误,请重新输入!");
goto exitPoint;
}
}

private static void qk4()
{
TXTcls txt = new TXTcls();
StringBuilder temp = new StringBuilder();
txt.ReadConfig("D", "D", "", temp, 255, ".\\TestFile.ini");

if (temp.Length != 0)
{
ge5 = Convert.ToInt32(temp[3]);
shi5 = Convert.ToInt32(temp[2]);
bai5 = Convert.ToInt32(temp[1]);
ge5++;
}
if (ge5 == 91)
{
shi5++;
ge5 = 65;
}
if (shi5 == 91)
{
bai5++;
shi5 = 65;
}
char ge1 = Convert.ToChar(ge5);
char shi1 = Convert.ToChar(shi5);
char bai1 = Convert.ToChar(bai5);
string code = "Z" + bai1 + shi1 + ge1;
txt.Writeconfig("D", "D", code, ".\\TestFile.ini");
code4 = code;
Console.WriteLine("{0}", code4);
}

private static void qk3()
{
TXTcls txt = new TXTcls();
StringBuilder temp = new StringBuilder();
txt.ReadConfig("C", "C", "", temp, 255, ".\\TestFile.ini");

if (temp.Length != 0)
{
ge4 = Convert.ToInt32(temp[3]);
shi4 = Convert.ToInt32(temp[2]);
bai4 = Convert.ToInt32(temp[1]);
ge4++;
}
if (ge4 == 91)
{
shi4++;
ge4 = 65;
}
if (shi4 == 91)
{
bai4++;
shi4 = 65;
}
char ge1 = Convert.ToChar(ge4);
char shi1 = Convert.ToChar(shi4);
char bai1 = Convert.ToChar(bai4);
string code = "V" + bai1 + shi1 + ge1;
txt.Writeconfig("C", "C", code, ".\\TestFile.ini");
code3 = code;
Console.WriteLine("{0}", code3);
}

private static void qk2()
{
TXTcls txt = new TXTcls();
StringBuilder temp = new StringBuilder();
txt.ReadConfig("B", "B", "", temp, 255, ".\\TestFile.ini");

if (temp.Length != 0)
{
ge3 = Convert.ToInt32(temp[3]);
shi3 = Convert.ToInt32(temp[2]);
bai3 = Convert.ToInt32(temp[1]);
ge3++;
}
if (ge3 == 91)
{
shi3++;
ge3 = 65;
}
if (shi3 == 91)
{
bai3++;
shi3 = 65;
}
char ge1 = Convert.ToChar(ge3);
char shi1 = Convert.ToChar(shi3);
char bai1 = Convert.ToChar(bai3);
string code = "P" + bai1 + shi1 + ge1;
txt.Writeconfig("B", "B", code, ".\\TestFile.ini");
code2 = code;
Console.WriteLine("{0}", code2);
}

private static void qk1()
{
TXTcls txt = new TXTcls();
StringBuilder temp = new StringBuilder();
txt.ReadConfig("A", "A", "", temp, 255, ".\\TestFile.ini");

if (temp.Length != 0)
{
ge2 = Convert.ToInt32(temp[3]);
shi2 = Convert.ToInt32(temp[2]);
bai2 = Convert.ToInt32(temp[1]);
ge2++;
}

if (ge2 == 91)
{
shi2++;
ge2 = 65;
}
if (shi2 == 91)
{
bai2++;
shi2 = 65;
}
char ge1 = Convert.ToChar(ge2);
char shi1 = Convert.ToChar(shi2);
char bai1 = Convert.ToChar(bai2);
string code = "A" + bai1 + shi1 + ge1;
txt.Writeconfig("A", "A", code, ".\\TestFile.ini");
code1 = code;
Console.WriteLine("{0}", code1);
}
}
}

问题补充:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
class TXTcls
{
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public TXTcls()
{

}
public bool Writeconfig(string senction, string key, string val, string filepath)
{
if (WritePrivateProfileString(senction, key, val, filepath))
{
return true;
}
else
{
return false;
}

}
public int ReadConfig(string section, string key, string def, StringBuilder retval, int size, string filepath)
{
return GetPrivateProfileString(section, key, def, retval, size, filepath);

}
}
}

夏天online的主页 夏天online | 初学一级 | 园豆:193
提问于:2012-06-04 12:09
< >
分享
最佳答案
0
using System;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("\n请输入您需要的code的选项!\nA:医院code,B:药店code,C:经销商code,D:其它code");
                string name = Console.ReadLine().ToUpper();
                switch (name)
                {
                    case "A":
                    case "B":
                    case "C":
                    case "D":
                        qk(name);
                        break;
                    default:
                        Console.WriteLine("输入错误,请重新输入!");
                        break;
                }
            }
        }

        private static string IncString(string code)
        {
            StringBuilder builder = new StringBuilder(code);
            IncString(builder);
            return builder.ToString();
        }

        private static void IncString(StringBuilder code)
        {
            if (code == null)
            {
                return;
            }
            if (code.Length == 0)
            {
                code.Append("AAA");//完成初始化
            }
            bool carry;
            for (int i = code.Length - 1; i >= 0; i--)
            {
                code[i] = IncChar(code[i], out carry);
                if (!carry)
                {
                    break;
                }
            }
        }

        private static char IncChar(char code, out bool carry)
        {
            code++;
            if (carry = code > 'Z')
            {
                code = 'A';
            }
            return code;
        }

        private static void qk(string name)
        {
            TXTcls txt = new TXTcls();
            StringBuilder temp = new StringBuilder();
            txt.ReadConfig(name, name, "", temp, 255, ".\\TestFile.ini");
            IncString(temp);
            Console.WriteLine("{0}", temp.ToString());
        }
    }
}
收获园豆:5
无之无 | 大侠五级 |园豆:5085 | 2012-06-04 14:59

感谢您的帮助,但是输出的结果怎么不增加呀一直是AAB?

夏天online | 园豆:193 (初学一级) | 2012-06-04 15:06

@、習慣就好″: 

不好意思。少写了一个write方法:

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("\n请输入您需要的code的选项!\nA:医院code,B:药店code,C:经销商code,D:其它code");
                string name = Console.ReadLine().ToUpper();
                switch (name)
                {
                    case "A":
                    case "B":
                    case "C":
                    case "D":
                        qk(name);
                        break;
                    default:
                        Console.WriteLine("输入错误,请重新输入!");
                        break;
                }
            }
        }

        private static string IncString(string code)
        {
            StringBuilder builder = new StringBuilder(code);
            IncString(builder);
            return builder.ToString();
        }

        private static void IncString(StringBuilder code)
        {
            if (code == null)
            {
                return;
            }
            if (code.Length == 0)
            {
                code.Append("AAA");//完成初始化
            }
            bool carry;
            for (int i = code.Length - 1; i >= 0; i--)
            {
                code[i] = IncChar(code[i], out carry);
                if (!carry)
                {
                    break;
                }
            }
        }

        private static char IncChar(char code, out bool carry)
        {
            code++;
            if (carry = code > 'Z')
            {
                code = 'A';
            }
            return code;
        }

        private static void qk(string name)
        {
            TXTcls txt = new TXTcls();
            StringBuilder temp = new StringBuilder();
            txt.ReadConfig(name, name, "", temp, 255, ".\\TestFile.ini");
            IncString(temp);
            string code = temp.ToString();
            txt.Writeconfig(name, name, code, ".\\TestFile.ini");
            Console.WriteLine("{0}",  code));
        }
    }
}
无之无 | 园豆:5085 (大侠五级) | 2012-06-04 15:34

@、習慣就好″: 

另外,你这个写法,会丢失“AAA”这个编码的,把IncString方法修改为以下内容则不会出现:

        private static void IncString(StringBuilder code)
        {
            if (code == null)
            {
                return;
            }
            if (code.Length == 0)
            {
                code.Append("AAA");//完成初始化
            }
            else
            {
                bool carry;
                for (int i = code.Length - 1; i >= 0; i--)
                {
                    code[i] = IncChar(code[i], out carry);
                    if (!carry)
                    {
                        break;
                    }
                }
            }
        }
无之无 | 园豆:5085 (大侠五级) | 2012-06-04 15:47

@笨笨蜗牛:    非常感谢你  但是有个问题 :每个选项个开头是固定的  A-A  B-P  C-V  D-Z  后面加3个是增加的

夏天online | 园豆:193 (初学一级) | 2012-06-04 16:43

@笨笨蜗牛: 

做个映射就解决了。如果不会改变,也可以按照以下方式写,这样性能会更好点:

        private static void qk(string name, string pre)
        {
            TXTcls txt = new TXTcls();
            StringBuilder temp = new StringBuilder();
            txt.ReadConfig(name, name, "", temp, 255, ".\\TestFile.ini");
            IncString(temp);
            string code =temp.ToString();
            txt.Writeconfig(name, name, code, ".\\TestFile.ini");
            Console.WriteLine("{0}", pre +code));
        }

 

                switch (name)
                {
                    case "A":
                        qk(name, "A");
                        break;
                    case "B":
                        qk(name, "P");
                        break;
                    case "C":
                        qk(name, "V");
                        break;
                    case "D":
                        qk(name, "Z");
                        break;
                    default:
                        Console.WriteLine("输入错误,请重新输入!");
                        break;
                }
无之无 | 园豆:5085 (大侠五级) | 2012-06-04 16:59

@笨笨蜗牛: 非常感谢您的细心指导   学生愚钝  没弄出来--!

夏天online | 园豆:193 (初学一级) | 2012-06-04 17:05

@、習慣就好″: 晕倒,把我后来贴的两段代码分别替换相应部分就好。

无之无 | 园豆:5085 (大侠五级) | 2012-06-04 17:29

@、習慣就好″: 

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("\n请输入您需要的code的选项!\nA:医院code,B:药店code,C:经销商code,D:其它code");
                string name = Console.ReadLine().ToUpper();
                switch (name)
                {
                    case "A":
                        qk(name, "A");
                        break;
                    case "B":
                        qk(name, "P");
                        break;
                    case "C":
                        qk(name, "V");
                        break;
                    case "D":
                        qk(name, "Z");
                        break;
                    default:
                        Console.WriteLine("输入错误,请重新输入!");
                        break;
                }
            }
        }

        private static string IncString(string code)
        {
            StringBuilder builder = new StringBuilder(code);
            IncString(builder);
            return builder.ToString();
        }

        private static void IncString(StringBuilder code)
        {
            if (code == null)
            {
                return;
            }
            if (code.Length == 0)
            {
                code.Append("AAA");//完成初始化
        }
            else
            {
                bool carry;
                for (int i = code.Length - 1; i >= 0; i--)
                {
                    code[i] = IncChar(code[i], out carry);
                    if (!carry)
                    {
                        break;
                    }
                }
            }
        }

        private static char IncChar(char code, out bool carry)
        {
            code++;
            if (carry = code > 'Z')
            {
                code = 'A';
            }
            return code;
        }

        private static void qk(string name, string pre)
        {
            TXTcls txt = new TXTcls();
            StringBuilder temp = new StringBuilder();
            txt.ReadConfig(name, name, "", temp, 255, ".\\TestFile.ini");
            IncString(temp);
            string code = temp.ToString();
            txt.Writeconfig(name, name, code, ".\\TestFile.ini");
            Console.WriteLine("{0}",  pre + code));
        }
    }
}
无之无 | 园豆:5085 (大侠五级) | 2012-06-04 17:31

@笨笨蜗牛: 非常谢谢

夏天online | 园豆:193 (初学一级) | 2012-06-04 17:38

@笨笨蜗牛: 您好,如果第一个code不固定了    确定它的范围   该怎么写   帮帮忙  非常感谢

A  (AAAA-OZZZ)

B   (PAAA-UZZZ)

C   (VAAA-YZZZ)

D   (ZAAA-ZZZZ)

夏天online | 园豆:193 (初学一级) | 2012-06-13 11:21

@、習慣就好″: 你应该能举一反三才可以。我把那些弄好了,你现在再补充这个功能也是很简单的。

无之无 | 园豆:5085 (大侠五级) | 2012-06-13 11:34

@笨笨蜗牛: 是的   我看了的   不确定在那块改     在switch那改是不是就可以了  初始值AAAA   方法去掉pre   

夏天online | 园豆:193 (初学一级) | 2012-06-13 12:05

@、習慣就好″: 

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("\n请输入您需要的code的选项!\nA:医院code,B:药店code,C:经销商code,D:其它code");
                string name = Console.ReadLine().ToUpper();
                switch (name)
                {
                    case "A":
                        qk(name, "A");
                        break;
                    case "B":
                        qk(name, "P");
                        break;
                    case "C":
                        qk(name, "V");
                        break;
                    case "D":
                        qk(name, "Z");
                        break;
                    default:
                        Console.WriteLine("输入错误,请重新输入!");
                        break;
                }
            }
        }

        private static string IncString(string code)
        {
            StringBuilder builder = new StringBuilder(code);
            IncString(builder);
            return builder.ToString();
        }

        private static void IncString(StringBuilder code, string pre)
        {
            if (code == null)
            {
                return;
            }
            if (code.Length == 0)
            {
                code.AppendFormat("{0}AAA", pre);//完成初始化
        }
            else
            {
                bool carry;
                for (int i = code.Length - 1; i >= 0; i--)
                {
                    code[i] = IncChar(code[i], out carry);
                    if (!carry)
                    {
                        break;
                    }
                }
            }
        }

        private static char IncChar(char code, out bool carry)
        {
            code++;
            if (carry = code > 'Z')
            {
                code = 'A';
            }
            return code;
        }

        private static void qk(string name, string pre)
        {
            TXTcls txt = new TXTcls();
            StringBuilder temp = new StringBuilder();
            txt.ReadConfig(name, name, "", temp, 255, ".\\TestFile.ini");
            IncString(temp, pre);
            string code = temp.ToString();
            txt.Writeconfig(name, name, code, ".\\TestFile.ini");
            Console.WriteLine(code));
        }
    }
}

我这个代码还有个小BUG,你可以自己排查看。

无之无 | 园豆:5085 (大侠五级) | 2012-06-13 12:36

@笨笨蜗牛: 谢谢

夏天online | 园豆:193 (初学一级) | 2012-06-13 12:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册