You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#并行数组匹配货币代码实现汇率转换问题求助

修正货币汇率匹配与转换的C#代码问题

我从文本文件读取数据创建了4个并行数组,分别存储货币代码、货币描述、汇率及完整数据,需要将交易文件中的货币代码与数组匹配,用对应汇率乘以交易金额完成转换。但当前代码无法正确匹配货币代码,要么按顺序执行计算,要么输出数组长度,请求修正。

输入文件格式

Rates.txt

CANCanadian Dollar   000001.24
ATSAustrian Shilling 000011.61
GBPBritish Pound     000000.73

Trans.txt

CRC000500.00
ILS000250.00
EUR010000.00

原代码核心问题

  1. 错误的索引使用:用全局变量x递增来获取汇率,不管当前匹配的货币代码对应哪个索引,导致顺序取数组元素,完全不匹配实际货币代码。
  2. 错误的输出内容:匹配后输出arrayExchange.Length,这是数组总长度,不是计算后的转换金额。
  3. 未处理无匹配的情况:交易文件中的货币代码(如CRC、ILS、EUR)在汇率文件中不存在时,没有任何提示或处理。

修正后的完整代码

using System;
using System.IO;

namespace CurrencyConversion
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建并行数组存储汇率数据
            int rateCount = 0;
            string[] arrayRate = new string[10];
            string[] arrayCode = new string[10];
            string[] arrayDescription = new string[10];
            decimal[] arrayExchange = new decimal[10];

            string strRInputPath = "Rates.txt";
            string strROutputPath = "Report.txt";
            
            // 读取汇率文件并填充数组
            using (StreamReader sr = new StreamReader(strRInputPath))
            using (StreamWriter sw = new StreamWriter(strROutputPath))
            {
                string strRInputLine;
                while ((strRInputLine = sr.ReadLine()) != null)
                {
                    // 解析汇率行数据
                    string RCurrencyCode = strRInputLine.Substring(0, 3);
                    string CurrencyDescription = strRInputLine.Substring(3, 18).Trim(); // 去除多余空格
                    decimal ExchangeRate = Convert.ToDecimal(strRInputLine.Substring(21, 9));

                    arrayRate[rateCount] = strRInputLine;
                    arrayCode[rateCount] = RCurrencyCode;
                    arrayDescription[rateCount] = CurrencyDescription;
                    arrayExchange[rateCount] = ExchangeRate;
                    rateCount++;
                }

                // 将汇率数据写入报告(可选)
                sw.WriteLine("=== 汇率数据 ===");
                for (int i = 0; i < rateCount; i++)
                {
                    sw.WriteLine($"代码: {arrayCode[i]}, 描述: {arrayDescription[i]}, 汇率: {arrayExchange[i]}");
                }
            }

            // 处理交易文件并完成转换
            string strTInputPath = "Trans.txt";
            string strTOutputPath = "Report1.txt";
            
            using (StreamReader srt = new StreamReader(strTInputPath))
            using (StreamWriter swt = new StreamWriter(strTOutputPath))
            {
                swt.WriteLine("=== 交易转换结果 ===");
                string strTInputLine;
                while ((strTInputLine = srt.ReadLine()) != null)
                {
                    // 解析交易行数据
                    string TCurrencyCode = strTInputLine.Substring(0, 3);
                    decimal Amount = Convert.ToDecimal(strTInputLine.Substring(3, 9));
                    decimal convertedAmount = Amount;
                    bool found = false;

                    // 遍历汇率数组,匹配货币代码
                    for (int i = 0; i < rateCount; i++)
                    {
                        if (TCurrencyCode.Equals(arrayCode[i], StringComparison.OrdinalIgnoreCase))
                        {
                            convertedAmount = Amount * arrayExchange[i];
                            found = true;
                            break; // 找到匹配后立即退出循环
                        }
                    }

                    // 写入转换结果
                    if (found)
                    {
                        swt.WriteLine($"货币代码: {TCurrencyCode}, 原始金额: {Amount}, 转换后金额: {convertedAmount:F2}");
                    }
                    else
                    {
                        swt.WriteLine($"货币代码: {TCurrencyCode}, 原始金额: {Amount}, 提示: 未找到对应汇率");
                    }
                }
            }
        }
    }
}

关键改进说明

  1. 正确的索引匹配:改用for循环遍历数组索引,找到匹配的货币代码后,直接用当前索引i获取对应的汇率,确保代码与汇率一一对应。
  2. 重置匹配状态:为每笔交易单独判断是否找到汇率,避免全局变量导致的索引混乱。
  3. 修正输出逻辑:输出实际的转换金额,并对无匹配汇率的交易给出明确提示。
  4. 优化数据处理:对货币描述去除多余空格,让输出更整洁;用rateCount记录实际读取的汇率条数,避免遍历数组中的空元素。
  5. 大小写不敏感匹配:使用StringComparison.OrdinalIgnoreCase判断代码匹配,提升兼容性(如果文件中有大小写差异时)。

更优方案建议

并行数组管理起来容易出错,推荐使用Dictionary<string, decimal>存储货币代码与汇率的映射关系,代码会更简洁高效:

// 替换并行数组为Dictionary
Dictionary<string, decimal> exchangeRates = new Dictionary<string, decimal>();
// 读取汇率文件时直接添加到字典
exchangeRates[RCurrencyCode] = ExchangeRate;
// 匹配时直接通过键获取汇率
if (exchangeRates.TryGetValue(TCurrencyCode, out decimal rate))
{
    convertedAmount = Amount * rate;
    found = true;
}

内容的提问来源于stack exchange,提问作者HackyShack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 01:50:00