项目作者: nakov

项目描述 :
Nakov.IO.Cin - Console input for C# in the C++ Style: cin >> x >> y
高级语言: C#
项目地址: git://github.com/nakov/Nakov.io.cin.git
创建时间: 2012-04-29T08:05:52Z
项目社区:https://github.com/nakov/Nakov.io.cin

开源协议:MIT License

下载


Nakov.IO.Cin: the C# console input working as cin in C++

Nakov.IO.Cin is a console-based input reader for C#, which reads numbers and text in the C++ cin / cout / iostream style.

Install the NuGet Package

First, install the NuGet package Nakov.IO.Cin:

  1. Install-Package Nakov.IO.Cin

Now you are ready to translate C++ cin / cout / iostream code to C#.

Sample C++ Code

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n;
  6. cin >> n;
  7. int* numbers = new int[n];
  8. for (int i = 0; i < n; i++)
  9. cin >> numbers[i];
  10. for (int i = 0; i < n; i++)
  11. cout << numbers[i] << ' ';
  12. }

Corresponsing C# Code

  1. using System;
  2. using Nakov.IO; // see http://www.nakov.com/tags/cin
  3. public class EnteringNumbers
  4. {
  5. static void Main()
  6. {
  7. int n = Cin.NextInt();
  8. int[] numbers = new int[n];
  9. for (int i = 0; i < n; i++)
  10. numbers[i] = Cin.NextInt();
  11. for (int i = 0; i < n; i++)
  12. Console.Write(numbers[i] + " ");
  13. }
  14. }

More Detailed Example

  1. using System;
  2. using Nakov.IO; // See http://www.nakov.com/tags/cin
  3. public class CinExample
  4. {
  5. static void Main()
  6. {
  7. Console.Write("Enter your name: ");
  8. string name = Console.ReadLine();
  9. Console.Write("Enter two integers x and y separated by whitespace: ");
  10. // cin >> x >> y;
  11. int x = Cin.NextInt();
  12. double y = Cin.NextDouble();
  13. Console.Write("Enter your age: ");
  14. int age = int.Parse(Console.ReadLine());
  15. Console.WriteLine("Name: {0}, Age: {1}", name, age);
  16. Console.WriteLine("x={0}, y={1}", x, y);
  17. Console.Write("Enter a positive integer number N: ");
  18. // cin >> n;
  19. int n = Cin.NextInt();
  20. Console.Write("Enter N decimal numbers separated by a space: ");
  21. decimal[] numbers = new decimal[n];
  22. for (int i = 0; i < n; i++)
  23. {
  24. // cin >> numbers[i];
  25. numbers[i] = Cin.NextDecimal();
  26. }
  27. Array.Sort(numbers);
  28. Console.WriteLine("The numbers in ascending order: {0}",
  29. string.Join(' ', numbers));
  30. Console.Write("Enter two strings seperated by a space: ");
  31. // cin >> firstStr >> secondStr;
  32. string firstStr = Cin.NextToken();
  33. string secondStr = Cin.NextToken();
  34. Console.WriteLine("First str={0}", firstStr);
  35. Console.WriteLine("Second str={0}", secondStr);
  36. }
  37. }

This is a sample input and output from the above example:

  1. Enter your name: Albert Einstein
  2. Enter two integers x and y separated by whitespace:
  3. 10
  4. 20
  5. Enter your age: 25
  6. Name: Albert Einstein, Age: 25
  7. x=10, y=20
  8. Enter a positive integer number N:
  9. 5
  10. Enter N decimal numbers separated by a space: 10 30 40
  11. 50
  12. 20
  13. The numbers in ascending order: 10 20 30 40 50
  14. Enter two strings seperated by a space:
  15. Visual Studio
  16. First str=Visual
  17. Second str=Studio

Note that input numbers and string tokens can be separated by single space, by a new line or by a sequence of white space characters.

Learn more at: http://www.nakov.com/tags/cin.