一個結構體可以作為另一個結構體的成員類型:
(1)
struct phoneBook
{
public string name;
public uint age;
public string phone;
public struct address
{
public string city;
public string street;
public uint no;
}
}
但是由一個問題:
如何對結構的結構體的成員賦值?
有如下例子:
phoneBook M_1;
M_1.name=Console.ReadLine();
……
M_1.address.city=Console.ReadLine();
……
這樣訪問的話,編譯報錯:
error CS0572:“address”:無法通過運算式參考型別,請嘗試“phoneBook.address”。
在這裡M_1.address這種引用方式應該是錯誤的,因為address僅僅是一個類型名,而不是phoneBook的成員名,所以說無法引用。
(2)
還嘗試另外一種方式給他賦值:
phoneBook M_1;
M_1.name=Console.ReadLine();
……
phoneBook.address ad;
ad.city=Console.ReadLine();
……
這樣賦值倒是沒問題,但ad和M_1有什麼關係?基本是兩個不想乾的對象。
這樣看來,目前還無法解決,只能在結構體本身想辦法了。
(3)
那麼,結構體這樣定義如何?
struct phoneBook
{
public string name;
public uint age;
public string phone;
public struct address
{
public string city;
public string street;
public uint no;
} ad;
}
注意:在聲明address類型的時候同時聲明他的對象ad作為phoneBook的成員,這在C++裡應該是沒問題的。
但還是無法通過編譯:類、結構或介面成員聲明中的標記“;”無效
不知道為什麼,可能c#不支援
(4)
最後一個辦法:把結構體裡的結構體拿出來,其對象作為另一個結構體的成員:
struct phoneBook
{
public string name;
public uint age;
public string phone;
public address ad;
}
struct address
{
public string city;
public string street;
public uint no;
}
這樣肯定就沒問題了,但是已經跟原來問題不大相關了。
(5)
經請教,得知可以這樣寫:
struct phoneBook
{
public string name;
public uint age;
public string phone;
public address ad;
public struct address
{
public string city;
public string street;
public uint no;
}
}
其實,這就是方式(3)所要做的,只是方式(3)是c++的方法在C#裡不起作用而已,其實質和方式(4)卻相同。
下面是所有代碼,作為參考:
版本2代碼
using System;
struct phoneBook
{
public string name;
public uint age;
public string phone;
public address ad;
}
struct address
{
public string city;
public string street;
public uint no;
}
class Welcome
{
static void Main()
{
try
{
phoneBook M_1;
while (true)
{
Console.WriteLine("請輸入你的姓名:");
M_1.name = Console.ReadLine();
if (M_1.name != "")
break;
else
{
Console.WriteLine("請輸入!");
}
}
while (true)
{
try
{
Console.WriteLine("請輸入你的年齡:");
M_1.age = Convert.ToUInt32(Console.ReadLine());
break;
}
catch (FormatException e)
{
Console.WriteLine("請輸入-9的數字!");
}
}
while (true)
{
Console.WriteLine("請輸入你的電話:");
M_1.phone = Console.ReadLine();
if (M_1.phone != "")
break;
else
{
Console.WriteLine("請輸入!");
}
}
Console.WriteLine("請輸入你的住址:");
while (true)
{
Console.WriteLine("請輸入你的城市:");
M_1.ad.city = Console.ReadLine();
//M_1.address.city=Console.ReadLine();//通過此種方式無法直接存取結構體address的成員
if (M_1.ad.city != "")
break;
else
{
Console.WriteLine("請輸入!");
}
}
while (true)
{
Console.WriteLine("請輸入你的街道:");
M_1.ad.street = Console.ReadLine();
if (M_1.ad.street != "")
break;
else
{
Console.WriteLine("請輸入!");
}
}
while (true)
{
try
{
Console.WriteLine("請輸入你的門牌號:");
M_1.ad.no = Convert.ToUInt32(Console.ReadLine());
break;
}
catch (FormatException e)
{
Console.WriteLine("請輸入-9的數字!");
}
}
Console.WriteLine("OK now……"); Console.WriteLine("---------------------------------------------------------------------------");
Console.WriteLine("你的資訊如下:\n");
Console.WriteLine(" 姓名: {0}",M_1.name);
Console.WriteLine(" 年齡: {0}",M_1.age);
Console.WriteLine(" 電話: {0}",M_1.phone);
Console.WriteLine(" 住址: {0}市{1}街{2}號",M_1.ad.city,M_1.ad.street,M_1.ad.no);
}
catch(FormatException e/*,IOException i*/)
{
string msg = "\n-------------------------------------------------------------------------\n錯誤:\n 源 :"+e.Source +"\n 訊息:"+ e.Message+"\n 追蹤:\n"+e.StackTrace;
Console.WriteLine(msg);
}
}
}
版本3代碼:
using System;
using System.IO;
struct phoneBook
{
public string name;
public uint age;
public string phone;
public address ad;
public struct address
{
public string city;
public string street;
public uint no;
}
}
class Welcome
{
static phoneBook PB;
static string info="見到這條訊息說明你是笨蛋";
static string write_info = "CUG來客";
static string fileName = "資訊記錄.txt";
static void outMsg(int flag)//輸出資訊到控制台
{
Console.ForegroundColor = ConsoleColor.Green;
switch (flag)
{
case 0:
{
Console.ForegroundColor = ConsoleColor.Red;
//Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine("\n\n 2008世紀通訊錄 ");
Console.WriteLine("______________________________________________________________________________\n");
Console.WriteLine("錄入資訊(I):");
Console.WriteLine("查看記錄(V):");
Console.WriteLine("退出系統(E):");
break;
}
case 1:
{
Console.WriteLine("請輸入!");
break;
}
case 2:
{
Console.WriteLine("請輸入-9的數字!");
break;
}
case 3:
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("---------------------------------------------------");
Console.WriteLine(info);
break;
}
case 4:
{
Console.WriteLine("讀取檔案錯誤!");
break;
}
case 5:
{
Console.WriteLine("寫檔案錯誤!");
break;
}
case 6:
{
Console.WriteLine("非法字元!");
break;
}
case 7:
{
Console.WriteLine("操作成功!");
break;
}
default:
break;
}
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
}
static void writeFile(phoneBook pb) //將資訊輸出到檔案儲存
{
try
{
StreamWriter w = File.AppendText(fileName);
/*FileStream f_stream=new FileStream(filename,FileMode.OpenOrCreate);
StreamWriter swd = new StreamWriter(f_stream);
swd.WriteLine(info);*/
/*BinaryWriter bwd = new BinaryWriter(f_stream);
bwd.WriteString(info);*/
w.WriteLine(Environment.NewLine); w.WriteLine("///////////////////////////////////////////////////////////////////");
w.Write(write_info);
w.WriteLine(Environment.NewLine);
w.Close();
outMsg(7);
}
catch (IOException e)
{
outMsg(4);
string msg = "\n-------------------------------------------------------------------------
\n錯誤:\n 源 :" + e.Source + "\n 訊息:" + e.Message + "\n 追蹤:\n" + e.StackTrace;
Console.WriteLine(msg);
}
}
static void inPut(ref phoneBook pb)//從控制台輸入資訊
{
try
{
while (true)
{
Console.WriteLine("請輸入你的姓名:");
pb.name = Console.ReadLine();
if (pb.name != "")
break;
else
{
outMsg(1);
}
}
while (true)
{
try
{
Console.WriteLine("請輸入你的年齡:");
pb.age = Convert.ToUInt32(Console.ReadLine());
break;
}
catch
{
outMsg(2);
}
}
while (true)
{
Console.WriteLine("請輸入你的電話:");
pb.phone = Console.ReadLine();
if (pb.phone != "")
break;
else
{
outMsg(1);
}
}
Console.WriteLine("請輸入你的住址:");
while (true)
{
Console.WriteLine("請輸入你的城市:");
pb.ad.city = Console.ReadLine();
if (pb.ad.city != "")
break;
else
{
outMsg(1);
}
}
while (true)
{
Console.WriteLine("請輸入你的街道:");
pb.ad.street = Console.ReadLine();
if (pb.ad.street != "")
break;
else
{
outMsg(1);
}
}
while (true)
{
try
{
Console.WriteLine("請輸入你的門牌號:");
pb.ad.no = Convert.ToUInt32(Console.ReadLine());
break;
}
catch
{
outMsg(2);
}
}
info = "你的資訊如下:\r\n" + " 姓名: " + pb.name + "\r\n 年齡: " + pb.age + "\r\n
電話: " + pb.phone + "\r\n 住址: " + pb.ad.city + "市" + pb.ad.street +
"街" + pb.ad.no + "號";
write_info = "姓名: " + pb.name + "\r\n年齡: " + pb.age + "\r\n電話: " + pb.phone +
"\r\n住址: " + pb.ad.city + "市" + pb.ad.street + "街" + pb.ad.no + "號";
}
catch (FormatException e/*,IOException i*/)
{
string msg = "\n-------------------------------------------------------------------------\n
錯誤:\n 源 :" + e.Source + "\n 訊息:" + e.Message + "\n 追蹤:\n" + e.StackTrace;
Console.WriteLine(msg);
}
}
static void readFile(phoneBook pb)//從檔案讀取資訊
{
try
{
StreamReader srd = File.OpenText(fileName);
string str;
int count=0;//記錄資訊的條數
while(srd.Peek()>=0)
{
str = srd.ReadLine();
if (str.Contains("姓名"))//以出現姓名為標誌,處理條數記錄
{
count++;
}
Console.WriteLine(str);
//在此處僅僅是將讀取的字串輸出,其實還可以做進一步的處理,得到一個phoneBook
//結構做其他用處,這也是最初的目的,但由於時間原因,也就罷了。
}
Console.WriteLine(Environment.NewLine);
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("目前共有{0}條記錄", count);
srd.Close();
}
catch
{
outMsg(6);
}
}
static void Main()
{
do
{
outMsg(0);
char choice = Console.ReadKey().KeyChar;
Console.WriteLine("\r\n");
switch (choice)
{
case 'i':
case 'I': //輸入資訊
{
inPut(ref PB);
outMsg(3);
writeFile(PB);
break;
}
case 'v':
case 'V': //查看記錄
{
//Console.WriteLine("查看記錄");
readFile(PB);
break;
}
case 'e':
case 'E': //退出系統
{
Console.WriteLine("機器名 :{0} \r\nOS版本 :{1} \r\n已耗用時間:{2}毫秒",
Environment.MachineName, Environment.OSVersion, Environment.TickCount);
Console.WriteLine("退出系統");
Environment.Exit(0);
//Application.Exit();
break;
}
default:
{
outMsg(6);
break;
}
}
} while (true);
}
}