Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- double get_res(char *buff) {
- double arg;
- if (sscanf(buff, "sin(%lf)%*[ ]s=", &arg)) {
- char *nxt = strchr(buff, '=') + 1;
- *nxt = 0;
- return sin(arg);
- }
- if (sscanf(buff, "cos(%lf)%*[ ]s=", &arg)) {
- char *nxt = strchr(buff, '=') + 1;
- *nxt = 0;
- return cos(arg);
- }
- if (sscanf(buff, "tg(%lf)%*[ ]s=", &arg)) {
- char *nxt = strchr(buff, '=') + 1;
- *nxt = 0;
- return tan(arg);
- }
- if (sscanf(buff, "ctg(%lf)%*[ ]s=", &arg)) {
- char *nxt = strchr(buff, '=') + 1;
- *nxt = 0;
- return 1. / tan(arg);
- }
- return NAN;
- }
- int process(const char *in, const char *out) {
- FILE *fin = fopen(in, "r"),
- *fout = fopen(out, "w");
- if (!fin) {
- fclose(fout);
- return 0;
- }
- char buff[100];
- while (fgets(buff, 100, fin)) {
- double res = get_res(buff);
- if (isnan(res)) {
- fclose(fout);
- return 0;
- }
- fprintf(fout, "%s %lf\n", buff, res);
- }
- fclose(fin);
- fclose(fout);
- return 1;
- }
- int main(void) {
- if (!process("input.txt", "output.txt")) {
- printf("Error!\n");
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment