Application en langage C pour le calcul des moyennes
Application en langage C pour le calcul des moyennes
Cet article propose en détaille une application en langage C pour le calcul des moyennes.
Vous pourrez télécharger le fichier au format zip il contient le code source complet.
On peut choisir ses propres coefficients (de 1 à 99), sauvegarder les notes, charger des tableaux de notes, on peut également afficher la médiane et l'étendue des notes. L'application permet aussi de faire un screenshot de la fenêtre.
Extrait du code source :
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <richedit.h>
#include <commctrl.h>
#include "define.h"
//Variables globales et prototypes
HINSTANCE hinst;
void Refresh();
void Draw(BOOL calcul);
int __stdcall HwndToBmpFile(HWND hwnd,char *pszflname);
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
BOOL APIENTRY Dialog1Proc1(HWND,UINT,WPARAM,LPARAM);
BOOL APIENTRY Dialog1Proc2(HWND,UINT,WPARAM,LPARAM);
BOOL APIENTRY ProcCoefficients(HWND,UINT,WPARAM,LPARAM);
static HBRUSH ColorStatic(HWND,WPARAM,LPARAM);
int nbritem=0;
char nbritem2[100];
float afficheMoy=0;
int calcul=1;
int popup=0;
HANDLE fo,fp;
CHAR szFile[MAX_PATH]={0};
HWND hwnd,texte,ok,edit,annuler,recommencer,texte_background,list,combo,background,box,box2,goback,enregistrer,imprimer,
check,coeff05,coeff1,coeff2,coeffautre,coeff,note,about,charger,coeffBAC,hTTip;
//Déclaration de la fenêtre
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {
//Variables
MSG msg;
hinst=hinstance;
//Paramètres de la fenêtre
WNDCLASS wc;
wc.style=0;
wc.lpfnWndProc=MainWndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hinstance;
wc.hIcon=LoadIcon(hinstance,MAKEINTRESOURCE(1));
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)(COLOR_3DFACE+1);
wc.lpszMenuName= NULL;
wc.lpszClassName="MaWinClass";
if(!RegisterClass(&wc)) return FALSE;
//Création de la fenêtre
int cxS=GetSystemMetrics(SM_CXSCREEN);
int cyS=GetSystemMetrics(SM_CYSCREEN);
hwnd=CreateWindowEx(WS_EX_WINDOWEDGE,"MaWinClass","Aucune valeur",WS_CAPTION|WS_MINIMIZEBOX|WS_SYSMENU,(cxS-700)/2,(cyS-275)/2,320,275,HWND_DESKTOP,NULL,hinstance,NULL);
//Mise en place des contrôles
box=CreateWindowEx(0,"BUTTON","Aucune valeur",SS_LEFT|WS_CHILD|WS_VISIBLE|BS_GROUPBOX,10,10,280,225,hwnd,(HMENU)ID_BOX,hinstance,NULL);
SendMessage(box,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
box2=CreateWindowEx(0,"BUTTON","Propriétés :",SS_LEFT|WS_CHILD|WS_VISIBLE|BS_GROUPBOX,90,242,200,58,hwnd,(HMENU)ID_BOX2,hinstance,NULL);
SendMessage(box2,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
edit=CreateWindowEx(WS_EX_STATICEDGE|WS_EX_CLIENTEDGE,"EDIT","12.5",WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_EX_STATICEDGE,110,40,55,20,hwnd,(HMENU)ID_EDIT,hinstance,NULL);
SendMessage(edit,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
texte_background=CreateWindowEx(WS_EX_STATICEDGE,"STATIC","",SS_LEFT|WS_CHILD|WS_VISIBLE,110,150,170,74,hwnd,(HMENU)ID_TEXTE_BACKGROUND,hinstance,NULL);
SendMessage(texte_background,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
texte=CreateWindowEx(0,"STATIC","Astuce : Pensez à ramener vos notes sur 20 afin que les coefficients (s'ils sont activés) soient proportionnels.",WS_CHILD|WS_VISIBLE,120,160,150,60,hwnd,(HMENU)ID_TEXTE,hinstance,NULL);
SendMessage(texte,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
ok=CreateWindowEx(0,"BUTTON","OK",BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,170,40,30,20,hwnd,(HMENU)ID_OK,hinstance,NULL);
SendMessage(ok,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
annuler=CreateWindowEx(0,"BUTTON","Annuler",BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE|WS_DISABLED,110,70,90,20,hwnd,(HMENU)ID_ANNULER,hinstance,NULL);
SendMessage(annuler,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
recommencer=CreateWindowEx(0,"BUTTON","Recommencer",BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE|WS_DISABLED,110,90,90,20,hwnd,(HMENU)ID_RECOMMENCER,hinstance,NULL);
SendMessage(recommencer,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
list=CreateWindowEx(WS_EX_STATICEDGE,"LISTBOX",NULL,WS_CHILD|WS_VISIBLE|WS_VSCROLL,20,40,80,200,hwnd,(HMENU)ID_LIST,hinstance,NULL);
SendMessage(list,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
combo=CreateWindowEx(0,"COMBOBOX",NULL,WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,110,120,170,0,hwnd,(HMENU)ID_COMBO,hinstance,NULL);
SendMessage(combo,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
goback=CreateWindowEx(WS_EX_STATICEDGE,"BUTTON",">",BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,295,35,15,40,hwnd,(HMENU)ID_GOBACK,hinstance,NULL);
SendMessage(goback,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
enregistrer=CreateWindowEx(0,"BUTTON","Enregistrer",BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,95,255,90,20,hwnd,(HMENU)ID_ENREGISTRER,hinstance,NULL);
SendMessage(enregistrer,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
imprimer=CreateWindowEx(0,"BUTTON","Impression Ecran",BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,195,255,90,20,hwnd,(HMENU)ID_IMPRIMER,hinstance,NULL);
SendMessage(imprimer,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
charger=CreateWindowEx(0,"BUTTON","Charger...",BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,95,275,90,20,hwnd,(HMENU)ID_CHARGER,hinstance,NULL);
SendMessage(charger,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
check=CreateWindowEx(0,"BUTTON","Afficher la grille",WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,195,275,89,20,hwnd,(HMENU)ID_CHECK,hinstance,NULL);
SendMessage(check,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
background=CreateWindowEx(WS_EX_STATICEDGE,"STATIC",NULL,SS_LEFT|WS_CHILD|WS_VISIBLE,205,40,75,70,hwnd,(HMENU)ID_BACKGROUND,hinstance,NULL);
SendMessage(background,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
coeff05=CreateWindowEx(0,"BUTTON","0.5",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,210,44,69,17,hwnd,(HMENU)ID_COEFF05,hinstance,NULL);
SendMessage(coeff05,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
coeff1=CreateWindowEx(0,"BUTTON","1",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,210,59,69,17,hwnd,(HMENU)ID_COEFF1,hinstance,NULL);
SendMessage(coeff1,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
coeff2=CreateWindowEx(0,"BUTTON","2",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,210,74,69,17,hwnd,(HMENU)ID_COEFF2,hinstance,NULL);
SendMessage(coeff2,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
coeffautre=CreateWindowEx(0,"BUTTON","autre...",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,210,89,69,17,hwnd,(HMENU)ID_COEFFAUTRE,hinstance,NULL);
SendMessage(coeffautre,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
note=CreateWindowEx(0,"STATIC","Note :",WS_CHILD|WS_VISIBLE,110,25,50,14,hwnd,(HMENU)ID_NOTE,hinstance,NULL);
SendMessage(note,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
coeff=CreateWindowEx(0,"STATIC","Coefficient :",WS_CHILD|WS_VISIBLE,205,25,60,14,hwnd,(HMENU)ID_COEFF,hinstance,NULL);
SendMessage(coeff,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
note=CreateWindowEx(0,"STATIC","Note (coefficient)",WS_CHILD|WS_VISIBLE,20,25,90,14,hwnd,(HMENU)ID_EXEMPLE,hinstance,NULL);
SendMessage(note,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
about=CreateWindowEx(WS_EX_STATICEDGE,"BUTTON","A propos...",BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,10,274,70,22,hwnd,(HMENU)ID_ABOUT,hinstance,NULL);
SendMessage(about,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
coeffBAC=CreateWindowEx(0,"BUTTON","Coeffs BAC",BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,10,255,70,20,hwnd,(HMENU)ID_BAC,hinstance,NULL);
SendMessage(coeffBAC,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE,0));
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)"Moyenne (avec coefficients)");
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)"Moyenne (sans coefficients)");
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)"Nombre de notes >= 10");
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)"Nombre de notes < 10");
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)"Etendue (différence max - min)");
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)"Médiane (avec coefficients)");
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)"Médiane (sans coefficients)");
SendMessage(combo,CB_SELECTSTRING,0,(LPARAM)"Moyenne (avec coefficients)");
CheckDlgButton(hwnd,ID_COEFF1,BST_CHECKED);
//Création des Tooltips
TOOLINFO ti;
RECT rect;
hTTip=CreateWindow(TOOLTIPS_CLASS,NULL,WS_POPUP|TTS_NOPREFIX,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,hwnd,NULL,hinstance,NULL);
GetClientRect(hwnd,&rect);
ti.cbSize=sizeof(TOOLINFO);
ti.uFlags=TTF_SUBCLASS;
GetClientRect(hwnd,&rect);
for(int a=1;a <= 137;a++) {
ti.hwnd=GetDlgItem(hwnd,a);
ti.uId=0;
if (a == ID_OK)
ti.lpszText="Ajouter la note à la liste";
else if (a == ID_ANNULER)
ti.lpszText="Annuler la dernière note";
else if (a == ID_RECOMMENCER)
ti.lpszText="SUpprimer toutes les notes";
else if (a == ID_LIST)
ti.lpszText="Liste des notes";
else if (a == ID_EDIT)
ti.lpszText="Tapez vos notes ici";
else if (a == ID_COMBO)
ti.lpszText="Interractions sur les notes";
else if (a == ID_GOBACK)
ti.lpszText="Afficher/cacher le graphe";
else if (a == ID_ENREGISTRER)
ti.lpszText="Enregistrer le tableau de notes actuel";
else if (a == ID_IMPRIMER)
ti.lpszText="Faire une 'impression-écran' de cette fenêtre (permet de sauvegarder le graphe)";
else if (a == ID_CHECK)
ti.lpszText="Afficher/cacher la grille sur le graphe";
else if (a == ID_COEFFAUTRE)
ti.lpszText="Choisir son propre coefficient (entre 1 et 99)";
else if (a == ID_ABOUT)
ti.lpszText="Afficher l'auteur du logiciel";
else if (a == ID_CHARGER)
ti.lpszText="Charger un tableau de notes";
ti.rect.left=rect.left;
ti.rect.top=rect.top;
ti.rect.right=rect.right;
ti.rect.bottom=rect.bottom;
SendMessage(hTTip,TTM_ADDTOOL,0,(LPARAM)&ti);
}
SetFocus(edit);
if (!hwnd) return FALSE;
ShowWindow(hwnd,nCmdShow);
while (GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//Traitement des messages Client-Machine
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CONTEXTMENU:
HMENU hmenu,hpopup;
hmenu=LoadMenu(hinst,"LEMENU");
hpopup=GetSubMenu(hmenu,0);
TrackPopupMenuEx(hpopup,0,LOWORD(lParam),HIWORD(lParam),hwnd,NULL);
DestroyMenu(hmenu);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case 111:
DialogBox(hinst,"Coefficients",hwnd,(DLGPROC)ProcCoefficients);
break;
case 112:
DestroyWindow(hwnd);
break;
case ID_BAC:
DialogBox(hinst,"Coefficients",hwnd,(DLGPROC)ProcCoefficients);
break;
case ID_COEFFAUTRE:
DialogBox(hinst,"DIALOG1",hwnd,(DLGPROC)Dialog1Proc1);
break;
case ID_GOBACK:
RECT area;
GetWindowRect(hwnd,&area);
int width=area.right-area.left;
if (width == 320) {
SetWindowPos(hwnd,0,area.left,area.top,800,350,0);
SetWindowText(goback,"<");
}
else {
SetWindowPos(hwnd,0,area.left,area.top,320,275,0);
SetWindowText(goback,">");
}
break;
case ID_COMBO:
nbritem=SendMessage(list,LB_GETCOUNT,0,0);
if (nbritem > 0) Refresh();
break;
case ID_ANNULER:
nbritem=SendMessage(list,LB_GETCOUNT,0,0)-1;
SendMessage(list,LB_DELETESTRING,nbritem,0);
Refresh();
break;
case ID_RECOMMENCER:
if (MessageBox(hwnd,"Etes-vous sûr(e) de vouloir annuler tous les calculs en cours ?\t\n",
"Avertissement",MB_ICONQUESTION|MB_OKCANCEL) == IDOK) {
while (SendMessage(list,LB_GETCOUNT,0,0)*2)
SendMessage(list,LB_DELETESTRING,0,0);
SetWindowText(edit,"");
SendMessage(combo,CB_SELECTSTRING,0,(LPARAM)"Moyenne (avec coefficients)");
CheckDlgButton(hwnd,ID_COEFF05,BST_UNCHECKED);
CheckDlgButton(hwnd,ID_COEFF2,BST_UNCHECKED);
CheckDlgButton(hwnd,ID_COEFFAUTRE,BST_UNCHECKED);
CheckDlgButton(hwnd,ID_COEFF1,BST_CHECKED);
SetWindowText(coeffautre,"autre...");
EnableWindow(recommencer,FALSE);
Refresh();
}
break;
case ID_CHECK:
Refresh();
InvalidateRect(hwnd,NULL,FALSE);
UpdateWindow(hwnd);
break;
case ID_OK:
{
char text[10];
GetWindowText(edit,text,10);
int text2=atoi(text);
for(int a=1;a <= 20;a++) {
if (text2 == a) {
int b=7;
while (b<=10) {
if (IsDlgButtonChecked(hwnd,b)) {
char textcoeff[5];
double verif=1.5;
GetWindowText(GetDlgItem(hwnd,b),textcoeff,10);
if (strchr(textcoeff,'a') != NULL) {
sscanf(textcoeff,"autre: %lf",&verif);
sprintf(textcoeff,"%lf",verif);
}