Practise C#:

来源:互联网 发布:淘宝上的减肥药可信吗 编辑:程序博客网 时间:2024/05/21 12:45


----------------------------------------

1.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

class Program

    {

               static void Main(string[] args)

        {

            int i, j, sum = 0;

            bool prim;        // To log a number whether a prime;

            prim = true;

 

            for (i = 2; i <= 100; i++)

            {

 

                for (j = 2; j < (int) Math.Sqrt(i)+1; j++)

                {

                    if ((i % j) == 0) // This number is not a prime, jump out and find the next;

                    {

                        prim = false;

                        break;

                    }

                    //else           // “else” here was implied to apply, equal to show it;

                    {

                      prim = true;

                       

                    }

                }

                if (prim == true)   // This is a prime, print it and then adding it to “sum”;

                {

                    Console.Write(i+ ", ");

                    sum += i;

                }

 

            }

            Console.WriteLine("sum={0} ", sum);

 

            Console.ReadLine();

}

}

}

 

2.

using System;

using System.IO;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

namespace ConsoleApplication1

{

    class Program

{

        static void Main(string[] args)

{

            Console.WriteLine( "please give a full test location such as: d:\\temp" ); // Let user give a location;

            string pathtostart = Console.ReadLine();

            DirectoryInfo f = new DirectoryInfo( pathtostart );

            if (f.Exists == true)

{

                do

{

                    Console.WriteLine("Directory have existed, please re-input location again...");

                    pathtostart = Console.ReadLine(); // Inputted location will be the root directory;

                    f = new DirectoryInfo(pathtostart);

 

                  } while (f.Exists == true); // It will be acceptable only when this is a new location;

             }

             // Following lines start to initialize the files and subfolders in the location:

            Directory.CreateDirectory(pathtostart + "\\1"); // Create subdirectory;

            Directory.CreateDirectory(pathtostart + "\\2");

            File.Create(pathtostart + "\\1.doc").Close();   // Create sub file, then close it is necessary;

            File.Create(pathtostart + "\\2.doc").Close();

            Directory.CreateDirectory(pathtostart + "\\1\\1");

            File.Create(pathtostart + "\\1\\1\\1.doc").Close(); // Create multi hierarchy file;

            File.Create(pathtostart + "\\1\\1\\2.doc").Close();

            Console.WriteLine("Initialize default complete, you could view it now...");

 

            String Action1;

            do

{

                Console.WriteLine("Do you want to rename all files (.doc-->.txt)in this folder now? y/n");

                Action1 = Console.ReadLine();

}

            while (Action1 != "y"); // User want to start change them now ?

             FindDirectory(pathtostart); // Start change all, invoke method “FindDirectory”;

 

             Console.WriteLine("Completely!! Please check it now... ");

           

            Console.ReadLine();

}

 

        public static void FindDirectory(string BaseDir) // Recursion all directories in root location;

        {

            DirectoryInfo Finddir = new DirectoryInfo(BaseDir);

            DirectoryInfo[] Add = Finddir.GetDirectories();

            if (Directory.GetFiles(BaseDir).Length > 0) // If first level have files, change them;

                StartToChange (BaseDir);

            if (Add.Length == 0)  // If no any sub directory in this level, then start change;

                StartToChange(BaseDir);

            else                  // if sub directory existing, Recursion it;

            {

                for (int i = 0; i < Add.Length; i++)

                    FindDirectory(Add[i].FullName);

            }

 

        }

 

        public static void StartToChange (string path_last) // Start replace files in directory;

        {

            string path;

            path = path_last;

            int N;

            DirectoryInfo dir = new DirectoryInfo(path); // Get one directory info to “dir”;

            N = System.IO.Directory.GetFiles(path, "*.doc").Length; // Record total number of all .doc files;

           

            FileInfo[] listfile = dir.GetFiles("*.doc");

            for (int j = 0; j < N; j++) // Start to change file name one bye one;

            {

               

                string s = listfile[j].Name;

                int a = s.IndexOf(".");

                string filename = s.Substring(0, a); // Get the filename string before .doc;

                string filename1 = filename + ".txt"; // And using the getting string to create .txt string;

               

                string path0 = path + "\\" + listfile[j]; // Get its absolute path;

                string path01 = path + "\\" + filename1;

               

                File.Move(path0, path01); // Replace the name completely;

            }

        }

 

 }

}

 

3.1

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

