charactor selection from cmd
Dear All,
after compiling, I run the program as:
program.exe infile outfile foldpoint vel base. I.e. I type 6 arguments.
The program works good. Now I want to make some modifications and dont know how to do it.
I want to make 1 extra argument (it can be either 't' or 's'), so that I type
program.exe infile outfile foldpoint vel base t or s.
If I type t, then in the following code (line 86), fprintf should be executed.
If I type s, then in the following code (line 87), /* fprintf...*/ should be executed. Please help me how can I do it?
Another help (but not important!):
In the program dp is the integer value obtained from line 70. And this integer value is used in fprintf (refer line 86 or 87). I cannot use this value directly without including lines 73-80. So I need to repeat the lines 48-56 two times. The lines 48-56 and 73-80 are same. Are there any way to avoid 73-80? So the program looks more compact.
cos is cosine! 3.14 is pi, instead of using 3.14 I want to declare pi constant efficiently in c program.
For this C codes I was actually interested to make my own program (https://cboard.cprogramming.com/c-pr...plication.html). But it seems it is difficult and I dropped my plan.
My code:
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10000
int main(int argc, char *argv[])
{
FILE *fs, *ft;
char *foldpoint, line[MAX];
int i, low, high, n[MAX], size,sum,dp;
double fp,v,b;
if (argc != 6) {
puts("Improper number of arguments\nType rawfilename outputfilename foldingpoint velocity baseline");
exit(0);
}
fs = fopen(argv[1], "r");
if (fs == NULL) {
puts("Cannot open source file");
exit(0);
}
ft = fopen(argv[2], "w");
if (ft == NULL) {
puts("Cannot open target file");
exit(0);
}
foldpoint = argv[3];
sscanf(foldpoint, "%lf", &fp);
printf("Folding point= %lf\n", fp);
sscanf(argv[4], "%lf", &v);
printf("Drive velocity= %lf\n", v);
sscanf(argv[5], "%lf", &b);
printf("Base line counts= %lf\n", b);
i = 0;
while (fgets(line, sizeof(line), fs)) {
sscanf(line, "%d", &n[i]);
i++;
if (i == (int)fp) {
printf("%lf is %s", fp, line);
}
}
size = i;
if (fp == (int)fp) {
low = (int)fp;
high = low;
}
else {
low = (int)fp;
high = low + 1;
}
sum=0;
low--;
high--;
i = 0;
while (low >= 0 && high < size) {
sum=sum+((n[low]-n[high])*(n[low]-n[high]));
i++;
low--;
high++;
dp=i;
}
printf("Number of data points= %d\n", dp);
printf("Sum of the sqaured differences= %d\n", sum);
if (fp == (int)fp) {
low = (int)fp;
high = low;
}
else {
low = (int)fp;
high = low + 1;
}
low--;
high--;
i = 0;
while (low >= 0 && high < size) {
fprintf(ft, "%lf %lf\n", ((2*i)-dp+1)*v/(dp-1), (n[low]+n[high])*100/b);
/*fprintf(ft, "%lf %lf\n", v*cos(2*i*3.14/(dp-1)), (n[low]+n[high])*100/b);*/
i++;
low--;
high++;
}
fclose(fs);
fclose(ft);
return 0;
}
Thanks for your help.
Cheers,
Raja.