C#入门经典 第26章 源码 1.测试

项目名称:调查问卷web站点。

开发工具及运行平台:vs2005+.net 2.0+XML。

一。创建polls.xml放在E:/下。

1。内容如下:

 

< Pool  Question ="What development language do you primarily use?" >
  
< Answer  Text ="C#"  Votes ="37"   />
  
< Answer  Text ="C++"  Votes ="41"   />
  
< Answer  Text ="Java"  Votes ="28"   />
  
< Answer  Text ="JavaScript"  Votes ="12"   />
  
< Answer  Text ="Visual Basic"  Votes ="13"   />
</ Pool >

 

 二。创建类库。

1。创建Polling的类库工程。

2。把class1.cs改名为Poll.cs

3。引用代码库 System.Web.

4。在Poll.cs中添加如下代码:

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Data;
using  System.IO;
using  System.Xml;
using  System.Web;

namespace  Polling
{
    
public class Poll
    
{
        
public Poll(string fileName)
        
{

            myFilename 
= fileName;
            DataSet myDataSet 
= new DataSet();
            FileStream fsReadXml 
= new FileStream(fileName, FileMode.Open);
            XmlTextReader myXmlReader 
= new System.Xml.XmlTextReader(fsReadXml);
            myDataSet.ReadXml(myXmlReader);
            myXmlReader.Close();

            myAnswersCount 
= myDataSet.Tables[1].Rows.Count;
            myQuestion 
= myDataSet.Tables[0].Rows[0].ItemArray[1].ToString();
            myAnswers 
= new string[myAnswersCount + 1];
            
string answer;
            
for (int i = 0; i < myAnswersCount; i++)
            
{
                answer 
= myDataSet.Tables[1].Rows[i].ItemArray[0].ToString();
                myAnswers[i 
+ 1= answer;
            }


            myVotes 
= new int[myAnswersCount + 1];
            
string votes;
            
for (int i = 0; i < myAnswersCount; i++)
            
{
                votes 
= myDataSet.Tables[1].Rows[i].ItemArray[1].ToString();
                myVotes[i 
+ 1= int.Parse(votes);
            }

        }

        
public void Vote(int answer)
        
{
            
//被隐去的部分后面要用到。
            /*
            if (HttpContext.Current.Request.Cookies["myPoll"] != null)
                if (HttpContext.Current.Request.Cookies["myPoll"].Value == myQuestion)
                {
                    throw (new Exception("You have already voted this poll"));
                }
            
            
*/

            
if (answer == 0 || answer > myAnswersCount)
            
{
                
throw (new Exception("Invalid choice of answer."));
            }

            
            myVotes[answer]
++;

            DataSet myDataSet 
= new DataSet();
            FileStream fsReadXml 
= new FileStream(myFilename, System.IO.FileMode.Open);
            XmlTextReader myXmlReader 
= new System.Xml.XmlTextReader(fsReadXml);
            myDataSet.ReadXml(myXmlReader);
            myXmlReader.Close();
            
string votes = myDataSet.Tables[1].Rows[answer - 1].ItemArray[1].ToString();
            
int voteInt = int.Parse(votes);
            DataRow myVoteRow 
= myDataSet.Tables[1].Rows[answer - 1];
            myVoteRow[
"Votes"= (voteInt + 1).ToString();

            StreamWriter myStream 
= new StreamWriter(myFilename);
            myDataSet.WriteXml(myStream, XmlWriteMode.IgnoreSchema);
            myStream.Close();
            
/*
            if (HttpContext.Current != null)
            {
                HttpCookie myCookie = new HttpCookie("myPoll");
                myCookie.Value = myQuestion;
                HttpContext.Current.Response.Cookies.Add(myCookie);
            }
            
*/

        }


        
public static Poll Current()
        
{
            Poll myPoll 
= new Poll(@"E:/polls.xml");
            
return myPoll;

        }


        
public int AnswerCount
        
{
            
get
            
{
                
return myAnswersCount;
            }

        }


        
public string Question
        
{
            
get
            
{
                
return myQuestion;
            }

        }


        
public string[] Answers
        
{
            
get
            
{
                
return myAnswers;
            }

        }


        
public int[] Votes
        
{
            
get
            
{
                
return myVotes;
            }

        }



        
private string myQuestion;
        
private string[] myAnswers;
        
private int[] myVotes;
        
private int myAnswersCount;
        
private string myFilename;
    }

}

5。构建代码库。

6。构建成功。

三。测试类库。

1。新创建控制台工程。

2。引用上面创建的代码库Polling。

3。在program.cs添加如下代码:

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  Polling;

namespace  PollC
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            Poll myPoll 
= Poll.Current();
            DisplayPoll(myPoll);
            myPoll.Vote(
1);
            Console.WriteLine(
"Votes registered against" + myPoll.Answers[2]);
            DisplayPoll(myPoll);
        }


        
private static void DisplayPoll(Poll aPoll)
        
{
            Console.WriteLine(
"===Poll===");
            Console.WriteLine(aPoll.Question);
            
for (int i = 1; i <= (aPoll.AnswerCount); i++)
            
{
                Console.Write(aPoll.Answers[i] 
+ ":");
                Console.WriteLine(aPoll.Votes[i] 
+ "votes");
            }


            Console.WriteLine(
"==========");
        }

    }

}

 

 

 4。运行成功。

四。附图:

五。C#入门经典第26章源码1.测试,完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值