之前用数组的数据填进链表都没有问题,为什么这次的字符串会不适用呢?
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include"student.h"
typedef struct course
{
char name[5];
int ID;
int section;
}course;
typedef struct student
{
char Firstname[100];
char Lastname[100];
char SSN[15];
int number;//number of course attending
course Arraycourse[10];
struct student* next;
}student;
void ReadInputFile(char* filename, student* stu)
{
FILE* fp;
fopen_s(&fp, filename, "rt");
if (!fp)
{
printf("open file error");
}
stu = (student*)malloc(sizeof(student) * 10);
for (int i = 0; i < 2; i++)
{
fscanf_s(fp, "%s%s%s%d", stu[i].Firstname, sizeof(stu[i].Firstname),
stu[i].Lastname, sizeof(stu[i].Lastname),
stu[i].SSN, sizeof(stu[i].SSN),
&stu[i].number);
printf("%s\n%s\n%s\n%d\n", stu[i].Firstname,
stu[i].Lastname,
stu[i].SSN,
stu[i].number);
for (int j = 0; j < stu[i].number; j++)
{
fscanf_s(fp, "%s%d%d", stu[i].Arraycourse[j].name, sizeof(stu[i].Arraycourse[j].name),
&stu[i].Arraycourse[j].ID, &stu[i].Arraycourse[j].section);
printf("%s %d %d\n", stu[i].Arraycourse[j].name,
stu[i].Arraycourse[j].ID, stu[i].Arraycourse[j].section);
}
}
fclose(fp);
}
void AddToList(student* addStudent, student* head)
{
student* prev, * p;
prev = (student*)malloc(sizeof(student));
head = prev;
for (int i = 0; i < 10; i++)
{
p = (student*)malloc(sizeof(student));
p->Firstname = addStudent[i].Firstname;
p->Lastname = addStudent[i].Lastname;
p->SSN = addStudent[i].SSN;
p->number = addStudent[i].number;
for (int j = 0; j < p->number; j++)
{
p->Arraycourse[j].name = addStudent[i].Arraycourse[j].name;
p->Arraycourse[j].ID = addStudent[i].Arraycourse[j].ID;
p->Arraycourse[j].section = addStudent[i].Arraycourse[j].section;
}
p->next = NULL;
prev->next = p;
prev = p;
}
}
参考GPT和自己的思路:在代码中,字符串类型的变量是使用字符数组来定义的,所以不能直接使用赋值运算符将一个字符串赋值给另一个字符串变量。在AddToList函数中,你应该使用strcpy函数来将字符串复制到链表节点中,而不是直接使用赋值运算符。例如:p->Firstname = addStudent[i].Firstname 应该改为 strcpy(p->Firstname, addStudent[i].Firstname)。同样,需要对所有的字符串类型的变量都进行这个更改。
不知道你这个问题是否已经解决, 如果还没有解决的话:#include <stdio.h>
double calc_pow(double x, int n);
int main()
{
double x;
int n;
scanf("%lf %d", &x, &n);
printf("%.0f\n", calc_pow(x, n));
return 0;
}
int i=1;
double calc_pow(double x, int n)
{
printf("%f %d\n", x, n);
//if(n==1) return x; //如果用这个判断的话,会少一个,如果n是0就不可以了
if (n == 0)
return 1;
if (n % 2 == 0)
{
//printf("%f %d\n", x, n);
return calc_pow(x * x, n / 2);
}
else
{
//printf("%f %d\n", x, n);
return calc_pow(x * x, n / 2) * x; //记住函数最终是返回当前函数的值,返回完之后会继续执行下面的语句,直到执行本次函数之后才会返回上层函数
}
}