{

        static void Main(string[] args)

{

            string S, S1 = "";

            Console.WriteLine("please input the string you want to change...");

            S = Console.ReadLine();

            char[] C = S.ToCharArray();        // Change the string to array characters;

            int length = S.Length;            // Record the length of the array;

          

            for (int i = 0; i < length; i++)

{

                if (char.IsLower(C[i]))     // Is a lower character?

                    C[i]=Char.ToUpper(C[i]); // Reverse it;

                else if (char.IsUpper(C[i])) // Or is a upper character?

                    C[i] = Char.ToLower(C[i]); // Reverse it;

                S1 += C[i].ToString();

}

            Console.WriteLine("old letters are: {0}", S);

            Console.WriteLine("new letters are: {0}", S1);

            Console.ReadLine();

 }

 }

}

 

3.2

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication1

{

    class Program

    {

        static int i = 0;

 

        static void Main(string[] args)

{

            // To test input-box for limitation reports to view in report,case expected actions are:

            // 1>it only allow to input int numbers,and the range must be int>=0;

            // 2> give warning message once input a character;

            // 3> the input int number with no limitation for Max, all will be acceptable if it >0

            // 4> input with only '0' without any response, but with no any warning info also

            // 5> input such as "003565",without any warning info, but saved data will delete pre_index sring "00"

           

 

            Console.WriteLine("Please input the data into report...");

            string s_input = Console.ReadLine();

            Char[] Array = s_input.ToCharArray();

            Boolean AcceptData = true;

            for (int k = 0; k < Array.Length; k++)

{

                if (Array[k] >= '0' && Array[k] <= '9')

{

                int j = (int)Array[k] - 48;  // Asc.II make it from character->number;

 

                int data = (i * 10) + j;

                i = data;                    // Revise the characters to an actual value;

                AcceptData = true;

}

                else

{

                Console.WriteLine("Unacceptable character, you can only type a number here!");

                AcceptData = false;

                break;                      // Veto as soon as met a un_number character;

}

}

            if (AcceptData)

              Console.WriteLine("the saved value actually was :{0}", i);

            Console.ReadLine();

}         

}

}

 

4.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace TestgroupInBeijing

{

    class Program

    {

        static void Main(string[] args)

        {

            TeamAutomatic Autogroup1= new TeamAutomatic();

            TeamAutomatic Autogroup2= new TeamAutomatic();

            TeamFunctional Funcgroup1= new TeamFunctional();

            TeamFunctional Funcgroup2 = new TeamFunctional();

            Leader KscLeader = new Leader("aaa");

            Leader KesLeader = new Leader("bbb");

            // Add more info for testgroup...

            // show test group Beijing structure

 

            Console.ReadKey(true);

        }

    }

 

 

public interface ITester         // public interface, all test group in BeiJing share it;

{

    int employee_ID;

    string name;

    string pcID;

    void worker();

    private void Behavior();

 

}

public abstract class Team        // create an abstract class for team

{

    public int x;                // general public member

    string teamname;

    public void A(int m)

    {

        x = m;

    }                          // struct function

 

    abstract public int fun1();  // alert abstract function

 

 

}

 

public class Leader : ITester

{

    string teamname;

    string leadername;

    int teammembers;

    string teamproductname;

    void Behavior();

   

   public void Leader(string leadername)

    {

        this.leadername = leadername;

    }

    void responsiblities(int N, String tasknames)

    {

        // what Leader's tasks,for one by one

    }

 

}

 

public class general_tester : ITester

{

    int level;

    private void Behavior();

 

}

public class senior_tester : ITester

{

    int level;

    private void Behavior();

}

 

public class TeamAutomatic : Team

{

    public int x;

    string product1;

    string product2;

    int Totalnum;

    int Maletotal;

    int Femaltoal;

    public void A(int m, int n)

    {

 

    }

 

    public int fun1(); // fufill abstract class's fuction

 

}

public class TeamFunctional : Team

{

    public int x;

    public int x;

    string product1;

    string product2;

    int Totalnum;

    int Maletotal;

    int Femaltoal;

    public void A(int m, int n)

    {

        //dosomething here

    }

 

    public int fun1(); // fufill abstract class's fuction

 

}

public class manager : Leader, ITester

{

    string team_name;

    int level;

 

    void Behavior(); // overwrited something

    void responsiblities(int N, String tasknames)

    {

        //what manager's tasks,for one by one

    }

}

}

 



0 0
原创粉丝点击