tak
0
3
skoro jezyk C to jak rozszerzenie cpp?
jak kalkulator na strukturach? Przeciez jak masz liczbe, to struktura jak ma to zapisac?
Moze na strukutrze (jednej?)
kod jest na podstawie Twojego bys lepiej zrozumial
3
Może przydać Ci się coś takiego:
#include <stdio.h>
#include <string.h>
struct args {
float a, b;
};
float add(struct args args) {
return args.a + args.b;
};
float sub(struct args args) {
return args.a - args.b;
}
float mul(struct args args) {
return args.a * args.b;
}
typedef float(*calc_func)(struct args);
struct named_func {
const char *name;
calc_func func;
};
int main(void) {
struct named_func named_funcs[] = {
{ "plus", &add },
{ "minus", &sub },
{ "times", &mul }
};
char chosen_func[64];
struct args args;
while(scanf("%f %63s %f", &args.a, chosen_func, &args.b) == 3) {
for(int i = 0; i < sizeof(named_funcs)/sizeof(named_funcs[0]); ++i) {
if(!strcmp(chosen_func, named_funcs[i].name)) {
float result = named_funcs[i].func(args);
printf("%.2f %s %.2f = %.2f\n", args.a, chosen_func, args.b, result);
break;
}
}
}
return 0;
}
stdin |
stdout |
---|
2 plus 5
2 minus 3
10 times 3
|
2.00 plus 5.00 = 7.00
2.00 minus 3.00 = -1.00
10.00 times 3.00 = 30.00
0
olisadebe napisał(a):
float sum(struct one a,struct one b);
oducz się takich głupot - one to typ one, żadne struct tam nie jest potrzebne.
0
On chcial to w formie metody chyba.
1
Podrzucam jeszcze to, może się przyda
//Plik nagłówkowy "str.h"
struct arg {
float x, y;
};
//main
#include "stdafx.h"
#include "stdio.h"
#include "cstdlib"
#include "str.h"
float suma(struct arg a) {
float c = a.x + a.y;
return c;
}
void main() {
struct arg a;
printf("Podaj argumenty: ");
scanf_s("%f %f",&a.x,&a.y);
float c = suma(a);
printf("%f", c);
system("PAUSE");
}