搜集了网上C语言入门案例,并适当的做了一些修改和注释。一共65的案例,推荐按Ctrl+F输入关键词快速定位案例。想要原代码的,拉到最低,点击链接下载。
例1. 使用 printf() 输出 “Hello, World!”。
#include
#include //包含system()函数//
int main()
{
printf("Hello, World!\n");
system("pause"); //仿真时,按任意键推出应用程序,方便仿真查看结果//
return 0;
}
仿真结果:
例2. 使用 printf() 与 %d 格式化输出整数。
#include
#include //包含system()函数//
int main()
{
int number;
printf("输入一个整数: ");
scanf("%d", &number); //通过键盘给程序中的变量赋值//
printf("你输入的整数是: %d\n", number);
system("pause"); //仿真时,按任意键推出应用程序,方便仿真查看结果//
return 0;
}
仿真结果:
例3. 使用 printf() 与 %c 格式化输出一个字符。
#include
#include //包含system()函数//
int main()
{
char c; // 声明一个字符串变量C//
printf("输入一个字符(仅限1个字母): ");
scanf("%c", &c); //通过键盘给程序中的变量赋值//
printf("输入的字符为 %c\n", c);
system("pause"); //仿真时,按任意键推出应用程序,方便仿真查看结果//
return 0;
}
仿真结果:
例4. 使用 printf() 与 %f 输出浮点数。
#include
#include
int main()
{
float f; // 声明浮点数变量
printf("输入一个浮点数: ");
scanf("%f", &f); //通过键盘给程序中的变量赋值//
printf("输入的浮点数为 %f\n", f);
system("pause"); //仿真时,按任意键推出应用程序,方便仿真查看结果//
return 0;
}
仿真结果:
例5. 使用 printf() 与 %le 输出双精度数。
#include
#include
int main()
{
double d; // 声明双精度变量
printf("输入一个双精度数: ");
scanf("%le", &d); //通过键盘给程序中的变量赋值//
printf("输入的双精度数为 %le\n", d);
system("pause"); //仿真时,按任意键推出应用程序,方便仿真查看结果//
return 0;
}
仿真结果:
例6. 使用 scanf() 来接收输入, printf() 与 %d 格式化输出整数,实现两数相加。
#include
#include
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
printf("输入两个数(以空格分割): ");
scanf("%d %d", &firstNumber, &secondNumber); // 通过 scanf() 函数接收用户输入的两个整数
sumOfTwoNumbers = firstNumber + secondNumber;
printf("%d + %d = %d\n", firstNumber, secondNumber, sumOfTwoNumbers);
system("pause");
return 0;
}
#include
#include
int main()
{
double firstNumber, secondNumber, product;
printf("输入两个浮点数: ");
scanf("%lf %lf", &firstNumber, &secondNumber);
product = firstNumber * secondNumber;
printf("%lf * %lf = %.2lf\n", firstNumber, secondNumber,product); // 输出结果, %.2lf 保留两个小数点
system("pause");
return 0;
}
#include
#include
int main()
{
char c;
printf("输入一个字符: ");
scanf("%c", &c);
printf("%c 的 ASCII 为 %d\n", c, c);
system("pause");
return 0;
}
仿真结果:
附ASCII表
例9. 两数相除,如果有余数,输出余数。
#include
#include
int main()
{
int dividend, divisor, quotient, remainder;//4个变量分别是被除数、除数、商和余数//
printf("输入被除数: ");
scanf("%d", ÷nd);
printf("输入除数: ");
scanf("%d", &divisor);
quotient = dividend / divisor; //运算符/用来计算商//
remainder = dividend % divisor; //运算符%用来计算余数//
printf("商 = %d\n", quotient);
printf("余数 = %d\n", remainder);
system("pause");
return 0;
}
#include
#include
int main()
{
double a, b;
printf("输入第一个数:");
scanf("%le",&a);
printf("输入第二个数:");
scanf("%le",&b);
if(a > b)
printf("%le 大于 %le\n",a,b);
else
printf("%le 小于等于 %le\n",a,b);
system("pause");
return 0;
}
#include
#include
int main()
{
int a, b, c;
printf("输入第一个整数:");
scanf("%d",&a);
printf("输入第二个整数:");
scanf("%d",&b);
printf("输入第三个整数:");
scanf("%d",&c);
if ( a > b && a > c )
printf("%d 最大\n", a);
else if ( b > a && b > c )
printf("%d 最大\n", b);
else if ( c > a && c > b )
printf("%d 最大\n", c);
else
printf("有两个或三个数值相等\n");
system("pause");
return 0;
}
仿真结果:
例11. 计算 int, float, double 和 char 字节大小
#include
#include
int main()
{
int integerType; //整数类型//
float floatType; //浮点型//
double doubleType; //双精度型//
char charType; //字符型//
printf("Size of int: %ld bytes\n",sizeof(integerType)); //sizeof()函数用于计算变量的大小//
printf("Size of float: %ld bytes\n",sizeof(floatType));
printf("Size of double: %ld bytes\n",sizeof(doubleType));
printf("Size of char: %ld byte\n",sizeof(charType));
system("pause");
return 0;
}
#include
#include
int main()
{
printf("第一种方法,采用临时变量\n");
double firstNumber, secondNumber, temporaryVariable;
printf("输入第一个数字: ");
scanf("%lf", &firstNumber);
printf("输入第二个数字: ");
scanf("%lf",&secondNumber);
temporaryVariable = firstNumber;
firstNumber = secondNumber;
secondNumber = temporaryVariable;
printf("交换后, firstNumber = %.2lf\n", firstNumber);
printf("交换后, secondNumber = %.2lf\n", secondNumber);
system("pause");
printf("第二种方法,不采用临时变量\n");
printf("交换前 - \n第一个数字 = %lf, 第二个数字 = %lf \n", firstNumber, secondNumber);
firstNumber = firstNumber + secondNumber; // firstNumber的变量为两数之和,secondNumber未改变
secondNumber = firstNumber - secondNumber;
firstNumber = firstNumber - secondNumber;
printf("交换后 - \n第一个数字 = %lf, 第二个数字 = %lf \n", firstNumber, secondNumber);
system("pause");
return 0;
}
#include
#include
int main()
{
int number;
printf("请输入一个整数: ");
scanf("%d", &number);
if(number % 2 == 0)
printf("%d 是偶数\n", number);
else
printf("%d 是奇数\n", number);
system("pause");
return 0;
}
#include
#include
int main()
{
int i;
printf("100以内的偶数:");
for(i = 1; i <= 100; i++)
{
if(i%2 == 0)
printf("%2d ", i); //%2d表示将数字按宽度为2,采用右对齐方式输出//
}
printf("\n100以内的奇数:");
for(i = 1; i <= 100; i++) {
if(i%2 != 0)
printf("%2d ", i);
}
printf("\n");
system("pause");
return 0;
}
仿真结果:
例15. 判断元音辅音。
知识点:英语有26个字母,元音只包括 a、e、i、o、u 这五个字母,其余的都为辅音。y是半元音、半辅音字母,但在英语中都把他当作辅音。
#include
#include
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;
printf("输入一个字母: ");
scanf("%c",&c);
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');//小写字母,值为1时说明是元音//
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');//大写字母,值为1时说明是元音//
if (isLowercaseVowel || isUppercaseVowel)
printf("%c 是元音\n", c);
else
printf("%c 是辅音\n", c);
system("pause");
return 0;
}
#include
#include
int main()
{
double n1, n2, n3;
printf("请输入三个数,以空格分隔: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if( n1>=n2 && n1>=n3 )
printf("%.2f 是最大数\n", n1);
if( n2>=n1 && n2>=n3 )
printf("%.2f 是最大数\n", n2);
if( n3>=n1 && n3>=n2 )
printf("%.2f 是最大数\n", n3);
system("pause");
return 0;
}
仿真结果:
例17. 求一元二次方程:ax2+bx+c=0 的根。
#include
#include
#include
int main()
{
float a,b,c,x1,x2,d;
printf("求公式ax^2+bx+c=0的根\n");
printf("输入方程的第一个系数:a=");
scanf("%f",&a);
printf("输入方程的第二个系数:b=");
scanf("%f",&b);
printf("输入方程的第三个系数:c=");
scanf("%f",&c);
if(a!=0)
{
if(b*b-4*a*c>=0)
{
d=sqrt(b*b-4*a*c);
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
if(x1<x2)
printf("%0.2f %0.2f\n",x2,x1);
else
printf("%0.2f %0.2f\n",x1,x2);
}
else
printf("输入值有问题!\n");
}
system("pause");
return 0;
}
#include
#include
int main()
{
int year;
printf("输入年份: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0) // 能被4,100,400整除,则是闰年//
printf("%d 是闰年\n", year);
else
printf("%d 不是闰年\n", year);
}
else
printf("%d 是闰年\n", year ); //能被4整除,不能被100整除,是闰年//
}
else
printf("%d 不是闰年\n", year);
system("pause");
return 0;
}
#include
#include
int main()
{
double number;
printf("输入一个数字: ");
scanf("%lf", &number);
if (number <= 0.0)
{
if (number == 0.0)
printf("你输入的是 0\n");
else
printf("你输入的是负数\n");
}
else
printf("你输入的是正数\n");
system("pause");
return 0;
}
#include
#include
int main()
{
char c;
printf("输入一个字符: ");
scanf("%c",&c);
if( (c>='a' && c<='z') || (c>='A' && c<='Z')) //a~z的ASCII区间是97~122,A~Z的ASCII区间是65~90//
printf("是字母\n");
else
printf("不是字母\n");
system("pause");
return 0;
}
#include
#include
int addNumbers(int n); //定义一个子函数//
int main()
{
int n, i, sum = 0;
printf("第一种方法,使用for\n");
printf("输入一个正整数,确定从1加到这个正整数: ");
scanf("%d",&n);
for(i=1; i <= n; ++i)
{
sum += i; // sum = sum+i;
}
printf("Sum = %d\n",sum);
sum=0; //sum清零//
printf("第二种方法,使用while\n");
printf("输入一个正整数,确定从1加到这个正整数: ");
scanf("%d",&n);
i = 1;
while ( i <=n )
{
sum += i;
++i;
}
printf("Sum = %d\n",sum);
printf("第三种方法,使用递归\n");
printf("输入一个正整数,确定从1加到这个正整数: ");
scanf("%d", &n);
printf("Sum = %d\n",addNumbers(n));
system("pause");
return 0;
}
int addNumbers(int m)
{
if(m != 0)
return m + addNumbers(m-1);
else
return m;
}
#include
#include
int main()
{
int i = 0;
int j = 0;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++){
printf("%dx%d=%d\t",j,i,i*j); //\t表示水平制表(HT) (跳到下一个TAB位置)//
}
printf("\n");
}
system("pause");
}
仿真结果:
例23. 斐波那契数列。
知识点:斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89… 这个数列从第3项开始,每一项都等于前两项之和。
#include
#include
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("方法一,使用for\n");
printf("从1开始,输出几项: ");
scanf("%d", &n);
printf("斐波那契数列: ");
for (i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf("\n");
system("pause");
printf("方法二,使用while\n");
t1 = 0, t2 = 1, nextTerm=0;
printf("输入,最大值不超过: ");
scanf("%d", &n);
printf("斐波那契数列: %d, %d, ", t1, t2);
nextTerm = t1 + t2;
while(nextTerm <= n)
{
printf("%d, ",nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
printf("\n");
system("pause");
return 0;
}
#include
#include
int hcf(int n1, int n2); //为方案四服务//
int main()
{
int n1, n2, i, gcd;
printf("方案一,使用for和if\n");
printf("输入两个正整数,以空格分隔: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
if(n1%i==0 && n2%i==0)
gcd = i;
}
printf("%d 和 %d 的最大公约数是 %d\n", n1, n2, gcd);
printf("方案二,使用while和if\n");
printf("输入两个数,以空格分隔: ");
scanf("%d %d",&n1,&n2);
while(n1!=n2)
{
if(n1 > n2)
n1 -= n2; //-=表示把左边的数减去右边的数的差赋值给左边的数//
else
n2 -= n1;
}
printf("最大公约数是 %d\n",n1);
printf("方案三,包含正数和负数时\n");
printf("输入两个数,以空格分隔: ");
scanf("%d %d",&n1,&n2);
n1 = ( n1 > 0) ? n1 : -n1; //当n1>0时,n1=n1否则n1=-n1
n2 = ( n2 > 0) ? n2 : -n2; //当n2>0时,n2=n2否则n2=-n2
while(n1!=n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("最大公约数是 %d\n",n1);
printf("方案四,使用递归方法\n");
printf("输入两个正整数: ");
scanf("%d %d", &n1, &n2);
printf("%d 和 %d 的最大公约数为 %d\n", n1, n2, hcf(n1,n2));
system("pause");
return 0;
}
int hcf(int n1, int n2)
{
if (n2 != 0)
return hcf(n2, n1%n2);
else
return n1;
}
#include
#include
int main()
{
printf("方案一,使用while和if\n");
int n1, n2, minMultiple;
printf("输入两个正整数:");
scanf("%d %d", &n1, &n2);
minMultiple = (n1>n2) ? n1 : n2;
while(1)
{
if( minMultiple%n1==0 && minMultiple%n2==0 )
{
printf("%d 和 %d 的最小公倍数为 %d\n", n1, n2,minMultiple);
break;
}
++minMultiple;
}
printf("方案二,通过最大公约数计算\n");
int i,gcd,lcm;
printf("输入两个正整数: ");
scanf("%d %d",&n1,&n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
if(n1%i==0 && n2%i==0)
gcd = i;
}
lcm = (n1*n2)/gcd;
printf("%d 和 %d 的最小公倍数为 %d\n", n1, n2, lcm);
system("pause");
return 0;
}
仿真结果:
例26. 阶乘。
知识点:一个正整数的阶乘是所有小于及等于该数的正整数的积,并且0的阶乘为1。自然数n的阶乘写作n!。
n!=1×2×3×…×n。阶乘亦可以递归方式定义:0!=1,n!=(n-1)!×n。
#include
#include
long int multiplyNumbers(int n); //为方案二服务//
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("方案一:使用for\n");
printf("输入一个整数: ");
scanf("%d",&n);
if (n < 0)
printf("Error! 负数没有阶乘\n");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i;
}
printf("%d! = %llu\n", n, factorial);
}
system("pause");
printf("方案二,使用递归\n");
printf("输入一个整数: ");
scanf("%d", &n);
printf("%d! = %ld\n", n, multiplyNumbers(n));
system("pause");
return 0;
}
long int multiplyNumbers(int n)
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}
#include
#include
int main()
{
char c;
for(c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
printf("\n");
system("pause");
printf("考虑大小写方案\n");
printf("输入 u 显示大写字母,输入 l 显示小写字母: ");
scanf("%c", &c);
if(c== 'U' || c== 'u')
{
for(c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
}
else if (c == 'L' || c == 'l')
{
for(c = 'a'; c <= 'z'; ++c)
printf("%c ", c);
}
else
printf("Error! 输入非法字符");
printf("\n");
system("pause");
return 0;
}
#include
#include
int main()
{
long long n;
int count = 0;
printf("输入一个整数: ");
scanf("%lld", &n); //lld是long long整型//
while(n != 0)
{
n /= 10;
++count;
}
printf("数字是 %d 位数\n", count);
system("pause");
}
#include
#include
#include
int power(int n1, int n2);
int main()
{
int base1, exponent1;
long long result1 = 1;
printf("方案一,使用while\n");
printf("基数: ");
scanf("%d", &base1);
printf("指数: ");
scanf("%d", &exponent1);
while (exponent1 != 0)
{
result1 *= base1;
--exponent1;
}
printf("结果:%lld\n", result1);
printf("方案二,使用pow()函数\n");
double base2, exponent2, result2;
printf("基数: ");
scanf("%lf", &base2);
printf("指数: ");
scanf("%lf", &exponent2);
result2 = pow(base2, exponent2);
printf("%.1lf^%.1lf = %.2lf\n", base2, exponent2, result2);
system("pause");
printf("方案三,使用递归\n");
int base3, powerRaised3, result3;
printf("基数: ");
scanf("%d",&base3);
printf("指数(正整数): ");
scanf("%d",&powerRaised3);
result3 = power(base3, powerRaised3);
printf("%d^%d = %d\n", base3, powerRaised3, result3);
system("pause");
return 0;
}
int power(int base, int powerRaised)
{
if (powerRaised != 0)
return (base*power(base, powerRaised-1));
else
return 1;
}
仿真结果:
例30. 判断回文数。
知识点:设n是一任意自然数。若将n的各位数字反向排列所得自然数n1与n相等,则称n为一回文数。例如,若n=1234321,则称n为一回文数;但若n=1234567,则n不是回文数。
#include
#include
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("输入一个整数:");
scanf("%d", &n);
originalInteger = n;
while( n!=0 ) //while函数实现翻转功能//
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
if (originalInteger == reversedInteger)
printf("%d 是回文数\n", originalInteger);
else
printf("%d 不是回文数\n", originalInteger);
system("pause");
return 0;
}
仿真结果:
例31. 判断素数。
知识点:质数(prime number)又称素数,有无限个。质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数,这样的数称为质数。
#include
#include
int checkPrimeNumber(int n);
int main()
{
int n, i, flag = 0;
printf("判断一个正整数是否为素数\n");
printf("输入一个正整数: ");
scanf("%d",&n);
for(i=2; i<=n/2; ++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d 是素数\n",n);
else
printf("%d 不是素数\n",n);
printf("判断两个数之间的素数\n");
int low, high;
flag=0;
printf("输入两个整数: ");
scanf("%d %d", &low, &high);
printf("%d 与 %d 之间的素数为: ", low, high);
while (low < high)
{
flag = 0;
for(i = 2; i <= low/2; ++i)
{
if(low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", low);
++low;
}
printf("\n");
printf("使用函数判断两数间的素数\n");
int n1,n2;
flag=0;
printf("输入两个正整数: ");
scanf("%d %d", &n1, &n2);
printf("%d 和 %d 间的素数为: ", n1, n2);
for(i=n1+1; i<n2; ++i)
{
flag = checkPrimeNumber(i);
if(flag == 1)
printf("%d ",i);
}
if (i<=n1+1)
printf("无");
printf("\n");
system("pause");
return 0;
}
int checkPrimeNumber(int n)
{
int j, flag = 1;
for(j=2; j <= n/2; ++j)
{
if (n%j == 0)
{
flag =0;
break;
}
}
return flag;
}
仿真结果:
例32. 判断Armstrong数(阿姆斯壮数)。
Armstrong 数,就是n位数的各位数的n次方之和等于该数。
#include
#include
#include
int checkPrimeNumber(int n);
int checkArmstrongNumber(int n);
int main()
{
printf("判断Armstrong数\n");
int number, originalNumber;
double remainder;
double result = 0;
printf("输入四位数: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder*remainder;
originalNumber /= 10;
}
if(result == number)
printf("%d 是 Armstrong 数\n",number);
else
printf("%d 不是 Armstrong 数\n",number);
system("pause");
printf("寻找两数之间的Armstrong数\n");
int low, high, i, temp1, temp2, n = 0;
result = 0;
printf("输入两个整数: ");
scanf("%d %d", &low, &high);
printf("%d 和 %d 之间的 Armstrong 数为: ", low, high);
for(i = low + 1; i < high; ++i)
{
temp2 = i;
temp1 = i;
// 计算
while (temp1 != 0) //先计算是几位数//
{
temp1 /= 10;
++n;
}
while (temp2 != 0) //计算Armstrong 数//
{
remainder = temp2 % 10;
result += pow(remainder, n);
temp2 /= 10;
}
if (result == i)
{
printf("%d ", i);
}
n = 0;
result = 0;
}
printf("\n");
system("pause");
printf("使用函数判断Armstrong 数\n");
int flag;
printf("输入正整数: ");
scanf("%d", &n);
flag = checkPrimeNumber(n);
if (flag == 1)
printf("%d 是素数。\n", n);
else
printf("%d 不是素数\n", n);
flag = checkArmstrongNumber(n);
if (flag == 1)
printf("%d 是 Armstrong 数\n", n);
else
printf("%d 不是 Armstrong 数\n",n);
system("pause");
return 0;
}
int checkPrimeNumber(int n)
{
int i,flag = 1;
for(i=2; i<=n/2; ++i)
{
if(n%i == 0)
{
flag = 0;
break;
}
}
return flag;
}
int checkArmstrongNumber(int number)
{
int originalNumber, n = 0, flag;
double remainder, result=0;
originalNumber = number;
while (originalNumber != 0)
{
originalNumber /= 10;
++n;
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += pow(remainder, n);
originalNumber /= 10;
}
if(result == number)
flag = 1;
else
flag = 0;
return flag;
}
仿真结果:
例33. 求一个数的因数。
知识点:假如a*b=c(a、b、c都是整数),那么我们称a和b就是c的因数。
#include
#include
int main()
{
int number, i;
printf("输入一个整数: ");
scanf("%d",&number);
printf("%d 的因数有: ", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",i);
}
}
printf("\n");
system("pause");
return 0;
}
#include
#include
int main()
{
int i, j, rows;
printf("使用*号创建三角形\n");
printf("行数: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
printf("使用数字创建三角形\n");
printf("行数: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
printf("使用字母创建三角形\n");
char input, alphabet = 'A';
printf("输入大写字母:");
scanf("%s",&input);
for(i=1; i <= (input-'A'+1); ++i)
{
for(j=1;j<=i;++j)
{
printf("%c", alphabet);
}
++alphabet;
printf("\n");
}
printf("使用*号创建倒三角形\n");
printf("行数: ");
scanf("%d",&rows);
for(i=rows; i>=1; --i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
printf("使用数字创建倒三角形\n");
printf("行数: ");
scanf("%d",&rows);
for(i=rows; i>=1; --i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
printf("使用*号创建金字塔\n");
int space, k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i, k=0)
{
for(space=1; space<=rows-i; ++space)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
printf("使用数字创建金字塔\n");
int count = 0, count1 = 0;
k=0;
printf("行数: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(space=1; space <= rows-i; ++space)
{
printf(" ");
++count;
}
while(k != 2*i-1)
{
if (count <= rows-1)
{
printf("%d ", i+k);
++count;
}
else
{
++count1;
printf("%d ", (i+k-2*count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
printf("使用*号创建倒金字塔\n");
printf("行数: ");
scanf("%d",&rows);
for(i=rows; i>=1; --i)
{
for(space=0; space < rows-i; ++space)
printf(" ");
for(j=i; j <= 2*i-1; ++j)
printf("* ");
for(j=0; j < i-1; ++j)
printf("* ");
printf("\n");
}
printf("创建杨辉三角\n");
int coef = 1;
printf("行数: ");
scanf("%d",&rows);
for(i=0; i<rows; i++)
{
for(space=1; space <= rows-i; space++)
printf(" ");
for(j=0; j <= i; j++)
{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;
printf("%4d", coef);
}
printf("\n");
}
printf("弗洛伊德三角形\n");
int l,N;
printf("行数: ");
scanf("%d",&N);
for(i=1,j=1;i<=N;i++)
{
for(l=1;l<=i;l++,j++)
printf("%5d",j);
printf("\n");
}
system("pause");
return 0;
}
#include
#include
int main()
{
printf("将 1~100 的数据以 10x10 矩阵格式输出\n");
int i, j, count;
for(i = 1; i <= 10; i++) {
for(j = i; j <=100; j += 10 )
printf(" %3d", j);
printf("\n");
}
system("pause");
printf("等差数列输出 10x10 矩阵格式\n");
int start, end;
start = 2, end = 10;
for(i = start; i <= end; i++)
{
count = i;
for(j = 1; j <= 10; j++) {
printf(" %3d", count*j);
}
printf("\n");
}
system("pause");
printf("乘法格式运算\n");
int n = 3;
j = 1;
for(i = n; i <= (n*10); i+=n) {
printf("%3d x %2d = %3d\n", n, j, i);
j++;
}
system("pause");
return 0;
}
# include
# include
int main()
{
char operate;
double firstNumber,secondNumber;
printf("输入操作符 (+, -, *, /): ");
scanf("%c", &operate);
printf("输入两个数字: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operate) //switch case函数//
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber - secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber / secondNumber);
break;
default:
printf("Error! operator is not correct");
}
printf("\n");
system("pause");
return 0;
}
#include
#include
int checkPrime(int n);
int main()
{
int n, i, flag = 0;
printf("输入正整数: ");
scanf("%d", &n);
for(i = 2; i <= n/2; ++i)
{
if (checkPrime(i) == 1)
{
if (checkPrime(n-i) == 1)
{
printf("%d = %d + %d", n, i, n - i);
flag = 1;
}
}
}
if (flag == 0)
printf("%d 不能分解为两个素数。", n);
printf("\n");
system("pause");
return 0;
}
int checkPrime(int n) //判断一个数是否为素数//
{
int i, isPrime = 1;
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
{
isPrime = 0;
break;
}
}
return isPrime;
}
#include
#include
#include
int convertBinaryToDecimal(long long n1);
long long convertDecimalToBinary(int n2);
int main()
{
printf("先进行二进制转十进制\n");
long long n1;
int n2;
printf("输入一个二进制数: ");
scanf("%lld", &n1);
printf("二进制数 %lld 转换为十进制为 %d\n", n1, convertBinaryToDecimal(n1));
system("pause");
printf("下面进行十进制转二进制\n");
printf("输入一个十进制数: ");
scanf("%d", &n2);
printf("十进制数 %d 转换为二进制位 %lld\n", n2, convertDecimalToBinary(n2));
system("pause");
return 0;
}
int convertBinaryToDecimal(long long n1)
{
int decimalNumber1 = 0, remainder1;
double i1=0;
while (n1!=0)
{
remainder1 = n1%10;
n1 /= 10;
decimalNumber1 += remainder1*pow(2,i1);
++i1;
}
return decimalNumber1;
}
long long convertDecimalToBinary(int n2)
{
long long binaryNumber2 = 0;
int remainder2, i2 = 1, step = 1;
while (n2!=0)
{
remainder2 = n2%2;
printf("Step %d: %d/2, 余数 = %d, 商 = %d\n", step++, n2, remainder2, n2/2);
n2 /= 2;
binaryNumber2 += remainder2*i2;
i2 *= 10;
}
return binaryNumber2;
}
#include
#include
void reverseSentence();
int main()
{
printf("输入一个字符串: \n");
reverseSentence();
printf("\n");
system("pause");
return 0;
}
void reverseSentence()
{
char c;
scanf("%c", &c);
if( c != '\n')
{
reverseSentence();
printf("%c",c);
}
}
#include
#include
int main()
{
printf("计算1,2,3,4,5,6,7,8,9,0十个数字的平均值\n");
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop;
float sum, avg;
sum = avg = 0.0;
for(loop = 0; loop < 10; loop++) {
sum = sum + array[loop];
}
avg = (float)sum / loop;
printf("平均值为 %.2f\n", avg);
system("pause");
printf("接下来设计用户自定义输入数字取平均值\n");
int n, i;
float num[100], average;
sum = 0.0;
printf("输入元素个数: ");
scanf("%d", &n);
while (n > 100 || n <= 0)
{
printf("Error! 数字需要在1 到 100 之间。\n");
printf("再次输入: ");
scanf("%d", &n);
}
for(i = 0; i < n; ++i)
{
printf("%d. 输入数字: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
average = sum / n;
printf("平均值 = %.2f\n", average);
system("pause");
return 0;
}
#include
#include
int main()
{
printf("使用 for 循环正向输出数组\n");
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop;
for(loop = 0; loop < 10; loop++)
printf("%d ", array[loop]);
printf("\n使用 for 循环逆向输出数组\n");
for(loop = 9; loop >= 0; loop--)
printf("%d ", array[loop]);
printf("\n");
system("pause");
return 0;
}
#include
#include
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum, loop;
sum = 0;
for(loop = 9; loop >= 0; loop--) {
sum = sum + array[loop];
}
printf("元素和为:%d\n", sum);
system("pause");
return 0;
}
#include
#include
int main()
{
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop, largest;
largest = array[0];
for(loop = 1; loop < 10; loop++) {
if( largest < array[loop] )
largest = array[loop];
}
printf("最大元素为 %d\n", largest);
printf("接下来,进行用户自定义输入数组找最大值\n");
system("pause");
int i, n;
float arr[100];
printf("输入元素个数(0~100): ");
scanf("%d", &n);
for(i = 0; i < n; ++i)
{
printf("输入数字 %d: ", i+1);
scanf("%f", &arr[i]);
}
for(i = 1; i < n; ++i)
{
if(arr[0] < arr[i])
arr[0] = arr[i];
}
printf("最大元素为 = %.2f\n", arr[0]);
system("pause");
return 0;
}
#include
#include
int main()
{
printf("将一个数组拆分为两个数组,一个为奇数数组,一个为偶数数组。\n");
printf("被拆分数组为:0, 1, 2, 3, 4, 5, 6, 7, 8, 9\n");
int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int even[10], odd[10];
int loop, e, d;
e = d = 0;
for(loop = 0; loop < 10; loop++) {
if(array[loop]%2 == 0) {
even[e] = array[loop];
e++;
}else {
odd[d] = array[loop];
d++;
}
}
printf("原始数组 -> ");
for(loop = 0; loop < 10; loop++)
printf(" %d", array[loop]);
printf("\n偶数 -> ");
for(loop = 0; loop < e; loop++)
printf(" %d", even[loop]);
printf("\n奇数 -> ");
for(loop = 0; loop < d; loop++)
printf(" %d", odd[loop]);
printf("\n接下来,将奇数数组与偶数数组合并为一个数组:\n");
system("pause");
int emptyarray[10];
int index, e_len, o_len;
e_len = o_len = 5;
index = 0;
for(loop = 0; loop < e_len; loop++) {
array[index] = even[loop];
index++;
}
for(loop = 0; loop < o_len; loop++) {
array[index] = odd[loop];
index++;
}
printf("偶数 -> ");
for(loop = 0; loop < e_len; loop++)
printf(" %d", even[loop]);
printf("\n奇数 -> ");
for(loop = 0; loop < o_len; loop++)
printf(" %d", odd[loop]);
printf("\n合并后 -> ");
for(loop = 0; loop < 10; loop++)
printf(" %d", array[loop]);
system("pause");
return 0;
}
#include
#include
int main()
{
printf("将一个数组复制到另一个数组\n");
printf("原数组:1,2,3,4,5,6,7,8,9,0\n");
system("pause");
int original[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int copied[10];
int loop;
for(loop = 0; loop < 10; loop++) {
copied[loop] = original[loop];
}
printf("元素数组 -> 拷贝后的数组 \n");
for(loop = 0; loop < 10; loop++) {
printf(" %2d %2d\n", original[loop], copied[loop]);
}
system("pause");
return 0;
}
#include
#include
#include
float calculateSD(float data[]);
int main()
{
int i;
float data[10];
printf("输入10个元素: ");
for(i=0; i < 10; ++i)
scanf("%f", &data[i]);
printf("标准偏差 = %.6f\n", calculateSD(data));
system("pause");
return 0;
}
float calculateSD(float data[])
{
float sum = 0.0, mean, standardDeviation = 0.0;
int i;
for(i=0; i<10; ++i)
{
sum += data[i];
}
mean = sum/10;
for(i=0; i<10; ++i)
standardDeviation += pow(data[i] - mean, 2);
return sqrt(standardDeviation/10);
}
#include
#include
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("输入行数 ( 1 ~ 100): ");
scanf("%d", &r);
printf("输入列数 ( 1 ~ 100): ");
scanf("%d", &c);
printf("输入第一维数组的元素:\n");
for(i=0; i<r; ++i) //第i行//
for(j=0; j<c; ++j) //第j列//
{
printf("输入元素 a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\n第一维数组为: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",a[i][j]);
if(j==c-1)
{
printf("\n\n");
}
}
printf("输入第二维数组的元素:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("输入元素 a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]);
}
printf("\n第二维数组为: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",b[i][j]);
if(j==c-1)
{
printf("\n\n");
}
}
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
printf("二维数组相加结果: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
{
printf("\n\n");
}
}
system("pause");
return 0;
}
#include
#include
int main()
{
struct Matrixs //定义一个结构体//
{
int elements[100][100];
int row;
int col;
}matrixA, matrixB, matrixC;
int m, k, n;
printf("请输入矩阵A行数和列数:");
scanf("%d %d", &matrixA.row, &matrixA.col);
matrixB.row = matrixA.col; //矩阵A的列数等于矩阵B的行数//
printf("请输入矩阵B列数(矩阵B行数等于矩阵A的列数):");
scanf("%d", &matrixB.col);
printf("\n输入矩阵A:\n");
for (m = 0; m < matrixA.row; ++m) //矩阵A行数//
for (k = 0; k < matrixA.col; ++k) //矩阵A列数//
{
printf("请输入元素A(%d,%d):", m+1, k+1);
scanf("%d", &matrixA.elements[m][k]);
}
printf("\n输入矩阵B:\n");
for (k = 0; k < matrixA.col; ++k) //矩阵B行数//
for (n = 0; n < matrixB.col; ++n) //矩阵B列数//
{
printf("请输入元素B(%d,%d):", k+1, n+1);
scanf("%d", &matrixB.elements[k][n]);
}
for (m = 0; m < matrixA.row; ++m)
for (n = 0; n < matrixB.col; ++n)
for (k = 0; k < matrixA.col; ++k)
{
if (k == 0) matrixC.elements[m][n] = 0;//为新矩阵C每个元素初始化//
matrixC.elements[m][n] += matrixA.elements[m][k] * matrixB.elements[k][n];
}
printf("\n\n矩阵A为:\n\n");
for (m = 0; m < matrixA.row; ++m)
{
printf(" |");
for (k = 0; k < matrixA.col; ++k)
{
printf(" %-5d", matrixA.elements[m][k]);
}
printf("\b\b\b|\n");
}
printf("\n\n矩阵B为:\n\n");
for (k = 0; k < matrixB.row; ++k)
{
printf(" |");
for (n = 0; n < matrixB.col; ++n)
{
printf(" %-6d", matrixB.elements[k][n]);
}
printf("\b\b|\n");
}
printf("\n矩阵A左乘矩阵B为:\n\n");
for (m = 0; m < matrixA.row; ++m)
{
printf(" |");
for (n = 0; n < matrixB.col; ++n)
{
printf(" %-6d",matrixC.elements[m][n]);
}
printf("\b\b|\n");
}
printf("\n");
system("pause");
return 0;
}
#include
#include
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("输入矩阵的行与列: ");
scanf("%d %d", &r, &c);
printf("输入矩阵元素:\n");
for(i=0; i<r; ++i) //行//
for(j=0; j<c; ++j) //列//
{
printf("输入元素 a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
printf("\n输入矩阵: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}
//转职//
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
printf("\n转换后矩阵:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
printf("\n\n");
}
system("pause");
return 0;
}
#include
#include
int main()
{
int data[5], i;
printf("输入5个元素,以逗号分割: ");
for(i = 0; i < 5; ++i)
scanf("%d", data + i);//scanf("%d",data+i)等于scanf("%d",&data[i])功能//
printf("你输入的是: \n");
for(i = 0; i < 5; ++i)
printf("%d\n", *(data + i));
system("pause");
return 0;
}
#include
#include
void cyclicSwap(int *a,int *b,int *c);
int main()
{
printf("a、b、c 三个变量,通过引用按顺序循环替换他们的值\n");
int a, b, c;
printf("输入 a, b 和 c 的值(以空格分隔): ");
scanf("%d %d %d",&a,&b,&c);
printf("交换前:\n");
printf("a = %d \nb = %d \nc = %d\n",a,b,c);
cyclicSwap(&a, &b, &c);
printf("交换后:\n");
printf("a = %d \nb = %d \nc = %d",a, b, c);
printf("\n");
system("pause");
return 0;
}
void cyclicSwap(int *a,int *b,int *c)
{
int temp;
temp = *b;
*b = *a;
*a = *c;
*c = temp;
}
#include
#include
int main()
{
int i, num;
float *data;
printf("输入元素个数(1 ~ 100): ");
scanf("%d", &num);
data = (float*) calloc(num, sizeof(float)); //分配num个float大小的内存//
if(data == NULL)
{
printf("Error!!! 内存没有分配\n");
system("pause");
exit(0);
}
for(i = 0; i < num; ++i)
{
printf("输入数字 %d: ", i + 1);
scanf("%f", &data[i]); //也可以写成scanf("%f",data+i);//
}
for(i = 1; i < num; ++i)
{
if(*data < *(data + i))
*data = *(data + i);
}
printf("最大元素 = %.2f", *data);
printf("\n");
system("pause");
return 0;
}
#include
#include
int main()
{
char line[150];
int i, j;
printf("输入一个字符串: ");
fgets(line, (sizeof line / sizeof line[0]), stdin);
for(i = 0; line[i] != '\0'; ++i)
{
//'\0'就是运行到字符串结尾时结束//
while (!( (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || line[i] == '\0') )
{
//当第i个字符不为字母时才运行while循环里的内容//
for(j = i; line[j] != '\0'; ++j)
{
line[j] = line[j+1];
}
line[j] = '\0';
}
}
printf("输出: ");
puts(line);//puts()函数用来向标准输出设备(屏幕)输出字符串并换行//
system("pause");
return 0;
}
#include
#include
int main()
{
char s1[100], s2[100], i, j;
printf("输入第一个字符串: ");
scanf("%s", s1);
printf("输入第二个字符串: ");
scanf("%s", s2);
for(i = 0; s1[i] != '\0'; ++i);
for(j = 0; s2[j] != '\0'; ++j, ++i)
{
s1[i] = s2[j];
}
s1[i] = '\0';
printf("连接后: %s\n", s1);
system("pause");
return 0;
}
#include
#include
#include
int main()
{
printf("使用strlen()函数\n");
char s[1000];
int len,i;
printf("输入一个字符串: ");
scanf("%s", s);
len = strlen(s);
printf("字符串长度: %d\n", len);
printf("接下来,不使用strlen()函数\n");
printf("输入一个字符串: ");
scanf("%s", s);
for(i = 0; s[i] != '\0'; ++i);
printf("字符串长度: %d\n", i);
system("pause");
return 0;
}
#include
#include
int main()
{
char str[1000], ch ;
int i, frequency = 0,strlen=0,str0len=0;
printf("输入字符串: ");
fgets(str, (sizeof str / sizeof str[0]), stdin);
strlen =sizeof str;
str0len=sizeof str[0];
printf("共有字节数str为:%d,\n一个元素str[0]占字节总数为:%d\n",strlen, str0len);
printf("输入要查找的字符: ");
scanf("%c",&ch);
for(i = 0; str[i] != '\0'; ++i)
{
if(ch == str[i])
++frequency;
}
printf("字符 %c 在字符串中出现的次数为 %d\n", ch, frequency);
system("pause");
return 0;
}
#include
#include
int main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces = 0;
printf("输入一个字符串: ");
scanf("%[^\n]", line);
for(i=0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U') //找元音个数//
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z')) //找大小写字母个数//
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9') //找数字个数//
{
++digits;
}
else if (line[i]==' ') //读空白的次数//
{
++spaces;
}
}
printf("元音: %d",vowels);
printf("\n辅音: %d",consonants);
printf("\n数字: %d",digits);
printf("\n空白符: %d\n", spaces);
system("pause");
return 0;
}
#include
#include
#include
int main()
{
printf("使用 strcpy()函数\n");
char src[40];
char dest[100];
memset(dest, '\0', sizeof(dest)); //将某一块内存中的内容全部设置为指定的值//
strcpy(src, "This is my money!"); //把含有'\0'结束符的字符串复制到另一个地址空间//
strcpy(dest, src);
printf("最终的目标字符串: %s\n", dest);
system("pause");
printf("接下来,不使用strcpy()函数\n");
char s1[100], s2[100], i;
printf("字符串 s1: ");
scanf("%s",s1);
for(i = 0; s1[i] != '\0'; ++i)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("字符串 s2: %s\n", s2);
system("pause");
return(0);
}
#include
#include
#include
int main()
{
int i, j;
char str[6][50], temp[25];
printf("输入6个英文单词(单个单词最大字母不能超过25):\n");
for(i=0; i<6; ++i)
{
scanf("%s[^\n]",str[i]); //接收字符串但不接收\n//
}
for(i=0; i<5; ++i)
{
for(j=i+1; j<6 ; ++j)
{
if(strcmp(str[i], str[j])>0) //两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇'\0'为止//
{
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
printf("排序后: \n");
for(i=0; i<6; ++i)
{
puts(str[i]);
}
system("pause");
return 0;
}
#include
#include
struct players
{
char name[30];
int num;
float height;
float weight;
}p;
int main()
{
printf("输入信息:\n");
printf("名字: ");
scanf("%s", p.name);
printf("编号: ");
scanf("%d", &p.num);
printf("身高: ");
scanf("%f", &p.height);
printf("体重: ");
scanf("%f", &p.weight);
printf("显示信息:\n");
printf("名字: ");
puts(p.name);
printf("编号: %d\n",p.num);
printf("身高: %.1f\n", p.height);
system("pause");
return 0;
}
#include
#include
typedef struct complex
{
float real;
float imag;
} complex;
complex add(complex n1,complex n2);
int main()
{
complex n1, n2, temp; //三个一样的结构体//
printf("第一个复数 \n");
printf("输入实部和虚部:\n");
scanf("%f %f", &n1.real, &n1.imag);
printf("第二个复数 \n");
printf("输入实部和虚部:\n");
scanf("%f %f", &n2.real, &n2.imag);
temp = add(n1, n2);
printf("Sum = %.1f + %.1fi\n", temp.real, temp.imag);
system("pause");
return 0;
}
complex add(complex n1, complex n2)
{
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return(temp);
}
#include
#include
struct TIME
{
int seconds;
int minutes;
int hours;
};
//*diff是因为运行被调用函数时可改变主调用函数变量的值//
void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);
int main()
{
struct TIME startTime, stopTime, diff;
printf("输入开始时间: \n");
printf("输入小时、分钟、秒:");
scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
printf("输入停止时间: \n");
printf("输入小时、分钟、秒: ");
scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
differenceBetweenTimePeriod(startTime, stopTime, &diff);
printf("差值: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
system("pause");
return 0;
}
void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
{
if(stop.seconds > start.seconds)
{
--start.minutes;
start.seconds += 60;
}
diff->seconds = start.seconds - stop.seconds;
if(stop.minutes > start.minutes){
--start.hours;
start.minutes += 60;
}
diff->minutes = start.minutes - stop.minutes;
diff->hours = start.hours - stop.hours;
}
#include
#include
int main()
{
char sentence[1000];
FILE *fptr;
fptr = fopen("mybook.txt", "w"); //文件指针名=fopen(文件名,使用文件方式)//
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("输入字符串:\n");
fgets(sentence, (sizeof sentence / sizeof sentence[0]), stdin);
fprintf(fptr,"%s", sentence); //fprintf()函数根据指定的格式(format),向输出流(stream)写入数据(argument)//
fclose(fptr);
system("pause");
return 0;
}
#include
#include
int main()
{
char c[1000];
FILE *fptr;
if ((fptr = fopen("mybook.txt", "r")) == NULL)
{
printf("Error! opening file");
system("pause");
exit(1);
}
fscanf(fptr,"%[^\n]", c);
printf("读取内容:\n%s", c);
fclose(fptr);
printf("\n");
system("pause");
return 0;
}
#include
#include
int main() {
FILE *fp;
char c;
fp = fopen(__FILE__,"r");
do {
c = getc(fp); //从流中取字符//
putchar(c); //该函数将指定的表达式的值所对应的字符输出到标准输出终端上//
}
while(c != EOF);
fclose(fp);
system("pause");
return 0;
}