本文目錄一覽:
- 1、誰有C語言計算機模擬器的源代碼加註釋。709232876@qq.com
- 2、怎麼用C語言編程測試計算機的MIPS或MFLOPS值
- 3、編寫一個C語言程序,模擬一個計算器。要求:
- 4、用C語言編程模擬處理機調度(實現一種演算法)
誰有C語言計算機模擬器的源代碼加註釋。709232876@qq.com
/*/////////////////////////////////////////////////////////////
* 功能:實現一個計算器程序 改一下輸入輸出,,,把頭文件換了就行了餓我的是C++寫的
* 作者: 曾勝 mayi_qiong 我拿的我同學的題目修改的,,已經很完美了,,要求完全一樣
* 時間:
*////////////////////////////////////////////////////////////*/
#includeiostream
#includestring
#include stdlib.h
#include conio.h
#includevector
static int i;
using namespace std;
class calculator
{
private:
char ch;
double a[10],b[10]; //定義數組
double s[10];
char c;
public:
int Show()
{
cout”請輸入等式:”endl;
for( i=0;i10;i++)
{ cout”請輸入’數字’,’符號’,’數字’字元處理請按’0′”endl;
cina[i]; if(a[i]==0) goto kk;
cinc;
cinb[i];
system(“pause”);
kk:
if(a[i]==0)
{ cout “清除全部按A,清除上一個按C,輸出以上按算式及結果按S”endl;
char ch;
cinch;
switch(ch)
{ case ‘a’ :
case ‘A’ :
system(“cls”); i=-1; cout”數據完全清除任意鍵繼續”endl; system(“pause”); break;
case ‘C’:
case ‘c’:
system(“cls”);
aa();
break;
case ‘S’ :
case ‘s’ :
show_all(); system(“pause”); break;
} cin.clear();//清掉cin,因為cin會導致死循環
}
else
switch(c)
{
case ‘+’:
s[i]=a[i]+b[i];
break;
case ‘-‘:
s[i]=a[i]-b[i];
break;
case’*’:
s[i]=a[i]*b[i];
break;
case’/’:
s[i]=a[i]/b[i];
break;
}
system(“cls”);
cin.clear();//清掉cin,因為cin會導致死循環
}
return 0;
}
void show_all()
{
for(int j=0;ji;j++)
{ couta[j]cb[j]”=”s[j]endl; } }
void aa(){ do{ i–; for(int j=0;j=i-1;j++)
{ couta[j]cb[j]”=”s[j]endl;
}
cout”繼續請按C,其他退出”endl;
cinch;
}while(ch==’C’||ch==’c’); }
void show(){
system(“cls”);
cout”\n\n\n\n\n\n\n”;
cout”===============================================================================”endl;
cout”\n\n”;
cout” 作者: 曾勝”endl;
cout”\n\n”;
cout” 學號:”XXXXXXXendl;
cout”\n\n”;
cout” 專業:軟體工程”endl;
cout”\n\n”;
cout” 學院:軟體學院”endl;
cout”\n\n”;
cout” 指導老師:李祥”endl;
cout”\n\n”;
cout”================================================================================”endl;
coutendl;
}
};
int main()
{
system(“color 0A”);
calculator s;//實例化對象
s.show();
getch();//暫停
system(“cls”);
s.Show();
return main();
return 0;
}
怎麼用C語言編程測試計算機的MIPS或MFLOPS值
準確測試cpu的mips或者mflops一般是設計體系結構時候用cpu模擬器或者verilog前仿得到的。對於用C語言比較準確的測試mips或者mflops,你可以用一個程序讀取系統時間,然後執行第二個程序,第二個程序執行完成後再記錄執行的時間,然後反彙編第二個程序,統計第二個程序中執行的指令條數,通常第二個程序中執行的指令數是確定的,(分支和循環的次數是可確定的)。mips和mflops在risc cpu的評價中比較有價值,感覺cisc稍差一些。
編寫一個C語言程序,模擬一個計算器。要求:
支持
加減乘除括弧負數開根乘方
#includestdio.h
#includemath.h
#includemalloc.h
double
jisuan(char
a[])
{
int
i=1,j,k,m,cnt=0,t1=0,t2=0,t3=0;
char
nibo[50],zhan2[50];
double
x,n,l,z=0,zhan3[50];
typedef
struct
{
double
d1;
int
d2;
}dd;
typedef
struct
{
dd
data[50];
int
top;
}zhan1;
zhan1
*shu;
shu=(zhan1
*)malloc(sizeof(zhan1));
shu-top=0;
while(a[i]!=’\0′)
{
if(a[i]=’0’a[i]=’9′)
{
z=0;
j=i+1;
while(a[j]=’0’a[j]=’9′)
{j++;}
j–;
for(k=i;k=j;k++)
{
z=z*10+a[k]-‘0’;
}
j=j+1;
x=z;
if(a[j]==’.’)
{
l=1;
i=j+1;
j=i+1;
while(a[j]=’0’a[j]=’9′)
{j++;}
j–;
for(k=i;k=j;k++)
{
n=pow(0.1,l);
l=l+1;
x=x+n*(a[k]-‘0’);
}
i=j+1;
}
else
i=j;
shu-data[++shu-top].d1=x;
shu-data[shu-top].d2=++cnt;
nibo[++t1]=’0’+shu-data[shu-top].d2;
nibo[t1+1]=’\0′;
}
else
if(a[i]=='(‘)
{
zhan2[++t2]=a[i];
i++;
}
else
if(a[i]==’)’)
{
j=t2;
while(zhan2[j]!='(‘)
{
nibo[++t1]=zhan2[j];
nibo[t1+1]=’\0′;
j–;
}
t2=j-1;
i++;
}
else
if(a[i]==’+’)
{
while(t20zhan2[t2]!='(‘)
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]=’\0′;
t2–;
}
zhan2[++t2]=a[i];
i++;
}
else
if(a[i]==’-‘)
{
if(a[i-1]==’$’)
{
a[0]=’0′;
i=0;
}
else
if(a[i-1]=='(‘)
{
a[i-1]=’0′;
a[i-2]='(‘;
i=i-2;
t2–;
}
else
{
while(t20zhan2[t2]!='(‘)
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]=’\0′;
t2–;
}
zhan2[++t2]=a[i];
i++;
}
}
else
if(a[i]==’*’||a[i]==’/’)
{
while(zhan2[t2]==’*’||zhan2[t2]==’/’||zhan2[t2]==’^’||zhan2[t2]==’#’)
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]=’\0′;
t2–;
}
zhan2[++t2]=a[i];
i++;
}
else
if(a[i]==’^’||a[i]==’#’)
{
while(zhan2[t2]==’^’||zhan2[t2]==’#’)
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]=’\0′;
t2–;
}
zhan2[++t2]=a[i];
i++;
}
}
while(t20)
{
nibo[++t1]=zhan2[t2];
nibo[t1+1]=’\0′;
t2–;
}
j=1;t3=0;
while(j=t1)
{
if(nibo[j]=’0’nibo[j]!=’^’nibo[j]!=’#’)//
{
for(i=1;i=shu-top;i++)
{
if((int)(nibo[j]-‘0’)==shu-data[i].d2)
{
m=i;
break;
}
}
zhan3[++t3]=shu-data[m].d1;
}
else
if(nibo[j]==’+’)
{
zhan3[t3-1]=zhan3[t3-1]+zhan3[t3];
t3–;
}
else
if(nibo[j]==’-‘)
{
zhan3[t3-1]=zhan3[t3-1]-zhan3[t3];
t3–;
}
else
if(nibo[j]==’*’)
{
zhan3[t3-1]=zhan3[t3-1]*zhan3[t3];
t3–;
}
else
if(nibo[j]==’/’)
{
zhan3[t3-1]=zhan3[t3-1]/zhan3[t3];
t3–;
}
else
if(nibo[j]==’^’)
{
zhan3[t3-1]=pow(zhan3[t3-1],zhan3[t3]);
t3–;
}
else
if(nibo[j]==’#’)
{
zhan3[t3]=sqrt(zhan3[t3]);
}
j++;
}
return
zhan3[t3];
}
void
main()
{
for(;;)
{
char
x,a[50];
double
jieguo;
int
i=0;
a[0]=’$’;
printf(“#表示開方,^表示乘方(支持負數)\n”);
printf(“請輸入表達式,退出請輸入q:\n\n”);
scanf(“%c”,x);
if(x==’q’)
break;
while(x!=’\n’)
{
a[++i]=x;
scanf(“%c”,x);
}
a[i+1]=’\0′;
jieguo=jisuan(a);
printf(“\n”);
printf(“結果為:%lf”,jieguo);
printf(“\n\n\n\n\n”);
}
}
用C語言編程模擬處理機調度(實現一種演算法)
#include stdlib.h
#include conio.h
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
struct pcb { /* 定義進程式控制制塊PCB */
char name[10];
char state;
int super;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB;
void sort() /* 建立對進程進行優先順序排列函數*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p-super)(ready-super))) /*優先順序最大者,插入隊首*/
{
p-link=ready;
ready=p;
}
else /* 進程比較優先順序,插入適當的位置中*/
{
first=ready;
second=first-link;
while(second!=NULL)
{
if((p-super)(second-super)) /*若插入進程比當前進程優先數大,*/
{ /*插入到當前進程前面*/
p-link=second;
first-link=p;
second=NULL;
insert=1;
}
else /* 插入進程優先數最低,則插入到隊尾*/
{
first=first-link;
second=second-link;
}
}
if(insert==0) first-link=p;
}
}
void input() /* 建立進程式控制制塊函數*/
{
int i,num;
system(“cls”); /*清屏*/
printf(“\n 請輸入進程數: “);
scanf(“%d”,num);
for(i=1;i=num;i++)
{
printf(“\n 進程號No.%d:\n”,i);
p=getpch(PCB);
printf(“\n 輸入進程名:”);
scanf(“%s”,p-name);
printf(“\n 輸入進程優先數:”);
scanf(“%d”,p-super);
printf(“\n 輸入進程運行時間:”);
scanf(“%d”,p-ntime);
printf(“\n”);
p-rtime=0;p-state=’W’;
p-link=NULL;
sort(); /* 調用sort函數*/
}
}
int space()
{
int l=0;
PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr-link;
}
return(l);
}
void disp(PCB * pr) /*建立進程顯示函數,用於顯示當前進程*/
{
printf(“\n 進程名\t 狀態\t 優先數\t 需要運行時間\t 已經運行時間\n”);
printf(“|%s\t”,pr-name);
printf(“|%c\t”,pr-state);
printf(“|%d\t”,pr-super);
printf(“|%d\t\t”,pr-ntime);
printf(“|%d\t”,pr-rtime);
printf(“\n”);
}
void check() /* 建立進程查看函數 */
{
PCB* pr;
printf(“\n **** 當前正在運行的進程是:\n”); /*顯示當前運行進程*/
disp(p);
pr=ready;
printf(“\n **** 當前就緒隊列狀態為:\n”); /*顯示就緒隊列狀態*/
while(pr!=NULL)
{
disp(pr);
pr=pr-link;
}
}
void destroy() /*建立進程撤消函數(進程運行結束,撤消進程)*/
{
printf(“\n 進程 [%s] 已完成.\n”,p-name);
free(p);
}
void running() /* 建立進程就緒函數(進程運行時間到,置就緒狀態*/
{
(p-rtime)++;
if(p-rtime==p-ntime)
destroy(); /* 調用destroy函數*/
else
{
(p-super)–;
p-state=’W’;
sort(); /*調用sort函數*/
}
}
void main() /*主函數*/
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)(ready!=NULL))
{
ch=getchar();
h++;
printf(“—————————————————–“);
printf(“\n 現在是第%d次運行: \n”,h);
p=ready;
ready=p-link;
p-link=NULL;
p-state=’R’;
check();
running();
printf(“\n 按任意鍵繼續……\n”);
}
printf(“\n\n 進程已經完成.\n”);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/238343.html