本文目錄一覽:
c語言:編寫汽車結構體car,擁有成員:品牌(brand,如 寶馬)、型號(type,如 x5)
//Car struct contains brand of the Car and the type
struct Car
{
char brand[5];
char type[5];
};
//The variable of the Struct Car car
struct Car car;
//Use the strcpy() function to copy the string to the struct Car
strcpy(car.brand, “BM”);
strcpy(car.type, “x6”);
//Output the result
printf(“Car brand :%s\nCar type :%s”, car.brand, car.type);
//Also you can use like this:
struct Car
{
char *brand;
char *type;
};
//Initialise the brand and the type of the Car struct
car.brand = “BM”;
car.type = “x6”;
//Output the info. of the struct Car
printf(“Car brand :%s\nCar type :%s”, car.brand, car.type);
C語言我不回幫忙下
1,
#includestdio.h
#includestdlib.h
main()
{ int A=19,B=22,C=650,D;
D=A*B*C;
printf(“%d”,D);
system(“pause”);
}
2,
#includestdio.h
#includestdlib.h
int main()
{
float b,c,a1,a2;
b=35.425;c=52.954;
a1=b*c;
a2=c%b;
printf(“a1=%d,a2=%d”a1,a2);
system(“pause”);
}
第二個應該用浮點型的!呵呵
C語言代碼 終於寫完了 本人初學者 寫的不好地方請提意見 跪謝
#includestdio.h
#includestdlib.h//需要加上這兩個頭文件,stdlib.h提供給srand和rand,time.h提供給time(NULL)
#include time.h
void main()
{
char name[20];
int i=0,c;
int j;
char array[20][20];//對應後面的i 50,這裡應該改為array[50][20]
char bj[5][5]={“寶馬”,”悍馬”,”吉普”,”賓利”,”拖拉機”};//中文是雙字節,所以這裡要改成bj[5][10]更安全
char bm[20][20];//對應後面的i 50,這裡應該改為bm[50][20]
FILE *p1,*p2;
printf(“請輸入的名字:”);
scanf(“%s”,name);//name改為name
if((p2=fopen(“suonanluobu.txt”,”r”))==0)
{
printf(“文件無法讀取\n”);
}
for(i=0;i50;i++)
{
fscanf(p2,”%s %s\n”,array[i],bm[i]);
}
fclose(p2);
for(i=0;i50;i++)
{
if(strcmp(name,array[i])==0)
{
printf(“不好意思你已經測試過了\n”);
printf(“%s會開%s”,name,bm[i]);
exit(0);
}
}
srand((unsigned)time(NULL));
for(i=0;i1;i++)
{
c=rand()%5;
}
printf(“———————-\n”);
//建議把下面的代碼改為switch case 結構好看點
if(c==0)
{
printf(“%s將來會開寶馬\n”,name);
i=0;}
else if(c==1){
printf(“%s將來會開悍馬\n”,name);
i=1;}
else if(c==2){
printf(“%s將來會開吉普\n”,name);
i=2;}
else if(c==3){
printf(“%s將來會開賓利\n”,name);
i=3;}
else if(c==4){
printf(“%s將來會開拖拉機\n”,name);
i=4;}
j=i;
if((p1=fopen(“suonanluobu.txt”,”a”))==0)
{
printf(“文件打開失敗\n”);
exit(0);
}
for(i=0;i1;i++)
{
fprintf(p1,”%s %s\n”,name,bj[j]);//bj[j]應改為bj[i],因為上面的if else語句用的是i
}
fclose(p1);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/249596.html