Определить где в файле больше нулей - на четных или на нечетных позициях? - C#
Формулировка задачи:
Добрый день! Задача такая: дан файл целых чисел. Определить где больше НУЛЕЙ - на четных или на нечетных позициях.
Вроде разобрался с BinaryWriter и Reader. Вот мой код
с помощью while я прохожусь по файлу,но не могу понять как найти парные и непарные,ведь это не массив.. как обратиться к элементу 1 если он записан как 2 0 0 0 к примеру,там где 4 байта. Помогите пожалуйста: Просто если был бы такой массив целых чисел я бы сделал for а в нём if(array[i]%2==0&&array[i]==0) и т.д. а как сделать тут. Прошу помощи. Спасибо заранее!
using System;
using System.IO;
using System.Text;
namespace proga
{
class Program
{
static void Read(string path) {
BinaryReader read = new BinaryReader(File.Open(path,FileMode.Open));
int x1 = read.ReadInt32();
int d1 = 0;
int d2=0;
while (read.PeekChar() != -1)
{
// вот тут мой недоалгоритм...
/* if (x1 % 2 == 0 && x1 == 0)
{
d1 += d1 + 1;
}
if (x1%2==1&&x1==0) {
d2 += d2 + 1;
}
*/
}
}
static void Information(string path) {
BinaryReader read1 = new BinaryReader(File.Open(path, FileMode.Open));
while (read1.PeekChar() != -1)
{
Console.WriteLine("{0,4}",read1.Read());
}
read1.Close();
} //выводим содержимое нашего файла
static void Create(int x) {
try
{
Random rand = new Random();
string path =@"D:\proga\note0.dat";//путь
BinaryWriter write = new BinaryWriter(File.Open(path, FileMode.OpenOrCreate));
for (int i = 0; i < 20; i++)
{
x = rand.Next(0,50);
write.Write(x);
}
write.Close();//закрЫваем наш поток
Information(path);
Read(path);
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static void Main(string[] args)
{
try
{
Console.WriteLine("***");
Create(0);
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
}
}Решение задачи: «Определить где в файле больше нулей - на четных или на нечетных позициях?»
textual
Листинг программы
using System;
using System.IO;
using System.Text;
namespace proga
{
class Program
{
static void Read(string path) {
BinaryReader read = new BinaryReader(File.Open(path,FileMode.Open));
int temp;
int i = 0;
int s1 = 0;
int s2 = 0;
while (read.PeekChar() != -1)
{
i++;
temp = read.ReadInt32();
if (i%2==1&&temp==0) {
s1 += 1;
}
if (i%2==0&&temp==0)
{
s2 += 1;
}
}
read.Close();
if (s1 > s2)
{
Console.WriteLine("На парных позициях больше");
}
else if (s1<s2)
{
Console.WriteLine("На непарных больше");
}
}
static void Information(string path) {
BinaryReader read1 = new BinaryReader(File.Open(path, FileMode.Open));
while (read1.PeekChar()!= -1)
{
Console.WriteLine("{0,4}",read1.ReadInt32());
}
read1.Close();
} //выводим содержимое нашего файла
static void Create(int x) {
try
{
Random rand = new Random();
string path =@"D:\proga\random2.dat";//путь
BinaryWriter write = new BinaryWriter(File.Open(path, FileMode.OpenOrCreate));
for (int i = 0; i < 20; i++)
{
x = rand.Next(0,6);
write.Write(x);
}
write.Close();//закрЫваем наш поток
Information(path);
Read(path);
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static void Main(string[] args)
{
try
{
Console.WriteLine("***");
Create(0);
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
}
}