本文目录一览:
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);
}