在上实验课,然后teacher要求写题目,题目挺简单
#include<stdio.h>
#include<math.h>
#include<stdio.h>
#include<iostream>
//using namespace std;
void t5()
{
for (int i = 9; i > 0; i--)
{
if (i<=5)
{
for (int temp = 5-i; temp > 0; temp--)
{
printf_s(" ");
}
for (int temp=i*2-1; temp > 0; temp--)
{
printf_s("0 ");
}
}
else
{
for (int temp = i - 5; temp > 0; temp--)
{
printf_s(" ");
}
for (int temp=(10-i)*2-1; temp > 0; temp--)
{
printf_s("0 ");
}
}
printf_s("\n");
}
return 0;
}
void t4()
{
for (int x = 1; x <= 9; x++)
{
for (int y = 1; y <= x; y++)
{
int temp = x * y;
printf_s("%d * %d = %d ", x, y, temp);
}
printf_s("\n");
}
return 0;
}
void t3()
{
int x = 0, y = 0, z = 0;
printf_s("please input 30个字符或者随你吧\n");
char c = getchar();
while ((c = getchar()) != '\n')
{
if (c >= '0' && c <= '9')
{
x++;
}
else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
{
y++;
}
else
{
z++;
}
}
printf_s("数字有%d,字母有%d,其余有%d\n", x, y, z);
return 0;
}
void t2()
{
int n;
double sum = 0;
printf_s("please input n\n");
scanf_s("%d", &n);
double a =0;
int i = 0;
for (i ; i <= n; i++)
{
a = 1.0 / (i * i + 1);
if (i % 2 == 0)
{
printf_s("%.4lf + ", a);
sum = sum + a;
}
else
{
printf_s("%.4lf - ", a);
sum = sum - a;
}
}
printf_s("0 =%.4lf\n", sum);
return 0;
}
void t1()
{
int s = 5;
int a, b = 0;
while (s > 0)
{
printf_s("选择菜单\n");
printf_s(" 1 求两个数的和\n");
printf_s(" 2 求两个数的差\n");
printf_s(" 3 求两个数的商和余数\n");
printf_s(" 4 求两个数的倒数之和\n");
printf_s(" 0 退出系统\n");
scanf_s("%d", &s);
if (s <= 4 && s >= 1)
{
printf_s("input 2 number a b\n");
scanf_s("%d%d", &a, &b);
if (s == 1)
{
printf_s("a+b=%d\n", a + b);
printf_s("input any key you can return");
system("pause");
}
else if (s == 2)
{
printf_s("a-b=%d\n", a - b);
printf_s("input any key you can return");
system("pause");
}
else if (s == 3)
{
printf_s("a/b=%.2lf\n", 1.0 * a / b);
printf_s("余数为%d\n", a - (a / b) * b);
printf_s("input any key you can return");
system("pause");
}
else if (s == 4)
{
double m = 1.0 / a + 1.0 / b;
printf_s("1/a+1/b=%.2lf\n", m);
printf_s("input any key you can return");
system("pause");
}
}
}
return 0;
}
int main()
{
int s = 0;
int sm = 1;
while (sm != 0)
{
printf_s("please input the 题目 you want to out put 1-5\n");
scanf_s("%d",&s);
if (s == 5)
{
t5();
}
else if (s == 4)
{
t4();
}
else if (s == 3)
{
t3();
}
else if (s == 2)
{
t2();
}
else if (s == 1)
{
t1();
}
printf_s("do you 想要再来一次?1/0\n");
scanf_s("%d", &sm);
}
system("pause");
return 0;
}
其余的运行都没问题,但是t1和t2都直接跳过了自定义函数
我尝试过把t1和t2的函数单独拿出来测试,都没问题可以运行
在t1和t2函数的开头清空一下缓存,添加如下语句:rewind(stdin); (或者fflush(stdin) ,但是fflush(stdin)在vs2015版本后已经被废弃)。t1和t2函数修改如下:(t3/t4/t5函数最好也在开始清空一下缓存)
void t2()
{
int n;
double sum = 0;
rewind(stdin);
printf_s("please input n\n");
scanf_s("%d", &n);
double a = 0;
int i = 0;
for (i; i <= n; i++)
{
a = 1.0 / (i * i + 1);
if (i % 2 == 0)
{
printf_s("%.4lf + ", a);
sum = sum + a;
}
else
{
printf_s("%.4lf - ", a);
sum = sum - a;
}
}
printf_s("0 =%.4lf\n", sum);
return ;
}
void t1()
{
int s = 5;
int a, b = 0;
rewind(stdin);
while (s > 0)
{
printf_s("选择菜单\n");
printf_s(" 1 求两个数的和\n");
printf_s(" 2 求两个数的差\n");
printf_s(" 3 求两个数的商和余数\n");
printf_s(" 4 求两个数的倒数之和\n");
printf_s(" 0 退出系统\n");
scanf_s("%d", &s);
if (s <= 4 && s >= 1)
{
printf_s("input 2 number a b\n");
scanf_s("%d%d", &a, &b);
if (s == 1)
{
printf_s("a+b=%d\n", a + b);
printf_s("input any key you can return");
system("pause");
}
else if (s == 2)
{
printf_s("a-b=%d\n", a - b);
printf_s("input any key you can return");
system("pause");
}
else if (s == 3)
{
printf_s("a/b=%.2lf\n", 1.0 * a / b);
printf_s("余数为%d\n", a - (a / b) * b);
printf_s("input any key you can return");
system("pause");
}
else if (s == 4)
{
double m = 1.0 / a + 1.0 / b;
printf_s("1/a+1/b=%.2lf\n", m);
printf_s("input any key you can return");
system("pause");
}
}
}
return ;
}