一个c语言语句中至少包含一个,数据结构中遇到的有关问题error C2016: C 要结构或联合至少有一个成员...

数据结构中遇到的问题error C2016: C 要求一个结构或联合至少有一个成员

代码是:请大神们赐教

#include

#include

#include

#define LIST_INIT_SIZE 100

#define LISTINCREMENT 10

struct ElemType{

char cityname[20];

int number;

char introduct[20];

};

typedef struct{

ElemType elem[LIST_INIT_SIZE];

int length;

int listsize;

}SqList;

void CreatList_Sq(SqList *&L,ElemType *a,int n){//建立顺序表

L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));

if(!L->elem) exit(0);

for(int i=0;i

{

strcpy(L->elem[i].cityname,a[i].cityname);

L->elem[i].number=a[i].number;

strcpy(L->elem[i].introduct,a[i].introduct);

}

}

void DisplayList(SqList *&L)         //输出线性表

{

for(int i=0;ilength;i++)

printf("%s %d %s\n",L->elem[i].cityname,L->elem[i].number,L->elem[i].introduct);

printf("线性表的长度是:%d\n",L->length);

}

void LocateElem(SqList *&L)            //查询线性表中的元素

{

int m;

printf("请输入要查询的城市的区号:");

scanf("%d",&m);

for(int i=0;ilength;i++)

{

if(m==L->elem[i].number)

printf("%s %d %s\n",L->elem[i].cityname,L->elem[i].number,L->elem[i].introduct);

}

}

int ListInsert(SqList *&L,int i){

//在线性表L中第i个位置前插入新的元素,

//i的合法值为1<=i<=L->length+1

ElemType *newbase;

if(i<1||i>L->length+1)

return 0;

if(L->length>=L->listsize){     //当前存储空间已满,增加分配

newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));

if(!newbase) exit(0);

L->elem=newbase;

L->listsize+=LISTINCREMENT;

}

char n[20]="GuangDong";

int num=5;

char intro[20]="In southern China";

for(int j=L->length;j>i;j--)

{

strcpy(L->elem[j].cityname,L->elem[j-1].cityname);

L->elem[j].number=L->elem[j-1].number;

strcpy(L->elem[j].introduct,L->elem[j-1].introduct);

}

strcpy(L->elem[i].cityname,n);

L->elem[i].number=num;

strcpy(L->elem[i].introduct,intro);

L->length++;

printf("线性表的长度是:%d\n",L->length);

return 0;

}

void DeletList(SqList *&L,int i)

//删除线性表的第i个元素

{

for(int j=i;jlength;j++)

{

strcpy(L->elem[j].cityname,L->elem[j+1].cityname);

L->elem[j].number=L->elem[j+1].number;

strcpy(L->elem[j].introduct,L->elem[j+1].introduct);

}

L->length--;

printf("线性表的长度是:%d\n",L->length);

}

void DestroyList(SqList *&L)

{

free(L);

}

void main(){

struct ElemType b[4]={

{"BeiJing",1,"capital city"},

{"WuHan",2,"beautiful"},

{"XiaMen",3,"near sea"},

{"HanDan",4,"my home"}

};

SqList *List;

printf("原线性表中的元素是:\n");

CreatList_Sq(List,b,4);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 用C语言编写计算器的方法有很多种,但是最简单的方式是使用C语言的标准库函数,比如printf()和scanf()函数,以及其他数学函数。另外,还可以使用预先定义的函数,如add(),subtract(),multiply()和divide()等函数来实现计算器的功能。 ### 回答2: 使用C语言编写一个计算器,首先需要定义基本的数据结构和函数。 1. 定义一个结构体用于保存操作数和操作符: ``` typedef struct { double operand1; double operand2; char operator; } Calculator; ``` 2. 定义一个函数来接收输入的操作数和操作符,并进行相应的计算: ``` double calculate(Calculator calculator) { switch(calculator.operator) { case '+': return calculator.operand1 + calculator.operand2; case '-': return calculator.operand1 - calculator.operand2; case '*': return calculator.operand1 * calculator.operand2; case '/': return calculator.operand1 / calculator.operand2; default: printf("错误的运算符\n"); return 0; } } ``` 3. 主函数进行输入和调用计算函数的操作: ``` int main() { double operand1, operand2; char operator; printf("请输入第一个操作数: "); scanf("%lf", &operand1); printf("请输入运算符(+, -, *, /): "); scanf(" %c", &operator); printf("请输入第二个操作数: "); scanf("%lf", &operand2); Calculator calculator; calculator.operand1 = operand1; calculator.operand2 = operand2; calculator.operator = operator; double result = calculate(calculator); printf("结果: %.2lf\n", result); return 0; } ``` 这样就完成了一个简单的四则运算的计算器。用户可以通过依次输入操作数和操作符,程序将会输出结果。例如,输入"2 + 3"将会输出结果"5.00"。 ### 回答3: 要用C语言一个计算器,可以按照以下步骤进行: 1. 首先,我们需要定义变量来存储用户输入的数值和操作符。可以使用整型、浮点型或字符型的变量。 2. 接下来,使用printf函数向用户展示可以进行的操作,如加法、减法、乘法和除法等。 3. 使用scanf函数来获取用户输入的操作符和数值,并将它们存储到相应的变量。 4. 根据用户选择的操作符,使用if-else语句或switch语句来执行相应的操作。例如,当用户选择加法时,将存储的两个数值相加,并将结果打印出来。 5. 可以使用循环语句来让计算器持续运行,直到用户选择退出。 以下是一个简单的示例代码: ``` #include <stdio.h> int main() { char operator; float num1, num2; while (1) { printf("\nEnter an operator (+, -, *, /) or q to quit: "); scanf("%c", &operator); if (operator == 'q') { break; } printf("Enter two numbers: "); scanf("%f %f", &num1, &num2); switch (operator) { case '+': printf("%.2f + %.2f = %.2f\n", num1, num2, num1 + num2); break; case '-': printf("%.2f - %.2f = %.2f\n", num1, num2, num1 - num2); break; case '*': printf("%.2f * %.2f = %.2f\n", num1, num2, num1 * num2); break; case '/': if (num2 != 0) { printf("%.2f / %.2f = %.2f\n", num1, num2, num1 / num2); } else { printf("Error: Division by zero\n"); } break; default: printf("Invalid operator\n"); break; } getchar(); // Removes the trailing newline character from the input buffer } return 0; } ``` 以上代码实现了一个基本的计算器,用户可以选择不同的操作符来执行相应的数学计算,并得到结果。当用户选择退出时,程序会终止运行。请注意,此示例代码仅作为参考,你可以根据自己的需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值