从键盘上输入三个double类型的数,定义函数从小到大的顺序,排序以引用型参数调用函数,然后返回主函数,输出结果(利用wpf完成)
求支支招!!
基于Monster 组和GPT的调写:
MainWindow.xaml:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Sorting Numbers" Height="150" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Enter three numbers:" Grid.Row="0" Grid.Column="0"/>
<TextBox x:Name="tb1" Grid.Row="0" Grid.Column="1"/>
<TextBox x:Name="tb2" Grid.Row="1" Grid.Column="1"/>
<TextBox x:Name="tb3" Grid.Row="2" Grid.Column="1"/>
<Button x:Name="btnSort" Content="Sort" Grid.Row="3" Grid.ColumnSpan="2" Margin="10" Click="btnSort_Click"/>
<TextBlock x:Name="tbResult" Grid.Row="4" Grid.ColumnSpan="2" Margin="10" TextWrapping="Wrap"/>
</Grid>
</Window>
MainWindow.xaml.cs:
using System;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnSort_Click(object sender, RoutedEventArgs e)
{
double num1, num2, num3;
// Parse user input as doubles
if (!double.TryParse(tb1.Text, out num1) ||
!double.TryParse(tb2.Text, out num2) ||
!double.TryParse(tb3.Text, out num3))
{
MessageBox.Show("Please enter three valid numbers.");
return;
}
// Sort the numbers in ascending order
SortNumbers(ref num1, ref num2, ref num3);
// Display the sorted numbers
tbResult.Text = $"Sorted numbers: {num1}, {num2}, {num3}";
}
private static void SortNumbers(ref double num1, ref double num2, ref double num3)
{
if (num1 > num2)
{
Swap(ref num1, ref num2);
}
if (num2 > num3)
{
Swap(ref num2, ref num3);
}
if (num1 > num2)
{
Swap(ref num1, ref num2);
}
}
private static void Swap(ref double a, ref double b)
{
double temp = a;
a = b;
b = temp;
}
}
}
private int GetAttributeValue(TopicHotTenModel topicHotTenModel, string propertyName)
{
int propertyValue = 0;
Type t = typeof(TopicHotTenModel);
// 全反射方式
var type = typeof(TopicHotTenModel);
// 属性赋值
PropertyInfo pInfo = type.GetProperty(propertyName);
if (pInfo != null)
{
var pValue = pInfo.GetValue(topicHotTenModel);
if (pValue != null)
{
int.TryParse(pValue.ToString(), out propertyValue);
}
}
return propertyValue;
}