4. 用传统流程图解以下问题
4.1 有两个瓶子A和B,A放醋,B放酱油,要求换成A放酱油,B放醋。
图1 4.1流程图
代码:
#include "stdio.h"
#include "string.h"
void main() {
//char A[] = "醋";
char A[20] = "vinegar";
char B[20] = "soy sauce";
printf("bottle A is %s,bottle B is %s\n", A, B);
char C[20] = "";
strcpy(C, A);
strcpy(A, B);
strcpy(B, C);
printf("bottle A is %s,bottle B is %s\n", A, B);
}
输出结果:
4.2 依次将十个数输入,要求输入其中最大值。
图2 4.2流程图(判断语句向下是,向右否)
代码:
#include "stdio.h"
void main() {
int i = 0;
int max = 0;
int a = 0;
do
{
printf("please input a value:");
scanf("%d", &a);
if(a > max) max = a;
i++;
//printf("a = %d, i = %d\n", a, i);
} while (i < 10);
printf("the max value of your 10 input values is %d", max);
}
输出结果:
4.3 有3个数a、b、c,要求按从大到小的顺序输出。
图3 4.3流程图
代码:
#include "stdio.h"
void changeNumber(int *x, int *y);
void main() {
int a = 236;
int b =12;
int c = 1203;
int temp = 0;
if(a < b) {
changeNumber(&a, &b);
}
if(a < c) {
changeNumber(&a, &c);
}
if(b < c) {
changeNumber(&b, &c);
}
printf("output the 3 values from max to min:%d,%d,%d\n", a, b, c);
}
void changeNumber(int *x, int *y){
int temp;
temp = y[0];
y[0] = x[0];
x[0] = temp;
}
输出结果: