Professional Documents
Culture Documents
The Lecture For Turbo C
The Lecture For Turbo C
Turbo C
David Van Schumacher
BUPT-QM 07B05
Friday, December 28, 2007
Section A
STRINGS FUNCTIONS
Fundamental functions
• Input • Output
– scanf – printf
– getchar – putchar
– gets – puts
Grammar
• scanf(“input format”,&variety name);
• gets(variety name);
• variety name = getchar();
What’s string in turbo C
• It’s NOT a string anymore.
• It is seemed as a series of
characters, which is a character
array.
• The number of the character array is
just the same as the number of
letters of the word or sentence plus
one.
• The one more element is “\0” which
is printed as “\n” by gets and printed
as “”(NULL) by other words.
Library Functions
<string.h>
• strcat(string1,string2)
– Appends string2 to the end of string1
– Usage: Create the name of data file.
• strlen(string)
– Returns the length of string. Doesn’t
include the “\0”!!!!!!!!!
– Usage: Determine whether a input data
is valid for your program.
Library Functions
<string.h>
• strcpy(string1,string2)
– Copies string2 to string1, including the
“\0”
• strncpy(string1,string2,n)
– Copies at most n characters of string2 to
string1. If string2 has fewer than n
characters, it pads string1 with “\0”s.
– Usage: Used for Create a short file name
if a string is too long.
Library Functions
<string.h>
• strcmp(string1,string2)
– Compares string1 to string2. Returns a
negative integer if string1<string2. 0 if
string1==string2. positive integer if
string1>string2.
• strncmp(string1,string2,n)
– Compares the first n letters of string1 and
string2 return the value as function
“strcmp”.
• Usage: We can use it for comparing if
your program work properly when you
call some data file.
Library Functions
<ctype.h>
• isalpha (determine whether is it a
letter)
• isupper (determine whether is it
uppercase)
• islower (determine whether is it
lowercase)
• isdigit (determine whether is it a
number)
• ispunct (determine whether is it a
punctuation)
• toupper (change lowercase to
uppercase)
Usage
• To make your name of data file more
clearly.
• And we can use it for determine
whether a input in valid.
Library Functions
<stdlib.h>
• atoi
– Converts an ASCII string to an integer.
Conversion stops at the first noninteger
character.
• atof
– Converts an ASCII string to a double-
precision number. Conversion stops at the
first character that cannot be interpreted
as a double.
• itoa
– Converts an integer to an ASCII string.
– Format: itoa(number,string,n)
Section B
• sscanf
• Please see page 372 of the book
3.Declare a character array
4.Grammar: sscanf(character array
name, “format”, varieties to store
the integer of the character)
Solution Ⅰ
char x[40],a[4][10];
int i,j,b[4];
printf("\n\nPlease input four
words:");
gets(x);
sscanf(x,"%s %s %s %s",
a[0],a[1],a[2],a[3]);
for(i=3;i>-1;i--) printf("%s
",a[i]);
Solution Ⅰ
for(i=0;i<4;i++) b[i]=strlen(a[i])-1;
for(i=0;i<4;i++)
{
for(j=b[i];j>=0;j--)
printf("%c",a[i][j]);printf(" ");
}
printf("\n");
for(i=3;i>=0;i--)
{
for(j=b[i];j>=0;j--)
printf("%c",a[i][j]);printf(" ");
}
WAY Ⅱ
• Save as a string.
2.Use Gets to receive the input and
check whether is it valid for your
program.
3.If that is valid, do the next step.
Solution Ⅱ
int i,j;
char str[4][11];
printf("Please insert four strings :\n");
for(i=0;i<4;i++)
scanf("%s",&str[i]);
printf("The four strings you have just
input are:\n");
for(i=0;i<4;i++)
printf(" %s",str[i]);
printf("\n");
Solution Ⅱ
for(i=3;i>=0;i--) for(i=3;i>=0;i--)
printf(" %s",str[i]); {
printf("\n"); for(j=6;j>=0;j--)
{
for(i=0;i<4;i++)
{ printf("%c",str[i][j])
for(j=6;j>=0;j--)
;
{
}
printf("%c",str[i][j] }
); printf("\n");
} return 0;
}
Way Ⅲ
• Use the sentence
• while((c = getchar()) &&
“conditions”)
Solution Ⅲ
• We omit it here. Because it isn’t good
at dealing with strings
Summarize
• Use the method of coordinate can
deal with the strings character by
character.
Section C
INPUT VALIDATION
Exercise
• Write a C program that reads several
positive numbers and uses the
function round_to_nearest to round
each of these numbers to the nearest
integer. The program should print
both the original number and the
rounded number.
Answer
int main()
{
float a;
void round_to_nearest(float);
printf("Please input a real number:");
scanf("%f",&a);
round_to_nearest(a);
return 0;
}
Answer
void round_to_nearest(float a)
{
int z;
float x;
z=a;x=a-z;
if(x>=0.5) z++;
printf("\nthe original number is:%f",a);
printf("\nthe rounded number
is:%d",z);
}
Question
• What will happen if I enter
“AB.CD”???
FIND HELP
Need help?
• Don’t go to the teacher or Zhangfan
or me.
• Try to think about it by yourself.
• If you need more ,help please try to
use the help in turbo C.
Let me show some
examples
• Study time functions.
Let me show some
examples
• Study time functions.
A constant
Time functions
• In fact, the faction clock() doesn’t
give the program a real time. What
the function give to the program can
be seemed as a “time number”.
• The constant CLK_TCK can turn the
difference of “time number” to real
time. The CLK_TCK equals to 18.200
Constant
Let me show some
examples
• Study random functions
Let me show some
examples
• Study random functions
b e o mitted
e n t e n c e c an
o s o , th is s Can be placed by
If y o u d
time(NULL)
Let me show some
examples
• Study random functions
rand
• The random function rand can only
offer some fixed random number.
• If you run the same program for
several times, the random numbers
offered by rand() will always be the
same numbers.
Example
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i;
for(i=1;i<=10;i++)
printf ("number %d:%d\n",i,rand());
printf ("\n");
return 0;
}
Output
• Source code
• Game
• TT
WHITE PROGRAM
Note
• check {} ()
• use space properly
• made the structure of your program
more clearly.
• How to name your variety
The End