EDIT: Here's a program I had to code for my programming class.
I began studying Computer Scince a few month ago. (I knew how to code C before)
The program solves the equation a1*x + a0 = 0
/*
Author: Bob Dole.. Bob Dole... Bob Dole...... bob dole.... bob... dole....
Martikelnummer: xxxxxx
Aufgabe 10
----
Programm liest aj der Gleichung a1*x + a0 = 0 ein, und berechnet x
*/
#include <stdio.h>
int main(void) {
double a0=0; // erster Faktor der Gleichung
double a1=0; // zweiter Faktor der Gleichung
double x=0; // x das berechnet werden soll
puts("a1*x + a0 = 0");
puts("a1 und a0 eingeben:");
scanf("%lf %lf",&a1,&a0); // Zahlen vom Typ double einlesen
/*
Lösung der Gleichung:
a1 * x + a0 = 0
a1 * x = -a0
x = -a0/a1
*/
// Keine Lösung für a1=0, weil man nicht du 0 teilen darf, also Fehler abfangen.
if (a1 != 0) {
x = -a0/a1;
printf("x= %.2lf\n",x);
} else {
puts("Keine Lösung der Gleichung für a1 = 0");
}
return 0;
} |
Any comments on the coding style and the way I solved the problem, Ekted?
I'd like to know if an axperienced coder like you tells me the same things as my lecturer, because I doubt what he says sometimes.
Bob Dole.. Bob Dole... Bob Dole...... bob dole.... bob... dole....
Anonymous - Thu Oct 28, 2004 2:52 pm
Post subject:
I prefer to put opening curly brackets on their own line as you can then find code blocks easier. Although when I first started I only had 160x160 screen and 4k file size limit so I was trying to cram as much as posible onto each line.
Also when passing parameters and such I put a space after the comma so it reads well.
Mr Ekted - Thu Oct 28, 2004 5:50 pm
Post subject:
Reformatted to my tastes. Also, scanf may fail if the input isn't very specific. You should use gets(), parse, and display appropriate errors.
#include <stdio.h>
int main (void)
{
double a0 = 0; // erster Faktor der Gleichung
double a1 = 0; // zweiter Faktor der Gleichung
double x = 0; // x das berechnet werden soll
puts("a1*x + a0 = 0");
puts("a1 und a0 eingeben:");
scanf("%lf %lf", &a1, &a0); // Zahlen vom Typ double einlesen
/*
Lösung der Gleichung:
a1 * x + a0 = 0
a1 * x = -a0
x = -a0/a1
*/
// Keine Lösung für a1=0, weil man nicht du 0 teilen darf, also Fehler abfangen.
if (a1 != 0)
{
x = -a0 / a1;
printf("x= %.2lf\n", x);
}
else
puts("Keine Lösung der Gleichung für a1 = 0");
return (0);
} |
Mr Ekted - Thu Oct 28, 2004 5:53 pm
Post subject:
Here's a picture I made showing most of my style issues...
CypherJF - Thu Oct 28, 2004 6:38 pm
Post subject:
style natzi
jk... I think it's good advice to have a consistent clean programming style.
Mine GO BOOM - Fri Oct 29, 2004 1:07 am
Post subject:
I like the function's to be indented. Other than that, its pretty much the same. I have the {}'s indented or not depending on whatever editor I use at the time sets up as the default.
I prefer /* */ for large comment blocks over //'s. And when I do, I always have each new line started with a *, and all the *'s line up vertically.
Onlyone - Fri Oct 29, 2004 3:14 am
Post subject:
Dang Ekted... i thought i was a neat freak
Solo Ace - Fri Oct 29, 2004 5:35 am
Post subject: Astyle
I dislike a few things in Ekted's style, although most of it is the same as mine.
Sourceforge has some project called "astyle", I hate how it was written but it works, anyway, it can restyle source code into a bunch of styles.
Could be interesting for "style nazis".
Cyan~Fire - Fri Oct 29, 2004 6:25 am
Post subject:
I basically have the same style as MGB: Ekted's style but with indented functions. Actually, why do you not indent your functions, Ekted?
Mr Ekted - Fri Oct 29, 2004 2:13 pm
Post subject:
I don't indent functions because it's just extra work, wasted space, at the lowest level. Since function are always delimited by green comments, it's easy to see where they begin and end. It's not important to me that everyone use my style. They just need to be consistent within their own style, and be relatively clean. When people code using their own style, it's easier for them to code with fewer mistakes. I also believe in 1-file 1-coder methodology. I do not share code on projects. If someone takes over file(s) of yours, they own it and can format it how they wish.
Dr Brain - Fri Oct 29, 2004 3:04 pm
Post subject:
That's bacicly how I feel about peoeple's styles, with a few exceptions. When I code for ASSS, I try to follow the ASSS coding style. As it's not all that different from the way I code, this wasn't a big strech. Though I've recently given up this habit, I do feel that people should make a small effort to keep files in a public project similar to each other.
Mr Ekted - Fri Oct 29, 2004 4:16 pm
Post subject:
I found the ASSS source files that I saw to be unreadable. grel and I are on opposite ends of the spectrum for code design, coding style, and open source issues, so it's best that we don't work together anyways.