本文目錄一覽:
- 1、C++和C語言的轉換
- 2、如何把下列c++語言轉換成c語言: 急用~~~~~~!!!!!!!!!!
- 3、c語言轉換為c++
- 4、如何把c語言代碼轉化為c 語言,如下
- 5、C語言中輸入輸出語句轉換成C++語句
- 6、C語言,C++中顯式轉換和隱式轉換的區別?
C++和C語言的轉換
全部手工編寫的,按照的是string和int的鍵值對插入的。 c++內置的stl map為紅黑樹實現的,insert和查找時間複雜度均為logn,一般來說c語言實現紅黑樹太過複雜,而且就論查找來說hashtable的時間複雜度查找為O1級別的常數級別,最快速度的。insert也是O1級別。所以是一種最好的替代方案。 由於本人水平有限,測試不算太完美,有可能會有疏漏還請見諒,測試數據存儲在test.txt中
helloworld 1
helloworad 2
helloworla 3
chinadododo 23
nihao 33
dadadadada 31
changge 33333
meizi 2222111
shax 22334
fengzi 333
wangzi 4
nvxia 98
#includestdio.h
#includestring.h
#includestdlib.h
#includeassert.h
#define MAXSIZE 30011//one big prime
#define MAXLEN 80//string maxsize
typedef struct hashnode hashnode;
struct hashnode{
char *str;
int value;
struct hashnode *next;
};
//hashtable and hashtable length
hashnode *hashtable[MAXSIZE];
int len=0;
unsigned int hash(char *s)
{
int i;
unsigned int key=0;
for(i=0;s[i];i++)
{
key=(key3)+s[i]-‘0’;
key%=MAXSIZE;
}
//printf(“%u\n”,key);
return key;
}
hashnode* createnode(char *s,int v)
{
hashnode *p=(hashnode*)malloc(sizeof(hashnode*));
p-value=v;
p-str=(char*)malloc(MAXLEN*sizeof(char));
strcpy(p-str,s);
p-next=NULL;
return p;
}
hashnode* insert(char *s,int v)
{
++len;
unsigned int key=hash(s);
if(strlen(s)3)
return NULL;
if(hashtable[key]==NULL)
{
hashtable[key]=createnode(s,v);
printf(“insert success!hash key is %u \n”,key);
return hashtable[key];
}else
{
hashnode *p=createnode(s,v);
p-next=hashtable[key];
hashtable[key]=p;
printf(“insert success!hash key is %u \n”,key);
return p;
}
}
hashnode* lookup(char *s)
{
unsigned int key=hash(s);
hashnode *tmp=hashtable[key];
while(tmp!=NULL)
{
if(strcmp(tmp-str,s)==0)
{
return tmp;
}
else
tmp=tmp-next;
}
return NULL;
}
void freehash()//free the hashtable
{
hashnode *p,*tmp;
int i;
for(i=0;iMAXSIZE;i++)
{
p=hashtable[i];
while(p!=NULL)
{
tmp=p;
p=p-next;
free(p);
}
}
}
int main()
{
FILE *fin=fopen(“test.txt”,”r”);
assert(fin);
char str[MAXLEN];
int value;
while(!feof(fin))
{
memset(str,0,MAXLEN);
fscanf(fin,”%s %d”,str,value);
insert(str,value);
}
printf(“the table length is %d \n”,len);
while(1)
{
printf(“input the string you want the look up:\n”);
scanf(“%s”,str);
hashnode *p;
p=lookup(str);
if(p!=NULL)
printf(“the %s’s value is %d!\n”,str,p-value);
else
printf(“no found!!\n”);
}
freehash();
fclose(fin);
return 0;
}
如何把下列c++語言轉換成c語言: 急用~~~~~~!!!!!!!!!!
說不能改是胡說的,任何高級語言寫出來的代碼都可以用c實現,只是複雜程度不一樣。大家不愛動手而已。才10分。
思路是這樣的:
1.c++的類可以換成c的結構體,同時提供一個與結構體對應的句柄
2.類的成員方法可以換成c的普通函數,以句柄代替this指針,windows
api就是這樣做的。
3.關於簡單的泛型,可以使用宏來模擬,這也就是泛型最初的來歷。你目前只是表示了一個元素類型,可以直接用void*
類型的存儲,配上一個size來保存各種類型的元素。
c語言轉換為c++
頭文件
#include iostream
#include algorithm
using namespace std;
主函數
int main( )
{
……
return 0;(不能少)
}
scanf和 printf語句我都改一個,你自己照着做就可以了,比如:
scanf(“%d%d”,all[i].t,all[i].x)
cin all[i].t all[i].x;
printf(“Case %d: %d\n”,tt++,maxt);
cout “Case ” (tt++) “: ” maxt endl;
如果不會M我
很簡單的:
#include iostream
#include algorithm
using namespace std;
。。。。。。
int main()
{
int t,tt=1;
cin t;
while(t–)
{
maxt=1000001;
mint=-4000001;
cin n m;
for(int i=0;in;i++)
{
cin all[i].t all[i].x;
if(maxtall[i].t) maxt=all[i].t;
}
qsort(all,n,sizeof(event),cmp);
while(maxt=mint)
{
int temp=(maxt+mint)1;
if(greed(temp)) mint=temp+1;
else maxt=temp-1;
}
if(greed(maxt)) mint=maxt;
cout “Case ” (tt++) ” : ” maxt endl;
}
return 0;
}
如果有錯,檢查下程序是否出錯
如何把c語言代碼轉化為c 語言,如下
你的問題是“C語言代碼轉化為c 語言”,比較奇怪。你所附代碼里出現了:
using namespace std;
因此這應該是C++代碼。
C和C++兩者的比較明顯的差別,就是前者面向過程,後者面向目標。若把C++的源代碼,變成C,則需要把C++所定義的class之類,都拆開,按照C的方式進行組合。
具體到你的源代碼,C++專有的只有2處。可以做如下改變:
using namespace std; — 這一行刪除
cin t; — 這一行改成如下的C格式
scanf(“%d”, t);
因為你所附的源代碼被緊縮到一塊了,比較不方便進一步查看。你可以試試看,有問題繼續交流。
C語言中輸入輸出語句轉換成C++語句
c語言中的輸入輸出語句換成C++語句是將printf()函數換成cout對象,將scanf()函數換成cin對象。
問題中提及的代碼:
printf(“第%d名的名字:”,t);
scanf(“%s”,p-date.ach[t-1].name);對應的C++代碼:cout”第”t”名的名字:”;
cinp-date.ach[t-1].name;
C語言,C++中顯式轉換和隱式轉換的區別?
顯式是強制轉換,而隱式是編譯器自動轉換的,一般同一數據類型(如byte,int,float,double),由低級轉為高級為隱式(如int轉為float或double),而高級轉為低級為強制類型轉換,即顯式轉換,須用戶在代碼中寫的(如float轉為int)。
隱式數據類型轉換是有編譯器完成的,例如:
char a = ‘a’;
int x = a;
顯示需要編程過程中強制轉換,例如
int a = 10;
double x = (double)a;
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/246308.html