简体   繁体   中英

What's the equivalent scaffholding of this project in C (coming from C#)?

I'm interested in learning C, and one of the things I like to do when I'm first learning a language is to try a handful of Project Euler questions.

If I were doing this in C#, I would write an interface that all of my questions would implement, and I would include things like a PrintSolution() method and a ProblemStatement property. Then I would create a custom class in individual files for each question, using a naming convention like Problem1.cs ... Problem123.cs , etc. Each of these objects would implement the problem interface. This allows me to start fresh with each question instead of writing potentially long methods called SolveProblem1 , SolveProblem2 , etc. in one file, and then choosing which one to call in main .

Finally, for testing or revisiting a problem, I would have a driver class with a main method, and then to quickly show the solution to any problem I had solved, I could call this (replacing the 1 with the number of the problem I wanted):

Problem1 testProblem = new Problem1(); 
testProblem.PrintSolution();
Console.Read();

I know C isn't an object-oriented language, but is there a similar way to architect a solution in Visual Studio such that I can keep all my code in one location and run the answer to a problem on the fly?

Pointers to functions may help you to achieve the desirable code composition in a procedural world.

Function pointers in C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM