rpn calculator function issue? - c#

trying to make a rpn calculator in c# not sure why its not working. is there a error in my program or class file?
class Program
{
static void Main(string[] args)
{
IntStack mystack = new IntStack();
mystack.Push(10);
System.Console.WriteLine(mystack.Pop());
mystack.Push(20);
mystack.Push(30);
mystack.Push(40);
System.Console.WriteLine(mystack.Pop());
System.Console.WriteLine(mystack.Pop());
System.Console.WriteLine(mystack.Pop());
System.Console.ReadKey();
}
}

Try following modifications :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IntStack mystack = new IntStack();
mystack.Push(10);
System.Console.WriteLine(mystack.Pop());
mystack.Push(20);
mystack.Push(30);
mystack.Push(40);
System.Console.WriteLine(mystack.Pop());
System.Console.WriteLine(mystack.Pop());
System.Console.WriteLine(mystack.Pop());
System.Console.ReadKey();
}
}
public class IntStack
{
private List<int> stack = new List<int>();
public int? Pop()
{
if (stack.Count == 0) return null;
int value = stack[0];
stack.RemoveAt(0);
return value;
}
public void Push(int value)
{
stack.Insert(0, value);
}
}
}

Related

How to fix duplicated questions (using Readline() and return)

I'm new to C#.
I'm creating a small program that input number by a user returns to the number and display it.
It works but a user is asked the same questions twice.
I just need one.
I'd appreciate if anyone help me with that.
Here is output of the program.
Here is my programming.
Class1
using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;
namespace aaaa
{
public class Program
{
public static void Main(string[] args)
{
TryReturn tryreturn = new TryReturn();
tryreturn.template();
}
}
}
Class2
using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;
namespace aaaa
{
public class TryReturn
{
public int entryNum;
public void template()
{
returnyNumber();
DisplayNumber();
}
public void DisplayNumber()
{
entryNum = returnyNumber();
WriteLine("number is " + entryNum);
Read();
}
public int returnyNumber()
{
int entryNum;
Write("Choose 1 or 2 >> ");
do
{
int.TryParse(ReadLine(), out entryNum);
if (entryNum == 1 || entryNum == 2)
{
break;
}
else
{
Write("Invalid entry! Enter either '1' or '2' >> ");
}
}
while (true);
return entryNum;
}
}
}
//remove the call of returnyNumber from template as you are calling it from DisplayNumber
public void template()
{
// returnyNumber();
DisplayNumber();
}
public void DisplayNumber()
{
entryNum = returnyNumber();
WriteLine("number is " + entryNum);
Read();
}

What to troubleshoot MEF C#

The following demo application on MEF technology does not work.
Can't build the simplest extensible application. I started with the simplest, a console application that will simply display text on the screen according to the specified parameters. And even here he did not go to me. Where to dig?
IDemoInterface.cs
public interface IDemoInretface
{
void Run(int sleep, int count);
}
MEFDataDemo
Program.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
namespace MEFDataDemo
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Run();
}
public void Run()
{
DoImport();
int count = 1;
foreach (var item in Plugin)
{
item.Value.Run(count * 1000, count * 10);*** This is Error
count++;
}
}
[ImportMany(typeof(IDemoInretface))]
public IEnumerable<Lazy<IDemoInretface>> Plugin { get; set; }
public void DoImport()
{
var catalog = new AggregateCatalog();
var path = Path.Combine(Directory.GetCurrentDirectory(), "Plugins");
catalog.Catalogs.Add(new DirectoryCatalog(path));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
}
Plugin1 Class1.cs
using System;
using System.ComponentModel.Composition;
using System.Threading;
namespace Plugin1
{
[Export(typeof(IDemoInretface))]
internal class Class1 : IDemoInretface
{
public void Run(int sleep, int count)
{
for (int i = 0; i < count; i++)
{
Console.WriteLine("This plugin1 print {0}/{1} in {2}ms", i, count, sleep);
Thread.Sleep(sleep);
}
}
}
}
What am I doing wrong?
The program compiles, but either does not read plugins, or reads but does not run the (RUN) method

im getting error of program does not contain a static 'main' method suitable for an entry point

I'm getting an error, "Program does not contain a static 'main' method suitable for an entry point."
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
class Program<T>
{
List<T> a1 = new List<T>();
public void AddData(T data1)
{
a1.Add(data1);
}
public void Insert(T arrayValue, int arrayPosition)
{
// var key = arrayPosition.ToInt();
var key1 = arrayValue.ToString();
for (int i = 0; i < a1.Count; i++)
{
if (i == arrayPosition)
{
a1.Add(arrayValue);
break;
}
}
}
public void Delete(int arrayPosition)
{
for (int i = 0; i < a1.Count; i++)
{
if (i == arrayPosition)
{
a1.Remove(a1[i]);
break;
}
}
}
public void DisplayData()
{
foreach (T x in a1)
{
Console.WriteLine(x);
}
}
static void Main(string[] args)
{
Program<int> obj = new Program<int>();
obj.AddData(123);
obj.AddData(56);
obj.AddData(34);
obj.AddData(87);
obj.DisplayData();
obj.Insert(125, 3);
obj.DisplayData();
obj.Delete(2);
obj.DisplayData();
}
}
}
Per Microsoft documentation (https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0402) you cannot put your main function in a generic class. It's strange that this was reported as a warning, and the error was less helpful, but that is your problem.
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public static class Program
{
[STAThread]
public static void Main(string[] args)
{
List<Int32> obj = new List<Int32>();
obj.Add(123);
obj.Add(56);
obj.Add(34);
obj.Add(87);
foreach (Int32 value in obj)
Console.WriteLine(value.ToString());
obj.Insert(3, 125);
foreach (Int32 value in obj)
Console.WriteLine(value.ToString());
obj.RemoveAt(2);
foreach (Int32 value in obj)
Console.WriteLine(value.ToString());
}
}
}
Defining a custom class to handle this tasks looked to me like an overkill. This code should reproduct what you were attempting to do without errors. Anyway, I suggest you to take a look at some basic C# tutorial.

how to add to the list an object of class property in c sharp

I cannot add class instances correctly to a List. It adds only the last object. And when I debug the List vocabulary shows only adding the last class instance. So by second looping it has two entries of second object, by third looping it has three entries of third object. What I am doing wrong. Here is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace myVocabulary
{
public class Word
{
string _original_word;
string _translated_word;
public string Original_Word
{
get
{
return this._original_word;
}
set
{
this._original_word = value;
}
}
public string Translated_Word
{
get
{
return this._translated_word;
}
set
{
this._translated_word = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace myVocabulary
{
class Program
{
static List<Word> vocabulary = new List<Word>();
static void Main(string[] args)
{
Word entry = new Word();
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Enter word");
entry.Original_Word = Console.ReadLine();
Console.WriteLine("Enter Translation");
entry.Translated_Word = Console.ReadLine();
vocabulary.Add(entry);
}
}
}
}
Try this - You need to create a new Word object with each iteration of your loop, otherwise you're just overwriting the same instance repeatedly.
namespace myVocabulary
{
class Program
{
static List<Word> vocabulary = new List<Word>();
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
Word entry = new Word();
Console.WriteLine("Enter word");
entry.Original_Word = Console.ReadLine();
Console.WriteLine("Enter Translation");
entry.Translated_Word = Console.ReadLine();
vocabulary.Add(entry);
}
}
}
}

Why won't my c# program run?

I'm a uni student just starting to learn c#. I'm sure there is a simple solution to this but I have searched and I don't think know enough yet.
this is my program, note that I have not finished a few functions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
public void welcome()
{
Console.WriteLine("Fuel Consumption Calculator "+"r/n"+"Are you using Metric 1 or Imperial 2 ?");
}
public void check()
{
string choice;
choice = Console.ReadLine();
if (choice == "1")
{
calcmetric();
}
else
{
calcimperial();
}
}
public void calcmetric()
{
}
public void calcimperial()
{
}
}
}
}
In Visual Studio I have two errors: one saying a '}' is expected after Main; and there is an error at the very end saying "type or namespace definition error".
You are declaring methods inside a method. This is wrong.
Change it:
class Program
{
static void Main(string[] args)
{
//call other methods here
welcome();
check();
//....
}
public static void welcome()
{
Console.WriteLine("Fuel Consumption Calculator "+"r/n"+"Are you using Metric 1 or Imperial 2 ?");
}
public static void check()
{
string choice;
choice = Console.ReadLine();
if (choice == "1")
{
calcmetric();
}
else
{
calcimperial();
}
}
public static void calcmetric()
{
}
public static void calcimperial()
{
}
}

Categories