Skip to content

Instantly share code, notes, and snippets.

@snuxoll
Created April 10, 2009 22:17
Show Gist options
  • Save snuxoll/93333 to your computer and use it in GitHub Desktop.
Save snuxoll/93333 to your computer and use it in GitHub Desktop.
using GLib;
namespace snuxoll {
class StdinReader : Object {
public StdinReader () {
}
public bool eof() {
return stdin.eof();
}
public string? read_line() {
var line = new StringBuilder();
var buffer = new char[1024];
while ( ( !line.str.has_suffix ("\n") ) || this.eof() ) {
string chunk = stdin.gets(buffer);
if (chunk != null) {
line.append(chunk);
} else {
return null;
}
}
return line.str;
}
}
}
using GLib;
using snuxoll;
class SumApplication : Object {
private uint64 total { get; set; }
public SumApplication() {
this.total = 0;
}
public void run() {
var reader = new StdinReader();
while ( !stdin.eof() ) {
var line = reader.read_line();
if ( line != null ) {
var nums = line.strip().split(" ");
foreach(string num in nums) {
this.total += num.to_int();
}
}
}
stdout.printf("Grand Total: %llu\n", total);
}
static int main(string[] args) {
var app = new SumApplication();
app.run();
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment