Created
March 29, 2023 15:54
-
-
Save Adwaith-Rajesh/b2fad618527786174ce7311937d1b5ce to your computer and use it in GitHub Desktop.
Adding two polynomials in zig using arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const stdout = std.io.getStdOut().writer(); | |
const stdin = std.io.getStdIn().reader(); | |
const stderr = std.io.getStdErr().writer(); | |
const expect = std.testing.expect; | |
const exit = std.os.exit; | |
const Term = struct { | |
coef: f32, | |
exp: i32, | |
}; | |
const MAX_TERMS = 100; | |
var polys: [MAX_TERMS]Term = undefined; | |
var avail: u32 = 0; | |
fn displayPoly(start: u32, end: u32) !void { | |
var i = start; | |
while (i < end) : (i += 1) { | |
try stdout.print("{}x^{}", .{ polys[i].coef, polys[i].exp }); | |
if (i == end - 1) { | |
try stdout.print("\n", .{}); | |
} else { | |
try stdout.print(" + ", .{}); | |
} | |
} | |
} | |
fn attach(coef: f32, exp: i32) !void { | |
if (avail == MAX_TERMS - 1) { | |
try stderr.print("MAX TERM size reached", .{}); | |
exit(1); | |
} | |
polys[avail].coef = coef; | |
polys[avail].exp = exp; | |
avail += 1; | |
} | |
fn compare(a: i32, b: i32) i32 { | |
return if (a == b) 0 else (if (a < b) -1 else 1); | |
} | |
fn add(sa: u32, fa: u32, sb: u32, fb: u32, startd: *u32, finishd: *u32) !void { | |
var starta = sa; | |
var startb = sb; | |
var finisha = fa; | |
var finishb = fb; | |
startd.* = avail; | |
while (starta < finisha and startb < finishb) { | |
switch (compare(polys[starta].exp, polys[startb].exp)) { | |
0 => { | |
try attach(polys[starta].coef + polys[startb].coef, polys[starta].exp); | |
starta += 1; | |
startb += 1; | |
}, | |
-1 => { | |
try attach(polys[startb].coef, polys[startb].exp); | |
startb += 1; | |
}, | |
1 => { | |
try attach(polys[starta].coef, polys[starta].exp); | |
starta += 1; | |
}, | |
else => {}, | |
} | |
} | |
while (starta < finisha) : (starta += 1) { | |
try attach(polys[starta].coef, polys[starta].exp); | |
} | |
while (startb < finishb) : (startb += 1) { | |
try attach(polys[startb].coef, polys[startb].exp); | |
} | |
finishd.* = avail; | |
} | |
fn readPoly() !u32 { | |
// returns the length of the poly | |
var buf: [10]u8 = undefined; | |
var terms: u32 = undefined; | |
var coef: f32 = undefined; | |
var exp: i32 = undefined; | |
try stdout.print("Enter the number of terms: ", .{}); | |
if (try stdin.readUntilDelimiterOrEof(buf[0..], '\n')) |user_input| { | |
terms = try std.fmt.parseInt(u32, user_input, 10); | |
} else { | |
try stderr.print("Cannot read user input\n", .{}); | |
exit(1); | |
} | |
try stdout.print("Enter the term in desc order of exp\n", .{}); | |
var i: u32 = 0; | |
while (i < terms) : (i += 1) { | |
if (try stdin.readUntilDelimiterOrEof(buf[0..], '\n')) |user_input| { | |
var it: std.mem.SplitIterator(u8) = std.mem.split(u8, user_input[0..], " "); | |
coef = try std.fmt.parseFloat(f32, it.next().?); | |
exp = try std.fmt.parseInt(i32, it.next().?, 10); | |
try attach(coef, exp); | |
} | |
} | |
return terms; | |
} | |
pub fn main() !void { | |
try stdout.print("Hello, World\n", .{}); | |
var p1 = try readPoly(); | |
try displayPoly(0, p1); | |
var p2 = try readPoly(); | |
try displayPoly(p1, p1 + p2); | |
var startd: u32 = undefined; | |
var finishd: u32 = undefined; | |
try add(0, p1, p1, p1 + p2, &startd, &finishd); | |
try displayPoly(startd, finishd); | |
} | |
test "compare" { | |
try expect(compare(1, 1) == 0); | |
try expect(compare(1, 2) == -1); | |
try expect(compare(2, 1) == 1); | |
} | |
// output | |
// Hello, World | |
// Enter the number of terms: 2 | |
// Enter the term in desc order of exp | |
// 2 2 | |
// 3 1 | |
// 2.0e+00x^2 + 3.0e+00x^1 | |
// Enter the number of terms: 3 | |
// Enter the term in desc order of exp | |
// 5 3 | |
// 2 2 | |
// 3 1 | |
// 5.0e+00x^3 + 2.0e+00x^2 + 3.0e+00x^1 | |
// 5.0e+00x^3 + 4.0e+00x^2 + 6.0e+00x^1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment