1-我需要将2d数组存储为2个数组-标题和作者。有没有一种方法可以将每个作者的姓名存储为一个字符串一次?
每行代码都是我的,我只是想对其进行改编以使其看起来更简单。整个代码都可以正常工作,我只是在寻求帮助,以将2d数组转换为2个数组而不破坏整个内容。是的,这是大学的作业,我要问如何转换2d数组。我并不是要您以任何方式为我做这份工作,因为如果我愿意,我可以轻松提交并获得80%的赔偿。 –
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UofSarrePublishing
{
public partial class EPOS : Form
{
/*Definition of constants for Distributors,Titles and Authors.*/
const int Authors = 3;
const int Distributors = 3;
const int Titles = 5;
/*Definition of arrays.*/
static string[,] books = new string[Titles, 2]; //Array containing books and their authors.//
static double[] prices = { 6.47, 0.99, 29.14, 10.50, 319.56 }; //Price of each book title.//
static int[] stock = new int[Titles];
static int[,] sales = new int[Titles,Distributors]; //Sales for book title by distributor.//
/*Array for distributors.*/
static int[] basket = new int[5]; //Current basket.//
static string[] distributors = { "University of Sarre Bookshop", "Amaze-oon.co.uk", "Earthmarble Bookshops" };
private int a;
/*Function is prompted at the beggining to fill in books,authors and stock.*/
void insertData()
{
books[0, 0] = "1000 Best Jokes on Milling and Grinding";
books[1, 0] = "10000 Awful Jokes on Milling and Grinding";
books[2, 0] = "Civet Cat Husbandry in the UK";
books[3, 0] = "When Milling goes wrong: stories of death in Milling";
books[4, 0] = "Everything you ever wanted to know about mill construction... and then some: The world's biggest bumper book of mill construction";
books[0, 1] = "S Presso";
books[1, 1] = "S Presso";
books[2, 1] = "S Presso";
books[3, 1] = "Win D Power";
books[4, 1] = "Dr Footing";
stock[0] = 8;
stock[1] = 3238;
stock[2] = 3;
stock[3] = 37;
stock[4] = 2;
/*Writes stock in the textboxes.*/
uiStockCatTextBox.Text = "3";
uiStockWhenTextBox.Text = "37";
uiStockEverythingTextBox.Text = "2";
uiStock10000TextBox.Text = "3238";
uiStock1000TextBox.Text = "8";
}
/*Function to retrieve values from the basket (textboxes).*/
void getBasket()
{
/* Parse string into integer and save into basket array.*/
basket[0] += int.Parse(uiBasket1000TextBox.Text);
basket[1] += int.Parse(uiBasket10000TextBox.Text);
basket[2] += int.Parse(uiBasketCatTextBox.Text);
basket[3] += int.Parse(uiBasketWhenTextBox.Text);
basket[4] += int.Parse(uiBasketEverythingTextBox.Text);
}
/*Function to remove items from basket.*/
void emptyBasket()
{
/*Zero elements of basket array.*/
for (int a = 0; a < basket.Length; a++)
{
basket[a] = 0;
}
}
/*Function to reset basket's values.*/
void resetBasket()
{
uiBasketCatTextBox.Text = "0";
uiBasketEverythingTextBox.Text = "0";
uiBasketWhenTextBox.Text = "0";
uiBasket1000TextBox.Text = "0";
uiBasket10000TextBox.Text = "0";
emptyBasket();
}
/*Function that creates a new sale by checking/getting customer's basket.*/
void createSale(int id)
{
/*Get values of the basket - Defines total price.*/
getBasket();
double totalPrice = 0;
int totalItems = 0;
/*Summary of price/sum of item values in the basket.*/
for (int a = 0; a < Titles; a++)
{
totalPrice += prices[a] * basket[a];
totalItems = totalItems + basket[a];
}
/*Check if stock is available to proceed sale.
In case of no stock an error message is displayed.*/
for (int a = 0; a < stock.Length; a++)
{
/*Check if there is enough stock.*/
if (stock[a]- basket[a] < 0)
{
MessageBox.Show(this, "Out of stock. Order can't be completed!",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
/*Removes items from the basket and cancels sale.*/
emptyBasket();
return;
}
}
/*Proceeds with sale if enough stock.*/
string message = "The total price is £" + totalPrice.ToString("F")+". Proceed?";
string caption = "Sale confirmation";
MessageBoxButtons buttons = MessageBoxButtons.OKCancel;
DialogResult result = MessageBox.Show(this, message, caption, buttons,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
/*If user clicks buttom "OK" sale will be stored in the sale array and stock is updated.*/
if (result == DialogResult.OK)
{
for (int a = 0; a < Titles; a++)
{
sales[a, id] += basket[a];
}
updateStock();
uiStock1000TextBox.Text = stock[0].ToString();
uiStock10000TextBox.Text = stock[1].ToString();
uiStockCatTextBox.Text = stock[2].ToString();
uiStockWhenTextBox.Text = stock[3].ToString();
uiStockEverythingTextBox.Text = stock[4].ToString();
/*Resets basket and starts a new sale.*/
resetBasket();
}
else
{
emptyBasket();
}
}
/*This function updates stock after a sale is succesfully completed.*/
void updateStock()
{
for (int a = 0; a < stock.Length; a++)
{
stock[a] -= basket[a];
}
}
/*Entry point*/
public EPOS()
{
InitializeComponent();
insertData();
}
private void Form1_Load(object sender, EventArgs e)
{
}
/*Function is prompted when "Sale" button is clicked.
It checks for a valid Customer ID - In case of error, a message is shown.*/
private void uiSaleButton_Click(object sender, EventArgs e)
{
string tmp = uiCustomerNumberTextBox.Text;
if ( tmp== "" || int.Parse(tmp)-1>2 || int.Parse(tmp)-1<0)
MessageBox.Show(this, "Please enter a valid Customer ID (enter 1=University of Sarre Bookshop, 2=Amaze-oon.co.uk, 3=Earthmarble Bookshop)",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
else
{
/*A valid Customer ID equals to 1,2 and 3 //
It substracts 1 to get 0,1,2.*/
int id = int.Parse(tmp)-1;
createSale(id);
}
}
/*Function to show a report of total sales per Author.
(Bubble sort is used).*/
private void uiAuthorChartButton_Click(object sender, EventArgs e)
{
/*2D array that stores sales per author.*/
string[,] salesPerAuthor = new string[3,2];
salesPerAuthor[0,0]= "S Presso";
salesPerAuthor[1, 0] = "Win D Power";
salesPerAuthor[2, 0] = "Dr Footing";
/*Resets previous text from the textbox.*/
uiAuthorChartTextBox.Clear();
for (int a = 0; a < Authors; a++)
{
/*Temporary variable that stores total sales.*/
int tmp = 0;
for (int b = 0; b < Titles; b++)
{
/*Find the correct author for specific book.*/
if (salesPerAuthor[a,0] != books[b, 1])
continue;
for (int c = 0; c < Distributors; c++)
{
/*Calculates total sales of distributors.*/
tmp+= sales[b, c];
}
}
/*Store the total sales to array.*/
salesPerAuthor[a, 1] = tmp.ToString();
tmp = 0;
}
/*Sorts sales per author in ascending order.*/
bubbleSort(salesPerAuthor);
/*Print total sales per Author in textbox.*/
for (int a = 0; a < salesPerAuthor.GetLength(0); a++)
{
uiAuthorChartTextBox.AppendText(salesPerAuthor[a, 0] + " ---> Total Sales:" + salesPerAuthor[a, 1] + "\n");
}
uiAuthorChartTextBox.AppendText("\n");
}
/*Bubble sort algorithm that sorts sales per author*/
private void bubbleSort(string[,] salesPerAuthor)
{
bool swap;
/*Iterates the salerperAuthor array and swaps elements.*/
for (int a = 0; a < salesPerAuthor.GetLength(0) - 1; a++)
{
swap = false;
for (int b = salesPerAuthor.GetLength(0) - 2; b >= a; b--)
{
if (int.Parse(salesPerAuthor[b, 1]) > int.Parse(salesPerAuthor[b+1, 1]))
{
/*Swaps both names and number of sales for the specified author.*/
int tempNum = int.Parse(salesPerAuthor[b, 1]);
salesPerAuthor[b, 1] = salesPerAuthor[b + 1, 1];
salesPerAuthor[b + 1, 1] = tempNum.ToString();
swap = true;
string tmpName = salesPerAuthor[b, 0];
salesPerAuthor[b, 0] = salesPerAuthor[b + 1, 0];
salesPerAuthor[b + 1, 0] = tmpName;
}
}
/*If there is no swaps it breaks the loop.*/
if (!swap)
{
break;
}
}
}
/*Function to show a report of total sales per Distributor.*/
private void uiDealerChartButton_Click(object sender, EventArgs e)
{
/*Clears the textbox when button is clicked.*/
uiDealerChartTextBox.Clear();
/*Iterates distributors to print total sales for each distributor.*/
for (int a = 0; a < Distributors; a++)
{
uiDealerChartTextBox.AppendText(distributors[a] + "\n");
for (int b = 0; b < Titles; b++) {
uiDealerChartTextBox.AppendText(books[b,0]+" ---> Sales:");
uiDealerChartTextBox.AppendText(sales[b,a].ToString() + "\n");
}
uiDealerChartTextBox.AppendText("\n");
}
}
}
}
答案 0 :(得分:1)
使用类对实体建模并对属性进行分组。示例:
public class Book
{
public string Title { get; set; }
public List<string> Authors { get; } = new List<string>();
public decimal Price { get; set; }
}
类似这样的事情(我不确定您的模型如何)。然后,您可以为书籍创建List<Book>
,而不必并行放置多个数组。另外,List<T>
可以根据需要增长,其中数组的长度是固定的。
您还可以将模型扩展为具有作者类。然后,您可以在几本书中引用同一作者对象
public class Author
{
public string Name { get; set; }
public DateTime Born { get; set; }
public DateTime? Died { get; set; }
}
public class Book
{
public string Title { get; set; }
public List<Author> Authors { get; } = new List<Author>();
public decimal Price { get; set; }
}
示例
var author = new Author {
Name = "Arthur Conan Doyle",
Born = new DateTime(1859, 5, 22),
Died = new DateTime(1930, 7, 7)
};
var books = new List<Book>();
var book = new Book {
Title = "The Final Problem",
Price = 10.50m
};
book.Authors.Add(author);
books.Add(book);
var book = new Book {
Title = "The Adventure of the Empty House",
Price = 11.20m
};
book.Authors.Add(author); // Adds the same author as in the previous book. Note that a class is
// a reference type. I.e., this is not a copy of the author. It is
// a reference to the same and unique A. C. Doyle `Author` object.
books.Add(book);
var book = new Book {
Title = "Hilda Wade",
Price = 8.75m
};
book.Authors.Add(author);
book.Authors.Add(new Author {
Name = "Grant Allen",
Born = new DateTime(1848, 2, 24),
Died = new DateTime(1899, 10, 25)
}); // A co-author.
books.Add(book);
现在您可以访问类似的信息
foreach (Book b in books) {
Console.WriteLine($"Book \"{b.Title}\" (£ {b.Price:n2})");
foreach (Author a in b.Authors) {
Console.WriteLine($" {a.Name} *{a.Born}");
}
}