using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Ranking
{
class Program
{
static void Main(string[] args)
{
//keep a count of the number of lines
string line;
//Array to store each line infomation
string[] nameArray = File.ReadAllLines("c:\\output.txt");
//read the file information in and store it in file
StreamReader file = new StreamReader("c:\\output.txt");
//loop no more than the number of lines in the document
for (int i = 0; i < nameArray.Length; i++)
{
//if the file is not read the information and print it
if ((line = file.ReadLine()) != null)
{
Console.WriteLine(i + " " + line);
}
}
Console.ReadLine();
file.Close();
}
}
}
Marcus Randolph
Blog about XNA tutorials, C# tutorials and C++ tutorials
Friday, October 23, 2015
Sunday, January 29, 2012
Saturday, January 28, 2012
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
stack<int> stack1, stack2, temp_stack;
//loop to fill our stack with elememnts
for(int i = 0 ; i < 5; i++)
{
//push elements on to stack1
stack1.push(i);
//print elements to screen along with the slot the elements are in
cout << "slot " << i + 1 << " " << stack1.top() << endl;
}
//loop until stack1 is empty
while(!stack1.empty())
{
//push the top element of stack1 onto temp_stack
temp_stack.push(stack1.top());
//pop the elements off stack1 to get to the next element
stack1.pop();
}
//loop until temp_stack is empty
while(!temp_stack.empty())
{
// push temp_stack elements on the stack1 and stack2 before you pop
stack1.push(temp_stack.top());
stack2.push(temp_stack.top());
//pop temp_stack
temp_stack.pop();
//print stack1 and stack2
cout << "stack 1 " << stack1.top() << endl;
cout << "stack 2 " << stack2.top() << endl;
}
}
Friday, January 27, 2012
Taking elements from one stack and inserting them into another in the same order. C++
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
stack<int> stack1, stack2, temp_stack;
//loop to fill our stack with elememnts
for(int i = 0 ; i < 5; i++)
{
//push elements on to stack1
stack1.push(i);
//print elements to screen along with the slot the elements are in
cout << "slot " << i + 1 << " " << stack1.top() << endl;
}
//loop until stack1 is empty
while(!stack1.empty())
{
//push the top element of stack1 onto temp_stack
temp_stack.push(stack1.top());
//pop the elements off stack1 to get to the next element
stack1.pop();
}
//loop until temp_stack is empty
while(!temp_stack.empty())
{
// push temp_stack elements on the stack1 and stack2 before you pop
stack1.push(temp_stack.top());
stack2.push(temp_stack.top());
//pop temp_stack
temp_stack.pop();
//print stack1 and stack2
cout << "stack 1 " << stack1.top() << endl;
cout << "stack 2 " << stack2.top() << endl;
}
}
#include <stack>
#include <string>
using namespace std;
int main()
{
stack<int> stack1, stack2, temp_stack;
//loop to fill our stack with elememnts
for(int i = 0 ; i < 5; i++)
{
//push elements on to stack1
stack1.push(i);
//print elements to screen along with the slot the elements are in
cout << "slot " << i + 1 << " " << stack1.top() << endl;
}
//loop until stack1 is empty
while(!stack1.empty())
{
//push the top element of stack1 onto temp_stack
temp_stack.push(stack1.top());
//pop the elements off stack1 to get to the next element
stack1.pop();
}
//loop until temp_stack is empty
while(!temp_stack.empty())
{
// push temp_stack elements on the stack1 and stack2 before you pop
stack1.push(temp_stack.top());
stack2.push(temp_stack.top());
//pop temp_stack
temp_stack.pop();
//print stack1 and stack2
cout << "stack 1 " << stack1.top() << endl;
cout << "stack 2 " << stack2.top() << endl;
}
}
Sunday, January 22, 2012
Infix to prefix C++
/*The program prompts the user to enter an expression. The expression can
be as long as you want it to be because it is storing it as a string in "infix".
The program takes the users input and changes it to prefix notation. Help from
http://www.c4swimmers.esmartguy.com/in2pre.htm#. this site supplied the
algorythm. Also, http://www.cplusplus.com/forum/beginner/16722/
had some code for infix to "postfix" that i looked over and borrowed some ideas*/
#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
using namespace std;
/*This is a function that declares a temp variable and stores a number based on
the operator that the user inputs.If the operator is *,/,%, then temp is given
the number 2.That means it's priority is higher than +,- because tempgets a 1 when
one of those operators are entered.*/
int priority(char a)
{
int temp;
if (a == '*' || a == '/' || a == '%')
temp = 2;
else if (a == '+' || a == '-')
temp = 1;
return temp;
}
//start
int main()
{
//declare a string called "infix"
string infix;
/*Declare a sringstream called "output"We use "stringStream" this allows a string-based
object to be treated as a stream. This way we can perform extraction or insertion
operations from/to strings, which is especially useful to convert strings to numerical values
and vice versa.*/
stringstream output;
/*creates two stacks. s1 is for current data and s2 is for storing data in the
correct order for output*/
stack<char> s1, s2;
cout << "Infix to Prefix" << endl;
//prompts the user to eneter a string without including parenthesis
cout << "Enter an arithmetic expression with no perenthesis: " << endl;
//gets the users input and stores it in infix as a string
getline(cin, infix);
/*loop backwards to reverse the infix string and read one charcter at a time.
from right to left*/
for(int i = infix.length() - 1; i >= 0; i--)
{
//check the input against +,-,/,*,%
if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '%')
{
/*While the stack is not empty, and the top operator in the stack has greater
priority than the operator it is currently reading, send it to the top of
stack s1. Then, push the top of s1 to s2 to store it. After that, pop the top of s1.*/
while(!s1.empty() && priority(s1.top()) > priority(infix[i]))
{
output << s1.top();
s2.push(s1.top());
s1.pop();
}
/*if stack s1 is empty, then add the current operator to infix string*/
s1.push(infix[i]);
}
/*if the current input is not an operator, send it to the infix string, then
push the infix to stack s2 to store it*/
else
{
output << infix[i];
s2.push(infix[i]);
}
}
/*While s1 stack is not empty send the output to the top of it. Then, push the top of stack s1
to stack s2 for storage. we don't need the top of s1 stack antmore because we stored it in stack s2
so pop it off.*/
while(!s1.empty())
{
output << s1.top();
s2.push(s1.top());
s1.pop();
}
cout << "\nAnswer: ";
/*While stack s2 is not empty, display what is in the top position, then pop it off so we can check to
see if there are more charcters in the stack.*/
while(!s2.empty())
{
cout << s2.top();
s2.pop();
}
cout <<"\n\nPress enter to exit" << endl;
}
be as long as you want it to be because it is storing it as a string in "infix".
The program takes the users input and changes it to prefix notation. Help from
http://www.c4swimmers.esmartguy.com/in2pre.htm#. this site supplied the
algorythm. Also, http://www.cplusplus.com/forum/beginner/16722/
had some code for infix to "postfix" that i looked over and borrowed some ideas*/
#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
using namespace std;
/*This is a function that declares a temp variable and stores a number based on
the operator that the user inputs.If the operator is *,/,%, then temp is given
the number 2.That means it's priority is higher than +,- because tempgets a 1 when
one of those operators are entered.*/
int priority(char a)
{
int temp;
if (a == '*' || a == '/' || a == '%')
temp = 2;
else if (a == '+' || a == '-')
temp = 1;
return temp;
}
//start
int main()
{
//declare a string called "infix"
string infix;
/*Declare a sringstream called "output"We use "stringStream" this allows a string-based
object to be treated as a stream. This way we can perform extraction or insertion
operations from/to strings, which is especially useful to convert strings to numerical values
and vice versa.*/
stringstream output;
/*creates two stacks. s1 is for current data and s2 is for storing data in the
correct order for output*/
stack<char> s1, s2;
cout << "Infix to Prefix" << endl;
//prompts the user to eneter a string without including parenthesis
cout << "Enter an arithmetic expression with no perenthesis: " << endl;
//gets the users input and stores it in infix as a string
getline(cin, infix);
/*loop backwards to reverse the infix string and read one charcter at a time.
from right to left*/
for(int i = infix.length() - 1; i >= 0; i--)
{
//check the input against +,-,/,*,%
if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '%')
{
/*While the stack is not empty, and the top operator in the stack has greater
priority than the operator it is currently reading, send it to the top of
stack s1. Then, push the top of s1 to s2 to store it. After that, pop the top of s1.*/
while(!s1.empty() && priority(s1.top()) > priority(infix[i]))
{
output << s1.top();
s2.push(s1.top());
s1.pop();
}
/*if stack s1 is empty, then add the current operator to infix string*/
s1.push(infix[i]);
}
/*if the current input is not an operator, send it to the infix string, then
push the infix to stack s2 to store it*/
else
{
output << infix[i];
s2.push(infix[i]);
}
}
/*While s1 stack is not empty send the output to the top of it. Then, push the top of stack s1
to stack s2 for storage. we don't need the top of s1 stack antmore because we stored it in stack s2
so pop it off.*/
while(!s1.empty())
{
output << s1.top();
s2.push(s1.top());
s1.pop();
}
cout << "\nAnswer: ";
/*While stack s2 is not empty, display what is in the top position, then pop it off so we can check to
see if there are more charcters in the stack.*/
while(!s2.empty())
{
cout << s2.top();
s2.pop();
}
cout <<"\n\nPress enter to exit" << endl;
}
Monday, January 24, 2011
Testing for non-numeric input C #
Here is a problem that many programmers will encounter while programming. Testing weather a value is a number or anything other than. I found some rather strange ways of doing this, but to me, there is only one for sure way.
static void Main(string[] args)
{
double numbers = 0;
double runningTotal = 0;
//Prompts the user to enter the total number of numbers to avg
Console.Write("How many numbers do you want to average?: ");
//Saves the input
uint input;
//try to convert if not loop until it can convert
while (!uint.TryParse(Console.ReadLine(), out input) || input <= 0)
{
Console.WriteLine("Incorrect input.....please try again!!\n");
Console.Write("How many numbers do you want to average?: ");
}
//More code goes here
The TryParse method is a solid way to test this. TryParse can be used in so many ways that can be very beneficial to anyone.
Take the time to search the web for more ways to use this method
static void Main(string[] args)
{
double numbers = 0;
double runningTotal = 0;
//Prompts the user to enter the total number of numbers to avg
Console.Write("How many numbers do you want to average?: ");
//Saves the input
uint input;
//try to convert if not loop until it can convert
while (!uint.TryParse(Console.ReadLine(), out input) || input <= 0)
{
Console.WriteLine("Incorrect input.....please try again!!\n");
Console.Write("How many numbers do you want to average?: ");
}
//More code goes here
The TryParse method is a solid way to test this. TryParse can be used in so many ways that can be very beneficial to anyone.
Take the time to search the web for more ways to use this method
Subscribe to:
Posts (Atom)
