0% found this document useful (0 votes)
35 views3 pages

Stdafx H

This C++ program asks the user to input a student's score, validates that the input is a valid number between 0 and 10 with at most one decimal place, then parses and calculates the numeric value of the score and prints it. It uses a do-while loop to repeatedly get input until a valid number is entered. It then uses a series of if/else statements and a switch to iterate through each character, decrementing i and adding the correct value to a based on the character, to convert the string input to a float. Finally it prints the float value to 5 decimal places.

Uploaded by

shakarin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

Stdafx H

This C++ program asks the user to input a student's score, validates that the input is a valid number between 0 and 10 with at most one decimal place, then parses and calculates the numeric value of the score and prints it. It uses a do-while loop to repeatedly get input until a valid number is entered. It then uses a series of if/else statements and a switch to iterate through each character, decrementing i and adding the correct value to a based on the character, to convert the string input to a float. Finally it prints the float value to 5 decimal places.

Uploaded by

shakarin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include "stdafx.

h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
float a;
int i,j,t1,t2;
char s[100];
do
{
a=0;
do
{
printf("nhap diem sinh vien :");
flushall();
gets(s);
t1=0;
t2=0;
for(i=0;i<strlen(s);i++)
{
if(s[i]!='0'&& s[i]!='1'&&
s[i]!='2'&&s[i]!='3'&&s[i]!='4'&&s[i]!='5'&&s[i]!='6'&&s[i]!='7'&&s[i]!='8'&&s[i]!='9'&&s
[i]!='.'&&s[i]!=',')
t1++;
if(s[i]=='.'||s[i]==',')
t2++;
}
}
while(t1!=0||t2>1);
for(i=0;i<strlen(s);i++)
{
if(s[i]=='.'||s[i]==',')
break;
}
t1=i;
if(i==strlen(s))
{
for(j=0;j<strlen(s);j++)
{
switch(s[j])
{
case'0':
--i;
break;
case '1':
a=a+pow(10.0,--i);
break;
case '2':
a=a+2*pow(10.0,--i);
break;
case '3':
a=a+3*pow(10.0,--i);
break;
case '4':
a=a+4*pow(10.0,--i);
break;
case '5':
a=a+5*pow(10.0,--i);
break;
case '6':
a=a+6*pow(10.0,--i);
break;
case '7':
a=a+7*pow(10.0,--i);
break;
case '8':
a=a+8*pow(10.0,--i);
break;
case '9':
a=a+9*pow(10.0,--i);
break;
}
}
}
else
{
for(j=0;j<strlen(s);j++)
{
switch(s[j])
{
case'0':
--i;
break;
case '1':
a=a+pow(10.0,--i);
break;
case '2':
a=a+2*pow(10.0,--i);
break;
case '3':
a=a+3*pow(10.0,--i);
break;
case '4':
a=a+4*pow(10.0,--i);
break;
case '5':
a=a+5*pow(10.0,--i);
break;
case '6':
a=a+6*pow(10.0,--i);
break;
case '7':
a=a+7*pow(10.0,--i);
break;
case '8':
a=a+8*pow(10.0,--i);
break;
case '9':
a=a+9*pow(10.0,--i);
break;
default:
break;
}
}
}
}
while(a<0||a>10);
printf("%.5f",a);
getch();
return 0;
}

You might also like