用c语音编写程序,打印输出9种基本逻辑运算符(与、或、非、条件、双条件、异或、与非、或非、条件否定)的运算表到字符文件logic.txt中。
时间: 2023-10-01 16:06:03 浏览: 66
以下是基本逻辑运算符的运算表:
```
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("logic.txt", "w");
int a, b;
fprintf(fp, " | AND | OR | NOT | IF | IFF | XOR | NAND| NOR | NIF |\n");
fprintf(fp, "----+-----+-----+-----+-----+-----+-----+-----+-----+-----+\n");
for (a = 0; a <= 1; a++) {
for (b = 0; b <= 1; b++) {
fprintf(fp, " %d%d | %d | %d | %d | %d | %d | %d | %d | %d | %d |\n", a, b,
a && b, a || b, !a, (!a || b), (a == b), a ^ b, !(a && b), !(a || b), !(a == b));
}
}
fclose(fp);
return 0;
}
```
运行程序后,会在当前目录下生成一个名为logic.txt的文件,该文件中包含了基本逻辑运算符的运算表。
相关问题
c语音编写程序,打印输出9种基本逻辑运算符(与、或、非、条件、双条件、异或、与非、或非、条件否定)的运算表到字符文件logic.txt中。
```
#include <stdio.h>
int main() {
FILE* fp = fopen("logic.txt", "w"); // 打开文件
if (fp == NULL) {
printf("文件打开失败\n");
return 1;
}
fprintf(fp, "与运算表\n");
fprintf(fp, "a\tb\ta && b\n");
fprintf(fp, "0\t0\t%d\n", 0 && 0);
fprintf(fp, "0\t1\t%d\n", 0 && 1);
fprintf(fp, "1\t0\t%d\n", 1 && 0);
fprintf(fp, "1\t1\t%d\n", 1 && 1);
fprintf(fp, "\n");
fprintf(fp, "或运算表\n");
fprintf(fp, "a\tb\ta || b\n");
fprintf(fp, "0\t0\t%d\n", 0 || 0);
fprintf(fp, "0\t1\t%d\n", 0 || 1);
fprintf(fp, "1\t0\t%d\n", 1 || 0);
fprintf(fp, "1\t1\t%d\n", 1 || 1);
fprintf(fp, "\n");
fprintf(fp, "非运算表\n");
fprintf(fp, "a\t!a\n");
fprintf(fp, "0\t%d\n", !0);
fprintf(fp, "1\t%d\n", !1);
fprintf(fp, "\n");
fprintf(fp, "条件运算表\n");
fprintf(fp, "a\tb\ta ? b : 0\n");
fprintf(fp, "0\t0\t%d\n", 0 ? 0 : 0);
fprintf(fp, "0\t1\t%d\n", 0 ? 1 : 0);
fprintf(fp, "1\t0\t%d\n", 1 ? 0 : 0);
fprintf(fp, "1\t1\t%d\n", 1 ? 1 : 0);
fprintf(fp, "\n");
fprintf(fp, "双条件运算表\n");
fprintf(fp, "a\tb\ta ? b : !b\n");
fprintf(fp, "0\t0\t%d\n", 0 ? 0 : !0);
fprintf(fp, "0\t1\t%d\n", 0 ? 1 : !1);
fprintf(fp, "1\t0\t%d\n", 1 ? 0 : !0);
fprintf(fp, "1\t1\t%d\n", 1 ? 1 : !1);
fprintf(fp, "\n");
fprintf(fp, "异或运算表\n");
fprintf(fp, "a\tb\ta ^ b\n");
fprintf(fp, "0\t0\t%d\n", 0 ^ 0);
fprintf(fp, "0\t1\t%d\n", 0 ^ 1);
fprintf(fp, "1\t0\t%d\n", 1 ^ 0);
fprintf(fp, "1\t1\t%d\n", 1 ^ 1);
fprintf(fp, "\n");
fprintf(fp, "与非运算表\n");
fprintf(fp, "a\tb\t!(a && b)\n");
fprintf(fp, "0\t0\t%d\n", !(0 && 0));
fprintf(fp, "0\t1\t%d\n", !(0 && 1));
fprintf(fp, "1\t0\t%d\n", !(1 && 0));
fprintf(fp, "1\t1\t%d\n", !(1 && 1));
fprintf(fp, "\n");
fprintf(fp, "或非运算表\n");
fprintf(fp, "a\tb\t!(a || b)\n");
fprintf(fp, "0\t0\t%d\n", !(0 || 0));
fprintf(fp, "0\t1\t%d\n", !(0 || 1));
fprintf(fp, "1\t0\t%d\n", !(1 || 0));
fprintf(fp, "1\t1\t%d\n", !(1 || 1));
fprintf(fp, "\n");
fprintf(fp, "条件否定运算表\n");
fprintf(fp, "a\tb\t!(a ? b : 0)\n");
fprintf(fp, "0\t0\t%d\n", !(0 ? 0 : 0));
fprintf(fp, "0\t1\t%d\n", !(0 ? 1 : 0));
fprintf(fp, "1\t0\t%d\n", !(1 ? 0 : 0));
fprintf(fp, "1\t1\t%d\n", !(1 ? 1 : 0));
fprintf(fp, "\n");
fclose(fp); // 关闭文件
printf("运算表已输出到logic.txt中\n");
return 0;
}
```
阅读全文