本文目录一览:
- 用C语言编写二进制数运算模拟程序
- 用C语言编写程序 提示用户输入一个数字并显示该数 使用字符模拟七段显示器效果
- 用c语言编写一个计算机程序
- 用c语言编写程序,谢谢!!
- 用C语言编写并调试一个模拟的进程调度程序,采用“简单时间片轮转法”调度算法对五个进程进行调度。
- 如何用C语言编写一个简单的程序!
用C语言编写二进制数运算模拟程序
用数组模拟就行。 思路:
- 将输入转化成二进制数,逆序存入数组中。
- 用两数组模拟四则运算,并将运算的结果存入第三个数组中。
- 逆序输出第三个数组。 希望对你有帮助!
用C语言编写程序 提示用户输入一个数字并显示该数 使用字符模拟七段显示器效果
#include stdio.h
#include string.h
#define MAX_DIGHTS 10
const int segments[10][7] = {
{1, 1, 1, 1, 1, 1, 0}, /* 0 */
{0, 1, 1, 0, 0, 0, 0}, /* 1 */
{1, 1, 0, 1, 1, 0, 1}, /* 2 */
{1, 1, 1, 1, 0, 0, 1}, /* 3 */
{0, 1, 1, 0, 0, 1, 1}, /* 4 */
{1, 0, 1, 1, 0, 1, 1}, /* 5 */
{1, 0, 1, 1, 1, 1, 1}, /* 6 */
{1, 1, 1, 0, 0, 0, 0}, /* 7 */
{1, 1, 1, 1, 1, 1, 1}, /* 8 */
{1, 1, 1, 1, 0, 1, 1} /* 9 */
};
char dights[3][MAX_DIGHTS * 4];
void clear_dight_array(void);
void process_dights_array(int dight, int position);
void print_dights_array(void);
int main(void) {
unsigned char numbers[MAX_DIGHTS];
int ch, i = 0, j;
clear_dight_array();
printf("Enter a number: ");
while ((ch = getchar()) != EOF && ch != '\n')
if (i < MAX_DIGHTS && ch >= '0' && ch <= '9')
process_dights_array(ch - '0', i++);
print_dights_array();
return 0;
}
void clear_dight_array(void) {
memset(dights, ' ', sizeof dights);
}
void process_dights_array(int dight, int position) {
int n = position * 4;
if (segments[dight][0])
dights[0][n + 1] = '_';
if (segments[dight][1])
dights[1][n + 2] = '|';
if (segments[dight][2])
dights[2][n + 2] = '|';
if (segments[dight][3])
dights[2][n + 1] = '_';
if (segments[dight][4])
dights[2][n] = '|';
if (segments[dight][5])
dights[1][n] = '|';
if (segments[dight][6])
dights[1][n + 1] = '_';
}
void print_dights_array(void) {
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < MAX_DIGHTS * 4; j++) {
putchar(dights[i][j]);
}
putchar('\n');
}
}
用c语言编写一个计算机程序
我对你提出的问题的题意的理解是编一个计算器程序。。。。。。。。。如果要是那样子的话我给出代码:
#include stdio.h
int main() {
char cp;
int a, b;
scanf("%d %c %d", &a, &cp, &b);
if (cp == '-') printf("%d", a - b);
else if (cp == '+') printf("%d", a + b);
else if (cp == '*') printf("%d", a * b);
else if (cp == '%') printf("%d", a % b);
return 0;
}
如果要知道这几个符号在机器中的实现机理的话:
- 和 - 不说了,* 就相当于多做几遍加法。而 % 是用位运算之类的方法进行运算的所以 % 的效率最低 不知道是不是你的编译器有问题我的程序运行起来是得15的 你是否正确输入了????
用c语言编写程序,谢谢!!
纯模拟,看上去有点复杂,可是一点算法都没有。。。
#include<iostream>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
if (((n % 4 == 0) && (n % 100 != 0)) || (n % 400 == 0)) {
cout << "Leap year,";
if (m == 1) cout << "Winter,31" << endl;
if (m == 2) cout << "Winter,29" << endl;
if (m == 3) cout << "Spring,31" << endl;
if (m == 4) cout << "Spring,30" << endl;
if (m == 5) cout << "Spring,31" << endl;
if (m == 6) cout << "Summer,30" << endl;
if (m == 7) cout << "Summer,31" << endl;
if (m == 8) cout << "Summer,31" << endl;
if (m == 9) cout << "Fall,30" << endl;
if (m == 10) cout << "Fall,31" << endl;
if (m == 11) cout << "Fall,30" << endl;
if (m == 12) cout << "Winter,31" << endl;
} else {
cout << "Common year,";
if (m == 1) cout << "Winter,31" << endl;
if (m == 2) cout << "Winter,28" << endl;
if (m == 3) cout << "Spring,31" << endl;
if (m == 4) cout << "Spring,30" << endl;
if (m == 5) cout << "Spring,31" << endl;
if (m == 6) cout << "Summer,30" << endl;
if (m == 7) cout << "Summer,31" << endl;
if (m == 8) cout << "Summer,31" << endl;
if (m == 9) cout << "Fall,30" << endl;
if (m == 10) cout << "Fall,31" << endl;
if (m == 11) cout << "Fall,30" << endl;
if (m == 12) cout << "Winter,31" << endl;
}
return 0;
}
望采纳。。。
用C语言编写并调试一个模拟的进程调度程序,采用“简单时间片轮转法”调度算法对五个进程进行调度。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct PCB {
char NAME[10]; /*进程名*/
int ROUND; /*进程轮转时间片*/
int REACHTIME; /*进程到达时间*/
int CPUTIME; /*进程占用CPU时间*/
int COUNT; /*计数器*/
int NEEDTIME; /*进程完成还要的CPU时间*/
char STATE; /*进程的状态*/
struct PCB *NEXT; /*链指针*/
};
struct LINK { /*PCB的链结构*/
struct PCB *RUN; /*当前运行进程指针*/
struct PCB *READY; /*就绪队列头指针*/
struct PCB *TAIL; /*就绪队列尾指针*/
struct PCB *FINISH; /*完成队列头指针*/
};
void INIT(LINK *);
void INSERT(LINK *);
void FIRSTIN(LINK *);
void PRINT(LINK *);
void PR(struct PCB *);
int CREATE(LINK *, int);
void ROUNDSCH(LINK *);
void main() {
LINK pcbs;
int i;
INIT(pcbs);
i = 0;
printf("创建5个进程\n\n");
while (i < 5) {
if (CREATE(&pcbs, i + 1) == 1) {
printf("进程已创建\n\n");
i++;
} else
printf("进程创建失败\n\n");
}
FIRSTIN(&pcbs);
ROUNDSCH(&pcbs);
}
void ROUNDSCH(LINK *p) {
struct PCB *pcb;
while (p->RUN != NULL) {
pcb = (struct PCB *)malloc(sizeof(struct PCB));
strcpy(pcb->NAME, p->RUN->NAME);
pcb->ROUND = p->RUN->ROUND;
pcb->REACHTIME = p->RUN->REACHTIME;
pcb->CPUTIME = p->RUN->CPUTIME;
pcb->COUNT = p->RUN->COUNT;
pcb->NEEDTIME = p->RUN->NEEDTIME;
pcb->STATE = p->RUN->STATE;
pcb->NEXT = p->RUN->NEXT;
pcb->CPUTIME++;
pcb->NEEDTIME--;
pcb->COUNT++;
if (pcb->NEEDTIME == 0) {
pcb->NEXT = p->FINISH->NEXT;
p->FINISH->NEXT = pcb;
pcb->STATE = 'F';
p->RUN = NULL;
if (p->READY != p->TAIL)
FIRSTIN(p);
} else {
p->RUN = pcb;
if (pcb->COUNT == pcb->ROUND) {
pcb->COUNT = 0;
if (p->READY != p->TAIL) {
pcb->STATE = 'W';
INSERT(p);
FIRSTIN(p);
}
}
}
PRINT(p);
}
}
void INIT(LINK *p) {
p->RUN = NULL;
p->TAIL = p->READY = (struct PCB *)malloc(sizeof(struct PCB));
p->READY->NEXT = NULL;
p->FINISH = (struct PCB *)malloc(sizeof(struct PCB));
p->FINISH->NEXT = NULL;
}
int CREATE(LINK *p, int n) {
struct PCB *pcb, *q;
pcb = (struct PCB *)malloc(sizeof(struct PCB));
flushall();
printf("请输入第%d个进程的名称:\n", n);
gets(pcb->NAME);
printf("请输入第%d个进程的轮转时间片数:\n", n);
scanf("%d", &pcb->ROUND);
printf("请输入第%d个进程的到达时间:\n", n);
scanf("%d", &pcb->REACHTIME);
pcb->CPUTIME = 0;
pcb->COUNT = 0;
printf("请输入第%d个进程需运行的时间片数:\n", n);
scanf("%d", &pcb->NEEDTIME);
pcb->STATE = 'W';
pcb->NEXT = NULL;
if (strcmp(pcb->NAME, "") == 0 || pcb->ROUND == 0 || pcb->NEEDTIME == 0) /*输入错误*/
return 0;
q = p->READY;
while (q->NEXT != NULL && q->NEXT->REACHTIME <= pcb->REACHTIME)
q = q->NEXT;
pcb->NEXT = q->NEXT;
q->NEXT = pcb;
if (pcb->NEXT == NULL)
p->TAIL = pcb;
return 1;
}
void FIRSTIN(LINK *p) {
struct PCB *q;
q = p->READY->NEXT;
p->READY->NEXT = q->NEXT;
q->NEXT = NULL;
if (p->READY->NEXT == NULL)
p->TAIL = p->READY;
q->STATE = 'R';
p->RUN = q;
}
void INSERT(LINK *p) {
struct PCB *pcb;
pcb = (struct PCB *)malloc(sizeof(struct PCB));
strcpy(pcb->NAME, p->RUN->NAME);
pcb->ROUND = p->RUN->ROUND;
pcb->REACHTIME = p->RUN->REACHTIME;
pcb->CPUTIME = p->RUN->CPUTIME;
pcb->COUNT = p->RUN->COUNT;
pcb->NEEDTIME = p->RUN->NEEDTIME;
pcb->STATE = p->RUN->STATE;
pcb->NEXT = p->RUN->NEXT;
p->TAIL->NEXT = pcb;
p->TAIL = pcb;
p->RUN = NULL;
pcb->STATE = 'W';
}
void PRINT(LINK *p) {
struct PCB *pcb;
printf("执行一个时间片后的所有进程的状态:\n\n");
if (p->RUN != NULL)
PR(p->RUN);
if (p->READY != p->TAIL) {
pcb = p->READY->NEXT;
while (pcb != NULL) {
PR(pcb);
pcb = pcb->NEXT;
}
}
pcb = p->FINISH->NEXT;
while (pcb != NULL) {
PR(pcb);
pcb = pcb->NEXT;
}
}
void PR(struct PCB *p) {
printf("进程名:%s\n", p->NAME);
printf("进程轮转时间片:%d\n", p->ROUND);
printf("进程到达时间:%d\n", p->REACHTIME);
printf("进程占用CPU时间:%d\n", p->CPUTIME);
printf("计数器:%d\n", p->COUNT);
printf("进程完成还要的CPU时间:%d\n", p->NEEDTIME);
printf("进程的状态:%c\n\n", p->STATE);
}
如何用C语言编写一个简单的程序!
上了大学有很多同学都在学习C语言,C++就是C语言的一种,那么怎么用c语言写一个简单的程序hello world呢,下边来给大家演示一下
工具/材料
- 电脑
- C语言软件
步骤
- 鼠标左键双击C语言软件,打开,打开后界面如图,点击关闭即可。
- 点击上方程序窗口左上角的文件,选择新建。
- 在打开的窗口中选择文件,下边一般是第四个
C++ Source file
,输入文件名(hello.c),一定要以.c
为后缀结尾。 - 进入编辑页面,在页面编辑源代码就可以:
#include stdio.h
void main() {
printf("hello world!\n");
}
然后选择保存,打印,输出运行。
输出效果
hello world!
一个简单的C语言程序就写好了。
特别提示
所有的输入都要在英文半角的情况下输入,不然程序会不能识别,会报错。